hotfix(nordvpn): accept countries in SERVER_REGIONS
This commit is contained in:
42
internal/configuration/settings/nordvpn_retro.go
Normal file
42
internal/configuration/settings/nordvpn_retro.go
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
package settings
|
||||||
|
|
||||||
|
// Retro-compatibility because SERVER_REGIONS changed to SERVER_COUNTRIES
|
||||||
|
// and SERVER_REGIONS is now the continent field for servers.
|
||||||
|
// TODO v4 remove
|
||||||
|
func nordvpnRetroRegion(selection ServerSelection, validRegions, validCountries []string) (
|
||||||
|
updatedSelection ServerSelection) {
|
||||||
|
validRegionsMap := stringSliceToMap(validRegions)
|
||||||
|
validCountriesMap := stringSliceToMap(validCountries)
|
||||||
|
|
||||||
|
updatedSelection = selection.copy()
|
||||||
|
updatedSelection.Regions = make([]string, 0, len(selection.Regions))
|
||||||
|
for _, region := range selection.Regions {
|
||||||
|
_, isValid := validRegionsMap[region]
|
||||||
|
if isValid {
|
||||||
|
updatedSelection.Regions = append(updatedSelection.Regions, region)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
_, isValid = validCountriesMap[region]
|
||||||
|
if !isValid {
|
||||||
|
// Region is not valid for the country or region
|
||||||
|
// just leave it to the validation to fail it later
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// Region is not valid for a region, but is a valid country
|
||||||
|
// Handle retro-compatibility and transfer the value to the
|
||||||
|
// country field.
|
||||||
|
updatedSelection.Countries = append(updatedSelection.Countries, region)
|
||||||
|
}
|
||||||
|
|
||||||
|
return updatedSelection
|
||||||
|
}
|
||||||
|
|
||||||
|
func stringSliceToMap(slice []string) (m map[string]struct{}) {
|
||||||
|
m = make(map[string]struct{}, len(slice))
|
||||||
|
for _, s := range slice {
|
||||||
|
m[s] = struct{}{}
|
||||||
|
}
|
||||||
|
return m
|
||||||
|
}
|
||||||
@@ -88,6 +88,14 @@ func (ss *ServerSelection) validate(vpnServiceProvider string,
|
|||||||
return err // already wrapped error
|
return err // already wrapped error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Retro-compatibility
|
||||||
|
switch vpnServiceProvider {
|
||||||
|
case providers.Nordvpn:
|
||||||
|
*ss = nordvpnRetroRegion(*ss, filterChoices.Regions, filterChoices.Countries)
|
||||||
|
case providers.Surfshark:
|
||||||
|
*ss = surfsharkRetroRegion(*ss)
|
||||||
|
}
|
||||||
|
|
||||||
err = validateServerFilters(*ss, filterChoices)
|
err = validateServerFilters(*ss, filterChoices)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("for VPN service provider %s: %w", vpnServiceProvider, err)
|
return fmt.Errorf("for VPN service provider %s: %w", vpnServiceProvider, err)
|
||||||
@@ -163,7 +171,6 @@ func getLocationFilterChoices(vpnServiceProvider string,
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return models.FilterChoices{}, fmt.Errorf("%w: %w", ErrRegionNotValid, err)
|
return models.FilterChoices{}, fmt.Errorf("%w: %w", ErrRegionNotValid, err)
|
||||||
}
|
}
|
||||||
*ss = surfsharkRetroRegion(*ss)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return filterChoices, nil
|
return filterChoices, nil
|
||||||
|
|||||||
Reference in New Issue
Block a user