From 960ab9687cff1dd5fb1fc2e7b6b6ca8b640e90c1 Mon Sep 17 00:00:00 2001 From: tsosunchia <59512455+tsosunchia@users.noreply.github.com> Date: Thu, 12 Oct 2023 17:41:48 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=97=A0v6=E7=BD=91=E7=BB=9C?= =?UTF-8?q?=E4=B8=8B=E7=9A=84=E8=BF=9E=E6=8E=A5=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- util/latency.go | 40 ++++++++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/util/latency.go b/util/latency.go index 93f45df..9f1a3e9 100644 --- a/util/latency.go +++ b/util/latency.go @@ -21,6 +21,7 @@ type ResponseInfo struct { var ( results = make(chan ResponseInfo) + timeout = 5 * time.Second ) var FastIpCache = "" @@ -38,6 +39,11 @@ func GetFastIP(domain string, port string, enableOutput bool) string { log.Fatal("DNS resolution failed, please check your system DNS Settings") } + if len(ips) == 0 { + // 添加默认IP 45.88.195.154 + ips = append(ips, net.ParseIP("45.88.195.154")) + } + for _, ip := range ips { go checkLatency(domain, ip.String(), port) } @@ -46,21 +52,22 @@ func GetFastIP(domain string, port string, enableOutput bool) string { select { case result = <-results: - case <-time.After(1 * time.Second): - log.Fatal("IP connection has been timeout, please check your network") + //等待5s没有结果 视为连不上API了 + case <-time.After(timeout): + log.Println("IP connection has been timeout, please check your network") } - if len(ips) > 0 { - if enableOutput { - _, _ = fmt.Fprintf(color.Output, "%s prefered API IP - %s - %s - %s", - color.New(color.FgWhite, color.Bold).Sprintf("[NextTrace API]"), - color.New(color.FgGreen, color.Bold).Sprintf("%s", result.IP), - color.New(color.FgCyan, color.Bold).Sprintf("%sms", result.Latency), - color.New(color.FgGreen, color.Bold).Sprintf("%s", result.Content), - ) - } + //if len(ips) > 0 { + if enableOutput { + _, _ = fmt.Fprintf(color.Output, "%s prefered API IP - %s - %s - %s", + color.New(color.FgWhite, color.Bold).Sprintf("[NextTrace API]"), + color.New(color.FgGreen, color.Bold).Sprintf("%s", result.IP), + color.New(color.FgCyan, color.Bold).Sprintf("%sms", result.Latency), + color.New(color.FgGreen, color.Bold).Sprintf("%s", result.Content), + ) } + //} FastIpCache = result.IP return result.IP } @@ -79,31 +86,32 @@ func checkLatency(domain string, ip string, port string) { TLSClientConfig: &tls.Config{ ServerName: domain, }, - TLSHandshakeTimeout: 1 * time.Second, + TLSHandshakeTimeout: timeout, } client := &http.Client{ Transport: transport, - Timeout: 2 * time.Second, + Timeout: timeout, } //此处虽然是 https://domain/ 但是实际上会使用指定的IP连接 req, err := http.NewRequest("GET", "https://"+ip+":"+port+"/", nil) if err != nil { - results <- ResponseInfo{IP: ip, Latency: "error", Content: ""} + // !!! 此处不要给results返回任何值 + //results <- ResponseInfo{IP: ip, Latency: "error", Content: ""} return } req.Host = domain resp, err := client.Do(req) if err != nil { - results <- ResponseInfo{IP: ip, Latency: "error", Content: ""} + //results <- ResponseInfo{IP: ip, Latency: "error", Content: ""} return } defer resp.Body.Close() bodyBytes, err := io.ReadAll(resp.Body) if err != nil { - results <- ResponseInfo{IP: ip, Latency: "error", Content: ""} + //results <- ResponseInfo{IP: ip, Latency: "error", Content: ""} return } bodyString := string(bodyBytes)