Repository navigation
Expand file tree
/
Copy pathmain.go
More file actions
732 lines (650 loc) · 17.1 KB
/
Copy pathmain.go
File metadata and controls
732 lines (650 loc) · 17.1 KB
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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
package main
import (
"bufio"
"context"
"crypto/tls"
"encoding/json"
"fmt"
"io"
"math/rand"
"net"
"net/http"
"os"
"path/filepath"
"sort"
"strconv"
"strings"
"sync"
"time"
)
// 命令行版本的入口
func main() {
initLocations()
showMenu()
}
// 显示主菜单
func showMenu() {
scanner := bufio.NewScanner(os.Stdin)
for {
fmt.Println("----------------------------------------")
fmt.Println("1. IPV4 优选 (TLS)")
fmt.Println("2. IPV4 优选 (非 TLS)")
fmt.Println("3. IPV6 优选 (TLS)")
fmt.Println("4. IPV6 优选 (非 TLS)")
fmt.Println("5. 单 IP 测速 (TLS)")
fmt.Println("6. 单 IP 测速 (非 TLS)")
fmt.Println("7. 清空缓存")
fmt.Println("8. 更新数据")
fmt.Println("0. 退出")
fmt.Print("请选择菜单 (默认 0): ")
if !scanner.Scan() {
break
}
input := strings.TrimSpace(scanner.Text())
if input == "" {
input = "0"
}
switch input {
case "0":
fmt.Println("退出成功")
return
case "1":
runIPSelector(4, true)
case "2":
runIPSelector(4, false)
case "3":
runIPSelector(6, true)
case "4":
runIPSelector(6, false)
case "5":
runSingleSpeedTest(true)
case "6":
runSingleSpeedTest(false)
case "7":
clearCache()
case "8":
updateData()
default:
fmt.Println("无效输入,请重新选择")
}
}
}
// runIPSelector 运行 IP 优选流程
func runIPSelector(ipType int, useTLS bool) {
var bandwidth int
var taskNum int
fmt.Print("请设置期望的带宽大小 (默认最小 1,单位 Mbps): ")
scanner := bufio.NewScanner(os.Stdin)
if scanner.Scan() {
input := strings.TrimSpace(scanner.Text())
if input == "" {
bandwidth = 1
} else {
val, err := strconv.Atoi(input)
if err != nil || val <= 0 {
fmt.Println("输入无效,已使用默认值 1 Mbps")
bandwidth = 1
} else {
bandwidth = val
}
}
}
fmt.Print("请设置 RTT 测试进程数 (默认 50,最大 100): ")
if scanner.Scan() {
input := strings.TrimSpace(scanner.Text())
if input == "" {
taskNum = 50
} else {
val, err := strconv.Atoi(input)
if err != nil {
fmt.Println("输入无效,已使用默认值 50")
taskNum = 50
} else if val <= 0 {
fmt.Println("进程数不能为 0,自动设置为默认值")
taskNum = 50
} else {
taskNum = val
}
if taskNum > 100 {
fmt.Println("超过最大进程限制,自动设置为最大值")
taskNum = 100
}
}
}
speed := bandwidth * 128
startTime := time.Now()
// 执行 Cloudflare 测试
anycast, max, avgms, dataCenter := cloudflareTest(ipType, useTLS, taskNum, speed)
realBandwidth := max / 128
endTime := time.Now()
fmt.Println()
fmt.Println("优选 IP:", anycast)
fmt.Println("设置带宽:", bandwidth, "Mbps")
fmt.Println("实测带宽:", realBandwidth, "Mbps")
fmt.Println("峰值速度:", max, "kB/s")
fmt.Println("往返延迟:", avgms, "毫秒")
fmt.Println("数据中心:", dataCenter)
fmt.Println("总计用时:", int(endTime.Sub(startTime).Seconds()), "秒")
}
// cloudflareTest 核心测试逻辑
func cloudflareTest(ipType int, useTLS bool, taskNum int, speed int) (string, int, int, string) {
downloadAllData()
filename := dataPath("ips-v4.txt")
if ipType == 6 {
filename = dataPath("ips-v6.txt")
}
content, err := getFileContent(filename)
if err != nil {
fmt.Println("读取 IP 列表失败:", err)
return "", 0, 0, ""
}
ipList := parseIPList(content)
fmt.Printf("正在从 %d 个子网中随机生成 IP...\n", len(ipList))
sampleSize := 100
if len(ipList) < sampleSize {
sampleSize = len(ipList)
}
for {
var rttResults []RTTResult
for {
sampled := randomSample(ipList, sampleSize)
var testIPs []string
if ipType == 6 {
testIPs = getRandomIPv6s(sampled)
} else {
testIPs = getRandomIPv4s(sampled)
}
fmt.Printf("已生成 %d 个测试 IP,开始 RTT 测试...\n", len(testIPs))
rttResults = runRTTTest(testIPs, taskNum, useTLS)
if len(rttResults) > 0 {
break
}
fmt.Println("当前所有 IP 都存在 RTT 丢包,继续新的 RTT 测试...")
}
fmt.Println("待测速的 IP 地址")
for _, r := range rttResults {
fmt.Printf("%s 往返延迟 %d 毫秒\n", r.IP, r.LatencyMs)
}
// 速度测试
for _, r := range rttResults {
fmt.Println("正在测试", r.IP)
speedPort := 80
if useTLS {
speedPort = 443
}
maxSpeed, tcpMs, dc := runSpeedTestSimple(r.IP, speedPort, useTLS)
fmt.Printf("%s 峰值速度 %d kB/s", r.IP, maxSpeed)
if dc != "" {
fmt.Printf(", 数据中心 %s", lookupDataCenter(dc))
}
fmt.Println()
if maxSpeed >= speed {
if dc != "" {
return r.IP, maxSpeed, tcpMs, lookupDataCenter(dc)
}
return r.IP, maxSpeed, tcpMs, dc
}
}
fmt.Println("当前所有 IP 都未达到期望带宽,重新开始新一轮测试...")
}
}
// randomSample 从列表中随机抽取 n 个元素
func randomSample(list []string, n int) []string {
shuffled := make([]string, len(list))
copy(shuffled, list)
randomMu.Lock()
randomGenerator.Shuffle(len(shuffled), func(i, j int) {
shuffled[i], shuffled[j] = shuffled[j], shuffled[i]
})
randomMu.Unlock()
if n > len(shuffled) {
n = len(shuffled)
}
return shuffled[:n]
}
// RTTResult RTT 测试结果
type RTTResult struct {
IP string
LatencyMs int
}
// runRTTTest 运行 RTT 测试(并发,带进度显示)
func runRTTTest(ipList []string, taskNum int, useTLS bool) []RTTResult {
if len(ipList) < taskNum {
taskNum = len(ipList)
}
var wg sync.WaitGroup
resultChan := make(chan RTTResult, len(ipList))
thread := make(chan struct{}, taskNum)
var count int
var mu sync.Mutex
total := len(ipList)
for _, ip := range ipList {
wg.Add(1)
thread <- struct{}{}
go func(ip string) {
defer func() {
<-thread
wg.Done()
mu.Lock()
count++
current := count
mu.Unlock()
if current%10 == 0 || current == total {
fmt.Printf("RTT 测试进度: %d/%d\n", current, total)
}
}()
avgMs := testRTT(ip, useTLS)
if avgMs > 0 {
resultChan <- RTTResult{IP: ip, LatencyMs: avgMs}
}
}(ip)
}
go func() {
wg.Wait()
close(resultChan)
}()
var results []RTTResult
for r := range resultChan {
results = append(results, r)
}
// 按最小延迟排序,最多保留前 10 个进入速度测试
sort.Slice(results, func(i, j int) bool {
return results[i].LatencyMs < results[j].LatencyMs
})
if len(results) > 10 {
fmt.Printf("RTT 测试完成,%d/%d 个 IP 有效,保留延迟最低的 10 个\n", len(results), total)
results = results[:10]
} else {
fmt.Printf("RTT 测试完成,%d/%d 个 IP 有效\n", len(results), total)
}
return results
}
// testRTT 测试单个 IP 的 RTT(TCP 连接 + 验证 CF-RAY)
// 连续 3 次取 TCP 连接时间,取平均延迟,中间任何一次失败直接丢弃
func testRTT(ip string, useTLS bool) int {
port := 80
if useTLS {
port = 443
}
var totalMs int
for range 3 {
start := time.Now()
conn, err := net.DialTimeout("tcp", net.JoinHostPort(ip, strconv.Itoa(port)), 1*time.Second)
if err != nil {
return 0
}
tcpDuration := time.Since(start)
conn.SetDeadline(start.Add(1 * time.Second))
var rwc net.Conn = conn
if useTLS {
tlsConn := tls.Client(conn, &tls.Config{ServerName: "cloudflare.com", InsecureSkipVerify: true})
if err := tlsConn.Handshake(); err != nil {
conn.Close()
return 0
}
rwc = tlsConn
}
reqStr := "GET / HTTP/1.1\r\nHost: cloudflare.com\r\nUser-Agent: Mozilla/5.0\r\nConnection: close\r\n\r\n"
_, err = rwc.Write([]byte(reqStr))
if err != nil {
rwc.Close()
return 0
}
reader := bufio.NewReader(rwc)
resp, err := http.ReadResponse(reader, nil)
rwc.Close()
if err != nil {
return 0
}
resp.Body.Close()
if resp.Header.Get("CF-RAY") == "" {
return 0
}
totalMs += int(tcpDuration.Milliseconds())
}
return totalMs / 3
}
// runSpeedTestSimple 简单速度测试,返回 (峰值速度 kB/s, TCP延迟ms, 三字码头)
func runSpeedTestSimple(ip string, port int, useTLS bool) (int, int, string) {
var tcpMs int
transport := &http.Transport{
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
start := time.Now()
conn, err := net.Dial("tcp", net.JoinHostPort(ip, strconv.Itoa(port)))
if err == nil {
tcpMs = int(time.Since(start).Milliseconds())
}
return conn, err
},
}
if useTLS {
transport.TLSClientConfig = &tls.Config{ServerName: speedTestDomain}
}
client := &http.Client{
Transport: transport,
Timeout: 5 * time.Second,
}
scheme := "http"
if useTLS {
scheme = "https"
}
testURL := fmt.Sprintf("%s://%s/%s", scheme, speedTestDomain, speedTestFile)
resp, err := client.Get(testURL)
if err != nil {
return 0, 0, ""
}
defer resp.Body.Close()
cfRay := resp.Header.Get("CF-RAY")
dataCenter := extractDataCenter(cfRay)
buf := make([]byte, 32*1024)
var totalBytes int64
var windowBytes int64
windowStart := time.Now()
maxSpeed := 0
for {
n, err := resp.Body.Read(buf)
totalBytes += int64(n)
windowBytes += int64(n)
if err != nil {
break
}
elapsed := time.Since(windowStart).Seconds()
if elapsed >= 1.0 {
speedKB := int(float64(windowBytes) / 1024 / elapsed)
if speedKB > maxSpeed {
maxSpeed = speedKB
}
windowBytes = 0
windowStart = time.Now()
}
}
// 最后一个不满 1 秒的窗口不参与峰值计算,避免时间过短导致速度虚高
return maxSpeed, tcpMs, dataCenter
}
// extractDataCenter 从 CF-RAY 头提取三字码头
func extractDataCenter(cfRay string) string {
if cfRay == "" {
return ""
}
parts := strings.Split(cfRay, "-")
if len(parts) < 2 {
return ""
}
return strings.TrimSpace(parts[len(parts)-1])
}
// lookupDataCenter 查找数据中心名称
func lookupDataCenter(colo string) string {
locationMu.RLock()
loc := locationMap[colo]
locationMu.RUnlock()
if loc.City != "" {
return loc.City
}
return colo
}
// runSingleSpeedTest 单 IP 测速
func runSingleSpeedTest(useTLS bool) {
scanner := bufio.NewScanner(os.Stdin)
fmt.Print("请输入需要测速的 IP: ")
if !scanner.Scan() {
return
}
ip := strings.TrimSpace(scanner.Text())
defaultPort := 80
if useTLS {
defaultPort = 443
}
fmt.Printf("请输入需要测速的端口 (默认%d): ", defaultPort)
var port int
if scanner.Scan() {
input := strings.TrimSpace(scanner.Text())
if input == "" {
port = defaultPort
} else {
val, err := strconv.Atoi(input)
if err != nil || val <= 0 {
fmt.Printf("输入无效,已使用默认端口 %d\n", defaultPort)
port = defaultPort
} else {
port = val
}
}
} else {
port = defaultPort
}
fmt.Printf("正在测速 %s 端口 %d\n", ip, port)
speedKB, tcpMs, dc := runSpeedTestSimple(ip, port, useTLS)
if dc != "" {
fmt.Printf("%s 平均速度 %d kB/s, TCP延迟 %dms, 数据中心=%s\n", ip, speedKB, tcpMs, lookupDataCenter(dc))
} else {
fmt.Printf("%s 平均速度 %d kB/s, TCP延迟 %dms\n", ip, speedKB, tcpMs)
}
}
// clearCache 清空缓存,删除所有数据文件,下次运行重新下载
func clearCache() {
for _, f := range []string{"locations.json", "ips-v4.txt", "ips-v6.txt", "url.txt"} {
os.Remove(dataPath(f))
}
fmt.Println("缓存已清空,下次操作会自动重新下载数据")
}
// updateData 重新下载所有数据
func updateData() {
fmt.Println("正在重新下载数据...")
for _, f := range []string{"locations.json", "ips-v4.txt", "ips-v6.txt", "url.txt"} {
os.Remove(dataPath(f))
}
initLocations()
}
// ----------------------- 工具函数 -----------------------
var (
dataDir string
randomMu sync.Mutex
randomGenerator = rand.New(rand.NewSource(time.Now().UnixNano()))
locationMap map[string]location
locationMu sync.RWMutex
speedTestDomain string
speedTestFile string
)
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 dataPath(name string) string {
if dataDir == "" {
return name
}
return filepath.Join(dataDir, name)
}
var downloadClient = &http.Client{Timeout: 30 * time.Second}
func getURLContent(targetURL string) (string, error) {
resp, err := downloadClient.Get(targetURL)
if err != nil {
return "", err
}
defer resp.Body.Close()
body, err := io.ReadAll(io.LimitReader(resp.Body, 32*1024*1024))
if err != nil {
return "", err
}
return string(body), nil
}
func getFileContent(filename string) (string, error) {
data, err := os.ReadFile(filename)
if err != nil {
return "", err
}
return string(data), nil
}
func saveToFile(filename, content string) error {
dir := filepath.Dir(filename)
if dir != "." && dir != "" {
if err := os.MkdirAll(dir, 0755); err != nil {
return err
}
}
return os.WriteFile(filename, []byte(content), 0644)
}
func parseIPList(content string) []string {
scanner := bufio.NewScanner(strings.NewReader(content))
var ipList []string
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if line != "" {
ipList = append(ipList, line)
}
}
return ipList
}
func nextRandomIntn(n int) int {
randomMu.Lock()
defer randomMu.Unlock()
return randomGenerator.Intn(n)
}
func getRandomIPv4s(ipList []string) []string {
var randomIPs []string
for _, subnet := range ipList {
subnet = strings.TrimSpace(subnet)
if subnet == "" {
continue
}
if idx := strings.Index(subnet, "/"); idx >= 0 {
subnet = subnet[:idx]
}
octets := strings.Split(subnet, ".")
if len(octets) == 4 {
octets[3] = fmt.Sprintf("%d", nextRandomIntn(256))
randomIPs = append(randomIPs, strings.Join(octets, "."))
}
}
return randomIPs
}
func getRandomIPv6s(ipList []string) []string {
var randomIPs []string
for _, subnet := range ipList {
subnet = strings.TrimSpace(subnet)
if subnet == "" {
continue
}
if idx := strings.Index(subnet, "/"); idx >= 0 {
subnet = subnet[:idx]
}
// 展开 :: 压缩,确保有 8 段
if strings.Contains(subnet, "::") {
parts := strings.Split(subnet, "::")
left := strings.Split(parts[0], ":")
var right []string
if len(parts) > 1 && parts[1] != "" {
right = strings.Split(parts[1], ":")
}
missing := 8 - len(left) - len(right)
sections := left
for range missing {
sections = append(sections, "0")
}
sections = append(sections, right...)
subnet = strings.Join(sections, ":")
}
sections := strings.Split(subnet, ":")
if len(sections) >= 3 {
sections = sections[:3]
for i := 3; i < 8; i++ {
sections = append(sections, fmt.Sprintf("%x", nextRandomIntn(65536)))
}
randomIPs = append(randomIPs, strings.Join(sections, ":"))
}
}
return randomIPs
}
// downloadAllData 确保所有数据文件存在,缺失则自动下载
func downloadAllData() {
urlFilename := dataPath("url.txt")
if _, err := os.Stat(urlFilename); os.IsNotExist(err) {
fmt.Println("本地", urlFilename, "不存在,正在下载...")
content, err := getURLContent("https://www.baipiao.eu.org/cloudflare/url")
if err != nil {
fmt.Println("下载测速 URL 失败:", err)
return
}
if err := saveToFile(urlFilename, content); err != nil {
fmt.Println("保存测速 URL 失败:", err)
return
}
}
content, err := getFileContent(urlFilename)
if err != nil {
fmt.Println("读取测速 URL 失败:", err)
return
}
content = strings.TrimSpace(content)
parts := strings.SplitN(content, "/", 2)
if len(parts) == 2 {
speedTestDomain = parts[0]
speedTestFile = parts[1]
} else {
fmt.Println("测速 URL 格式异常")
}
for _, item := range []struct{ file, url string }{
{"ips-v4.txt", "https://www.baipiao.eu.org/cloudflare/ips-v4"},
{"ips-v6.txt", "https://www.baipiao.eu.org/cloudflare/ips-v6"},
} {
fp := dataPath(item.file)
if _, err := os.Stat(fp); os.IsNotExist(err) {
fmt.Println("本地", fp, "不存在,正在下载...")
c, err := getURLContent(item.url)
if err != nil {
fmt.Println("下载 IP 列表失败:", err)
return
}
if err := saveToFile(fp, c); err != nil {
fmt.Println("保存 IP 列表失败:", err)
return
}
}
}
fp := dataPath("locations.json")
if _, err := os.Stat(fp); os.IsNotExist(err) {
fmt.Println("本地", fp, "不存在,正在下载...")
resp, err := downloadClient.Get("https://www.baipiao.eu.org/cloudflare/locations")
if err != nil {
fmt.Println("获取位置信息失败:", err)
return
}
body, err := io.ReadAll(io.LimitReader(resp.Body, 32*1024*1024))
resp.Body.Close()
if err != nil {
fmt.Println("读取响应内容失败:", err)
return
}
if err := saveToFile(fp, string(body)); err != nil {
fmt.Println("保存位置信息失败:", err)
return
}
}
}
// initLocations 初始化数据中心位置信息
func initLocations() {
downloadAllData()
fp := dataPath("locations.json")
body, err := os.ReadFile(fp)
if err != nil {
fmt.Println("读取位置文件失败:", err)
return
}
var locations []location
if err := json.Unmarshal(body, &locations); err != nil {
fmt.Println("解析位置信息 JSON 失败:", err)
return
}
loadedMap := make(map[string]location)
for _, loc := range locations {
loadedMap[loc.Iata] = loc
}
locationMu.Lock()
locationMap = loadedMap
locationMu.Unlock()
fmt.Printf("已加载 %d 个数据中心位置信息\n", len(loadedMap))
}