-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTraefikIPRules.go
More file actions
258 lines (217 loc) · 5.34 KB
/
TraefikIPRules.go
File metadata and controls
258 lines (217 loc) · 5.34 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
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
package TraefikIPRules
import (
"context"
"fmt"
"net"
"net/http"
"strings"
)
type Config struct {
Deny []string `json:"deny,omitempty"`
Allow []string `json:"allow,omitempty"`
Precedence string `json:"precedence,omitempty"` // "allow" or "deny"
}
type ipRange struct {
start net.IP
end net.IP
}
func CreateConfig() *Config {
return &Config{
Deny: make([]string, 0),
Allow: make([]string, 0),
Precedence: "deny", // Default to deny
}
}
type IPProcessor struct {
next http.Handler
name string
denyCIDRs []*net.IPNet
denyIPs []net.IP
denyRanges []ipRange
allowCIDRs []*net.IPNet
allowIPs []net.IP
allowRanges []ipRange
precedence string
}
func parseIPRange(ipRangeStr string) (*ipRange, error) {
parts := strings.Split(ipRangeStr, "-")
if len(parts) != 2 {
return nil, fmt.Errorf("invalid IP range format: %s", ipRangeStr)
}
start := net.ParseIP(strings.TrimSpace(parts[0]))
end := net.ParseIP(strings.TrimSpace(parts[1]))
if start == nil || end == nil {
return nil, fmt.Errorf("invalid IP address in range: %s", ipRangeStr)
}
if start.To4() == nil || end.To4() == nil {
return nil, fmt.Errorf("only IPv4 ranges are supported: %s", ipRangeStr)
}
for i := 0; i < len(start.To4()); i++ {
if start.To4()[i] > end.To4()[i] {
return nil, fmt.Errorf("invalid range: start IP must be less than end IP")
}
if start.To4()[i] < end.To4()[i] {
break
}
}
return &ipRange{
start: start.To4(),
end: end.To4(),
}, nil
}
func (r *ipRange) IPRangeContains(ip net.IP) bool {
if ip.To4() == nil {
return false
}
ip = ip.To4()
for i := 0; i < 4; i++ {
if ip[i] < r.start[i] || ip[i] > r.end[i] {
return false
}
if ip[i] > r.start[i] || ip[i] < r.end[i] {
break
}
}
return true
}
func New(ctx context.Context, next http.Handler, config *Config, name string) (http.Handler, error) {
if config.Precedence != "" && config.Precedence != "allow" && config.Precedence != "deny" {
return nil, fmt.Errorf("invalid precedence value: %s. Must be either 'allow' or 'deny'", config.Precedence)
}
processor := &IPProcessor{
next: next,
name: name,
precedence: config.Precedence,
}
// Process deny rules
for _, rule := range config.Deny {
// Try parsing as a single IP
if ip := net.ParseIP(rule); ip != nil {
processor.denyIPs = append(processor.denyIPs, ip)
continue
}
// Try parsing as CIDR
_, network, err := net.ParseCIDR(rule)
if err == nil {
processor.denyCIDRs = append(processor.denyCIDRs, network)
continue
}
// Try parsing as IP range
ipRange, err := parseIPRange(rule)
if err == nil {
processor.denyRanges = append(processor.denyRanges, *ipRange)
continue
}
return nil, fmt.Errorf("invalid IP, CIDR, or range in deny list: %s", rule)
}
// Process allow rules
for _, rule := range config.Allow {
// Try parsing as a single IP
if ip := net.ParseIP(rule); ip != nil {
processor.allowIPs = append(processor.allowIPs, ip)
continue
}
// Try parsing as CIDR
_, network, err := net.ParseCIDR(rule)
if err == nil {
processor.allowCIDRs = append(processor.allowCIDRs, network)
continue
}
// Try parsing as IP range
ipRange, err := parseIPRange(rule)
if err == nil {
processor.allowRanges = append(processor.allowRanges, *ipRange)
continue
}
return nil, fmt.Errorf("invalid IP, CIDR, or range in allow list: %s", rule)
}
return processor, nil
}
func (p *IPProcessor) getIP(req *http.Request) net.IP {
xff := req.Header.Get("X-Forwarded-For")
if xff != "" {
ips := strings.Split(xff, ",")
if len(ips) > 0 {
if ip := net.ParseIP(strings.TrimSpace(ips[0])); ip != nil {
return ip
}
}
}
host, _, err := net.SplitHostPort(req.RemoteAddr)
if err != nil {
if ip := net.ParseIP(req.RemoteAddr); ip != nil {
return ip
}
return nil
}
return net.ParseIP(host)
}
func (p *IPProcessor) checkIPInDenyList(clientIP net.IP) bool {
for _, denyIP := range p.denyIPs {
if denyIP.Equal(clientIP) {
return true
}
}
for _, denyCIDR := range p.denyCIDRs {
if denyCIDR.Contains(clientIP) {
return true
}
}
for _, denyRange := range p.denyRanges {
if denyRange.IPRangeContains(clientIP) {
return true
}
}
return false
}
func (p *IPProcessor) checkIPInAllowList(clientIP net.IP) bool {
if len(p.allowIPs) == 0 && len(p.allowCIDRs) == 0 && len(p.allowRanges) == 0 {
return false
}
for _, allowIP := range p.allowIPs {
if allowIP.Equal(clientIP) {
return true
}
}
for _, allowCIDR := range p.allowCIDRs {
if allowCIDR.Contains(clientIP) {
return true
}
}
for _, allowRange := range p.allowRanges {
if allowRange.IPRangeContains(clientIP) {
return true
}
}
return false
}
func (p *IPProcessor) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
clientIP := p.getIP(req)
if clientIP == nil {
http.Error(rw, "Invalid IP address", http.StatusForbidden)
return
}
var allowed bool
if p.precedence == "allow" {
if p.checkIPInAllowList(clientIP) {
allowed = true
} else if p.checkIPInDenyList(clientIP) {
allowed = false
} else {
allowed = false
}
} else {
if p.checkIPInDenyList(clientIP) {
allowed = false
} else if p.checkIPInAllowList(clientIP) {
allowed = true
} else {
allowed = false
}
}
if !allowed {
http.Error(rw, "Access denied", http.StatusForbidden)
return
}
p.next.ServeHTTP(rw, req)
}