diff --git a/monitoring/unit/ip.go b/monitoring/unit/ip.go index 6a4ffea..3e48254 100644 --- a/monitoring/unit/ip.go +++ b/monitoring/unit/ip.go @@ -1,14 +1,55 @@ package monitoring import ( + "context" "io" + "log" + "net" "net/http" "regexp" + "time" ) var userAgent = "curl/8.0.1" +func ipv4Transport() *http.Transport { + return &http.Transport{ + DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) { + d := net.Dialer{ + Timeout: 15 * time.Second, + KeepAlive: 30 * time.Second, + } + return d.DialContext(ctx, "tcp4", addr) // 锁v4防止出现问题 + }, + MaxIdleConns: 10, + IdleConnTimeout: 30 * time.Second, + TLSHandshakeTimeout: 10 * time.Second, + ExpectContinueTimeout: 1 * time.Second, + } +} + +func ipv6Transport() *http.Transport { + return &http.Transport{ + DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) { + d := net.Dialer{ + Timeout: 15 * time.Second, + KeepAlive: 30 * time.Second, + } + return d.DialContext(ctx, "tcp6", addr) // 锁v6防止出现问题 + }, + MaxIdleConns: 10, + IdleConnTimeout: 30 * time.Second, + TLSHandshakeTimeout: 10 * time.Second, + ExpectContinueTimeout: 1 * time.Second, + } +} + func GetIPv4Address() (string, error) { + client := &http.Client{ + Transport: ipv4Transport(), + Timeout: 15 * time.Second, + } + webAPIs := []string{ "https://www.visa.cn/cdn-cgi/trace", "https://www.qualcomm.cn/cdn-cgi/trace", @@ -16,7 +57,8 @@ func GetIPv4Address() (string, error) { "https://edge-ip.html.zone/geo", "https://vercel-ip.html.zone/geo", "http://ipv4.ip.sb", - "https://api.ipify.org?format=json"} + "https://api.ipify.org?format=json", + } for _, api := range webAPIs { // get ipv4 @@ -25,19 +67,19 @@ func GetIPv4Address() (string, error) { continue } req.Header.Set("User-Agent", userAgent) - resp, err := http.DefaultClient.Do(req) + resp, err := client.Do(req) if err != nil { continue } - defer resp.Body.Close() body, err := io.ReadAll(resp.Body) + _ = resp.Body.Close() // 获取后立即关闭防止堵塞 if err != nil { continue } - // 使用正则表达式从响应体中提取IPv4地址 re := regexp.MustCompile(`\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}`) ipv4 := re.FindString(string(body)) if ipv4 != "" { + log.Printf("Get IPV4 Success: %s", ipv4) return ipv4, nil } } @@ -45,11 +87,17 @@ func GetIPv4Address() (string, error) { } func GetIPv6Address() (string, error) { + client := &http.Client{ + Transport: ipv6Transport(), // 对于v6请求只使用v6 + Timeout: 15 * time.Second, + } + webAPIs := []string{ "https://v6.ip.zxinc.org/info.php?type=json", "https://api6.ipify.org?format=json", "https://ipv6.icanhazip.com", - "https://api-ipv6.ip.sb/geoip"} + "https://api-ipv6.ip.sb/geoip", + } for _, api := range webAPIs { // get ipv6 @@ -58,19 +106,21 @@ func GetIPv6Address() (string, error) { continue } req.Header.Set("User-Agent", userAgent) - resp, err := http.DefaultClient.Do(req) + resp, err := client.Do(req) if err != nil { continue } - defer resp.Body.Close() body, err := io.ReadAll(resp.Body) + _ = resp.Body.Close() // 获取后立即关闭防止堵塞 if err != nil { continue } + // 使用正则表达式从响应体中提取IPv6地址 re := regexp.MustCompile(`(([0-9A-Fa-f]{1,4}:){7})([0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){1,6}:)(([0-9A-Fa-f]{1,4}:){0,4})([0-9A-Fa-f]{1,4})`) ipv6 := re.FindString(string(body)) if ipv6 != "" { + log.Printf("Get IPV6 Success: %s", ipv6) return ipv6, nil } } @@ -80,10 +130,12 @@ func GetIPv6Address() (string, error) { func GetIPAddress() (ipv4, ipv6 string, err error) { ipv4, err = GetIPv4Address() if err != nil { + log.Printf("Get IPV4 Error: %v", err) ipv4 = "" } ipv6, err = GetIPv6Address() if err != nil { + log.Printf("Get IPV6 Error: %v", err) ipv6 = "" }