-
Notifications
You must be signed in to change notification settings - Fork 3
/
request_info_ip.go
92 lines (81 loc) · 1.79 KB
/
request_info_ip.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package httpKit
import (
"github.com/duke-git/lancet/v2/netutil"
"github.com/richelieu-yang/chimera/v3/src/core/strKit"
"net"
"net/http"
"strings"
)
var (
// GetRequestPublicIp 获取http请求的 "公网ip".
/*
涉及的请求头:
(1) "X-Forwarded-For"
(2) "X-Real-Ip"
*/
GetRequestPublicIp func(req *http.Request) string = netutil.GetRequestPublicIp
)
// GetRemoteIP 获取客户端IP地址(客户端的远程IP地址).
/*
PS: 参考 gin's Context.RemoteIP().
e.g.
当客户端通过代理服务器连接时,RemoteIP() 返回代理服务器的 IP 地址
*/
func GetRemoteIP(req *http.Request) string {
//ctx := &gin.Context{
// Request: req,
//}
//return ctx.RemoteIP()
ip, _, err := net.SplitHostPort(strings.TrimSpace(req.RemoteAddr))
if err != nil {
return ""
}
return ip
}
// GetClientIP
/*
PS: 参考 gin's Context.ClientIP().
*/
func GetClientIP(req *http.Request) string {
ip := GetClientIPFromHeader(req)
if strKit.IsEmpty(ip) {
ip = GetRemoteIP(req)
}
return ip
}
// GetClientIPFromHeader
/*
PS: 参考 gin's Context.ClientIP().
*/
func GetClientIPFromHeader(req *http.Request) string {
//ctx := &gin.Context{
// Request: req,
//}
//return ctx.ClientIP()
for _, headerName := range RemoteIPHeaders {
ip, valid := validateHeader(GetHeader(req.Header, headerName))
if valid {
return ip
}
}
return ""
}
func validateHeader(header string) (clientIP string, valid bool) {
if header == "" {
return "", false
}
items := strings.Split(header, ",")
for i := len(items) - 1; i >= 0; i-- {
ipStr := strings.TrimSpace(items[i])
ip := net.ParseIP(ipStr)
if ip == nil {
break
}
// X-Forwarded-For is appended by proxy
// Check IPs in reverse order and stop when find untrusted proxy
if i == 0 {
return ipStr, true
}
}
return "", false
}