From 026f40dc4caf88fc2e2766579cd6f1be1f926fca Mon Sep 17 00:00:00 2001 From: spiritlhl <103393591+spiritLHLS@users.noreply.github.com> Date: Fri, 1 Aug 2025 17:34:56 +0000 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=B7=BB=E5=8A=A0=E9=A2=9D=E5=A4=96?= =?UTF-8?q?=E7=9A=84=E6=9C=AC=E5=9C=B0=E8=8E=B7=E5=8F=96=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- upstreams/upstreams.go | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/upstreams/upstreams.go b/upstreams/upstreams.go index 68eaf581..5d6075f5 100644 --- a/upstreams/upstreams.go +++ b/upstreams/upstreams.go @@ -4,13 +4,14 @@ import ( "context" "encoding/json" "fmt" - "strings" - "time" - "github.com/imroc/req/v3" "github.com/oneclickvirt/UnlockTests/uts" bgptools "github.com/oneclickvirt/backtrace/bgptools" backtrace "github.com/oneclickvirt/backtrace/bk" + "net" + "os/exec" + "strings" + "time" ) type IpInfo struct { @@ -32,11 +33,24 @@ func fetchIP(ctx context.Context, url string, parse func([]byte) (string, error) ch <- ip } } - +func fetchLocalIP(ctx context.Context, ch chan<- string) { + cmd := exec.CommandContext(ctx, "bash", "-c", "ip addr show | awk '/inet .*global/ && !/inet6/ {print $2}' | sed -n '1p'") + output, err := cmd.Output() + if err != nil { + return + } + ipCidr := strings.TrimSpace(string(output)) + if ipCidr != "" { + ip, _, err := net.ParseCIDR(ipCidr) + if err == nil && ip.To4() != nil { + ch <- ip.String() + } + } +} func UpstreamsCheck() { ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) defer cancel() - ipChan := make(chan string, 3) + ipChan := make(chan string, 4) go fetchIP(ctx, "https://ipinfo.io", func(b []byte) (string, error) { var data IpInfo err := json.Unmarshal(b, &data) @@ -52,6 +66,7 @@ func UpstreamsCheck() { err := json.Unmarshal(b, &data) return data.Query, err }, ipChan) + go fetchLocalIP(ctx, ipChan) var ip string select { case ip = <-ipChan: