-
Notifications
You must be signed in to change notification settings - Fork 9
/
config.go
94 lines (77 loc) · 1.91 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
package main
import (
"errors"
"fmt"
"io/ioutil"
"net"
"regexp"
"gopkg.in/yaml.v3"
)
type Config struct {
DefaultBanishmentDuration uint
Whitelist []string
Rules []rule
}
func loadConfig(path string) (conf Config, err error) {
data, err := ioutil.ReadFile(path)
if err != nil {
return conf, err
}
m := make(map[interface{}]interface{})
err = yaml.Unmarshal([]byte(data), &m)
if err != nil {
return
}
//fmt.Printf("--- m:\n%v\n\n", m)
// DefaultBanishmentDuration
conf.DefaultBanishmentDuration = uint(m["defaultBanishmentDuration"].(int))
// whitelist
if m["whitelist"] != nil {
for _, ip := range m["whitelist"].([]interface{}) {
if net.ParseIP(ip.(string)) == nil {
return conf, fmt.Errorf("%s is not a valid IP for whitelist", ip.(string))
}
if conf.isIPWhitelisted(ip.(string)) {
return conf, fmt.Errorf("%s appears multiple time in your whitelist", ip.(string))
}
conf.Whitelist = append(conf.Whitelist, ip.(string))
}
}
// rules
for _, r := range m["rules"].([]interface{}) {
rule2add := rule{}
rs := r.(map[string]interface{})
// name
if rs["name"] == nil {
return conf, errors.New("required field 'name' is missing in a rule")
}
rule2add.Name = rs["name"].(string)
// ippos
if rs["IPpos"] != nil {
rule2add.IPpos = uint(rs["IPpos"].(int))
} else {
rule2add.IPpos = 0
}
// match
if rs["match"] == nil {
return conf, errors.New("required field 'match' is missing in a rule")
}
// to regex
rule2add.Match, err = regexp.Compile(rs["match"].(string))
if err != nil {
return conf, fmt.Errorf("failed to compile regex: %s, %v", rs["match"].(string), err)
}
// append rule
conf.Rules = append(conf.Rules, rule2add)
}
return
}
// check if ip is whitelisted
func (c Config) isIPWhitelisted(ip string) bool {
for _, ipw := range c.Whitelist {
if ip == ipw {
return true
}
}
return false
}