feat(nordvpn): filter with SERVER_CATEGORIES (#1806)

- update NordVPN servers data built-in
This commit is contained in:
Adam Hebden
2024-03-22 09:02:31 +00:00
committed by GitHub
parent c74e4178bb
commit b3ceece779
17 changed files with 70616 additions and 4232 deletions
@@ -139,6 +139,19 @@ func (s *serverData) hasVPNService(services map[uint32]serviceData) (ok bool) {
return false
}
// categories returns the list of categories for the server.
func (s *serverData) categories(groups map[uint32]groupData) (categories []string) {
categories = make([]string, 0, len(s.GroupIDs))
for _, groupID := range s.GroupIDs {
data, ok := groups[groupID]
if !ok || data.Type.Identifier == "regions" {
continue
}
categories = append(categories, data.Title)
}
return categories
}
// ips returns the list of IP addresses for the server.
func (s *serverData) ips() (ips []netip.Addr) {
ips = make([]netip.Addr, 0, len(s.IPs))
+6 -5
View File
@@ -78,11 +78,12 @@ func extractServers(jsonServer serverData, groups map[uint32]groupData,
}
server := models.Server{
Country: location.Country.Name,
Region: jsonServer.region(groups),
City: location.Country.City.Name,
Hostname: jsonServer.Hostname,
IPs: jsonServer.ips(),
Country: location.Country.Name,
Region: jsonServer.region(groups),
City: location.Country.City.Name,
Categories: jsonServer.categories(groups),
Hostname: jsonServer.Hostname,
IPs: jsonServer.ips(),
}
number, err := parseServerName(jsonServer.Name)
+18
View File
@@ -61,6 +61,10 @@ func filterServer(server models.Server,
return true
}
if filterAnyByPossibilities(server.Categories, selection.Categories) {
return true
}
if filterByPossibilities(server.Region, selection.Regions) {
return true
}
@@ -101,3 +105,17 @@ func filterByPossibilities[T string | uint16](value T, possibilities []T) (filte
}
return true
}
func filterAnyByPossibilities(values, possibilities []string) (filtered bool) {
if len(possibilities) == 0 {
return false
}
for _, value := range values {
if !filterByPossibilities(value, possibilities) {
return false // found a valid value
}
}
return true
}
+13
View File
@@ -179,6 +179,19 @@ func Test_FilterServers(t *testing.T) {
{City: "b", VPN: vpn.OpenVPN, UDP: true},
},
},
"filter by category": {
selection: settings.ServerSelection{
Categories: []string{"legacy_p2p"},
}.WithDefaults(providers.Nordvpn),
servers: []models.Server{
{Categories: []string{"legacy_p2p"}, VPN: vpn.OpenVPN, UDP: true},
{Categories: []string{"legacy_standard"}, VPN: vpn.OpenVPN, UDP: true},
{VPN: vpn.OpenVPN, UDP: true},
},
filtered: []models.Server{
{Categories: []string{"legacy_p2p"}, VPN: vpn.OpenVPN, UDP: true},
},
},
"filter by ISP": {
selection: settings.ServerSelection{
ISPs: []string{"b"},