forked from natureglobal/realip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
realip.go
208 lines (186 loc) · 4.83 KB
/
realip.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
package realip
import (
"errors"
"net"
"net/http"
"strings"
"sync"
)
// Popular Headers
const (
HeaderXForwardedFor = "x-forwarded-for"
HeaderXRealIP = "x-real-ip"
// we can also specify True-Client-IP, CF-Connecting-IP and so on to c.RealIPHeader
headerForwarded = "forwarded"
)
// Handler is the main object that acts as a middleware much like
// nginx's `ngx_http_realip_module`.
//
// Handlers must be constructed using the `Builder`. Using the zero
// value will result in undefined behavior.
type Handler struct {
trusted []*net.IPNet
srcHeader string // default: X-Real-IP. where to get the real IP from
dstHeader string // default: X-Real-IP. where to set the replacement IP to
recursive bool
next http.Handler
muCache *sync.RWMutex
cache map[string]struct{}
}
// Builder is used to construct a realip.Handler object.
type Builder struct {
h *Handler
err error
}
// New creates a new realip.Builder, which is used to
// construct a realip.Handler.
//
// The default configuration trusts all IP ranges, and uses the
// X-Real-IP header as the request header field whose value will
// be used to replace the client address.
func New() *Builder {
b := &Builder{}
b.Reset()
return b
}
func (b *Builder) Build() (*Handler, error) {
h, err := b.h, b.err
b.Reset()
if err != nil {
return nil, err
}
return h, nil
}
func (b *Builder) Reset() *Builder {
b.err = nil
b.h = &Handler{
srcHeader: HeaderXRealIP,
dstHeader: HeaderXRealIP,
cache: make(map[string]struct{}),
muCache: &sync.RWMutex{},
}
return b
}
// SourceHeader sets the header field to read the real IP from.
// Because this module does not supported the `Forwarded` header yet,
// specifying `Forwarded` will result in the builder failing to build
// a realip.Handler
//
// This is equivalent to `real_ip_header` directive in
// ngx_http_realip_module.
func (b *Builder) SourceHeader(name string) *Builder {
if b.err != nil {
return b
}
lowered := strings.ToLower(strings.TrimSpace(name))
if lowered == headerForwarded {
b.err = errors.New("realip.Builder: `Forwarded` header is not supported")
return b
}
b.h.srcHeader = lowered
return b
}
// DestinationHeader sets the header field to set the real IP to.
func (b *Builder) DestinationHeader(header string) *Builder {
if b.err != nil {
return b
}
b.h.dstHeader = strings.ToLower(header)
return b
}
// TrustedIP sets the list of IP ranges that are trusted to
// provide the real IP address of the client.
//
// This is equivalent to `set_real_ip_from` directive in
// ngx_http_realip_module.
func (b *Builder) TrustedIP(trusted ...*net.IPNet) *Builder {
if b.err != nil {
return b
}
b.h.trusted = append(b.h.trusted, trusted...)
return b
}
// Recursive enables the recursive mode, which will look for
// the most recent non-trusted IP address in the header field.
//
// This is equivalent to `real_ip_recursive` directive in
// ngx_http_realip_module.
func (b *Builder) Recursive(v bool) *Builder {
if b.err != nil {
return b
}
b.h.recursive = v
return b
}
// Wrap returns a http.Handler that wraps the given handler with the
// realip.Handler object, allowing you to use it as a middleware.
//
// For frameworks that require a http.HandleFunc instead of http.Handler,
// simply use this method and extract the `ServeHTTP` method from the
// return value, and pass it to the framework.
func (h *Handler) Wrap(next http.Handler) http.Handler {
h.next = next
return h
}
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
rawRemoteIP, _, _ := net.SplitHostPort(r.RemoteAddr)
if !h.trustedIP(net.ParseIP(rawRemoteIP), h.trusted) {
if rawRemoteIP != "" {
r.Header.Set(h.dstHeader, rawRemoteIP)
}
h.next.ServeHTTP(w, r)
return
}
var realIP string
switch h.srcHeader { // note: h.srcHeader is guaranteed to be lower cased
case HeaderXForwardedFor:
realIP = h.realIPFromXFF(r.Header.Get(HeaderXForwardedFor))
default:
realIP = r.Header.Get(h.srcHeader)
}
if realIP == "" {
realIP = rawRemoteIP
}
if realIP != "" {
r.Header.Set(h.dstHeader, realIP)
}
h.next.ServeHTTP(w, r)
}
func (h *Handler) trustedIP(ip net.IP, trusted []*net.IPNet) bool {
if len(trusted) == 0 {
// trust everybody
return true
}
ipstr := ip.String()
h.muCache.RLock()
_, cached := h.cache[ipstr]
h.muCache.RUnlock()
if cached {
return true
}
for _, fromIP := range trusted {
if fromIP.Contains(ip) {
h.muCache.Lock()
h.cache[ipstr] = struct{}{}
h.muCache.Unlock()
return true
}
}
return false
}
func (h *Handler) realIPFromXFF(xff string) string {
ips := strings.Split(xff, ",")
if len(ips) == 0 {
return ""
}
if !h.recursive {
return strings.TrimSpace(ips[len(ips)-1])
}
for i := len(ips) - 1; i >= 0; i-- {
ipStr := strings.TrimSpace(ips[i])
if !h.trustedIP(net.ParseIP(ipStr), h.trusted) {
return ipStr
}
}
return strings.TrimSpace(ips[0])
}