-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathfail2ban.go
156 lines (125 loc) · 3.69 KB
/
fail2ban.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
// Package fail2ban contains the Fail2ban mechanism for the plugin.
package fail2ban
import (
"context"
"fmt"
"log"
"net/http"
"os"
"strings"
"github.com/tomMoulard/fail2ban/pkg/chain"
"github.com/tomMoulard/fail2ban/pkg/fail2ban"
f2bHandler "github.com/tomMoulard/fail2ban/pkg/fail2ban/handler"
lAllow "github.com/tomMoulard/fail2ban/pkg/list/allow"
lDeny "github.com/tomMoulard/fail2ban/pkg/list/deny"
"github.com/tomMoulard/fail2ban/pkg/response/status"
"github.com/tomMoulard/fail2ban/pkg/rules"
uAllow "github.com/tomMoulard/fail2ban/pkg/url/allow"
uDeny "github.com/tomMoulard/fail2ban/pkg/url/deny"
)
func init() {
log.SetOutput(os.Stdout)
}
// List struct.
type List struct {
IP []string
Files []string
}
// Config struct.
type Config struct {
Denylist List `yaml:"denylist"`
Allowlist List `yaml:"allowlist"`
Rules rules.Rules `yaml:"port"`
// deprecated
Blacklist List `yaml:"blacklist"`
// deprecated
Whitelist List `yaml:"whitelist"`
}
// CreateConfig populates the Config data object.
func CreateConfig() *Config {
return &Config{
Rules: rules.Rules{
Bantime: "300s",
Findtime: "120s",
Enabled: true,
},
}
}
// ImportIP extract all ip from config sources.
func ImportIP(list List) ([]string, error) {
var rlist []string
for _, ip := range list.Files {
content, err := os.ReadFile(ip)
if err != nil {
return nil, fmt.Errorf("error when getting file content: %w", err)
}
rlist = append(rlist, strings.Split(string(content), "\n")...)
if len(rlist) > 1 {
rlist = rlist[:len(rlist)-1]
}
}
rlist = append(rlist, list.IP...)
return rlist, nil
}
// New instantiates and returns the required components used to handle a HTTP
// request.
func New(_ context.Context, next http.Handler, config *Config, _ string) (http.Handler, error) {
if !config.Rules.Enabled {
log.Println("Plugin: FailToBan is disabled")
return next, nil
}
allowIPs, err := ImportIP(config.Allowlist)
if err != nil {
return nil, fmt.Errorf("failed to parse allowlist IPs: %w", err)
}
if len(config.Whitelist.IP) > 0 || len(config.Whitelist.Files) > 0 {
log.Println("Plugin: FailToBan: 'whitelist' is deprecated, please use 'denylist' instead")
whiteips, err := ImportIP(config.Whitelist)
if err != nil {
return nil, fmt.Errorf("failed to parse whitelist IPs: %w", err)
}
allowIPs = append(allowIPs, whiteips...)
}
allowHandler, err := lAllow.New(allowIPs)
if err != nil {
return nil, fmt.Errorf("failed to parse whitelist IPs: %w", err)
}
denyIPs, err := ImportIP(config.Denylist)
if err != nil {
return nil, fmt.Errorf("failed to parse denylist IPs: %w", err)
}
if len(config.Blacklist.IP) > 0 || len(config.Blacklist.Files) > 0 {
log.Println("Plugin: FailToBan: 'blacklist' is deprecated, please use 'denylist' instead")
blackips, err := ImportIP(config.Blacklist)
if err != nil {
return nil, fmt.Errorf("failed to parse blacklist IPs: %w", err)
}
denyIPs = append(denyIPs, blackips...)
}
denyHandler, err := lDeny.New(denyIPs)
if err != nil {
return nil, fmt.Errorf("failed to parse blacklist IPs: %w", err)
}
rules, err := rules.TransformRule(config.Rules)
if err != nil {
return nil, fmt.Errorf("error when Transforming rules: %w", err)
}
log.Println("Plugin: FailToBan is up and running")
f2b := fail2ban.New(rules)
c := chain.New(
next,
denyHandler,
allowHandler,
uDeny.New(rules.URLRegexpBan, f2b),
uAllow.New(rules.URLRegexpAllow),
f2bHandler.New(f2b),
)
if rules.StatusCode != "" {
statusCodeHandler, err := status.New(next, rules.StatusCode, f2b)
if err != nil {
return nil, fmt.Errorf("failed to create status handler: %w", err)
}
c.WithStatus(statusCodeHandler)
}
return c, nil
}