/
config.go
157 lines (130 loc) · 3.78 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
156
157
package killgrave
import (
"errors"
"fmt"
"io/ioutil"
"os"
"path"
"gopkg.in/yaml.v2"
)
// Config representation of config file yaml
type Config struct {
ImpostersPath string `yaml:"imposters_path"`
Port int `yaml:"port"`
Host string `yaml:"host"`
CORS ConfigCORS `yaml:"cors"`
Proxy ConfigProxy `yaml:"proxy"`
Secure bool `yaml:"secure"`
Watcher bool `yaml:"watcher"`
}
// ConfigCORS representation of section CORS of the yaml
type ConfigCORS struct {
Methods []string `yaml:"methods"`
Headers []string `yaml:"headers"`
Origins []string `yaml:"origins"`
ExposedHeaders []string `yaml:"exposed_headers"`
AllowCredentials bool `yaml:"allow_credentials"`
}
// ConfigProxy is a representation of section proxy of the yaml
type ConfigProxy struct {
Url string `yaml:"url"`
Mode ProxyMode `yaml:"mode"`
}
// ProxyMode is enumeration of proxy server modes
type ProxyMode uint8
const (
// ProxyNone server is off
ProxyNone ProxyMode = iota
// ProxyMissing handle only missing requests are proxied
ProxyMissing
// ProxyAll all requests are proxied
ProxyAll
)
var (
errInvalidConfigPath = errors.New("invalid config file")
errEmptyImpostersPath = errors.New("imposters path can not be blank")
errEmptyHost = errors.New("host can not be blank")
errInvalidPort = errors.New("invalid port")
)
func (p ProxyMode) String() string {
m := map[ProxyMode]string{
ProxyNone: "none",
ProxyMissing: "missing",
ProxyAll: "all",
}
s, ok := m[p]
if !ok {
return "none"
}
return s
}
// StringToProxyMode convert string into a ProxyMode if not exists return a none mode and an error
func StringToProxyMode(t string) (ProxyMode, error) {
m := map[string]ProxyMode{
"none": ProxyNone,
"missing": ProxyMissing,
"all": ProxyAll,
}
p, ok := m[t]
if !ok {
return ProxyNone, fmt.Errorf("unknown proxy mode: %s", t)
}
return p, nil
}
// UnmarshalYAML implementation of yaml.Unmarshaler interface
func (p *ProxyMode) UnmarshalYAML(unmarshal func(interface{}) error) error {
var proxyMode string
if err := unmarshal(&proxyMode); err != nil {
return err
}
m, err := StringToProxyMode(proxyMode)
if err != nil {
return err
}
*p = m
return nil
}
// ConfigureProxy preparing the server with the proxy configuration that the user has indicated
func (cfg *Config) ConfigureProxy(proxyMode ProxyMode, proxyURL string) {
cfg.Proxy.Mode = proxyMode
cfg.Proxy.Url = proxyURL
}
// ConfigOpt function to encapsulate optional parameters
type ConfigOpt func(cfg *Config) error
// NewConfig initialize the config
func NewConfig(impostersPath, host string, port int, secure bool) (Config, error) {
if impostersPath == "" {
return Config{}, errEmptyImpostersPath
}
if host == "" {
return Config{}, errEmptyHost
}
if port < 0 || port > 65535 {
return Config{}, errInvalidPort
}
cfg := Config{
ImpostersPath: impostersPath,
Host: host,
Port: port,
Secure: secure,
}
return cfg, nil
}
// NewConfigFromFile unmarshal content of config file to initialize a Config struct
func NewConfigFromFile(cfgPath string) (Config, error) {
if cfgPath == "" {
return Config{}, errInvalidConfigPath
}
configFile, err := os.Open(cfgPath)
if err != nil {
return Config{}, fmt.Errorf("%w: error trying to read config file: %s, using default configuration instead", err, cfgPath)
}
defer configFile.Close()
var cfg Config
bytes, _ := ioutil.ReadAll(configFile)
if err := yaml.Unmarshal(bytes, &cfg); err != nil {
return Config{}, fmt.Errorf("%w: error while unmarshalling configFile file %s, using default configuration instead", err, cfgPath)
}
cfg.ImpostersPath = path.Join(path.Dir(cfgPath), cfg.ImpostersPath)
return cfg, nil
}