-
Notifications
You must be signed in to change notification settings - Fork 54
/
ip.go
279 lines (248 loc) · 5.23 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
package znet
import (
"errors"
"math"
"math/big"
"net"
"net/http"
"strconv"
"strings"
)
var localNetworks []*net.IPNet
var (
RemoteIPHeaders = []string{"X-Forwarded-For", "X-Real-IP"}
TrustedProxies = []string{"0.0.0.0/0"}
)
func init() {
localNetworks = make([]*net.IPNet, 5)
for i, sNetwork := range []string{
"127.0.0.0/8",
"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()
}
// func trustedProxies() []*net.IPNet {
// trusted := make([]*net.IPNet, 0)
// for i := range TrustedProxies {
// n, err := netCIDR(TrustedProxies[i])
// if err != nil {
// continue
// }
// trusted = append(trusted, n)
// }
// return trusted
// }
func getTrustedIP(r *http.Request, remoteIP string) string {
ip := remoteIP
for i := range RemoteIPHeaders {
key := RemoteIPHeaders[i]
val := r.Header.Get(key)
ips := parseHeadersIP(val)
for i := range ips {
Log.Debug(i, ips[i])
}
}
return ip
}
func getRemoteIP(r *http.Request) []string {
ips := make([]string, 0)
ip := RemoteIP(r)
trusted := ip == ""
if !trusted {
for i := range TrustedProxies {
proxy := TrustedProxies[i]
if InNetwork(ip, proxy) {
trusted = true
break
}
}
}
if trusted {
for i := range RemoteIPHeaders {
key := RemoteIPHeaders[i]
val := r.Header.Get(key)
ips = append(ips, parseHeadersIP(val)...)
}
}
return append(ips, ip)
}
func parseHeadersIP(val string) []string {
if val == "" {
return []string{}
}
str := strings.Split(val, ",")
l := len(str)
ips := make([]string, l)
for i := l - 1; i >= 0; i-- {
ips[l-1-i] = strings.TrimSpace(str[i])
}
return ips
}
// ClientIP ClientIP
func ClientIP(r *http.Request) (ip string) {
ips := getRemoteIP(r)
if len(ips) > 0 && ips[0] != "" {
return ips[0]
}
return
}
// ClientPublicIP ClientPublicIP
func ClientPublicIP(r *http.Request) string {
var ip string
ips := getRemoteIP(r)
for i := range ips {
ip = ips[i]
if ip != "" && !IsLocalAddrIP(ip) {
return ip
}
}
return ""
}
// RemoteIP Remote IP
func RemoteIP(r *http.Request) string {
if ip, _, err := net.SplitHostPort(strings.TrimSpace(r.RemoteAddr)); err == nil {
return ip
}
return ""
}
// IPToLong IPToLong
func IPToLong(ip string) (i uint, err error) {
return NetIPToLong(net.ParseIP(ip))
}
// LongToIP LongToIP
func LongToIP(i uint) (string, error) {
ip, err := LongToNetIP(i)
if err != nil {
return "", err
}
return ip.String(), nil
}
// NetIPToLong NetIPToLong
func NetIPToLong(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
}
// NetIPv6ToLong NetIPv6ToLong
func NetIPv6ToLong(ip net.IP) (*big.Int, error) {
if ip == nil {
return nil, errors.New("invalid ipv6 format")
}
IPv6Int := big.NewInt(0)
IPv6Int.SetBytes(ip.To16())
return IPv6Int, nil
}
// LongToNetIP LongToNetIP
func LongToNetIP(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
}
// LongToNetIPv6 LongToNetIPv6
func LongToNetIPv6(i *big.Int) (ip net.IP, err error) {
ip = i.Bytes()
return
}
// IsIP IsIP
func IsIP(ip string) bool {
return net.ParseIP(ip) != nil
}
// GetIPv GetIPv
func GetIPv(s string) int {
for i := 0; i < len(s); i++ {
switch s[i] {
case '.':
return 4
case ':':
return 6
}
}
return 0
}
func netCIDR(network string) (*net.IPNet, error) {
_, n, err := net.ParseCIDR(network)
if err != nil && IsIP(network) {
_, n, err = net.ParseCIDR(network + "/24")
}
if err != nil {
return nil, err
}
return n, nil
}
// InNetwork InNetwork
func InNetwork(ip, network string) bool {
n, err := netCIDR(network)
if err != nil {
return false
}
netIP := net.ParseIP(ip)
return n.Contains(netIP)
}
// Port 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")
}