fix bug: Get area information
This commit is contained in:
parent
e0003ea074
commit
aaea4183c2
12
pkg/ip/ip.go
12
pkg/ip/ip.go
@ -55,6 +55,15 @@ var (
|
|||||||
|
|
||||||
// GetRegionByIp queries the geolocation of an IP address using supported services.
|
// GetRegionByIp queries the geolocation of an IP address using supported services.
|
||||||
func GetRegionByIp(ip string) (*GeoLocationResponse, error) {
|
func GetRegionByIp(ip string) (*GeoLocationResponse, error) {
|
||||||
|
// 如果是域名,先解析成 IP
|
||||||
|
if net.ParseIP(ip) == nil {
|
||||||
|
ips, err := GetIP(ip)
|
||||||
|
if err != nil || len(ips) == 0 {
|
||||||
|
return nil, errors.Wrap(err, "无法解析域名为IP")
|
||||||
|
}
|
||||||
|
ip = ips[0] // 取第一个解析到的IP
|
||||||
|
}
|
||||||
|
|
||||||
for service, enabled := range queryUrls {
|
for service, enabled := range queryUrls {
|
||||||
if enabled {
|
if enabled {
|
||||||
response, err := fetchGeolocation(service, ip)
|
response, err := fetchGeolocation(service, ip)
|
||||||
@ -62,6 +71,9 @@ func GetRegionByIp(ip string) (*GeoLocationResponse, error) {
|
|||||||
zap.S().Errorf("Failed to fetch geolocation from %s: %v", service, err)
|
zap.S().Errorf("Failed to fetch geolocation from %s: %v", service, err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
if response.Country == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
return response, nil
|
return response, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user