-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathip.go
More file actions
36 lines (30 loc) · 831 Bytes
/
ip.go
File metadata and controls
36 lines (30 loc) · 831 Bytes
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
package supervisor
import (
"net/http"
"net/netip"
"strings"
)
var headerTrueClientIP = http.CanonicalHeaderKey("True-Client-IP")
var headerXRealIP = http.CanonicalHeaderKey("X-Real-IP")
var headerXForwardedFor = http.CanonicalHeaderKey("X-Forwarded-For")
// GetIPFromHeaders returns the IP address from the request headers
// The request headers can be injected and ips can be spoofed
// They are also injected by proxies
func GetIPFromHeaders(r *http.Request) netip.Addr {
ip := r.Header.Get(headerXForwardedFor)
if ip == "" {
ip = r.Header.Get(headerXRealIP)
if ip == "" {
ip = r.Header.Get(headerTrueClientIP)
}
}
if ip == "" {
return netip.IPv4Unspecified()
}
ips := strings.Split(ip, ",")
ipaddr, err := netip.ParseAddr(ips[0])
if err != nil {
return netip.IPv4Unspecified()
}
return ipaddr
}