-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClientIP.go
executable file
·54 lines (45 loc) · 1.28 KB
/
ClientIP.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
package http
import (
"errors"
"fmt"
"net"
"net/http"
"strings"
)
// GetClientIP (request)
// Returns the client ip address from HTTP headers
func ClientIP(r *http.Request) (string, error) {
var (
err error
ip string
clientIP net.IP
)
if r == nil {
return "", errors.New("*http.Request must not be nil")
}
// True-Client-IP, used by CloudFlare (attn: Enterprise plan only!)
if cfTrueClientIP := r.Header.Get("True-Client-IP"); cfTrueClientIP != "" {
return cfTrueClientIP, nil
}
// CF-Connecting-IP, used by CloudFlare
if cfConnectingIP := r.Header.Get("CF-Connecting-IP"); cfConnectingIP != "" {
return cfConnectingIP, nil
}
// used by most reverse proxies
if xForwardedFor := r.Header.Get("X-Forwarded-For"); xForwardedFor != "" {
// we are only interested in the client-ip
// (see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Forwarded-For)
c := strings.SplitN(xForwardedFor, ",", 2)
if len(c) == 2 {
return c[0], nil
}
return xForwardedFor, nil
}
if ip, _, err = net.SplitHostPort(r.RemoteAddr); err != nil {
return "", fmt.Errorf("cannot parse remote address %s into ip + port", r.RemoteAddr)
}
if clientIP = net.ParseIP(ip); clientIP == nil {
return "", fmt.Errorf("cannot parse remote ip %s", ip)
}
return ip, nil
}