From 88efe2d1d5ebccbb32734e872d6488feb816937d Mon Sep 17 00:00:00 2001 From: UUBulb <35923940+uubulb@users.noreply.github.com> Date: Sun, 24 Nov 2024 01:02:40 +0800 Subject: [PATCH] improve: use IndexByte to find the colon of ip address (#92) --- cmd/agent/main.go | 2 +- pkg/monitor/myip.go | 4 ++-- pkg/util/http.go | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/cmd/agent/main.go b/cmd/agent/main.go index e322ef3..857d45b 100644 --- a/cmd/agent/main.go +++ b/cmd/agent/main.go @@ -575,7 +575,7 @@ func handleTcpPingTask(task *pb.Task, result *pb.TaskResult) { result.Data = err.Error() return } - if strings.Contains(ipAddr, ":") { + if strings.IndexByte(ipAddr, ':') != -1 { ipAddr = fmt.Sprintf("[%s]", ipAddr) } printf("TCP-Ping Task: Pinging %s:%s", ipAddr, port) diff --git a/pkg/monitor/myip.go b/pkg/monitor/myip.go index 9f1605d..444916a 100644 --- a/pkg/monitor/myip.go +++ b/pkg/monitor/myip.go @@ -91,11 +91,11 @@ func fetchIP(servers []string, isV6 bool) string { } } // 没取到 v6 IP - if isV6 && !strings.Contains(newIP, ":") { + if isV6 && strings.IndexByte(newIP, ':') == -1 { continue } // 没取到 v4 IP - if !isV6 && !strings.Contains(newIP, ".") { + if !isV6 && strings.IndexByte(newIP, '.') == -1 { continue } ip = newIP diff --git a/pkg/util/http.go b/pkg/util/http.go index 4df8341..a1ed464 100644 --- a/pkg/util/http.go +++ b/pkg/util/http.go @@ -72,12 +72,12 @@ func resolveIP(addr string, ipv6 bool) (string, error) { for i := 0; i < len(res); i++ { ip := res[i].String() - if strings.Contains(ip, ".") && !ipv6 { + if strings.IndexByte(ip, '.') != -1 && !ipv6 { ipv4Resolved = true url[0] = ip break } - if strings.Contains(ip, ":") && ipv6 { + if strings.IndexByte(ip, ':') != -1 && ipv6 { ipv6Resolved = true url[0] = "[" + ip + "]" break