fix: 去除扩展避免计数错误

This commit is contained in:
spiritlhl
2025-03-31 07:43:21 +00:00
parent f0ebc0a0ac
commit 4b16a1bc8b
2 changed files with 17 additions and 11 deletions

View File

@@ -15,7 +15,6 @@ import (
"time" "time"
"github.com/imroc/req/v3" "github.com/imroc/req/v3"
"github.com/nfnt/resize"
"github.com/oneclickvirt/basics/model" "github.com/oneclickvirt/basics/model"
. "github.com/oneclickvirt/defaultset" . "github.com/oneclickvirt/defaultset"
) )
@@ -141,14 +140,14 @@ func GetActiveIpsCount(ip string, prefixNum int) (int, int, error) {
client.ImpersonateChrome() client.ImpersonateChrome()
cidrBase := fmt.Sprintf("%s/%d", ip, prefixNum) cidrBase := fmt.Sprintf("%s/%d", ip, prefixNum)
total := int(math.Pow(2, float64(32-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 { if err != nil {
return 0, 0, err return 0, 0, err
} }
return active, total, nil 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) resp, err := client.R().Get(url)
if err != nil { if err != nil {
return 0, err return 0, err
@@ -170,16 +169,17 @@ func countActiveIPs(client *req.Client, url string) (int, error) {
if err != nil { if err != nil {
return 0, fmt.Errorf("failed to decode PNG: %w", err) return 0, fmt.Errorf("failed to decode PNG: %w", err)
} }
// 调整图像大小 totalPixels := img.Bounds().Dx() * img.Bounds().Dy()
resizedImg := resize.Resize(0, 100, img, resize.Lanczos3)
count := 0 count := 0
for y := 0; y < resizedImg.Bounds().Dy(); y++ { for y := 0; y < img.Bounds().Dy(); y++ {
for x := 0; x < resizedImg.Bounds().Dx(); x++ { for x := 0; x < img.Bounds().Dx(); x++ {
c := color.RGBAModel.Convert(resizedImg.At(x, y)).(color.RGBA) c := color.RGBAModel.Convert(img.At(x, y)).(color.RGBA)
if c.R == 0 && c.G == 3 && c.B == 255 { if c.R == 0 && c.G >= 2 && c.G <= 4 && c.B == 255 {
count++ count++
} }
} }
} }
return count, nil // 计算比例并调整活跃 IP 估算值
adjustedActive := int(float64(count) / float64(totalPixels) * float64(total))
return adjustedActive, nil
} }

View File

@@ -6,7 +6,7 @@ import (
) )
func TestNeighborCount(t *testing.T) { func TestNeighborCount(t *testing.T) {
ip := "103.244.227.6" // 示例 IP ip := "207.174.22.39" // 示例 IP
neighborActive, neighborTotal, err := GetActiveIpsCount(MaskIP(ip), 24) neighborActive, neighborTotal, err := GetActiveIpsCount(MaskIP(ip), 24)
if err != nil { if err != nil {
fmt.Println("Error:", err) fmt.Println("Error:", err)
@@ -14,3 +14,9 @@ func TestNeighborCount(t *testing.T) {
} }
fmt.Printf("Neighbor Active: %d/%d\n", neighborActive, neighborTotal) 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"