Skip to content

Commit

Permalink
node: fix geolocation
Browse files Browse the repository at this point in the history
  • Loading branch information
maxmouchet committed Jul 5, 2022
1 parent 3aea3af commit 35fb854
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 15 deletions.
6 changes: 3 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,20 +103,20 @@ func getHostnameRoot(platform string) string {
case platforms.GENI:
// TODO: From slice name?
geoIP := network.GeoIP()
return strings.ToLower(fmt.Sprintf("geni-%s-%s", geoIP.CountryCode, geoIP.RegionCode))
return strings.ToLower(fmt.Sprintf("geni-%s-%s", geoIP.CountryCode, geoIP.Region))
case platforms.GCP:
region := platforms.GCPGetMetadata("instance/zone")
region = strings.Split(region, "/")[3]
return fmt.Sprintf("gcp-%s", region)
case platforms.NUC:
geoIP := network.GeoIP()
return strings.ToLower(fmt.Sprintf("nuc-%s-%s", geoIP.CountryCode, geoIP.RegionCode))
return strings.ToLower(fmt.Sprintf("nuc-%s-%s", geoIP.CountryCode, geoIP.Region))
case platforms.SCW:
meta := platforms.SCWGetMetadata()
return fmt.Sprintf("scw-%s", meta.Location.ZoneID)
default:
geoIP := network.GeoIP()
return strings.ToLower(fmt.Sprintf("%s-%s", geoIP.CountryCode, geoIP.RegionCode))
return strings.ToLower(fmt.Sprintf("%s-%s", geoIP.CountryCode, geoIP.Region))
}
}

Expand Down
32 changes: 20 additions & 12 deletions pkg/network/geoip.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,29 +25,37 @@ import (
)

type GeoIPResponse struct {
City string `json:"string"`
CountryCode string `json:"country_code"`
CountryName string `json:"country_name"`
IP net.IP `json:"ip"`
Latitude float64 `json:"latitude"`
Longitude float64 `json:"longitude"`
RegionCode string `json:"region_code"`
RegionName string `json:"region_name"`
Timezone string `json:"time_zone"`
Status string `json:"success"`
Country string `json:"country"`
CountryCode string `json:"countryCode"`
Region string `json:"region"`
RegionName string `json:"regionName"`
City string `json:"city"`
Zip string `json:"zip"`
Lat float64 `json:"lat"`
Lon float64 `json:"lon"`
Timezone string `json:"timezone"`
ISP string `json:"isp"`
Org string `json:"org"`
As string `json:"as"`
Query string `json:"query"`
}

// GeoIP returns the geographical location of the requester IP address
// using an external service.
func GeoIP() GeoIPResponse {
// NOTE: this might return an IPv6 address if the host has IPv6 connectivity.
resp, err := http.Get("https://freegeoip.app/json/")
resp, err := http.Get("http://ip-api.com/json/")
check(err)
defer resp.Body.Close()
var geoIP GeoIPResponse
err = json.NewDecoder(resp.Body).Decode(&geoIP)
check(err)
if geoIP.RegionCode == "" {
geoIP.RegionCode = geoIP.CountryCode
if geoIP.CountryCode == "" {
geoIP.CountryCode = "na"
}
if geoIP.Region == "" {
geoIP.Region = geoIP.CountryCode
}
return geoIP
}
Expand Down

0 comments on commit 35fb854

Please sign in to comment.