fix(publicip/api/ip2location): rename countries to match standard country names

This commit is contained in:
Quentin McGaw
2025-12-23 17:04:52 +00:00
parent 10a7c75aa6
commit c97bd1bb7c
+21
View File
@@ -8,18 +8,21 @@ import (
"net/netip" "net/netip"
"strings" "strings"
"github.com/qdm12/gluetun/internal/constants"
"github.com/qdm12/gluetun/internal/models" "github.com/qdm12/gluetun/internal/models"
) )
type ip2Location struct { type ip2Location struct {
client *http.Client client *http.Client
token string token string
countryCodes map[string]string
} }
func newIP2Location(client *http.Client, token string) *ip2Location { func newIP2Location(client *http.Client, token string) *ip2Location {
return &ip2Location{ return &ip2Location{
client: client, client: client,
token: token, token: token,
countryCodes: constants.CountryCodes(),
} }
} }
@@ -95,6 +98,24 @@ func (i *ip2Location) FetchInfo(ctx context.Context, ip netip.Addr) (
return result, fmt.Errorf("decoding response: %w", err) return result, fmt.Errorf("decoding response: %w", err)
} }
// Remove parentheses from country name
idx := strings.Index(data.CountryName, " (")
if idx != -1 {
data.CountryName = data.CountryName[:idx]
}
// Rename country to match country string obtained from other IP data sources
countryRenames := map[string]string{
"Netherlands": i.countryCodes["nl"],
"United States of America": i.countryCodes["us"],
"United Kingdom of Great Britain and Northern Ireland": i.countryCodes["gb"],
"Czechia": i.countryCodes["cz"],
"Korea": i.countryCodes["kr"],
}
if newName, ok := countryRenames[data.CountryName]; ok {
data.CountryName = newName
}
result = models.PublicIP{ result = models.PublicIP{
IP: data.IP, IP: data.IP,
Region: data.RegionName, Region: data.RegionName,