-
Notifications
You must be signed in to change notification settings - Fork 346
/
config.go
155 lines (133 loc) · 4.11 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package config
import (
"os"
"path/filepath"
"github.com/pkg/errors"
"github.com/zan8in/afrog/pkg/utils"
"gopkg.in/yaml.v2"
)
// Config is a afrog-config.yaml catalog helper implementation
type Config struct {
PocSizeWaitGroup int32 `yaml:"poc_sizewaitgroup"`
TargetSizeWaitGroup int32 `yaml:"target_sizewaitgroup"`
FingerprintSizeWaitGroup int32 `yaml:"fingerprint_sizewaitgroup"`
ConfigHttp ConfigHttp `yaml:"http"`
Reverse Reverse `yaml:"reverse"`
}
type ConfigHttp struct {
Proxy string `yaml:"proxy"`
DialTimeout int32 `yaml:"dial_timeout"`
ReadTimeout string `yaml:"read_timeout"`
WriteTimeout string `yaml:"write_timeout"`
MaxRedirect int32 `yaml:"max_redirect"`
MaxIdle string `yaml:"max_idle"`
Concurrency int `yaml:"concurrency"`
MaxConnsPerHost int `yaml:"max_conns_per_host"`
MaxResponseBodySize int `yaml:"max_responsebody_sizse"`
UserAgent string `yaml:"user_agent"`
}
type Reverse struct {
Ceye Ceye `yaml:"ceye"`
}
type Ceye struct {
ApiKey string `yaml:"api-key"`
Domain string `yaml:"domain`
}
const afrogConfigFilename = "afrog-config.yaml"
const Version = "1.3.9"
// Create and initialize afrog-config.yaml configuration info
func New() (*Config, error) {
if isExistConfigFile() != nil {
c := Config{}
c.PocSizeWaitGroup = 25
c.TargetSizeWaitGroup = 25
c.FingerprintSizeWaitGroup = 100
configHttp := c.ConfigHttp
configHttp.Proxy = ""
configHttp.DialTimeout = 10
configHttp.ReadTimeout = "500ms"
configHttp.WriteTimeout = "500ms"
configHttp.MaxIdle = "1h"
configHttp.MaxRedirect = 3
configHttp.Concurrency = 4096
configHttp.MaxConnsPerHost = 512 // MaxConnsPerHost是一个限流的参数,保证对一个Host最大的打开连接数,如果超过这个数字,则会直接拒绝,这里默认值是512,但如果你打算用来做压测之类的事情,需要增加这个值,比如这里我就增加到了16384
configHttp.MaxResponseBodySize = 1024 * 1024 * 2
configHttp.UserAgent = ""
c.ConfigHttp = configHttp
reverse := c.Reverse
reverse.Ceye.ApiKey = "fb1837116d366dd91f2aa9ab837efe66"
reverse.Ceye.Domain = "dflqxk.ceye.io"
c.Reverse = reverse
WriteConfiguration(&c)
}
return ReadConfiguration()
}
func isExistConfigFile() error {
homeDir, err := os.UserHomeDir()
if err != nil {
return errors.Wrap(err, "could not get home directory")
}
configFile := filepath.Join(homeDir, ".config", "afrog", afrogConfigFilename)
if utils.Exists(configFile) {
return nil
}
return errors.New("could not get config file")
}
func (c *Config) GetConfigPath() string {
homeDir, err := os.UserHomeDir()
if err != nil {
return ""
}
configFile := filepath.Join(homeDir, ".config", "afrog", afrogConfigFilename)
if !utils.Exists(configFile) {
return configFile
}
return configFile
}
func getConfigFile() (string, error) {
homeDir, err := os.UserHomeDir()
if err != nil {
return "", errors.Wrap(err, "could not get home directory")
}
configDir := filepath.Join(homeDir, ".config", "afrog")
_ = os.MkdirAll(configDir, 0755)
afrogConfigFile := filepath.Join(configDir, afrogConfigFilename)
return afrogConfigFile, nil
}
// ReadConfiguration reads the afrog configuration file from disk.
func ReadConfiguration() (*Config, error) {
afrogConfigFile, err := getConfigFile()
if err != nil {
return nil, err
}
file, err := os.Open(afrogConfigFile)
if err != nil {
return nil, err
}
defer file.Close()
config := &Config{}
if err := yaml.NewDecoder(file).Decode(config); err != nil {
return nil, err
}
return config, nil
}
// WriteConfiguration writes the updated afrog configuration to disk
func WriteConfiguration(config *Config) error {
afrogConfigYAML, err := yaml.Marshal(&config)
if err != nil {
return err
}
afrogConfigFile, err := getConfigFile()
if err != nil {
return err
}
file, err := os.OpenFile(afrogConfigFile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
return err
}
defer file.Close()
if _, err := file.Write(afrogConfigYAML); err != nil {
return err
}
return nil
}