From 4b16a1bc8b6c7a8bf83af9ce38dabbed293b83af Mon Sep 17 00:00:00 2001 From: spiritlhl <103393591+spiritLHLS@users.noreply.github.com> Date: Mon, 31 Mar 2025 07:43:21 +0000 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=8E=BB=E9=99=A4=E6=89=A9=E5=B1=95?= =?UTF-8?q?=E9=81=BF=E5=85=8D=E8=AE=A1=E6=95=B0=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- network/baseinfo/bgp.go | 20 ++++++++++---------- network/baseinfo/bgp_test.go | 8 +++++++- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/network/baseinfo/bgp.go b/network/baseinfo/bgp.go index 606ea51..af43f64 100644 --- a/network/baseinfo/bgp.go +++ b/network/baseinfo/bgp.go @@ -15,7 +15,6 @@ import ( "time" "github.com/imroc/req/v3" - "github.com/nfnt/resize" "github.com/oneclickvirt/basics/model" . "github.com/oneclickvirt/defaultset" ) @@ -141,14 +140,14 @@ func GetActiveIpsCount(ip string, prefixNum int) (int, int, error) { client.ImpersonateChrome() cidrBase := fmt.Sprintf("%s/%d", ip, prefixNum) total := int(math.Pow(2, float64(32-prefixNum))) - active, err := countActiveIPs(client, fmt.Sprintf("https://bgp.tools/pfximg/%s", cidrBase)) + active, err := countActiveIPs(client, fmt.Sprintf("https://bgp.tools/pfximg/%s", cidrBase), total) if err != nil { return 0, 0, err } return active, total, nil } -func countActiveIPs(client *req.Client, url string) (int, error) { +func countActiveIPs(client *req.Client, url string, total int) (int, error) { resp, err := client.R().Get(url) if err != nil { return 0, err @@ -170,16 +169,17 @@ func countActiveIPs(client *req.Client, url string) (int, error) { if err != nil { return 0, fmt.Errorf("failed to decode PNG: %w", err) } - // 调整图像大小 - resizedImg := resize.Resize(0, 100, img, resize.Lanczos3) + totalPixels := img.Bounds().Dx() * img.Bounds().Dy() count := 0 - for y := 0; y < resizedImg.Bounds().Dy(); y++ { - for x := 0; x < resizedImg.Bounds().Dx(); x++ { - c := color.RGBAModel.Convert(resizedImg.At(x, y)).(color.RGBA) - if c.R == 0 && c.G == 3 && c.B == 255 { + for y := 0; y < img.Bounds().Dy(); y++ { + for x := 0; x < img.Bounds().Dx(); x++ { + c := color.RGBAModel.Convert(img.At(x, y)).(color.RGBA) + if c.R == 0 && c.G >= 2 && c.G <= 4 && c.B == 255 { count++ } } } - return count, nil + // 计算比例并调整活跃 IP 估算值 + adjustedActive := int(float64(count) / float64(totalPixels) * float64(total)) + return adjustedActive, nil } diff --git a/network/baseinfo/bgp_test.go b/network/baseinfo/bgp_test.go index f5789d5..88bdc60 100644 --- a/network/baseinfo/bgp_test.go +++ b/network/baseinfo/bgp_test.go @@ -6,7 +6,7 @@ import ( ) func TestNeighborCount(t *testing.T) { - ip := "103.244.227.6" // 示例 IP + ip := "207.174.22.39" // 示例 IP neighborActive, neighborTotal, err := GetActiveIpsCount(MaskIP(ip), 24) if err != nil { fmt.Println("Error:", err) @@ -14,3 +14,9 @@ func TestNeighborCount(t *testing.T) { } fmt.Printf("Neighbor Active: %d/%d\n", neighborActive, neighborTotal) } + +// #!/bin/bash +// subnet="207.174.22" +// total_count=256 +// active_count=$(seq 1 254 | xargs -P 50 -I {} sh -c "ping -c 1 -W 1 $subnet.{} >/dev/null 2>&1 && echo {}" | wc -l) +// echo "活跃 IP 数量: $active_count / $total_count"