-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig.go
241 lines (203 loc) · 5.67 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package config
import (
"fmt"
"io/ioutil"
"path"
"path/filepath"
"github.com/fsnotify/fsnotify"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
)
const (
RUNTIME_LOCATION = "/run/mailway"
ROOT_LOCATION = "/etc/mailway"
CONFIG_LOCATION = ROOT_LOCATION + "/conf.d"
)
var (
CurrConfig *Config
)
func PrettyPrint() ([]byte, error) {
s, err := yaml.Marshal(&CurrConfig)
if err != nil {
return []byte(""), err
}
return s, nil
}
func readAll() ([]byte, error) {
config := []byte{}
files, err := ioutil.ReadDir(CONFIG_LOCATION)
if err != nil {
return config, err
}
for _, file := range files {
ext := filepath.Ext(file.Name())
if ext == ".yml" || ext == ".yaml" {
absFile := path.Join(CONFIG_LOCATION, file.Name())
content, err := ioutil.ReadFile(absFile)
if err != nil {
return config, err
}
config = append(config, content...)
}
}
return config, err
}
type Config struct {
LogLevel string `yaml:"log_level"`
LogFormat string `yaml:"log_format"`
ServerId string `yaml:"server_id"`
ServerJWT string `yaml:"server_jwt"`
InstanceHostname string `yaml:"instance_hostname"`
InstanceEmail string `yaml:"instance_email"`
InstanceMode string `yaml:"instance_mode"`
PortAuth int `yaml:"port_auth"`
PortForwarding int `yaml:"port_forwarding"`
PortMaildb int `yaml:"port_maildb"`
PortMailout int `yaml:"port_mailout"`
PortWebhook int `yaml:"port_webhook"`
PortFrontlineSMTP int `yaml:"port_frontline_smtp"`
PortFrontlineSMTPS int `yaml:"port_frontline_smtps"`
PortResponder int `yaml:"port_responder"`
OutSMTPHost string `yaml:"out_smtp_host"`
OutSMTPUsername string `yaml:"out_smtp_username"`
OutSMTPPassword string `yaml:"out_smtp_password"`
OutSMTPPort int `yaml:"out_smtp_port"`
OutDKIMPath string `yaml:"out_dkim_path"`
LogFrontlineError string `yaml:"log_frontline_error"`
LogFrontlineHTTPAccess string `yaml:"log_frontline_http_access"`
LogFrontlineHTTPError string `yaml:"log_frontline_http_error"`
ForwardingLoopDetectionCount int `yaml:"forwarding_loop_detection_count"`
ForwardingRateLimitingCount int `yaml:"forwarding_rate_limiting_count"`
MaildbPath string `yaml:"maildb_db_path"`
MailoutInsecureMX []string `yaml:"mailout_insecure_mx"`
MailoutRateAltSMTPCount int `yaml:"mailout_rate_alt_smtp_count"`
SpamFilter bool `yaml:"spam_filter"`
PassWatcherHIBPApiKey string `yaml:"passwatcher_hibp_api_key"`
}
func WriteServerJWT(jwt string) error {
file := path.Join(CONFIG_LOCATION, "server-jwt.yml")
data := fmt.Sprintf("server_jwt: \"%s\"\n", jwt)
err := ioutil.WriteFile(file, []byte(data), 0644)
if err != nil {
return errors.Wrap(err, "could not write file")
}
CurrConfig.ServerJWT = jwt
return nil
}
func WriteDKIM(keyPath string) error {
file := path.Join(CONFIG_LOCATION, "dkim.yml")
data := fmt.Sprintf("out_dkim_path: \"%s\"\n", keyPath)
err := ioutil.WriteFile(file, []byte(data), 0644)
if err != nil {
return errors.Wrap(err, "could not write file")
}
CurrConfig.OutDKIMPath = keyPath
return nil
}
func WriteInstanceConfig(mode, hostname, email string) error {
file := path.Join(CONFIG_LOCATION, "instance.yml")
data := ""
data += fmt.Sprintf("instance_mode: \"%s\"\n", mode)
data += fmt.Sprintf("instance_hostname: \"%s\"\n", hostname)
data += fmt.Sprintf("instance_email: \"%s\"\n", email)
err := ioutil.WriteFile(file, []byte(data), 0644)
if err != nil {
return errors.Wrap(err, "could not write file")
}
CurrConfig.InstanceMode = mode
CurrConfig.InstanceHostname = hostname
CurrConfig.InstanceEmail = email
return nil
}
func loadConfig() error {
data, err := readAll()
if err != nil {
return errors.Wrap(err, "could not read config")
}
var config Config
err = yaml.Unmarshal(data, &config)
if err != nil {
return errors.Wrap(err, "failed to parse")
}
*CurrConfig = config
return nil
}
func Init() error {
CurrConfig = new(Config)
if err := loadConfig(); err != nil {
return errors.Wrap(err, "failed to load config")
}
log.SetLevel(CurrConfig.GetLogLevel())
log.SetFormatter(CurrConfig.GetLogFormat())
go func() {
if err := watchConfig(); err != nil {
log.Errorf("could not watch config: %s", err)
}
}()
return nil
}
func watchConfig() error {
watcher, err := fsnotify.NewWatcher()
if err != nil {
return errors.Wrap(err, "could not create new watcher")
}
defer watcher.Close()
done := make(chan bool)
go func() {
for {
select {
case event, ok := <-watcher.Events:
if !ok {
break
}
log.Debugf("%s detected config change; reloading config", event.String())
if err := loadConfig(); err != nil {
log.Errorf("could not load config: %s", err)
}
case err, ok := <-watcher.Errors:
if !ok {
break
}
log.Errorf("error while watching files: %s", err)
}
}
}()
log.Debugf("start watching %s for changes", CONFIG_LOCATION)
if err := watcher.Add(CONFIG_LOCATION); err != nil {
return errors.Wrap(err, "failed to watch config")
}
<-done
return nil
}
func (c *Config) GetLogLevel() log.Level {
if c.LogLevel == "" {
c.LogLevel = "INFO"
}
switch c.LogLevel {
case "INFO":
return log.InfoLevel
case "DEBUG":
return log.DebugLevel
case "WARN":
return log.WarnLevel
}
log.Fatalf("unknown log level: '%s'", c.LogLevel)
panic("unreachable")
}
func (c *Config) GetLogFormat() log.Formatter {
if c.LogFormat == "" {
c.LogFormat = "text"
}
switch c.LogFormat {
case "text":
return &log.TextFormatter{}
case "json":
return &log.JSONFormatter{}
}
log.Fatalf("unknown log format: '%s'", c.LogFormat)
panic("unreachable")
}
func (c *Config) IsInstanceLocal() bool {
return c.InstanceMode == "local"
}