From 82d53e3d3465dbca8aec0d00ad33462b22d4453a Mon Sep 17 00:00:00 2001 From: yanweidonog Date: Fri, 27 Feb 2026 00:51:35 +0800 Subject: [PATCH] feat. --- go.mod | 3 ++ main.go | 98 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 101 insertions(+) create mode 100644 go.mod create mode 100644 main.go diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..624d203 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module ping_tg_proxy + +go 1.25.0 diff --git a/main.go b/main.go new file mode 100644 index 0000000..4b7d252 --- /dev/null +++ b/main.go @@ -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 +} \ No newline at end of file