-
Notifications
You must be signed in to change notification settings - Fork 21
/
ip.go
222 lines (200 loc) · 4.55 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
package main
import (
"bytes"
"fmt"
"io/ioutil"
"net"
"strconv"
"strings"
)
//IP struct
type IP struct {
Address string
Delay int
HostName string
}
//IPs []IP
type IPs []IP
//Len return the length of []IP
func (ips IPs) Len() int {
return len(ips)
}
//Swap swap two value of []IP
func (ips IPs) Swap(i, j int) {
ips[i], ips[j] = ips[j], ips[i]
}
//ByDelay sort by delay
type ByDelay struct {
IPs
}
//Less return false if the first value less than the second one
func (s ByDelay) Less(i, j int) bool {
return s.IPs[i].Delay < s.IPs[j].Delay
}
//get all sni ip range from sniip.txt file
func getSNIIPRange() []string {
m := make(map[string]string)
var ipRanges []string
bytes, err := ioutil.ReadFile(sniIPFileName)
checkErr(fmt.Sprintf("read file %s error: ", sniIPFileName), err, Error)
lines := strings.Split(string(bytes), "\n")
for _, line := range lines {
line = strings.Replace(line, "\r", "", -1)
line = strings.TrimSpace(line)
if len(line) > 7 {
m[line] = line
}
}
for _, v := range m {
ipRanges = append(ipRanges, v)
}
return ipRanges
}
/**
Parse sni ip range, support the following formats:
1. xxx.xxx.xxx.xxx
2. xxx.xxx.xxx.xxx/xx
3. xxx.xxx.xxx.xxx-xxx.xxx.xxx.xxx
4. xxx.xxx.xxx.xxx-xxx.
5. xxx.-xxx.
*/
func parseSNIIPRange(ipRange string) []string {
var ips []string
if strings.Contains(ipRange, "/") {
//CIDR: https://zh.wikipedia.org/wiki/%E6%97%A0%E7%B1%BB%E5%88%AB%E5%9F%9F%E9%97%B4%E8%B7%AF%E7%94%B1
ip, ipNet, err := net.ParseCIDR(ipRange)
checkErr(fmt.Sprintf("parse CIDR %s error: ", ipRange), err, Error)
for iptmp := ip.Mask(ipNet.Mask); ipNet.Contains(iptmp); inc(iptmp) {
ips = append(ips, iptmp.String())
}
// remove network address and broadcast address
return ips[1 : len(ips)-1]
} else if strings.Contains(ipRange, "-") {
startIP := ipRange[:strings.Index(ipRange, "-")]
endIP := ipRange[strings.Index(ipRange, "-")+1:]
if strings.HasSuffix(startIP, ".") {
switch strings.Count(startIP, ".") {
case 1:
startIP += "0.0.0"
case 2:
startIP += "0.0"
case 3:
startIP += "0"
}
}
if strings.HasSuffix(endIP, ".") {
switch strings.Count(endIP, ".") {
case 1:
endIP += "255.255.255"
case 2:
endIP += "255.255"
case 3:
endIP += "255"
}
}
sIP := net.ParseIP(startIP)
eIP := net.ParseIP(endIP)
for ip := sIP; bytes.Compare(ip, eIP) <= 0; inc(ip) {
ips = append(ips, ip.String())
}
} else {
ips = append(ips, ipRange)
}
return ips
}
//get all sni ip
func getSNIIP() []string {
var ips []string
ipRanges := getSNIIPRange()
for _, ipRange := range ipRanges {
ips = append(ips, parseSNIIPRange(ipRange)...)
}
return ips
}
//get all sni ip
func getSNIIPQueue() {
ipRanges := getSNIIPRange()
for _, ipRange := range ipRanges {
parsedips := parseSNIIPRange(ipRange)
for _, ip := range parsedips {
totalips <- ip
}
}
}
func inc(ip net.IP) {
for j := len(ip) - 1; j >= 0; j-- {
ip[j]++
if ip[j] > 0 {
break
}
}
}
//get last ip
func getLastOkIP() []IP {
m := make(map[string]IP)
var checkedip IP
var ips []IP
if isFileExist(sniResultFileName) {
bytes, err := ioutil.ReadFile(sniResultFileName)
checkErr(fmt.Sprintf("read file %s error: ", sniResultFileName), err, Error)
lines := strings.Split(string(bytes), "\n")
for _, line := range lines {
ipinfo := strings.Split(line, " ")
if len(ipinfo) == 2 || len(ipinfo) == 3 {
checkedip.Address = ipinfo[0]
delay, err := strconv.Atoi(ipinfo[1][:len(ipinfo[1])-2])
checkErr("delay conversion failed: ", err, Warning)
checkedip.Delay = delay
hostname := "-"
if len(ipinfo) == 3 {
hostname = ipinfo[2]
}
checkedip.HostName = hostname
m[ipinfo[0]] = checkedip
}
}
}
for _, v := range m {
ips = append(ips, v)
}
return ips
}
//get last no ip
func getLastNoIP() (ips []string) {
m := make(map[string]string)
if isFileExist(sniNoFileName) {
bytes, err := ioutil.ReadFile(sniNoFileName)
checkErr(fmt.Sprintf("read file %s error: ", sniNoFileName), err, Error)
lines := strings.Split(string(bytes), "\n")
for _, line := range lines {
line = strings.Replace(line, "\r", "", -1)
ipinfo := strings.Split(line, " ")
if len(ipinfo[0]) > 6 {
m[ipinfo[0]] = ipinfo[0]
}
}
}
for _, v := range m {
ips = append(ips, v)
}
return ips
}
//remove scanned ip
func getDifference(s, t []string) (ips []string) {
if len(t) == 0 {
return s
}
for _, vs := range s {
flag := false
for _, vt := range t {
if vs == vt {
flag = true
break
}
}
if !flag {
ips = append(ips, vs)
}
}
return ips
}