Merge pull request #19 from mogumc/main

fix: 修复部分纯v6环境无法获取ipv6
This commit is contained in:
Akizon
2025-08-14 11:15:04 +08:00
committed by GitHub

View File

@@ -1,14 +1,55 @@
package monitoring package monitoring
import ( import (
"context"
"io" "io"
"log"
"net"
"net/http" "net/http"
"regexp" "regexp"
"time"
) )
var userAgent = "curl/8.0.1" 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) { func GetIPv4Address() (string, error) {
client := &http.Client{
Transport: ipv4Transport(),
Timeout: 15 * time.Second,
}
webAPIs := []string{ webAPIs := []string{
"https://www.visa.cn/cdn-cgi/trace", "https://www.visa.cn/cdn-cgi/trace",
"https://www.qualcomm.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://edge-ip.html.zone/geo",
"https://vercel-ip.html.zone/geo", "https://vercel-ip.html.zone/geo",
"http://ipv4.ip.sb", "http://ipv4.ip.sb",
"https://api.ipify.org?format=json"} "https://api.ipify.org?format=json",
}
for _, api := range webAPIs { for _, api := range webAPIs {
// get ipv4 // get ipv4
@@ -25,19 +67,19 @@ func GetIPv4Address() (string, error) {
continue continue
} }
req.Header.Set("User-Agent", userAgent) req.Header.Set("User-Agent", userAgent)
resp, err := http.DefaultClient.Do(req) resp, err := client.Do(req)
if err != nil { if err != nil {
continue continue
} }
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body) body, err := io.ReadAll(resp.Body)
_ = resp.Body.Close() // 获取后立即关闭防止堵塞
if err != nil { if err != nil {
continue continue
} }
// 使用正则表达式从响应体中提取IPv4地址
re := regexp.MustCompile(`\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}`) re := regexp.MustCompile(`\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}`)
ipv4 := re.FindString(string(body)) ipv4 := re.FindString(string(body))
if ipv4 != "" { if ipv4 != "" {
log.Printf("Get IPV4 Success: %s", ipv4)
return ipv4, nil return ipv4, nil
} }
} }
@@ -45,11 +87,17 @@ func GetIPv4Address() (string, error) {
} }
func GetIPv6Address() (string, error) { func GetIPv6Address() (string, error) {
client := &http.Client{
Transport: ipv6Transport(), // 对于v6请求只使用v6
Timeout: 15 * time.Second,
}
webAPIs := []string{ webAPIs := []string{
"https://v6.ip.zxinc.org/info.php?type=json", "https://v6.ip.zxinc.org/info.php?type=json",
"https://api6.ipify.org?format=json", "https://api6.ipify.org?format=json",
"https://ipv6.icanhazip.com", "https://ipv6.icanhazip.com",
"https://api-ipv6.ip.sb/geoip"} "https://api-ipv6.ip.sb/geoip",
}
for _, api := range webAPIs { for _, api := range webAPIs {
// get ipv6 // get ipv6
@@ -58,19 +106,21 @@ func GetIPv6Address() (string, error) {
continue continue
} }
req.Header.Set("User-Agent", userAgent) req.Header.Set("User-Agent", userAgent)
resp, err := http.DefaultClient.Do(req) resp, err := client.Do(req)
if err != nil { if err != nil {
continue continue
} }
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body) body, err := io.ReadAll(resp.Body)
_ = resp.Body.Close() // 获取后立即关闭防止堵塞
if err != nil { if err != nil {
continue continue
} }
// 使用正则表达式从响应体中提取IPv6地址 // 使用正则表达式从响应体中提取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})`) 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)) ipv6 := re.FindString(string(body))
if ipv6 != "" { if ipv6 != "" {
log.Printf("Get IPV6 Success: %s", ipv6)
return ipv6, nil return ipv6, nil
} }
} }
@@ -80,10 +130,12 @@ func GetIPv6Address() (string, error) {
func GetIPAddress() (ipv4, ipv6 string, err error) { func GetIPAddress() (ipv4, ipv6 string, err error) {
ipv4, err = GetIPv4Address() ipv4, err = GetIPv4Address()
if err != nil { if err != nil {
log.Printf("Get IPV4 Error: %v", err)
ipv4 = "" ipv4 = ""
} }
ipv6, err = GetIPv6Address() ipv6, err = GetIPv6Address()
if err != nil { if err != nil {
log.Printf("Get IPV6 Error: %v", err)
ipv6 = "" ipv6 = ""
} }