-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.go
30 lines (26 loc) · 852 Bytes
/
util.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
package web
import (
"net"
"net/http"
)
// RealRemoteAddr will try to get the real IP address of the incoming connection taking proxies into
// consideration. This function looks for the `X-Real-IP`, `X-Forwarded-For`, and `CF-Connecting-IP`
// headers, and if those don't exist will return the remote address of the connection.
//
// Will never return nil, if it is unable to get a valid address it will return 0.0.0.0
func RealRemoteAddr(r *http.Request) net.IP {
if ip := net.ParseIP(r.Header.Get("X-Real-IP")); ip != nil {
return ip
}
if ip := net.ParseIP(r.Header.Get("X-Forwarded-For")); ip != nil {
return ip
}
if ip := net.ParseIP(r.Header.Get("CF-Connecting-IP")); ip != nil {
return ip
}
ipStr, _, _ := net.SplitHostPort(r.RemoteAddr)
if ip := net.ParseIP(ipStr); ip != nil {
return ip
}
return net.IPv4(0, 0, 0, 0)
}