From bd855023682a667cd254dc46cc3c0293db996fff Mon Sep 17 00:00:00 2001 From: wangbin Date: Tue, 8 Jul 2025 16:11:11 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E4=B8=BB=E5=A4=87=E6=A3=80?= =?UTF-8?q?=E6=B5=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Common/commonfunc.go | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/Common/commonfunc.go b/Common/commonfunc.go index 35186e4..d1f6553 100644 --- a/Common/commonfunc.go +++ b/Common/commonfunc.go @@ -1025,26 +1025,24 @@ func FormatTimes(now time.Time, format string) string { return now.Format(format) } -func CheckIPReachable(ip string, port int, timeout time.Duration) bool { - conn, err := net.DialTimeout("tcp", fmt.Sprintf("%s:%d", ip, port), timeout) - if err != nil { - return false - } - conn.Close() - return true -} - func GetAvailableEndpoint() string { primaryIP := LoadConfig().SeverData.ServerIP backupIP := LoadConfig().SeverData.BackUpServerIP // 替换为实际备选IP - port := 9800 - timeout := 3 * time.Second - if CheckIPReachable(primaryIP, port, timeout) { + if IsIPReachable(primaryIP) { return primaryIP } - if CheckIPReachable(backupIP, port, timeout) { + if IsIPReachable(backupIP) { return backupIP } return "" + +} +func IsIPReachable(ip string) bool { + conn, err := net.DialTimeout("ip4:icmp", ip, 10*time.Millisecond) + if err != nil { + return false + } + conn.Close() + return true }