-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
net.go
52 lines (47 loc) · 1.27 KB
/
net.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
package util
import (
"net"
"net/http"
"strings"
)
// IsPrivateNetwork 是否为私有地址
// https://en.wikipedia.org/wiki/Private_network
func IsPrivateNetwork(remoteAddr string) bool {
// removing optional port from remoteAddr
if strings.HasPrefix(remoteAddr, "[") { // ipv6
if index := strings.LastIndex(remoteAddr, "]"); index != -1 {
remoteAddr = remoteAddr[1:index]
} else {
return false
}
} else { // ipv4
if index := strings.LastIndex(remoteAddr, ":"); index != -1 {
remoteAddr = remoteAddr[:index]
}
}
if ip := net.ParseIP(remoteAddr); ip != nil {
return ip.IsLoopback() || // 127/8, ::1
ip.IsPrivate() || // 10/8, 172.16/12, 192.168/16, fc00::/7
ip.IsLinkLocalUnicast() // 169.254/16, fe80::/10
}
// localhost
if remoteAddr == "localhost" {
return true
}
// private domain eg. .cluster.local
if strings.HasSuffix(remoteAddr, ".local") {
return true
}
return false
}
// GetRequestIPStr get IP string from request
func GetRequestIPStr(r *http.Request) (addr string) {
addr = "Remote: " + r.RemoteAddr
if r.Header.Get("X-Real-IP") != "" {
addr = addr + " ,Real-IP: " + r.Header.Get("X-Real-IP")
}
if r.Header.Get("X-Forwarded-For") != "" {
addr = addr + " ,Forwarded-For: " + r.Header.Get("X-Forwarded-For")
}
return addr
}