-
Notifications
You must be signed in to change notification settings - Fork 0
/
banjax.go
369 lines (317 loc) · 9.89 KB
/
banjax.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
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
// Copyright (c) 2020, eQualit.ie inc.
// All rights reserved.
//
// This source code is licensed under the BSD-style license found in the
// LICENSE file in the root directory of this source tree.
package main
import (
"embed"
"encoding/json"
"flag"
"fmt"
"log"
"os"
"os/signal"
"regexp"
"sync"
"syscall"
"time"
"github.com/coreos/go-iptables/iptables"
"github.com/deflect-ca/banjax/internal"
"github.com/gonetx/ipset"
"gopkg.in/yaml.v2"
)
//go:embed internal/sha-inverse-challenge.html
var shaInvChallengeEmbed embed.FS
//go:embed internal/password-protected-path.html
var passProtPathEmbed embed.FS
func load_config(config *internal.Config, standaloneTestingPtr *bool, configFilenamePtr *string, restartTime int, debugPtr *bool) {
config.RestartTime = restartTime
config.ReloadTime = int(time.Now().Unix()) // XXX
hostname, err := os.Hostname()
if err != nil {
log.Println("couldn't get hostname! using dummy")
hostname = "unknown-hostname"
}
config.Hostname = hostname
log.Printf("INIT: hostname: %s", hostname)
configBytes, err := os.ReadFile(*configFilenamePtr) // XXX allow different location
if err != nil {
panic(err)
}
// log.Printf("read %v\n", string(configBytes[:]))
config.StandaloneTesting = *standaloneTestingPtr
err = yaml.Unmarshal(configBytes, config)
if err != nil {
log.Fatal(err)
}
// boolean default = false
if config.Debug {
log.Printf("read config %v\n", *config)
}
if config.ShaInvChallengeHTML != "" {
log.Printf("INIT: Reading SHA-inverse challenge HTML from %s", config.ShaInvChallengeHTML)
challengerBytes, err := os.ReadFile(config.ShaInvChallengeHTML)
if err != nil {
panic("!!! couldn't read sha-inverse-challenge.html")
}
config.ChallengerBytes = challengerBytes
} else {
log.Printf("INIT: Reading SHA-inverse challenge HTML from embed")
challengerBytes, err := shaInvChallengeEmbed.ReadFile("internal/sha-inverse-challenge.html")
if err != nil {
panic("!!! couldn't read sha-inverse-challenge.html")
}
config.ChallengerBytes = challengerBytes
}
if config.PasswordProtectedPathHTML != "" {
log.Printf("INIT: Reading Password protected path HTML from %s", config.PasswordProtectedPathHTML)
passwordPageBytes, err := os.ReadFile(config.PasswordProtectedPathHTML)
if err != nil {
panic("!!! couldn't read password-protected-path.html")
}
config.PasswordPageBytes = passwordPageBytes
} else {
log.Printf("INIT: Reading Password protected path HTML from embed")
passwordPageBytes, err := passProtPathEmbed.ReadFile("internal/password-protected-path.html")
if err != nil {
panic("!!! couldn't read password-protected-path.html")
}
config.PasswordPageBytes = passwordPageBytes
}
for i, _ := range config.RegexesWithRates {
re, err := regexp.Compile(config.RegexesWithRates[i].Regex)
if err != nil {
panic("bad regex")
}
config.RegexesWithRates[i].CompiledRegex = *re
}
for site, p_regex := range config.PerSiteRegexWithRates {
log.Printf("PerSiteRegexWithRates: %s\n", site)
for i, _ := range p_regex {
re, err := regexp.Compile(config.PerSiteRegexWithRates[site][i].Regex)
if err != nil {
panic("bad regex")
}
config.PerSiteRegexWithRates[site][i].CompiledRegex = *re
}
}
if config.Debug {
for site, failAction := range config.SitewideShaInvList {
log.Printf("load_config: sitewide site: %s, failAction: %s\n", site, failAction)
}
}
if !config.Debug && *debugPtr {
log.Printf("debug mode enabled by command line param")
config.Debug = true
}
if config.StandaloneTesting {
config.DisableKafka = true
}
}
const (
IPSetName = "banjax_ipset"
)
func init_ipset(config *internal.Config) ipset.IPSet {
if config.StandaloneTesting {
log.Println("init_ipset: Not init ipset in testing")
return nil
}
if err := ipset.Check(); err != nil {
log.Println("init_ipset: ipset.Check() failed")
panic(err)
}
newIPset, err := ipset.New(
IPSetName,
ipset.HashIp,
ipset.Exist(true),
ipset.Timeout(time.Duration(config.IptablesBanSeconds)*time.Second))
if err != nil {
log.Println("init_ipset: ipset.New() failed")
panic(err)
}
log.Println("init_ipset: new ipset:", newIPset.Name())
// enable ipset with iptables
// iptables -I INPUT -m set --match-set banjax_ipset src -j DROP
ipt, err := iptables.New()
if err != nil {
log.Println("init_ipset: iptables.New() failed")
panic(err)
}
err = ipt.Insert("filter", "INPUT", 1, "-m", "set", "--match-set", IPSetName, "src", "-j", "DROP")
if err != nil {
log.Println("init_ipset: iptables.Insert() failed, did not enable ipset")
panic(err)
}
return newIPset
}
func main() {
// XXX protects ipToRegexStates and failedChallengeStates
// (why both? because there are too many parameters already?)
var rateLimitMutex sync.Mutex
ipToRegexStates := internal.IpToRegexStates{}
failedChallengeStates := internal.FailedChallengeStates{}
var passwordProtectedPaths internal.PasswordProtectedPaths
// XXX protects decisionLists
var decisionListsMutex sync.Mutex
var decisionLists internal.DecisionLists
standaloneTestingPtr := flag.Bool("standalone-testing", false, "makes it easy to test standalone")
configFilenamePtr := flag.String("config-file", "/etc/banjax/banjax-config.yaml", "config file")
debugPtr := flag.Bool("debug", false, "debug mode with verbose logging")
flag.Parse()
restartTime := int(time.Now().Unix()) // XXX
log.Println("INIT: config file: ", *configFilenamePtr)
config := internal.Config{}
load_config(&config, standaloneTestingPtr, configFilenamePtr, restartTime, debugPtr)
sighup_channel := make(chan os.Signal, 1)
signal.Notify(sighup_channel, syscall.SIGHUP)
// XXX i forgot i had this config reload functionality.
// i don't think this will do everything it needs to. i think we'll need to restart
// RunHttpServer, RunLogTailer, etc. because they might have internal state that
// referenced old config values.
go func() {
for _ = range sighup_channel {
log.Println("HOT-RELOAD: got SIGHUP; reloading config")
rateLimitMutex.Lock()
config = internal.Config{}
load_config(&config, standaloneTestingPtr, configFilenamePtr, restartTime, debugPtr)
rateLimitMutex.Unlock()
configToStructs(&config, &passwordProtectedPaths, &decisionLists)
}
}()
if config.StandaloneTesting {
log.Println("!!! setting ServerLogFile to testing-log-file.txt")
config.ServerLogFile = "testing-log-file.txt"
config.BanningLogFile = "banning-log-file.txt"
}
if config.ServerLogFile == "" {
panic("config needs server_log_file!!")
}
// XXX should i verify all the config stuff is here before we get further?
if config.IptablesBanSeconds == 0 {
panic("config needs iptables_ban_seconds!!")
}
// XXX should i verify all the config stuff is here before we get further?
if len(config.KafkaBrokers) < 1 {
panic("config needs kafka_brokers!!")
}
log.Println("INIT: Kafka brokers: ", config.KafkaBrokers)
configToStructs(&config, &passwordProtectedPaths, &decisionLists)
// XXX this interface exists to make mocking out the iptables stuff
// in testing easier. there might be a better way to do it.
// at least it encapsulates the decisionlists and their mutex
// together, which should probably happen for the other things
// protected by a mutex.
banningLogFile, err := os.OpenFile(config.BanningLogFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Fatal(err)
}
if config.BanningLogFileTemp == "" {
config.BanningLogFileTemp = fmt.Sprintf("%s.tmp", config.BanningLogFile)
}
banningLogFileTemp, err := os.OpenFile(config.BanningLogFileTemp, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Fatal(err)
}
defer banningLogFile.Close()
defer banningLogFileTemp.Close()
banner := internal.Banner{
&decisionListsMutex,
&decisionLists,
log.New(banningLogFile, "", 0),
log.New(banningLogFileTemp, "", 0),
init_ipset(&config),
}
var wg sync.WaitGroup
wg.Add(1)
go internal.RunHttpServer(
&config,
&decisionListsMutex,
&decisionLists,
&passwordProtectedPaths,
&rateLimitMutex,
&ipToRegexStates,
&failedChallengeStates,
banner,
&wg,
)
wg.Add(1)
go internal.RunLogTailer(
&config,
banner,
&rateLimitMutex,
&ipToRegexStates,
&decisionListsMutex,
&decisionLists,
&wg,
)
if !config.DisableKafka {
log.Println("INIT: starting RunKafkaReader/RunKafkaWriter")
wg.Add(1)
go internal.RunKafkaReader(
&config,
&decisionListsMutex,
&decisionLists,
&wg,
)
wg.Add(1)
go internal.RunKafkaWriter(
&config,
&wg,
)
} else {
log.Println("INIT: not running RunKafkaReader/RunKafkaWriter due to config.DisableKafka")
}
metricsLogFileName := ""
if config.StandaloneTesting {
metricsLogFileName = "list-metrics.log"
} else {
metricsLogFileName = config.MetricsLogFileName
}
metricsLogFile, _ := os.Create(metricsLogFileName)
defer metricsLogFile.Close()
metricsLogEncoder := json.NewEncoder(metricsLogFile)
// statusTicker := time.NewTicker(5 * time.Second)
expireTicker := time.NewTicker(9 * time.Second)
statusTicker := time.NewTicker(19 * time.Second)
metricsTicker := time.NewTicker(29 * time.Second)
go func() {
for {
select {
case <-statusTicker.C:
// log.Println("calling ReportStatusMessage")
if !config.DisableKafka {
internal.ReportStatusMessage(
&config,
)
}
case <-expireTicker.C:
internal.RemoveExpiredDecisions(
&decisionListsMutex,
&decisionLists,
)
case <-metricsTicker.C:
internal.WriteMetricsToEncoder(
metricsLogEncoder,
&decisionListsMutex,
&decisionLists,
&rateLimitMutex,
&ipToRegexStates,
&failedChallengeStates,
)
}
}
}()
wg.Wait()
}
var configToStructsMutex sync.Mutex
func configToStructs(
config *internal.Config,
passwordProtectedPaths *internal.PasswordProtectedPaths,
decisionLists *internal.DecisionLists,
) {
configToStructsMutex.Lock()
defer configToStructsMutex.Unlock()
*passwordProtectedPaths = internal.ConfigToPasswordProtectedPaths(config)
*decisionLists = internal.ConfigToDecisionLists(config)
}