-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathiptest.go
More file actions
executable file
·826 lines (733 loc) · 21.6 KB
/
Copy pathiptest.go
File metadata and controls
executable file
·826 lines (733 loc) · 21.6 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
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
package main
import (
"bufio"
"bytes"
"encoding/csv"
"encoding/json"
"flag"
"fmt"
"github.com/oschwald/geoip2-golang"
"io"
"io/ioutil"
"net"
"net/http"
"os"
"os/exec"
"regexp"
"runtime"
"sort"
"strconv"
"strings"
"sync"
"sync/atomic"
"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 port ,就是IP和端口之间用空格隔开") // IP地址文件名称
outFile = flag.String("outfile", "ip.csv", "输出文件名称") // 输出文件名称
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是否启用
delay = flag.Int("delay", 0, "延迟阈值(ms),默认为0禁用延迟过滤") // 默认0,禁用过滤
enableIPAPI = flag.Bool("ips", false, "是否启用IPS类型检测(调用ipapi.is API)") // 默认不检测
)
type result struct {
ip string // IP地址
port int // 端口
dataCenter string // 数据中心
locCode string // 源IP位置
region string // 地区
city string // 城市
region_zh string // 地区(中文)
country string // 国家
city_zh string // 城市(中文)
emoji string // 国旗
latency string // 延迟
tcpDuration time.Duration // TCP请求延迟
outboundIP string // 出站IP
ipType string // 出站IP类型 (IPv4/IPv6)
countryName string // 出站 IP 位置
visitScheme string // 访问协议
tlsVersion string // TLS版本
sni string // SNI
httpVersion string // HTTP版本
warp string // WARP状态
gateway string // Gateway状态
rbi string // RBI状态
kex string // 密钥交换
timestamp string // 时间戳
autonomousSystemNumber uint // ASN号码
autonomousSystemOrganization string // ASN组织名称
ipsType string // IPS类型
}
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"`
Region_zh string `json:"region_zh"`
Country string `json:"country"`
City_zh string `json:"city_zh"`
Emoji string `json:"emoji"`
}
type ipapiResponse struct {
IsDatacenter bool `json:"is_datacenter"`
Company struct {
Type string `json:"type"`
} `json:"company"`
ASN struct {
Type string `json:"type"`
} `json:"asn"`
Error string `json:"error"`
}
// 尝试提升文件描述符的上限
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")
}
}
// 下载ASN数据库
func downloadASNDatabase() error {
fmt.Println("正在下载 GeoLite2-ASN.mmdb...")
resp, err := http.Get("https://jsd.onmicrosoft.cn/gh/seketiti/GeoLiet2@release/GeoLite2-ASN.mmdb")
if err != nil {
return fmt.Errorf("下载失败: %v", err)
}
defer resp.Body.Close()
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("读取数据失败: %v", err)
}
err = ioutil.WriteFile("GeoLite2-ASN.mmdb", data, 0644)
if err != nil {
return fmt.Errorf("保存文件失败: %v", err)
}
fmt.Println("GeoLite2-ASN.mmdb 下载完成")
return nil
}
func downloadCountryDatabase() error {
fmt.Println("正在下载 GeoLite2-Country.mmdb...")
resp, err := http.Get("https://jsd.onmicrosoft.cn/gh/seketiti/GeoLiet2@release/GeoLite2-Country.mmdb")
if err != nil {
return fmt.Errorf("下载失败: %v", err)
}
defer resp.Body.Close()
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("读取数据失败: %v", err)
}
err = ioutil.WriteFile("GeoLite2-Country.mmdb", data, 0644)
if err != nil {
return fmt.Errorf("保存文件失败: %v", err)
}
fmt.Println("GeoLite2-Country.mmdb 下载完成")
return nil
}
func main() {
flag.Parse()
var validCount int32 // 有效IP计数器
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://locations-adw.pages.dev/ 下载 locations.json")
resp, err := http.Get("https://locations-adw.pages.dev/")
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)
// 加载ASN数据库
var asnDB *geoip2.Reader
var dbErr error
// 检查ASN数据库文件是否存在
if _, err := os.Stat("GeoLite2-ASN.mmdb"); os.IsNotExist(err) {
fmt.Println("ASN数据库文件不存在,尝试下载...")
dbErr = downloadASNDatabase()
if dbErr != nil {
fmt.Printf("下载ASN数据库失败: %v\n", dbErr)
} else {
fmt.Println("ASN数据库下载成功")
}
}
// 尝试打开ASN数据库
asnDB, dbErr = geoip2.Open("GeoLite2-ASN.mmdb")
if dbErr != nil {
fmt.Printf("警告: 打开ASN数据库失败: %v (ASN信息将不可用)\n", dbErr)
} else {
defer asnDB.Close()
fmt.Println("ASN数据库加载成功")
}
for _, loc := range locations {
locationMap[loc.Iata] = loc
}
var countryDB *geoip2.Reader
if _, err := os.Stat("GeoLite2-Country.mmdb"); os.IsNotExist(err) {
fmt.Println("Country数据库不存在,尝试下载...")
if err := downloadCountryDatabase(); err != nil {
fmt.Printf("下载Country库失败: %v\n", err)
}
}
countryDB, dbErr = geoip2.Open("GeoLite2-Country.mmdb")
if dbErr != nil {
fmt.Printf("警告: 打开Country数据库失败: %v (国家信息不可用)\n", dbErr)
} else {
defer countryDB.Close()
fmt.Println("Country数据库加载成功")
}
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)
}
}()
parts := strings.Fields(ip)
if len(parts) != 2 {
fmt.Printf("IP地址格式错误: %s\n", ip)
return
}
ipAddr := parts[0]
portStr := parts[1]
port, err := strconv.Atoi(portStr)
if err != nil {
fmt.Printf("端口格式错误: %s\n", portStr)
return
}
dialer := &net.Dialer{
Timeout: timeout,
KeepAlive: 0,
}
start := time.Now()
conn, err := dialer.Dial("tcp", net.JoinHostPort(ipAddr, strconv.Itoa(port)))
if err != nil {
return
}
defer conn.Close()
tcpDuration := time.Since(start)
if *delay > 0 && tcpDuration.Milliseconds() > int64(*delay) {
return
}
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
}
defer resp.Body.Close()
buf := &bytes.Buffer{}
// 创建一个读取操作的超时
timeout := time.After(maxDuration)
// 使用一个 goroutine 来读取响应体
done := make(chan bool)
errChan := make(chan error)
go func() {
_, err := io.Copy(buf, resp.Body)
done <- true
errChan <- err
if err != nil {
return
}
}()
// 等待读取操作完成或者超时
select {
case <-done:
// 读取操作完成
case <-timeout:
// 读取操作超时
return
}
body := buf
err = <-errChan
if err != nil {
return
}
bodyStr := body.String()
if strings.Contains(bodyStr, "uag=Mozilla/5.0") {
if matches := regexp.MustCompile(`colo=([A-Z]+)[\s\S]*?loc=([A-Z]+)`).FindStringSubmatch(bodyStr); len(matches) > 2 {
dataCenter := matches[1]
locCode := matches[2]
loc, ok := locationMap[dataCenter]
// 解析所有key=value格式的字段
responseData := parseTraceResponse(bodyStr)
// 记录通过延迟检查的有效IP
atomic.AddInt32(&validCount, 1)
outboundIP := responseData["ip"]
country := ""
if countryDB != nil {
ip := net.ParseIP(outboundIP)
if ip != nil {
record, err := countryDB.Country(ip)
if err == nil && record.Country.Names["zh-CN"] != "" {
country = record.Country.Names["zh-CN"]
}
}
}
var asn uint = 0
var asnOrg string = ""
var ipsType string
if outboundIP != "" {
if *enableIPAPI {
ipsType = queryIPAPI(ipAddr)
} else {
ipsType = "N/A"
}
}
if asnDB != nil {
ip := net.ParseIP(outboundIP)
if ip != nil {
record, err := asnDB.ASN(ip)
if err == nil {
asn = record.AutonomousSystemNumber
asnOrg = record.AutonomousSystemOrganization
}
}
}
ipType := getIPType(outboundIP)
res := result{
ip: ipAddr,
port: port,
dataCenter: dataCenter,
locCode: locCode,
latency: fmt.Sprintf("%d ms", tcpDuration.Milliseconds()),
tcpDuration: tcpDuration,
outboundIP: outboundIP,
ipType: ipType,
countryName: country,
visitScheme: responseData["visit_scheme"],
tlsVersion: responseData["tls"],
sni: responseData["sni"],
httpVersion: responseData["http"],
warp: responseData["warp"],
gateway: responseData["gateway"],
rbi: responseData["rbi"],
kex: responseData["kex"],
timestamp: responseData["ts"],
autonomousSystemNumber: asn,
autonomousSystemOrganization: asnOrg,
ipsType: ipsType,
}
if ok {
res.region = loc.Region
res.city = loc.City
res.region_zh = loc.Region_zh
res.country = loc.Country
res.city_zh = loc.City_zh
res.emoji = loc.Emoji
fmt.Printf("发现有效IP %s 端口 %d 位置信息 %s 出站类型 %s 延迟 %d 毫秒\n",
ipAddr, port, loc.City_zh, ipType, tcpDuration.Milliseconds())
// 输出完整的响应内容
// fmt.Printf("========== Cloudflare Trace 响应 ==========\n")
// fmt.Printf("IP地址: %s 端口: %d\n", ipAddr, port)
// fmt.Printf("响应内容:\n%s\n", bodyStr)
// fmt.Printf("==========================================\n\n")
} else {
fmt.Printf("发现有效IP %s 端口 %d 位置信息未知 出站IP %s (%s) 延迟 %d 毫秒\n",
ipAddr, port, outboundIP, ipType, tcpDuration.Milliseconds())
}
resultChan <- res
}
}
}(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("找到符合条件的ip 共%d个\n", atomic.LoadInt32(&validCount))
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, res.port)
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", "数据中心", "IP位置",
"地区", "城市", "地区(中文)", "国家", "城市(中文)", "国旗",
"网络延迟", "下载速度",
"出站IP", "出站IP类型", "出站IP位置", "IPS类型",
"ASN号码", "ASN组织",
"访问协议", "TLS版本", "SNI", "HTTP版本",
"WARP", "Gateway", "RBI", "密钥交换", "时间戳",
})
} else {
writer.Write([]string{
"IP地址", "端口号", "TLS", "数据中心", "IP位置",
"地区", "城市", "地区(中文)", "国家", "城市(中文)", "国旗",
"网络延迟",
"出站IP", "出站IP类型", "出站IP位置", "IPS类型",
"ASN号码", "ASN组织",
"访问协议", "TLS版本", "SNI", "HTTP版本",
"WARP", "Gateway", "RBI", "密钥交换", "时间戳",
})
}
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.locCode,
res.result.region,
res.result.city,
res.result.region_zh,
res.result.country,
res.result.city_zh,
res.result.emoji,
res.result.latency,
fmt.Sprintf("%.0f kB/s", res.downloadSpeed),
res.result.outboundIP,
res.result.ipType,
res.result.countryName,
res.result.ipsType,
strconv.FormatUint(uint64(res.result.autonomousSystemNumber), 10),
res.result.autonomousSystemOrganization,
res.result.visitScheme,
res.result.tlsVersion,
res.result.sni,
res.result.httpVersion,
res.result.warp,
res.result.gateway,
res.result.rbi,
res.result.kex,
res.result.timestamp,
})
} else {
writer.Write([]string{
res.result.ip,
strconv.Itoa(res.result.port),
strconv.FormatBool(*enableTLS),
res.result.dataCenter,
res.result.locCode,
res.result.region,
res.result.city,
res.result.region_zh,
res.result.country,
res.result.city_zh,
res.result.emoji,
res.result.latency,
res.result.outboundIP,
res.result.ipType,
res.result.countryName,
res.result.ipsType,
strconv.FormatUint(uint64(res.result.autonomousSystemNumber), 10),
res.result.autonomousSystemOrganization,
res.result.visitScheme,
res.result.tlsVersion,
res.result.sni,
res.result.httpVersion,
res.result.warp,
res.result.gateway,
res.result.rbi,
res.result.kex,
res.result.timestamp,
})
}
}
writer.Flush()
// 清除输出内容
fmt.Print("\033[2J")
fmt.Printf("有效IP数量: %d | 成功将结果写入文件 %s,耗时 %d秒\n", atomic.LoadInt32(&validCount), *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() {
line := scanner.Text()
parts := strings.Fields(line)
if len(parts) != 2 {
fmt.Printf("行格式错误: %s\n", line)
continue
}
ipAddr := parts[0]
portStr := parts[1]
port, err := strconv.Atoi(portStr)
if err != nil {
fmt.Printf("端口格式错误: %s\n", portStr)
continue
}
ip := fmt.Sprintf("%s %d", ipAddr, port)
ips = append(ips, ip)
}
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, port int) 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")
req.Header.Set("Referer", "https://speed.cloudflare.com/")
// 创建TCP连接
dialer := &net.Dialer{
Timeout: timeout,
KeepAlive: 0,
}
conn, err := dialer.Dial("tcp", net.JoinHostPort(ip, strconv.Itoa(port)))
if err != nil {
return 0
}
defer conn.Close()
fmt.Printf("正在测试IP %s 端口 %d\n", ip, port)
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 端口 %d 测速无效\n", ip, port)
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 端口 %d 下载速度 %.0f kB/s\n", ip, port, speed)
return speed
}
// 解析Cloudflare trace响应
func parseTraceResponse(body string) map[string]string {
result := make(map[string]string)
lines := strings.Split(body, "\n")
for _, line := range lines {
line = strings.TrimSpace(line)
if line == "" {
continue
}
parts := strings.SplitN(line, "=", 2)
if len(parts) == 2 {
key := strings.TrimSpace(parts[0])
value := strings.TrimSpace(parts[1])
result[key] = value
}
}
return result
}
func getIPType(ip string) string {
if ip == "" {
return "未知"
}
parsedIP := net.ParseIP(ip)
if parsedIP == nil {
return "无效IP"
}
if parsedIP.To4() != nil {
return "IPv4"
}
return "IPv6"
}
func queryIPAPI(ip string) string {
url := "https://api.ipapi.is/?q=" + ip
client := http.Client{
Timeout: 2 * time.Second,
}
resp, err := client.Get(url)
if err != nil {
return ""
}
defer resp.Body.Close()
var data ipapiResponse
if err := json.NewDecoder(resp.Body).Decode(&data); err != nil {
return ""
}
if data.Error != "" {
return ""
}
companyType := strings.ToLower(data.Company.Type)
asnType := strings.ToLower(data.ASN.Type)
switch {
case companyType == "education" || asnType == "education":
return "🎓教育网"
case companyType == "government" || asnType == "government":
return "🏛政府"
case companyType == "banking" || asnType == "banking":
return "💰金融"
case companyType == "business" || asnType == "business":
return "🏢企业"
case companyType == "isp" && asnType == "isp":
return "✅家宽"
default:
return "🟥机房"
}
}