forked from XIU2/CloudflareSpeedTest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
IPRangeLoader.go
167 lines (160 loc) · 5.15 KB
/
IPRangeLoader.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
package main
import (
"bufio"
"log"
"net"
"os"
"strconv"
"strings"
)
// 根据子网掩码获取主机数量
func getCidrHostNum(maskLen int) int {
cidrIPNum := int(0)
if maskLen < 32 {
var i int = int(32 - maskLen - 1)
for ; i >= 1; i-- {
cidrIPNum += 1 << i
}
cidrIPNum += 2
} else {
cidrIPNum = 1
}
if cidrIPNum > 255 {
cidrIPNum = 255
}
return cidrIPNum
}
// 获取 IP 最后一段最小值和最大值
func getCidrIPRange(cidr string) (uint8, uint8) {
ip := strings.Split(cidr, "/")[0]
ipSegs := strings.Split(ip, ".")
maskLen, _ := strconv.Atoi(strings.Split(cidr, "/")[1])
seg4MinIP, seg4MaxIP := getIPSeg4Range(ipSegs, maskLen)
//ipPrefix := ipSegs[0] + "." + ipSegs[1] + "." + ipSegs[2] + "."
return seg4MinIP,
seg4MaxIP
}
// 获取 IP 最后一段的区间
func getIPSeg4Range(ipSegs []string, maskLen int) (uint8, uint8) {
ipSeg, _ := strconv.Atoi(ipSegs[3])
return getIPSegRange(uint8(ipSeg), uint8(32-maskLen))
}
// 根据输入的基础IP地址和CIDR掩码计算一个IP片段的区间
func getIPSegRange(userSegIP, offset uint8) (uint8, uint8) {
var ipSegMax uint8 = 255
netSegIP := ipSegMax << offset
segMinIP := netSegIP & userSegIP
segMaxIP := userSegIP&(255<<offset) | ^(255 << offset)
return uint8(segMinIP), uint8(segMaxIP)
}
func loadFirstIPOfRangeFromFile(ipFile string) []net.IPAddr {
file, err := os.Open(ipFile)
if err != nil {
log.Fatal(err)
}
firstIPs := make([]net.IPAddr, 0)
scanner := bufio.NewScanner(file)
scanner.Split(bufio.ScanLines)
for scanner.Scan() {
IPString := scanner.Text()
firstIP, IPRange, err := net.ParseCIDR(IPString)
//fmt.Println(firstIP)
//fmt.Println(IPRange)
if err != nil {
log.Fatal(err)
}
if !ipv6Mode { // IPv4
minIP, maxIP := getCidrIPRange(scanner.Text()) // 获取 IP 最后一段最小值和最大值
Mask, _ := strconv.Atoi(strings.Split(scanner.Text(), "/")[1]) // 获取子网掩码
MaxIPNum := getCidrHostNum(Mask) // 根据子网掩码获取主机数量
for IPRange.Contains(firstIP) {
if allip { // 如果是测速全部 IP
for i := int(minIP); i <= int(maxIP); i++ { // 遍历 IP 最后一段最小值到最大值
firstIP[15] = uint8(i)
firstIPCopy := make([]byte, len(firstIP))
copy(firstIPCopy, firstIP)
firstIPs = append(firstIPs, net.IPAddr{IP: firstIPCopy})
}
} else { // 随机 IP 的最后一段 0.0.0.X
firstIP[15] = minIP + randipEndWith(MaxIPNum)
firstIPCopy := make([]byte, len(firstIP))
copy(firstIPCopy, firstIP)
firstIPs = append(firstIPs, net.IPAddr{IP: firstIPCopy})
}
firstIP[14]++ // 0.0.(X+1).X
if firstIP[14] == 0 {
firstIP[13]++ // 0.(X+1).X.X
if firstIP[13] == 0 {
firstIP[12]++ // (X+1).X.X.X
}
}
}
} else { //IPv6
var tempIP uint8
for IPRange.Contains(firstIP) {
//fmt.Println(firstIP)
//fmt.Println(firstIP[0], firstIP[1], firstIP[2], firstIP[3], firstIP[4], firstIP[5], firstIP[6], firstIP[7], firstIP[8], firstIP[9], firstIP[10], firstIP[11], firstIP[12], firstIP[13], firstIP[14], firstIP[15])
firstIP[15] = randipEndWith(255) // 随机 IP 的最后一段
firstIP[14] = randipEndWith(255) // 随机 IP 的最后一段
firstIPCopy := make([]byte, len(firstIP))
copy(firstIPCopy, firstIP)
firstIPs = append(firstIPs, net.IPAddr{IP: firstIPCopy})
tempIP = firstIP[13]
firstIP[13] += randipEndWith(255)
if firstIP[13] < tempIP {
tempIP = firstIP[12]
firstIP[12] += randipEndWith(255)
if firstIP[12] < tempIP {
tempIP = firstIP[11]
firstIP[11] += randipEndWith(255)
if firstIP[11] < tempIP {
tempIP = firstIP[10]
firstIP[10] += randipEndWith(255)
if firstIP[10] < tempIP {
tempIP = firstIP[9]
firstIP[9] += randipEndWith(255)
if firstIP[9] < tempIP {
tempIP = firstIP[8]
firstIP[8] += randipEndWith(255)
if firstIP[8] < tempIP {
tempIP = firstIP[7]
firstIP[7] += randipEndWith(255)
if firstIP[7] < tempIP {
tempIP = firstIP[6]
firstIP[6] += randipEndWith(255)
if firstIP[6] < tempIP {
tempIP = firstIP[5]
firstIP[5] += randipEndWith(255)
if firstIP[5] < tempIP {
tempIP = firstIP[4]
firstIP[4] += randipEndWith(255)
if firstIP[4] < tempIP {
tempIP = firstIP[3]
firstIP[3] += randipEndWith(255)
if firstIP[3] < tempIP {
tempIP = firstIP[2]
firstIP[2] += randipEndWith(255)
if firstIP[2] < tempIP {
tempIP = firstIP[1]
firstIP[1] += randipEndWith(255)
if firstIP[1] < tempIP {
tempIP = firstIP[0]
firstIP[0] += randipEndWith(255)
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
return firstIPs
}