forked from weregoat/policyd-geoip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsettings.go
178 lines (166 loc) · 4.53 KB
/
settings.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
package main
import (
"fmt"
"github.com/oschwald/geoip2-golang"
"github.com/weregoat/goat-whois/pkg/whois"
"github.com/weregoat/goat-whois/pkg/whois/sources/program"
"gopkg.in/yaml.v2"
"io/ioutil"
"path/filepath"
"strings"
)
// Configuration is the struct holding the description of the YAML fields.
type Configuration struct {
Path string
Debug bool `yaml:"debug"`
Blacklist []string `yaml:"blacklist"`
GeoIP2Database string `yaml:"geoip2_database"`
Whitelist []string `yaml:"whitelist"`
Facility string `yaml:"syslog_facility"`
Tag string `yaml:"syslog_tag"`
RejectMessage string `yaml:"reject_message"`
WhoisProgram string `yaml:"whois_program"`
}
// Settings is the struct holding the properties required by the program after
// they have been parsed from the configuration.
type Settings struct {
Debug bool
Syslog Syslog
GeoIP2Database string
BlackList []string
WhiteList []string
WhoisClient *whois.Client
RejectMessage string
Configuration Configuration
}
// loadConfiguration opens a configuration file and parses the YAML into a struct.
func loadConfiguration(path string) (Configuration, error) {
var config Configuration
filename, err := filepath.Abs(path)
if err != nil {
return config, err
}
config.Path = filename
yamlFile, err := ioutil.ReadFile(filename)
if err != nil {
return config, err
}
err = yaml.Unmarshal(yamlFile, &config)
return config, err
}
// parseConfiguration converts the properties of a Configuration struct into
// a Settings struct.
func parseConfiguration(config Configuration) (settings Settings, err error) {
settings.Configuration = config
debug := config.Debug
facility, err := getFacility(config.Facility)
logger := Syslog{
Facility: facility,
Debugging: debug,
Tag: config.Tag,
}
if err != nil {
logger.Warning(
fmt.Sprintf(
"Failed to intialize syslog logging: %s",
err.Error(),
),
)
}
settings.Syslog = logger
settings.BlackList = getList(logger, "blacklist", config.Blacklist...)
settings.WhiteList = getList(logger, "whitelist", config.Whitelist...)
if len(config.GeoIP2Database) > 0 {
settings.GeoIP2Database, err = geoip2DatabasePath(config.GeoIP2Database)
if err != nil {
logger.Warning(err.Error())
}
} else {
logger.Notice(
fmt.Sprintf(
"No GeoIP2 database path defined in configuration file %s",
config.Path,
),
)
}
if len(config.WhoisProgram) > 0 {
source, err := program.New(config.WhoisProgram)
if err != nil {
logger.Warning(
fmt.Sprintf(
"Error accessing Whois program at %s: %s",
config.WhoisProgram,
err.Error(),
),
)
} else {
client := whois.New(source)
settings.WhoisClient = &client
}
}
rejectMessage := strings.TrimSpace(config.RejectMessage)
if len(rejectMessage) > 0 {
settings.RejectMessage = rejectMessage
}
return
}
// getList tries to build unique white and blacklists.
func getList(logger Syslog, name string, list ...string) []string {
var entries []string
for _, entry := range list {
entry = strings.TrimSpace(entry)
// Special treatment for IsoCodes
switch name {
case "blacklist":
if len(entry) == 2 {
entry = strings.ToUpper(entry)
} else {
logger.Notice(fmt.Sprintf("ignoring invalid string '%s' for ISO Country code", entry))
entry = ""
}
}
duplicate := false
if len(entry) > 0 {
for _, present := range entries {
if entry == present {
message := fmt.Sprintf("duplicated entry '%s' in %s", entry, name)
logger.Debug(message)
duplicate = true
}
}
if !duplicate {
entries = append(entries, entry)
}
}
}
return entries
}
// geoip2DatabasePath checks the path to the GeoIP2 works.
func geoip2DatabasePath(path string) (checked string, err error) {
var db *geoip2.Reader
checked, err = filepath.Abs(path)
if err != nil {
return
} else {
db, err = geoip2.Open(checked)
if err != nil {
return
}
defer db.Close()
return
}
}
// Show will return strings with the properties.
func (s Settings) Show() []string {
settings := []string{
fmt.Sprintf("Configuration file: %s", s.Configuration.Path),
fmt.Sprintf("GeoIP2 database: %s", s.GeoIP2Database),
fmt.Sprintf("Syslog tag: %s", s.Syslog.Tag),
fmt.Sprintf("Syslog facility: %s", getPriorityName(s.Syslog.Facility)),
fmt.Sprintf("Blacklist: %q", s.BlackList),
fmt.Sprintf("Whitelist: %q", s.WhiteList),
fmt.Sprintf("Whois program: %s", s.Configuration.WhoisProgram),
fmt.Sprintf("reject message: %s", s.RejectMessage),
}
return settings
}