This repository was archived by the owner on Apr 28, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathremoteaddr.go
65 lines (55 loc) · 1.79 KB
/
remoteaddr.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
package remoteaddr
import "net/http"
// RemoteAddr http handler
type RemoteAddr struct {
}
const (
TrueClientIP = "True-Client-IP" // Edge providers (such as Akamai)
TrueRealIP = "True-Real-IP" // Proxies (like Nginx)
XForwardedFor = "X-Forwarded-For" // External proxies (like an ELB or router)
XOriginatingIP = "X-Originating-IP" // Defacto email header
)
// New creates a new RemoteAddr handler
func New() *RemoteAddr {
r := &RemoteAddr{}
return r
}
// Handler provides a http.HandlerFunc interface
func (ra *RemoteAddr) Handler(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ra.handleActualRequest(w, r)
h.ServeHTTP(w, r)
})
}
// HandlerFunc provides a Martini compatible handler interface
func (ra *RemoteAddr) HandlerFunc(w http.ResponseWriter, r *http.Request) {
ra.handleActualRequest(w, r)
}
// ServeHTTP provides a Negroni compatible interface
func (ra *RemoteAddr) ServeHTTP(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
ra.handleActualRequest(w, r)
next(w, r)
}
// handleActualRequest applies a filterr function that attempts to source the
// address of a remote client through potential network manipulation. Once
// attached, it replaces the `RemoteAddr` so it can be treated as trusted.
func (ra *RemoteAddr) handleActualRequest(w http.ResponseWriter, r *http.Request) {
var ipAddress string
var ipSources = []string{
r.Header.Get(TrueClientIP),
r.Header.Get(TrueRealIP),
r.Header.Get(XForwardedFor),
r.Header.Get(XOriginatingIP),
}
// Iterate the ipSources to determine the valid address
for _, ip := range ipSources {
if ip != "" {
ipAddress = ip
break
}
}
// Set the RemoteAddr to the determine IP address, if detected
if ipAddress != "" {
r.RemoteAddr = ipAddress
}
}