-
Notifications
You must be signed in to change notification settings - Fork 0
/
iputil.go
71 lines (58 loc) · 1.63 KB
/
iputil.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
package main
import (
"log"
"net"
"net/http"
"strings"
)
var cidrs []*net.IPNet
func init() {
lancidrs := []string{
"127.0.0.1/8", "10.0.0.0/8", "169.254.0.0/16", "172.16.0.0/12", "192.168.0.0/16", "::1/128", "fc00::/7",
}
cidrs = make([]*net.IPNet, len(lancidrs))
for i, it := range lancidrs {
_, cidrnet, err := net.ParseCIDR(it)
if err != nil {
log.Fatalf("ParseCIDR error: %v", err) // assuming I did it right above
}
cidrs[i] = cidrnet
}
}
func isLocalAddress(addr string) bool {
for i := range cidrs {
myaddr := net.ParseIP(addr)
if cidrs[i].Contains(myaddr) {
return true
}
}
return false
}
// Request.RemoteAddress contains port, which we want to remove i.e.:
// "[::1]:58292" => "[::1]"
func ipAddrFromRemoteAddr(s string) string {
idx := strings.LastIndex(s, ":")
if idx == -1 {
return s
}
return s[:idx]
}
// RealIP return client's real public IP address
// from http request headers.
func RealIP(r *http.Request) string {
hdr := r.Header
hdrRealIP := hdr.Get("X-Real-Ip")
hdrForwardedFor := hdr.Get("X-Forwarded-For")
if len(hdrForwardedFor) == 0 && len(hdrRealIP) == 0 {
return ipAddrFromRemoteAddr(r.RemoteAddr)
}
// X-Forwarded-For is potentially a list of addresses separated with ","
for _, addr := range strings.Split(hdrForwardedFor, ",") {
// return first non-local address
addr = strings.TrimSpace(addr)
if len(addr) > 0 && !isLocalAddress(addr) {
return addr
}
}
return hdrRealIP
}