mirror of
https://github.com/qdm12/gluetun.git
synced 2026-05-10 04:30:20 +02:00
fix(purevpn/updater): parse country and city from hostname and merges with ip address information (#2991)
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
package updater
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/qdm12/gluetun/internal/constants"
|
||||
)
|
||||
|
||||
var countryCodeToName = constants.CountryCodes() //nolint:gochecknoglobals
|
||||
|
||||
//nolint:gochecknoglobals
|
||||
var countryCityCodeToCityName = map[string]string{
|
||||
"aume": "Melbourne",
|
||||
"aupe": "Perth",
|
||||
"ausd": "Sydney",
|
||||
"ukl": "London",
|
||||
"ukm": "Manchester",
|
||||
"usca": "Los Angeles",
|
||||
"usfl": "Miami",
|
||||
"usga": "Atlanta",
|
||||
"usil": "Chicago",
|
||||
"usnj": "Newark",
|
||||
"usny": "New York",
|
||||
"uspe": "Perth",
|
||||
"usphx": "Phoenix",
|
||||
"ussa": "Seattle",
|
||||
"ussf": "San Francisco",
|
||||
"ustx": "Houston",
|
||||
"usut": "Salt Lake City",
|
||||
"usva": "Ashburn",
|
||||
"uswdc": "Washington DC",
|
||||
}
|
||||
|
||||
func parseHostname(hostname string) (country, city string, warnings []string) {
|
||||
const minHostnameLength = 2 + 3 + 2 // 2 country code + 3 city code + "2-"
|
||||
if len(hostname) < minHostnameLength {
|
||||
warnings = append(warnings,
|
||||
fmt.Sprintf("hostname %q is too short to parse country and city codes", hostname))
|
||||
}
|
||||
countryCode := strings.ToLower(hostname[0:2])
|
||||
country, ok := countryCodeToName[countryCode]
|
||||
if !ok {
|
||||
warnings = append(warnings, fmt.Sprintf("unknown country code %q in hostname %q",
|
||||
countryCode, hostname))
|
||||
}
|
||||
|
||||
twoMinusIndex := strings.Index(hostname, "2-")
|
||||
switch twoMinusIndex {
|
||||
case -1:
|
||||
warnings = append(warnings,
|
||||
fmt.Sprintf("hostname %q does not contain '2-'", hostname))
|
||||
return country, city, warnings
|
||||
case 2: //nolint:mnd
|
||||
// no city code
|
||||
return country, "", warnings
|
||||
}
|
||||
countryCityCode := strings.ToLower(hostname[:twoMinusIndex])
|
||||
city, ok = countryCityCodeToCityName[countryCityCode]
|
||||
if !ok {
|
||||
warnings = append(warnings, fmt.Sprintf("unknown country-city code %q in hostname %q",
|
||||
countryCityCode, hostname))
|
||||
}
|
||||
return country, city, warnings
|
||||
}
|
||||
Reference in New Issue
Block a user