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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
|
package webircgateway
import (
"errors"
"net"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"github.com/gobwas/glob"
"gopkg.in/ini.v1"
)
// ConfigUpstream - An upstream config
type ConfigUpstream struct {
// Plugins may assign an arbitary address to an upstream network
NetworkCommonAddress string
Hostname string
Port int
TLS bool
Timeout int
Throttle int
WebircPassword string
ServerPassword string
GatewayName string
Proxy *ConfigProxy
Protocol string
LocalAddr string
}
// ConfigServer - A web server config
type ConfigServer struct {
LocalAddr string
BindMode os.FileMode
Port int
TLS bool
CertFile string
KeyFile string
LetsEncryptCacheDir string
}
type ConfigProxy struct {
Type string
Hostname string
Port int
TLS bool
Username string
Interface string
}
// Config - Config options for the running app
type Config struct {
gateway *Gateway
ConfigFile string
LogLevel int
Gateway bool
GatewayName string
GatewayWhitelist []glob.Glob
GatewayThrottle int
GatewayTimeout int
GatewayWebircPassword map[string]string
GatewayProtocol string
GatewayLocalAddr string
Proxy ConfigServer
Upstreams []ConfigUpstream
Servers []ConfigServer
ServerTransports []string
RemoteOrigins []glob.Glob
ReverseProxies []net.IPNet
Webroot string
ClientRealname string
ClientUsername string
ClientHostname string
Identd bool
RequiresVerification bool
SendQuitOnClientClose string
ReCaptchaURL string
ReCaptchaSecret string
ReCaptchaKey string
Secret string
Plugins []string
DnsblServers []string
// DnsblAction - "deny" = deny the connection. "verify" = require verification
DnsblAction string
}
func NewConfig(gateway *Gateway) *Config {
return &Config{gateway: gateway}
}
// ConfigResolvePath - If relative, resolve a path to it's full absolute path relative to the config file
func (c *Config) ResolvePath(path string) string {
// Absolute paths should stay as they are
if path[0:1] == "/" {
return path
}
resolved := filepath.Dir(c.ConfigFile)
resolved = filepath.Clean(resolved + "/" + path)
return resolved
}
func (c *Config) SetConfigFile(configFile string) {
// Config paths starting with $ is executed rather than treated as a path
if strings.HasPrefix(configFile, "$ ") {
c.ConfigFile = configFile
} else {
c.ConfigFile, _ = filepath.Abs(configFile)
}
}
// CurrentConfigFile - Return the full path or command for the config file in use
func (c *Config) CurrentConfigFile() string {
return c.ConfigFile
}
func (c *Config) Load() error {
var configSrc interface{}
if strings.HasPrefix(c.ConfigFile, "$ ") {
cmdRawOut, err := exec.Command("sh", "-c", c.ConfigFile[2:]).Output()
if err != nil {
return err
}
configSrc = cmdRawOut
} else {
configSrc = c.ConfigFile
}
cfg, err := ini.LoadSources(ini.LoadOptions{AllowBooleanKeys: true, SpaceBeforeInlineComment: true}, configSrc)
if err != nil {
return err
}
// Clear the existing config
c.Gateway = false
c.GatewayWebircPassword = make(map[string]string)
c.Proxy = ConfigServer{}
c.Upstreams = []ConfigUpstream{}
c.Servers = []ConfigServer{}
c.ServerTransports = []string{}
c.RemoteOrigins = []glob.Glob{}
c.GatewayWhitelist = []glob.Glob{}
c.ReverseProxies = []net.IPNet{}
c.Webroot = ""
c.ReCaptchaURL = ""
c.ReCaptchaSecret = ""
c.ReCaptchaKey = ""
c.RequiresVerification = false
c.Secret = ""
c.SendQuitOnClientClose = ""
c.ClientRealname = ""
c.ClientUsername = ""
c.ClientHostname = ""
c.DnsblServers = []string{}
c.DnsblAction = ""
for _, section := range cfg.Sections() {
if strings.Index(section.Name(), "DEFAULT") == 0 {
c.LogLevel = section.Key("logLevel").MustInt(3)
if c.LogLevel < 1 || c.LogLevel > 3 {
c.gateway.Log(3, "Config option logLevel must be between 1-3. Setting default value of 3.")
c.LogLevel = 3
}
c.Identd = section.Key("identd").MustBool(false)
c.GatewayName = section.Key("gateway_name").MustString("")
if strings.Contains(c.GatewayName, " ") {
c.gateway.Log(3, "Config option gateway_name must not contain spaces")
c.GatewayName = ""
}
c.Secret = section.Key("secret").MustString("")
c.SendQuitOnClientClose = section.Key("send_quit_on_client_close").MustString("Connection closed")
}
if section.Name() == "verify" {
captchaSecret := section.Key("recaptcha_secret").MustString("")
captchaKey := section.Key("recaptcha_key").MustString("")
if captchaSecret != "" && captchaKey != "" {
c.RequiresVerification = section.Key("required").MustBool(false)
c.ReCaptchaSecret = captchaSecret
}
c.ReCaptchaURL = section.Key("recaptcha_url").MustString("https://www.google.com/recaptcha/api/siteverify")
}
if section.Name() == "dnsbl" {
c.DnsblAction = section.Key("action").MustString("")
}
if section.Name() == "dnsbl.servers" {
c.DnsblServers = append(c.DnsblServers, section.KeyStrings()...)
}
if section.Name() == "gateway" {
c.Gateway = section.Key("enabled").MustBool(false)
c.GatewayTimeout = section.Key("timeout").MustInt(10)
c.GatewayThrottle = section.Key("throttle").MustInt(2)
validProtocols := []string{"tcp", "tcp4", "tcp6"}
c.GatewayProtocol = stringInSliceOrDefault(section.Key("protocol").MustString(""), "tcp", validProtocols)
c.GatewayLocalAddr = section.Key("localaddr").MustString("")
}
if section.Name() == "gateway.webirc" {
for _, serverAddr := range section.KeyStrings() {
c.GatewayWebircPassword[serverAddr] = section.Key(serverAddr).MustString("")
}
}
if strings.Index(section.Name(), "clients") == 0 {
c.ClientUsername = section.Key("username").MustString("")
c.ClientRealname = section.Key("realname").MustString("")
c.ClientHostname = section.Key("hostname").MustString("")
}
if strings.Index(section.Name(), "fileserving") == 0 {
if section.Key("enabled").MustBool(false) {
c.Webroot = section.Key("webroot").MustString("")
}
}
if strings.Index(section.Name(), "server.") == 0 {
server := ConfigServer{}
server.LocalAddr = confKeyAsString(section.Key("bind"), "127.0.0.1")
rawMode := confKeyAsString(section.Key("bind_mode"), "")
mode, err := strconv.ParseInt(rawMode, 8, 32)
if err != nil {
mode = 0755
}
server.BindMode = os.FileMode(mode)
server.Port = confKeyAsInt(section.Key("port"), 80)
server.TLS = confKeyAsBool(section.Key("tls"), false)
server.CertFile = confKeyAsString(section.Key("cert"), "")
server.KeyFile = confKeyAsString(section.Key("key"), "")
server.LetsEncryptCacheDir = confKeyAsString(section.Key("letsencrypt_cache"), "")
if strings.HasSuffix(server.LetsEncryptCacheDir, ".cache") {
return errors.New("Syntax has changed. Please update letsencrypt_cache to a directory path (eg ./cache)")
}
c.Servers = append(c.Servers, server)
}
if section.Name() == "proxy" {
server := ConfigServer{}
server.LocalAddr = confKeyAsString(section.Key("bind"), "0.0.0.0")
server.Port = confKeyAsInt(section.Key("port"), 7999)
c.Proxy = server
}
if strings.Index(section.Name(), "upstream.") == 0 {
upstream := ConfigUpstream{}
validProtocols := []string{"tcp", "tcp4", "tcp6", "unix"}
upstream.Protocol = stringInSliceOrDefault(section.Key("protocol").MustString(""), "tcp", validProtocols)
hostname := section.Key("hostname").MustString("127.0.0.1")
if strings.HasPrefix(strings.ToLower(hostname), "unix:") {
upstream.Protocol = "unix"
upstream.Hostname = hostname[5:]
} else {
upstream.Hostname = hostname
upstream.Port = section.Key("port").MustInt(6667)
upstream.TLS = section.Key("tls").MustBool(false)
}
upstream.Timeout = section.Key("timeout").MustInt(10)
upstream.Throttle = section.Key("throttle").MustInt(2)
upstream.WebircPassword = section.Key("webirc").MustString("")
upstream.ServerPassword = section.Key("serverpassword").MustString("")
upstream.LocalAddr = section.Key("localaddr").MustString("")
upstream.GatewayName = section.Key("gateway_name").MustString("")
if strings.Contains(upstream.GatewayName, " ") {
c.gateway.Log(3, "Config option gateway_name must not contain spaces")
upstream.GatewayName = ""
}
upstream.NetworkCommonAddress = section.Key("network_common_address").MustString("")
c.Upstreams = append(c.Upstreams, upstream)
}
// "engines" is now legacy naming
if section.Name() == "engines" || section.Name() == "transports" {
for _, transport := range section.KeyStrings() {
c.ServerTransports = append(c.ServerTransports, strings.Trim(transport, "\n"))
}
}
if strings.Index(section.Name(), "plugins") == 0 {
for _, plugin := range section.KeyStrings() {
c.Plugins = append(c.Plugins, strings.Trim(plugin, "\n"))
}
}
if strings.Index(section.Name(), "allowed_origins") == 0 {
for _, origin := range section.KeyStrings() {
match, err := glob.Compile(origin)
if err != nil {
c.gateway.Log(3, "Config section allowed_origins has invalid match, "+origin)
continue
}
c.RemoteOrigins = append(c.RemoteOrigins, match)
}
}
if strings.Index(section.Name(), "gateway.whitelist") == 0 {
for _, origin := range section.KeyStrings() {
match, err := glob.Compile(origin)
if err != nil {
c.gateway.Log(3, "Config section gateway.whitelist has invalid match, "+origin)
continue
}
c.GatewayWhitelist = append(c.GatewayWhitelist, match)
}
}
if strings.Index(section.Name(), "reverse_proxies") == 0 {
for _, cidrRange := range section.KeyStrings() {
_, validRange, cidrErr := net.ParseCIDR(cidrRange)
if cidrErr != nil {
c.gateway.Log(3, "Config section reverse_proxies has invalid entry, "+cidrRange)
continue
}
c.ReverseProxies = append(c.ReverseProxies, *validRange)
}
}
}
return nil
}
func confKeyAsString(key *ini.Key, def string) string {
val := def
str := key.String()
if len(str) > 1 && str[:1] == "$" {
val = os.Getenv(str[1:])
} else {
val = key.MustString(def)
}
return val
}
func confKeyAsInt(key *ini.Key, def int) int {
val := def
str := key.String()
if len(str) > 1 && str[:1] == "$" {
envVal := os.Getenv(str[1:])
envValInt, err := strconv.Atoi(envVal)
if err == nil {
val = envValInt
}
} else {
val = key.MustInt(def)
}
return val
}
func confKeyAsBool(key *ini.Key, def bool) bool {
val := def
str := key.String()
if len(str) > 1 && str[:1] == "$" {
envVal := os.Getenv(str[1:])
if envVal == "0" || envVal == "false" || envVal == "no" {
val = false
} else {
val = true
}
} else {
val = key.MustBool(def)
}
return val
}
|