修复IP位置显示

This commit is contained in:
Alex Yang
2025-11-30 13:00:47 +08:00
parent ed719255b7
commit 9311124602

View File

@@ -928,11 +928,60 @@ func (s *Server) GetMonthlyStats() map[string]int64 {
return result
}
// isPrivateIP 检测IP地址是否为内网IP
func isPrivateIP(ip string) bool {
// 解析IP地址
parsedIP := net.ParseIP(ip)
if parsedIP == nil {
return false
}
// 检查IPv4内网地址
if ipv4 := parsedIP.To4(); ipv4 != nil {
// 10.0.0.0/8
if ipv4[0] == 10 {
return true
}
// 172.16.0.0/12
if ipv4[0] == 172 && (ipv4[1] >= 16 && ipv4[1] <= 31) {
return true
}
// 192.168.0.0/16
if ipv4[0] == 192 && ipv4[1] == 168 {
return true
}
// 127.0.0.0/8 (localhost)
if ipv4[0] == 127 {
return true
}
// 169.254.0.0/16 (链路本地地址)
if ipv4[0] == 169 && ipv4[1] == 254 {
return true
}
return false
}
// 检查IPv6内网地址
// ::1/128 (localhost)
if parsedIP.IsLoopback() {
return true
}
// fc00::/7 (唯一本地地址)
if parsedIP[0]&0xfc == 0xfc {
return true
}
// fe80::/10 (链路本地地址)
if parsedIP[0]&0xfe == 0xfe && parsedIP[1]&0xc0 == 0x80 {
return true
}
return false
}
// getIpGeolocation 获取IP地址的地理位置信息
func (s *Server) getIpGeolocation(ip string) string {
// 检查IP是否为本地地址
if ip == "127.0.0.1" || ip == "::1" {
return "本地 本地"
// 检查IP是否为本地或内网地址
if isPrivateIP(ip) {
return "内网 内网"
}
// 先检查缓存