forked from badafans/Cloudflare-IP-SpeedTest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
438 lines (390 loc) · 11.1 KB
/
main.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
package main
import (
"bufio"
"bytes"
"encoding/csv"
"encoding/json"
"flag"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"os"
"os/exec"
"regexp"
"runtime"
"sort"
"strconv"
"strings"
"sync"
"time"
)
const (
requestURL = "speed.cloudflare.com/cdn-cgi/trace" // 请求trace URL
timeout = 1 * time.Second // 超时时间
maxDuration = 2 * time.Second // 最大持续时间
)
var (
File = flag.String("file", "ip.txt", "IP地址文件名称") // IP地址文件名称
outFile = flag.String("outfile", "ip.csv", "输出文件名称") // 输出文件名称
defaultPort = flag.Int("port", 443, "端口") // 端口
maxThreads = flag.Int("max", 100, "并发请求最大协程数") // 最大协程数
speedTest = flag.Int("speedtest", 5, "下载测速协程数量,设为0禁用测速") // 下载测速协程数量
speedTestURL = flag.String("url", "speed.cloudflare.com/__down?bytes=500000000", "测速文件地址") // 测速文件地址
enableTLS = flag.Bool("tls", true, "是否启用TLS") // TLS是否启用
)
type result struct {
ip string // IP地址
port int // 端口
dataCenter string // 数据中心
region string // 地区
city string // 城市
latency string // 延迟
tcpDuration time.Duration // TCP请求延迟
}
type speedtestresult struct {
result
downloadSpeed float64 // 下载速度
}
type location struct {
Iata string `json:"iata"`
Lat float64 `json:"lat"`
Lon float64 `json:"lon"`
Cca2 string `json:"cca2"`
Region string `json:"region"`
City string `json:"city"`
}
// 尝试提升文件描述符的上限
func increaseMaxOpenFiles() {
fmt.Println("正在尝试提升文件描述符的上限...")
cmd := exec.Command("bash", "-c", "ulimit -n 10000")
_, err := cmd.CombinedOutput()
if err != nil {
fmt.Printf("提升文件描述符上限时出现错误: %v\n", err)
} else {
fmt.Printf("文件描述符上限已提升!\n")
}
}
func main() {
flag.Parse()
startTime := time.Now()
osType := runtime.GOOS
if osType == "linux" {
increaseMaxOpenFiles()
}
var locations []location
if _, err := os.Stat("locations.json"); os.IsNotExist(err) {
fmt.Println("本地 locations.json 不存在\n正在从 https://speed.cloudflare.com/locations 下载 locations.json")
resp, err := http.Get("https://speed.cloudflare.com/locations")
if err != nil {
fmt.Printf("无法从URL中获取JSON: %v\n", err)
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Printf("无法读取响应体: %v\n", err)
return
}
err = json.Unmarshal(body, &locations)
if err != nil {
fmt.Printf("无法解析JSON: %v\n", err)
return
}
file, err := os.Create("locations.json")
if err != nil {
fmt.Printf("无法创建文件: %v\n", err)
return
}
defer file.Close()
_, err = file.Write(body)
if err != nil {
fmt.Printf("无法写入文件: %v\n", err)
return
}
} else {
fmt.Println("本地 locations.json 已存在,无需重新下载")
file, err := os.Open("locations.json")
if err != nil {
fmt.Printf("无法打开文件: %v\n", err)
return
}
defer file.Close()
body, err := ioutil.ReadAll(file)
if err != nil {
fmt.Printf("无法读取文件: %v\n", err)
return
}
err = json.Unmarshal(body, &locations)
if err != nil {
fmt.Printf("无法解析JSON: %v\n", err)
return
}
}
locationMap := make(map[string]location)
for _, loc := range locations {
locationMap[loc.Iata] = loc
}
ips, err := readIPs(*File)
if err != nil {
fmt.Printf("无法从文件中读取 IP: %v\n", err)
return
}
var wg sync.WaitGroup
wg.Add(len(ips))
resultChan := make(chan result, len(ips))
thread := make(chan struct{}, *maxThreads)
var count int
total := len(ips)
for _, ip := range ips {
thread <- struct{}{}
go func(ip string) {
defer func() {
<-thread
wg.Done()
count++
percentage := float64(count) / float64(total) * 100
fmt.Printf("已完成: %d 总数: %d 已完成: %.2f%%\r", count, total, percentage)
if count == total {
fmt.Printf("已完成: %d 总数: %d 已完成: %.2f%%\n", count, total, percentage)
}
}()
dialer := &net.Dialer{
Timeout: timeout,
KeepAlive: 0,
}
start := time.Now()
conn, err := dialer.Dial("tcp", net.JoinHostPort(ip, strconv.Itoa(*defaultPort)))
if err != nil {
return
}
defer conn.Close()
tcpDuration := time.Since(start)
start = time.Now()
client := http.Client{
Transport: &http.Transport{
Dial: func(network, addr string) (net.Conn, error) {
return conn, nil
},
},
Timeout: timeout,
}
var protocol string
if *enableTLS {
protocol = "https://"
} else {
protocol = "http://"
}
requestURL := protocol + requestURL
req, _ := http.NewRequest("GET", requestURL, nil)
// 添加用户代理
req.Header.Set("User-Agent", "Mozilla/5.0")
req.Close = true
resp, err := client.Do(req)
if err != nil {
return
}
duration := time.Since(start)
if duration > maxDuration {
return
}
buf := &bytes.Buffer{}
// 创建一个读取操作的超时
timeout := time.After(maxDuration)
// 使用一个 goroutine 来读取响应体
done := make(chan bool)
go func() {
_, err := io.Copy(buf, resp.Body)
done <- true
if err != nil {
return
}
}()
// 等待读取操作完成或者超时
select {
case <-done:
// 读取操作完成
case <-timeout:
// 读取操作超时
return
}
body := buf
if err != nil {
return
}
if strings.Contains(body.String(), "uag=Mozilla/5.0") {
if matches := regexp.MustCompile(`colo=([A-Z]+)`).FindStringSubmatch(body.String()); len(matches) > 1 {
dataCenter := matches[1]
loc, ok := locationMap[dataCenter]
if ok {
fmt.Printf("发现有效IP %s 位置信息 %s 延迟 %d 毫秒\n", ip, loc.City, tcpDuration.Milliseconds())
resultChan <- result{ip, *defaultPort, dataCenter, loc.Region, loc.City, fmt.Sprintf("%d ms", tcpDuration.Milliseconds()), tcpDuration}
} else {
fmt.Printf("发现有效IP %s 位置信息未知 延迟 %d 毫秒\n", ip, tcpDuration.Milliseconds())
resultChan <- result{ip, *defaultPort, dataCenter, "", "", fmt.Sprintf("%d ms", tcpDuration.Milliseconds()), tcpDuration}
}
}
}
}(ip)
}
wg.Wait()
close(resultChan)
if len(resultChan) == 0 {
// 清除输出内容
fmt.Print("\033[2J")
fmt.Println("没有发现有效的IP")
return
}
var results []speedtestresult
if *speedTest > 0 {
fmt.Printf("开始测速\n")
var wg2 sync.WaitGroup
wg2.Add(*speedTest)
count = 0
total := len(resultChan)
results = []speedtestresult{}
for i := 0; i < *speedTest; i++ {
thread <- struct{}{}
go func() {
defer func() {
<-thread
wg2.Done()
}()
for res := range resultChan {
downloadSpeed := getDownloadSpeed(res.ip)
results = append(results, speedtestresult{result: res, downloadSpeed: downloadSpeed})
count++
percentage := float64(count) / float64(total) * 100
fmt.Printf("已完成: %.2f%%\r", percentage)
if count == total {
fmt.Printf("已完成: %.2f%%\033[0\n", percentage)
}
}
}()
}
wg2.Wait()
} else {
for res := range resultChan {
results = append(results, speedtestresult{result: res})
}
}
if *speedTest > 0 {
sort.Slice(results, func(i, j int) bool {
return results[i].downloadSpeed > results[j].downloadSpeed
})
} else {
sort.Slice(results, func(i, j int) bool {
return results[i].result.tcpDuration < results[j].result.tcpDuration
})
}
file, err := os.Create(*outFile)
if err != nil {
fmt.Printf("无法创建文件: %v\n", err)
return
}
defer file.Close()
writer := csv.NewWriter(file)
if *speedTest > 0 {
writer.Write([]string{"IP地址", "端口", "TLS", "数据中心", "地区", "城市", "网络延迟", "下载速度"})
} else {
writer.Write([]string{"IP地址", "端口", "TLS", "数据中心", "地区", "城市", "网络延迟"})
}
for _, res := range results {
if *speedTest > 0 {
writer.Write([]string{res.result.ip, strconv.Itoa(res.result.port), strconv.FormatBool(*enableTLS), res.result.dataCenter, res.result.region, res.result.city, res.result.latency, fmt.Sprintf("%.0f kB/s", res.downloadSpeed)})
} else {
writer.Write([]string{res.result.ip, strconv.Itoa(res.result.port), strconv.FormatBool(*enableTLS), res.result.dataCenter, res.result.region, res.result.city, res.result.latency})
}
}
writer.Flush()
// 清除输出内容
fmt.Print("\033[2J")
fmt.Printf("成功将结果写入文件 %s,耗时 %d秒\n", *outFile, time.Since(startTime)/time.Second)
}
// 从文件中读取IP地址
func readIPs(File string) ([]string, error) {
file, err := os.Open(File)
if err != nil {
return nil, err
}
defer file.Close()
var ips []string
scanner := bufio.NewScanner(file)
for scanner.Scan() {
ipAddr := scanner.Text()
// 判断是否为 CIDR 格式的 IP 地址
if strings.Contains(ipAddr, "/") {
ip, ipNet, err := net.ParseCIDR(ipAddr)
if err != nil {
fmt.Printf("无法解析CIDR格式的IP: %v\n", err)
continue
}
for ip := ip.Mask(ipNet.Mask); ipNet.Contains(ip); inc(ip) {
ips = append(ips, ip.String())
}
} else {
ips = append(ips, ipAddr)
}
}
return ips, scanner.Err()
}
// inc函数实现ip地址自增
func inc(ip net.IP) {
for j := len(ip) - 1; j >= 0; j-- {
ip[j]++
if ip[j] > 0 {
break
}
}
}
// 测速函数
func getDownloadSpeed(ip string) float64 {
var protocol string
if *enableTLS {
protocol = "https://"
} else {
protocol = "http://"
}
speedTestURL := protocol + *speedTestURL
// 创建请求
req, _ := http.NewRequest("GET", speedTestURL, nil)
req.Header.Set("User-Agent", "Mozilla/5.0")
// 创建TCP连接
dialer := &net.Dialer{
Timeout: timeout,
KeepAlive: 0,
}
conn, err := dialer.Dial("tcp", net.JoinHostPort(ip, strconv.Itoa(*defaultPort)))
if err != nil {
return 0
}
defer conn.Close()
fmt.Printf("正在测试IP %s 端口 %s\n", ip, strconv.Itoa(*defaultPort))
startTime := time.Now()
// 创建HTTP客户端
client := http.Client{
Transport: &http.Transport{
Dial: func(network, addr string) (net.Conn, error) {
return conn, nil
},
},
//设置单个IP测速最长时间为5秒
Timeout: 5 * time.Second,
}
// 发送请求
req.Close = true
resp, err := client.Do(req)
if err != nil {
fmt.Printf("IP %s 端口 %s 测速无效\n", ip, strconv.Itoa(*defaultPort))
return 0
}
defer resp.Body.Close()
// 复制响应体到/dev/null,并计算下载速度
written, _ := io.Copy(io.Discard, resp.Body)
duration := time.Since(startTime)
speed := float64(written) / duration.Seconds() / 1024
// 输出结果
fmt.Printf("IP %s 端口 %s 下载速度 %.0f kB/s\n", ip, strconv.Itoa(*defaultPort), speed)
return speed
}