-
Notifications
You must be signed in to change notification settings - Fork 52
/
ip.go
193 lines (168 loc) · 3.8 KB
/
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package znet
import (
"errors"
"math"
"net"
"net/http"
"strconv"
"strings"
)
var localNetworks []*net.IPNet
func init() {
localNetworks = make([]*net.IPNet, 4)
for i, sNetwork := range []string{
"10.0.0.0/8",
"169.254.0.0/16",
"172.16.0.0/12",
"192.168.0.0/16",
} {
_, network, _ := net.ParseCIDR(sNetwork)
localNetworks[i] = network
}
}
// IsLocalAddrIP IsLocalAddrIP
func IsLocalAddrIP(ip string) bool {
return IsLocalIP(net.ParseIP(ip))
}
// IsLocalIP IsLocalIP
func IsLocalIP(ip net.IP) bool {
for _, network := range localNetworks {
if network.Contains(ip) {
return true
}
}
if ip.String() == "0.0.0.0" {
return true
}
return ip.IsLoopback() || ip.IsLinkLocalMulticast() || ip.IsLinkLocalUnicast()
}
// ClientIP ClientIP
func ClientIP(r *http.Request) (ip string) {
xForwardedFor := r.Header.Get("X-Forwarded-For")
ip = strings.TrimSpace(strings.Split(xForwardedFor, ",")[0])
if ip != "" {
return
}
ip = strings.TrimSpace(r.Header.Get("X-Real-Ip"))
if ip != "" {
return
}
ip, _, _ = net.SplitHostPort(strings.TrimSpace(r.RemoteAddr))
return
}
// ClientPublicIP ClientPublicIP
func ClientPublicIP(r *http.Request) string {
var ip string
for _, ip = range strings.Split(r.Header.Get("X-Forwarded-For"), ",") {
ip = strings.TrimSpace(ip)
if ip != "" && !IsLocalAddrIP(ip) {
return ip
}
}
ip = strings.TrimSpace(r.Header.Get("X-Real-Ip"))
if ip != "" && !IsLocalAddrIP(ip) {
return ip
}
if ip, _, err := net.SplitHostPort(strings.TrimSpace(r.RemoteAddr)); err == nil && !IsLocalAddrIP(ip) {
return ip
}
return ""
}
// RemoteIP RemoteIP
func RemoteIP(r *http.Request) string {
if ip, _, err := net.SplitHostPort(strings.TrimSpace(r.RemoteAddr)); err == nil {
return ip
}
return ""
}
// IPString2Long IPString2Long
func IPString2Long(ip string) (i uint, err error) {
b := net.ParseIP(ip).To4()
if b == nil {
err = errors.New("invalid ipv4 format")
return
}
i = uint(b[3]) | uint(b[2])<<8 | uint(b[1])<<16 | uint(b[0])<<24
return
}
// Long2IPString Long2IPString
func Long2IPString(i uint) (IP string, err error) {
if i > math.MaxUint32 {
err = errors.New("beyond the scope of ipv4")
return
}
ip := make(net.IP, net.IPv4len)
ip[0] = byte(i >> 24)
ip[1] = byte(i >> 16)
ip[2] = byte(i >> 8)
ip[3] = byte(i)
IP = ip.String()
return
}
// IP2Long IP2Long
func IP2Long(ip net.IP) (i uint, err error) {
b := ip.To4()
if b == nil {
err = errors.New("invalid ipv4 format")
return
}
i = uint(b[3]) | uint(b[2])<<8 | uint(b[1])<<16 | uint(b[0])<<24
return
}
// Long2IP Long2IP
func Long2IP(i uint) (ip net.IP, err error) {
if i > math.MaxUint32 {
err = errors.New("beyond the scope of ipv4")
return
}
ip = make(net.IP, net.IPv4len)
ip[0] = byte(i >> 24)
ip[1] = byte(i >> 16)
ip[2] = byte(i >> 8)
ip[3] = byte(i)
return
}
// IsIP IsIP
func IsIP(ip string) bool {
address := net.ParseIP(ip)
if address == nil {
return false
}
return true
}
// GetPort Check if the port is available, if not, then automatically get an available
func Port(port int, change bool) (newPort int, err error) {
host := ":" + strconv.Itoa(port)
listener, err := net.Listen("tcp", host)
if err != nil {
if !change && port != 0 {
return 0, err
}
listener, err = net.Listen("tcp", ":0")
if err != nil {
return 0, err
}
}
defer listener.Close()
addr := listener.Addr().String()
_, portString, err := net.SplitHostPort(addr)
if err != nil {
return 0, err
}
return strconv.Atoi(portString)
}
// MultiplePort Check if the multiple port is available, if not, then automatically get an available
func MultiplePort(ports []int, change bool) (int, error) {
last := len(ports) - 1
for k, v := range ports {
c := false
if last == k {
c = change
}
n, err := Port(v, c)
if err == nil {
return n, nil
}
}
return 0, errors.New("no available port")
}