-
Notifications
You must be signed in to change notification settings - Fork 1
/
remote_address.go
72 lines (62 loc) · 1.56 KB
/
remote_address.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
package util
import (
"fmt"
"net"
"net/http"
"strings"
)
var privateIPBlocks []*net.IPNet
func init() {
for _, cidr := range []string{
"127.0.0.0/8", // IPv4 loopback
"10.0.0.0/8", // RFC1918
"172.16.0.0/12", // RFC1918
"192.168.0.0/16", // RFC1918
"::1/128", // IPv6 loopback
"fe80::/10", // IPv6 link-local
"fc00::/7", // IPv6 unique local addr
} {
_, block, _ := net.ParseCIDR(cidr)
privateIPBlocks = append(privateIPBlocks, block)
}
}
// RemoteAddress resolves the address of a HTTP request to the real remote
// address. It takes in account proxies if the request is originated from a
// private IP range
func RemoteAddress(r *http.Request) (addr string) {
var err error
addr = r.RemoteAddr
if addr == "@" { // Support unix domain sockets
addr = "127.0.0.1:80"
}
addr, _, err = net.SplitHostPort(addr)
if err != nil {
panic(fmt.Errorf("failed to parse request RemoteAddr: %s", err))
}
if AddressIsLocal(addr) {
// This IP is in a private range, so we can trust its proxy headers,
// check if it has any
if h := r.Header.Get("X-Real-IP"); h != "" {
return h
} else if h := r.Header.Get("X-Forwarded-For"); h != "" {
return strings.Split(h, ", ")[0]
}
}
return addr
}
// AddressIsLocal checks if an IP address falls on a private subnet
func AddressIsLocal(ip string) bool {
if ip == "@" { // Support unix domain sockets
ip = "127.0.0.1"
}
ipp := net.ParseIP(ip)
if ipp == nil {
return false
}
for _, block := range privateIPBlocks {
if block.Contains(ipp) {
return true
}
}
return false
}