From 2083512607522d14612fe5a8ecb3f96178f355b8 Mon Sep 17 00:00:00 2001 From: naiba Date: Sun, 11 Aug 2024 00:14:25 +0800 Subject: [PATCH] =?UTF-8?q?improve:=20=E6=8F=90=E9=AB=98=E5=8D=95=E6=A0=88?= =?UTF-8?q?=E6=9C=BA=E5=99=A8=20IP=20=E8=8E=B7=E5=8F=96=E9=80=9F=E5=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/monitor/myip.go | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/pkg/monitor/myip.go b/pkg/monitor/myip.go index e72180b..51b8c0c 100644 --- a/pkg/monitor/myip.go +++ b/pkg/monitor/myip.go @@ -5,6 +5,7 @@ import ( "io" "net/http" "strings" + "sync" "time" "github.com/nezhahq/agent/pkg/util" @@ -27,8 +28,18 @@ var ( func UpdateIP(useIPv6CountryCode bool, period uint32) { for { util.Println(agentConfig.Debug, "正在更新本地缓存IP信息") - ipv4 := fetchIP(cfList, false) - ipv6 := fetchIP(cfList, true) + wg := new(sync.WaitGroup) + wg.Add(2) + var ipv4, ipv6 string + go func() { + defer wg.Done() + ipv4 = fetchIP(cfList, false) + }() + go func() { + defer wg.Done() + ipv6 = fetchIP(cfList, true) + }() + wg.Wait() if ipv4 == "" && ipv6 == "" { if period > 60 { @@ -67,6 +78,10 @@ func fetchIP(servers []string, isV6 bool) string { } else { resp, err = httpGetWithUA(httpClientV4, servers[i]) } + // 遇到单栈机器提前退出 + if err != nil && strings.Contains(err.Error(), "no route to host") { + return ip + } if err == nil { body, err := io.ReadAll(resp.Body) if err != nil {