mirror of
https://github.com/oneclickvirt/basics.git
synced 2025-10-10 11:00:22 +08:00
fix: 内部错误应该在外部写入日志,不应该在内部另起写入
This commit is contained in:
@@ -45,62 +45,50 @@ func GetCIDRPrefix(ip string) int {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if model.EnableLoger {
|
||||||
|
Logger.Info("Can not find ipv4 cidr, use default /24")
|
||||||
|
}
|
||||||
return 24
|
return 24
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetNeighborCount(ip string, prefixNum int) (int, int, error) {
|
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 := req.C()
|
||||||
client.ImpersonateChrome()
|
client.ImpersonateChrome()
|
||||||
cidrBase := fmt.Sprintf("%s/%d", ip, prefixNum)
|
cidrBase := fmt.Sprintf("%s/%d", ip, prefixNum)
|
||||||
neighborTotal := int(math.Pow(2, float64(32-prefixNum)))
|
neighborTotal := int(math.Pow(2, float64(32-prefixNum)))
|
||||||
neighborActive, err := countActiveIPs(client, fmt.Sprintf("https://bgp.tools/pfximg/%s", cidrBase))
|
neighborActive, err := countActiveIPs(client, fmt.Sprintf("https://bgp.tools/pfximg/%s", cidrBase))
|
||||||
if err != nil {
|
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
|
return 0, 0, err
|
||||||
}
|
}
|
||||||
if model.EnableLoger {
|
|
||||||
Logger.Info(fmt.Sprintf("Active IPs: %d/%d", neighborActive, neighborTotal))
|
|
||||||
}
|
|
||||||
return neighborActive, neighborTotal, nil
|
return neighborActive, neighborTotal, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func countActiveIPs(client *req.Client, url string) (int, error) {
|
func countActiveIPs(client *req.Client, url string) (int, error) {
|
||||||
resp, err := client.R().Get(url)
|
resp, err := client.R().Get(url)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if model.EnableLoger {
|
|
||||||
Logger.Info(fmt.Sprintf("Error sending request to %s: %s", url, err.Error()))
|
|
||||||
}
|
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
if !resp.IsSuccessState() {
|
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)
|
return 0, fmt.Errorf("HTTP request failed: %s", resp.Status)
|
||||||
}
|
}
|
||||||
// 读取 PNG 数据到内存
|
// 读取 PNG 数据到内存
|
||||||
data, err := io.ReadAll(resp.Body)
|
data, err := io.ReadAll(resp.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if model.EnableLoger {
|
|
||||||
Logger.Info(fmt.Sprintf("Error reading PNG data from %s: %s", url, err.Error()))
|
|
||||||
}
|
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
// 确保数据正确
|
// 确保数据正确
|
||||||
if len(data) < 8 || !bytes.HasPrefix(data, []byte("\x89PNG\r\n\x1a\n")) {
|
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")
|
return 0, fmt.Errorf("invalid PNG format")
|
||||||
}
|
}
|
||||||
// 解码 PNG
|
// 解码 PNG
|
||||||
img, err := png.Decode(bytes.NewReader(data))
|
img, err := png.Decode(bytes.NewReader(data))
|
||||||
if err != nil {
|
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)
|
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
|
return count, nil
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user