feat: tcp/icmp ping 提前取 IP

This commit is contained in:
naiba
2024-07-28 15:05:30 +08:00
parent 7ad3094cee
commit 80ef923a38
6 changed files with 61 additions and 22 deletions
-3
View File
@@ -153,9 +153,6 @@ func GetHost() *model.Host {
ret.IP = CachedIP
ret.Version = Version
// stub
ret.CountryCode = ""
return &ret
}
+9 -5
View File
@@ -18,9 +18,10 @@ var (
"https://dash.cloudflare.com/cdn-cgi/trace",
"https://cf-ns.com/cdn-cgi/trace", // 有国内节点
}
CachedIP, QueryIP string
httpClientV4 = util.NewSingleStackHTTPClient(time.Second*20, time.Second*5, time.Second*10, false)
httpClientV6 = util.NewSingleStackHTTPClient(time.Second*20, time.Second*5, time.Second*10, true)
CachedIP, GeoQueryIP, CachedCountryCode string
GeoQueryIPChanged bool
httpClientV4 = util.NewSingleStackHTTPClient(time.Second*20, time.Second*5, time.Second*10, false)
httpClientV6 = util.NewSingleStackHTTPClient(time.Second*20, time.Second*5, time.Second*10, true)
)
// UpdateIP 按设置时间间隔更新IP地址的缓存
@@ -44,11 +45,14 @@ func UpdateIP(useIPv6CountryCode bool, period uint32) {
CachedIP = fmt.Sprintf("%s/%s", ipv4, ipv6)
}
var newIP string
if !useIPv6CountryCode {
QueryIP = ipv4
newIP = ipv4
} else {
QueryIP = ipv6
newIP = ipv6
}
GeoQueryIPChanged = newIP != GeoQueryIP
GeoQueryIP = newIP
time.Sleep(time.Second * time.Duration(period))
}