-
-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathfail2ban.go
More file actions
168 lines (137 loc) · 4.49 KB
/
Copy pathfail2ban.go
File metadata and controls
168 lines (137 loc) · 4.49 KB
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
// Package fail2ban contains the Fail2ban mechanism for the plugin.
package fail2ban
import (
"context"
"fmt"
"net/http"
"os"
"strings"
"github.com/tomMoulard/fail2ban/pkg/chain"
"github.com/tomMoulard/fail2ban/pkg/fail2ban"
f2bHandler "github.com/tomMoulard/fail2ban/pkg/fail2ban/handler"
"github.com/tomMoulard/fail2ban/pkg/ipchecking"
lAllow "github.com/tomMoulard/fail2ban/pkg/list/allow"
lDeny "github.com/tomMoulard/fail2ban/pkg/list/deny"
"github.com/tomMoulard/fail2ban/pkg/logger"
"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"
)
// List struct.
type List struct {
IP []string
Files []string
}
// SourceCriterion defines how to determine the client IP for fail2ban evaluation.
type SourceCriterion struct {
// RequestHeaderName is the HTTP header from which to read the client IP.
// Useful when running behind a proxy/CDN (e.g. "Cf-Connecting-Ip" for Cloudflare).
// When empty, r.RemoteAddr is used.
RequestHeaderName string `yaml:"requestHeaderName"`
}
// Config struct.
type Config struct {
Denylist List `yaml:"denylist"`
Allowlist List `yaml:"allowlist"`
Rules rules.Rules `yaml:"port"`
SourceCriterion SourceCriterion `yaml:"sourceCriterion"`
EnableBlockLogs bool `yaml:"enableBlockLogs"`
// 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,
},
EnableBlockLogs: 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 {
logger.Info("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 {
logger.Warn("Plugin: FailToBan: 'whitelist' is deprecated, please use 'allowlist' instead")
whiteips, err := ImportIP(config.Whitelist)
if err != nil {
return nil, fmt.Errorf("failed to parse whitelist IPs: %w", err)
}
allowIPs = append(allowIPs, whiteips...)
}
allowNetIPs, err := ipchecking.ParseNetIPs(allowIPs)
if err != nil {
return nil, fmt.Errorf("failed to parse allowlist IPs: %w", err)
}
allowHandler, err := lAllow.New(allowIPs)
if err != nil {
return nil, fmt.Errorf("failed to parse allowlist 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 {
logger.Warn("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, config.EnableBlockLogs)
if err != nil {
return nil, fmt.Errorf("failed to parse denylist IPs: %w", err)
}
rules, err := rules.TransformRule(config.Rules)
if err != nil {
return nil, fmt.Errorf("error when Transforming rules: %w", err)
}
f2b := fail2ban.New(rules, allowNetIPs)
c := chain.New(
next,
config.SourceCriterion.RequestHeaderName,
denyHandler,
allowHandler,
uDeny.New(rules.URLRegexpBan, f2b, config.EnableBlockLogs),
uAllow.New(rules.URLRegexpAllow),
f2bHandler.New(f2b, config.EnableBlockLogs),
)
if rules.StatusCode != "" {
statusCodeHandler, err := status.New(next, rules.StatusCode, f2b, config.EnableBlockLogs)
if err != nil {
return nil, fmt.Errorf("failed to create status handler: %w", err)
}
c.WithStatus(statusCodeHandler)
}
return c, nil
}