mirror of
https://github.com/qdm12/gluetun.git
synced 2026-05-09 20:29:23 +02:00
fix(publicip/api/ip2location): rename countries to match standard country names
This commit is contained in:
@@ -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,
|
||||||
|
|||||||
Reference in New Issue
Block a user