-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhandler.go
165 lines (143 loc) · 3.64 KB
/
handler.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
package ipfilterware
import (
"errors"
"fmt"
"net"
"net/http"
"net/netip"
"strings"
"sync/atomic"
)
// Handler to filter client by IP.
type Handler struct {
next http.Handler
ipFilter atomic.Value
}
// Config for the handler.
type Config struct {
// AllowedIPs list of IPs and/or CIDRs.
AllowedIPs []string
// ForbiddenHandler will be invoked when client is blocked.
// If nil then http.Error will be used.
ForbiddenHandler http.Handler
}
// New creates a new handler which wraps handler given based on a config.
func New(next http.Handler, cfg *Config) (*Handler, error) {
h := &Handler{
next: next,
}
if err := h.Update(cfg); err != nil {
return nil, err
}
return h, nil
}
// Wrap a given handler.
func (h *Handler) Wrap(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
filter := h.ipFilter.Load().(*ipFilter)
ip, err := ipFromRequest(r)
if err != nil {
filter.forbiddenHandler.ServeHTTP(w, r)
return
}
if filter.isAllowed(ip) {
next.ServeHTTP(w, r)
} else {
filter.forbiddenHandler.ServeHTTP(w, r)
}
})
}
// ServeHTTP implements http.Handler interface.
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
filter := h.ipFilter.Load().(*ipFilter)
ip, err := ipFromRequest(r)
if err != nil {
filter.forbiddenHandler.ServeHTTP(w, r)
return
}
if filter.isAllowed(ip) {
h.next.ServeHTTP(w, r)
} else {
filter.forbiddenHandler.ServeHTTP(w, r)
}
}
// Update the handler with a config in a concurrent safe way.
func (h *Handler) Update(cfg *Config) error {
ipf, err := newIPFilter(cfg)
if err != nil {
return err
}
h.ipFilter.Store(ipf)
return nil
}
// IsAllowed reports whether given IP is allowed.
func (h *Handler) IsAllowed(ip netip.Addr) bool {
filter := h.ipFilter.Load().(*ipFilter)
return filter.isAllowed(ip)
}
// Same as http.Error func.
var defaultForbiddenHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
w.Header().Set("X-Content-Type-Options", "nosniff")
w.WriteHeader(http.StatusForbidden)
fmt.Fprintln(w, http.StatusText(http.StatusForbidden))
})
type ipFilter struct {
allowedIP map[netip.Addr]struct{}
allowedCIDR []*net.IPNet
forbiddenHandler http.Handler
}
func newIPFilter(cfg *Config) (*ipFilter, error) {
if cfg == nil {
return nil, errors.New("config cannot be nil")
}
ipf := &ipFilter{
forbiddenHandler: defaultForbiddenHandler,
}
if cfg.ForbiddenHandler != nil {
ipf.forbiddenHandler = cfg.ForbiddenHandler
}
var err error
ipf.allowedIP, ipf.allowedCIDR, err = parseIPWithCIDR(cfg.AllowedIPs)
if err != nil {
return nil, err
}
return ipf, nil
}
func (ipf *ipFilter) isAllowed(ip netip.Addr) bool {
if !ip.IsValid() {
return false
}
if _, ok := ipf.allowedIP[ip]; ok {
return true
}
for _, cidr := range ipf.allowedCIDR {
if cidr.Contains(net.ParseIP(ip.String())) {
return true
}
}
return false
}
func ipFromRequest(r *http.Request) (netip.Addr, error) {
ip := r.RemoteAddr
if strings.IndexByte(r.RemoteAddr, byte(':')) >= 0 {
ip, _, _ = net.SplitHostPort(r.RemoteAddr)
}
return netip.ParseAddr(ip)
}
func parseIPWithCIDR(nets []string) (map[netip.Addr]struct{}, []*net.IPNet, error) {
ips := make(map[netip.Addr]struct{}, len(nets))
cidrs := make([]*net.IPNet, 0, len(nets))
for _, n := range nets {
if _, cidr, err := net.ParseCIDR(n); err == nil {
cidrs = append(cidrs, cidr)
continue
}
if ip := net.ParseIP(n); ip != nil {
ips[netip.MustParseAddr(n)] = struct{}{}
continue
}
return nil, nil, fmt.Errorf("bad IP or CIDR: %q", n)
}
return ips, cidrs, nil
}