forked from sastation/MEOW
/
directip.go
41 lines (37 loc) · 858 Bytes
/
directip.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
package main
import (
"strings"
)
func ipShouldDirect(ip string) (direct bool) {
if strings.Contains(ip, ":") && !config.IPv6 {
// IPv6 addresses are connected directly
// IP为 IPv6 并且 IPv6 标识为 False,返回直连
return true
}
direct = false
defer func() {
if r := recover(); r != nil {
errl.Printf("error judging ip should direct: %s", ip)
}
}()
_, isPrivate := hostIsIP(ip)
if isPrivate {
return true
}
ipLong, err := ip2long(ip)
if err != nil {
return false
}
if ipLong == 0 {
return true
}
firstByte := ipLong >> 24
if CNIPDataRange[firstByte].end == 0 {
return false
}
ipIndex := searchRange(CNIPDataRange[firstByte].start, CNIPDataRange[firstByte].end, func(i int) bool {
return CNIPDataStart[i] > ipLong
})
ipIndex--
return ipLong <= CNIPDataStart[ipIndex]+(uint32)(CNIPDataNum[ipIndex])
}