Skip to content

Commit

Permalink
private and reserved range
Browse files Browse the repository at this point in the history
  • Loading branch information
czarcas7ic committed Jun 25, 2024
1 parent 8f730bd commit 875145b
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion p2p/switch.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,19 +166,21 @@ func NewSwitch(

type ipInfo struct {
Status string
Message string
CountryCode string
}

// getRegion retrieves the region associated with a given IP address.
// If the IP address is an empty string, it retrieves the region for the current machine's IP address.
// If the IP address is an empty string, it retrieves the region for the current machine's IP address.
func GetRegionFromIP(ip string) (string, error) {
var url string
if ip == "" {
url = "http://ip-api.com/json/"
} else if ip == "0.0.0.0" {
return "", fmt.Errorf("invalid IP address: %s", ip)
} else {
url = fmt.Sprintf("http://ip-api.com/json/%s?fields=status,countryCode", ip)
url = fmt.Sprintf("http://ip-api.com/json/%s?fields=status,message,countryCode", ip)
}

req, err := http.Get(url)
Expand All @@ -199,6 +201,9 @@ func GetRegionFromIP(ip string) (string, error) {
}

if ipInfo.Status != "success" {
if ipInfo.Message == "private range" || ipInfo.Message == "reserved range" {
return "Unknown", nil
}
return "", fmt.Errorf("failed to get country from IP %s", ip)
}

Expand Down

0 comments on commit 875145b

Please sign in to comment.