diff --git a/network/baseinfo/bgp.go b/network/baseinfo/bgp.go index bc5becd..1bba09c 100644 --- a/network/baseinfo/bgp.go +++ b/network/baseinfo/bgp.go @@ -45,62 +45,50 @@ func GetCIDRPrefix(ip string) int { } } } + if model.EnableLoger { + Logger.Info("Can not find ipv4 cidr, use default /24") + } return 24 } func GetNeighborCount(ip string, prefixNum int) (int, int, error) { + if ip == "" { + return 0, 0, fmt.Errorf("IP address cannot be empty") + } + if prefixNum < 0 || prefixNum > 32 { + return 0, 0, fmt.Errorf("prefixNum must be between 0 and 32") + } client := req.C() client.ImpersonateChrome() cidrBase := fmt.Sprintf("%s/%d", ip, prefixNum) neighborTotal := int(math.Pow(2, float64(32-prefixNum))) neighborActive, err := countActiveIPs(client, fmt.Sprintf("https://bgp.tools/pfximg/%s", cidrBase)) if err != nil { - if model.EnableLoger { - Logger.Info(fmt.Sprintf("Error counting active IPs for CIDR %s: %s", cidrBase, err.Error())) - } return 0, 0, err } - if model.EnableLoger { - Logger.Info(fmt.Sprintf("Active IPs: %d/%d", neighborActive, neighborTotal)) - } return neighborActive, neighborTotal, nil } func countActiveIPs(client *req.Client, url string) (int, error) { resp, err := client.R().Get(url) if err != nil { - if model.EnableLoger { - Logger.Info(fmt.Sprintf("Error sending request to %s: %s", url, err.Error())) - } return 0, err } if !resp.IsSuccessState() { - if model.EnableLoger { - Logger.Info(fmt.Sprintf("HTTP request failed for %s: %s", url, resp.Status)) - } return 0, fmt.Errorf("HTTP request failed: %s", resp.Status) } // 读取 PNG 数据到内存 data, err := io.ReadAll(resp.Body) if err != nil { - if model.EnableLoger { - Logger.Info(fmt.Sprintf("Error reading PNG data from %s: %s", url, err.Error())) - } return 0, err } // 确保数据正确 if len(data) < 8 || !bytes.HasPrefix(data, []byte("\x89PNG\r\n\x1a\n")) { - if model.EnableLoger { - Logger.Info(fmt.Sprintf("Invalid PNG format received from %s", url)) - } return 0, fmt.Errorf("invalid PNG format") } // 解码 PNG img, err := png.Decode(bytes.NewReader(data)) if err != nil { - if model.EnableLoger { - Logger.Info(fmt.Sprintf("Failed to decode PNG from %s: %s", url, err.Error())) - } return 0, fmt.Errorf("failed to decode PNG: %w", err) } // 调整图像大小 @@ -114,8 +102,5 @@ func countActiveIPs(client *req.Client, url string) (int, error) { } } } - if model.EnableLoger { - Logger.Info(fmt.Sprintf("Active IP count from PNG: %d", count)) - } return count, nil } diff --git a/network/network.go b/network/network.go index 43dff84..c36123c 100644 --- a/network/network.go +++ b/network/network.go @@ -73,7 +73,7 @@ func processPrintIPInfo(ipVersion string, ipResult *model.IpInfo) string { } // 仅处理 IPv4 的邻居信息 if ipVersion == "ipv4" && ipResult.Ip != "" { - prefixNum := baseinfo.GetCIDRPrefix(ipResult.Ip) + prefixNum := baseinfo.GetCIDRPrefix(ipResult.Ip) neighborActive, neighborTotal, err := baseinfo.GetNeighborCount(ipResult.Ip, prefixNum) if err == nil { info += fmt.Sprintf(" IPV4 Active IPs : %d/%d (CIDR /%d)\n", neighborActive, neighborTotal, prefixNum)