diff --git a/internal/configuration/settings/nordvpn_retro.go b/internal/configuration/settings/nordvpn_retro.go new file mode 100644 index 00000000..0d34416c --- /dev/null +++ b/internal/configuration/settings/nordvpn_retro.go @@ -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 +} diff --git a/internal/configuration/settings/serverselection.go b/internal/configuration/settings/serverselection.go index 57a10e90..62715cc0 100644 --- a/internal/configuration/settings/serverselection.go +++ b/internal/configuration/settings/serverselection.go @@ -88,6 +88,14 @@ func (ss *ServerSelection) validate(vpnServiceProvider string, 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) if err != nil { return fmt.Errorf("for VPN service provider %s: %w", vpnServiceProvider, err) @@ -163,7 +171,6 @@ func getLocationFilterChoices(vpnServiceProvider string, if err != nil { return models.FilterChoices{}, fmt.Errorf("%w: %w", ErrRegionNotValid, err) } - *ss = surfsharkRetroRegion(*ss) } return filterChoices, nil