feat.
This commit is contained in:
98
main.go
Normal file
98
main.go
Normal file
@@ -0,0 +1,98 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
const proxyListURL = "https://gh-proxy.org/https://github.com/hookzof/socks5_list/blob/master/proxy.txt"
|
||||
|
||||
func main() {
|
||||
// 1. 拉取代理列表
|
||||
resp, err := http.Get(proxyListURL)
|
||||
if err != nil {
|
||||
panic(fmt.Errorf("获取代理列表失败: %w", err))
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
panic(fmt.Errorf("获取代理列表失败,HTTP 状态码: %d", resp.StatusCode))
|
||||
}
|
||||
|
||||
scanner := bufio.NewScanner(resp.Body)
|
||||
var proxies []string
|
||||
for scanner.Scan() {
|
||||
line := strings.TrimSpace(scanner.Text())
|
||||
if line == "" || strings.HasPrefix(line, "#") {
|
||||
continue
|
||||
}
|
||||
// 每一行形如 "ip:port"
|
||||
proxies = append(proxies, line)
|
||||
}
|
||||
if err := scanner.Err(); err != nil {
|
||||
panic(fmt.Errorf("读取列表内容失败: %w", err))
|
||||
}
|
||||
|
||||
if len(proxies) == 0 {
|
||||
fmt.Println("代理列表为空")
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Printf("共读取到 %d 个代理,开始测试...\n", len(proxies))
|
||||
|
||||
// 2. 依次测试每个代理的 TCP 连接耗时
|
||||
var (
|
||||
bestProxy string
|
||||
bestLatency time.Duration
|
||||
)
|
||||
|
||||
for _, addr := range proxies {
|
||||
latency, err := testProxyTCP(addr, 3*time.Second)
|
||||
if err != nil {
|
||||
fmt.Printf("[失败] %s: %v\n", addr, err)
|
||||
continue
|
||||
}
|
||||
fmt.Printf("[成功] %s 延迟: %v\n", addr, latency)
|
||||
|
||||
if bestProxy == "" || latency < bestLatency {
|
||||
bestProxy = addr
|
||||
bestLatency = latency
|
||||
}
|
||||
}
|
||||
|
||||
// 3. 输出结果
|
||||
if bestProxy == "" {
|
||||
fmt.Println("没有找到可用的代理服务器")
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Println("====================================")
|
||||
fmt.Printf("最快代理服务器: %s,延迟: %v\n", bestProxy, bestLatency)
|
||||
|
||||
// 4. 输出一个可在 Telegram 中自动配置的 tg 链接(基于 SOCKS5)
|
||||
parts := strings.Split(bestProxy, ":")
|
||||
if len(parts) == 2 {
|
||||
server := parts[0]
|
||||
port := parts[1]
|
||||
tgLink := fmt.Sprintf("tg://socks?server=%s&port=%s", server, port)
|
||||
fmt.Println("Telegram 一键配置链接:")
|
||||
fmt.Println(tgLink)
|
||||
} else {
|
||||
fmt.Println("无法生成 Telegram 链接,代理地址不是 ip:port 格式:", bestProxy)
|
||||
}
|
||||
}
|
||||
|
||||
// testProxyTCP 尝试建立到代理服务器的 TCP 连接,并返回耗时
|
||||
func testProxyTCP(addr string, timeout time.Duration) (time.Duration, error) {
|
||||
start := time.Now()
|
||||
conn, err := net.DialTimeout("tcp", addr, timeout)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
defer conn.Close()
|
||||
return time.Since(start), nil
|
||||
}
|
||||
Reference in New Issue
Block a user