mirror of
https://github.com/qdm12/gluetun.git
synced 2026-05-06 20:10:11 +02:00
chore(provider/utils): remove unused code
This commit is contained in:
@@ -1,131 +0,0 @@
|
|||||||
package utils
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/qdm12/gluetun/internal/configuration/settings"
|
|
||||||
"github.com/qdm12/gluetun/internal/models"
|
|
||||||
)
|
|
||||||
|
|
||||||
func filterServers(servers []models.Server,
|
|
||||||
selection settings.ServerSelection,
|
|
||||||
) (filtered []models.Server) {
|
|
||||||
for _, server := range servers {
|
|
||||||
if filterServer(server, selection) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
filtered = append(filtered, server)
|
|
||||||
}
|
|
||||||
|
|
||||||
return filtered
|
|
||||||
}
|
|
||||||
|
|
||||||
func filterServer(server models.Server,
|
|
||||||
selection settings.ServerSelection,
|
|
||||||
) (filtered bool) {
|
|
||||||
// Note each condition is split to make sure
|
|
||||||
// we have full testing coverage.
|
|
||||||
if server.VPN != selection.VPN {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
if filterByProtocol(selection, server.TCP, server.UDP) {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
if *selection.MultiHopOnly && !server.MultiHop {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
if *selection.FreeOnly && !server.Free {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
if *selection.PremiumOnly && !server.Premium {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
if *selection.StreamOnly && !server.Stream {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
if *selection.OwnedOnly && !server.Owned {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
if *selection.PortForwardOnly && !server.PortForward {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
if *selection.SecureCoreOnly && !server.SecureCore {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
if *selection.TorOnly && !server.Tor {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
if filterByPossibilities(server.Country, selection.Countries) {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
if filterAnyByPossibilities(server.Categories, selection.Categories) {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
if filterByPossibilities(server.Region, selection.Regions) {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
if filterByPossibilities(server.City, selection.Cities) {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
if filterByPossibilities(server.ISP, selection.ISPs) {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
if filterByPossibilities(server.Number, selection.Numbers) {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
if filterByPossibilities(server.ServerName, selection.Names) {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
if filterByPossibilities(server.Hostname, selection.Hostnames) {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO filter port forward server for PIA
|
|
||||||
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
func filterByPossibilities[T string | uint16](value T, possibilities []T) (filtered bool) {
|
|
||||||
if len(possibilities) == 0 {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
for _, possibility := range possibilities {
|
|
||||||
if strings.EqualFold(fmt.Sprint(value), fmt.Sprint(possibility)) {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
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
|
|
||||||
}
|
|
||||||
@@ -1,313 +0,0 @@
|
|||||||
package utils
|
|
||||||
|
|
||||||
import (
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/qdm12/gluetun/internal/configuration/settings"
|
|
||||||
"github.com/qdm12/gluetun/internal/constants"
|
|
||||||
"github.com/qdm12/gluetun/internal/constants/providers"
|
|
||||||
"github.com/qdm12/gluetun/internal/constants/vpn"
|
|
||||||
"github.com/qdm12/gluetun/internal/models"
|
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
)
|
|
||||||
|
|
||||||
func Test_FilterServers(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
|
|
||||||
testCases := map[string]struct {
|
|
||||||
servers []models.Server
|
|
||||||
selection settings.ServerSelection
|
|
||||||
filtered []models.Server
|
|
||||||
}{
|
|
||||||
"no server available": {
|
|
||||||
selection: settings.ServerSelection{}.WithDefaults(providers.Mullvad),
|
|
||||||
},
|
|
||||||
"no filter": {
|
|
||||||
servers: []models.Server{
|
|
||||||
{VPN: vpn.OpenVPN, Hostname: "a", UDP: true},
|
|
||||||
{VPN: vpn.OpenVPN, Hostname: "b", UDP: true},
|
|
||||||
{VPN: vpn.OpenVPN, Hostname: "c", UDP: true},
|
|
||||||
},
|
|
||||||
selection: settings.ServerSelection{}.WithDefaults(providers.Mullvad),
|
|
||||||
filtered: []models.Server{
|
|
||||||
{VPN: vpn.OpenVPN, Hostname: "a", UDP: true},
|
|
||||||
{VPN: vpn.OpenVPN, Hostname: "b", UDP: true},
|
|
||||||
{VPN: vpn.OpenVPN, Hostname: "c", UDP: true},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
"filter by VPN protocol": {
|
|
||||||
selection: settings.ServerSelection{
|
|
||||||
VPN: vpn.Wireguard,
|
|
||||||
}.WithDefaults(providers.Mullvad),
|
|
||||||
servers: []models.Server{
|
|
||||||
{VPN: vpn.OpenVPN, Hostname: "a", UDP: true},
|
|
||||||
{VPN: vpn.Wireguard, Hostname: "b", UDP: true},
|
|
||||||
{VPN: vpn.OpenVPN, Hostname: "c", UDP: true},
|
|
||||||
},
|
|
||||||
filtered: []models.Server{
|
|
||||||
{VPN: vpn.Wireguard, Hostname: "b", UDP: true},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
"filter by network protocol": {
|
|
||||||
selection: settings.ServerSelection{
|
|
||||||
OpenVPN: settings.OpenVPNSelection{
|
|
||||||
Protocol: constants.TCP,
|
|
||||||
},
|
|
||||||
}.WithDefaults(providers.Ivpn),
|
|
||||||
servers: []models.Server{
|
|
||||||
{UDP: true, Hostname: "a", VPN: vpn.OpenVPN},
|
|
||||||
{UDP: true, TCP: true, Hostname: "b", VPN: vpn.OpenVPN},
|
|
||||||
{UDP: true, Hostname: "c", VPN: vpn.OpenVPN},
|
|
||||||
},
|
|
||||||
filtered: []models.Server{
|
|
||||||
{UDP: true, TCP: true, Hostname: "b", VPN: vpn.OpenVPN},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
"filter by multihop only": {
|
|
||||||
selection: settings.ServerSelection{
|
|
||||||
MultiHopOnly: boolPtr(true),
|
|
||||||
}.WithDefaults(providers.Surfshark),
|
|
||||||
servers: []models.Server{
|
|
||||||
{MultiHop: false, VPN: vpn.OpenVPN, UDP: true},
|
|
||||||
{MultiHop: true, VPN: vpn.OpenVPN, UDP: true},
|
|
||||||
{MultiHop: false, VPN: vpn.OpenVPN, UDP: true},
|
|
||||||
},
|
|
||||||
filtered: []models.Server{
|
|
||||||
{MultiHop: true, VPN: vpn.OpenVPN, UDP: true},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
"filter by free only": {
|
|
||||||
selection: settings.ServerSelection{
|
|
||||||
FreeOnly: boolPtr(true),
|
|
||||||
}.WithDefaults(providers.Surfshark),
|
|
||||||
servers: []models.Server{
|
|
||||||
{Free: false, VPN: vpn.OpenVPN, UDP: true},
|
|
||||||
{Free: true, VPN: vpn.OpenVPN, UDP: true},
|
|
||||||
{Free: false, VPN: vpn.OpenVPN, UDP: true},
|
|
||||||
},
|
|
||||||
filtered: []models.Server{
|
|
||||||
{Free: true, VPN: vpn.OpenVPN, UDP: true},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
"filter by premium only": {
|
|
||||||
selection: settings.ServerSelection{
|
|
||||||
PremiumOnly: boolPtr(true),
|
|
||||||
}.WithDefaults(providers.Surfshark),
|
|
||||||
servers: []models.Server{
|
|
||||||
{Premium: false, VPN: vpn.OpenVPN, UDP: true},
|
|
||||||
{Premium: true, VPN: vpn.OpenVPN, UDP: true},
|
|
||||||
{Premium: false, VPN: vpn.OpenVPN, UDP: true},
|
|
||||||
},
|
|
||||||
filtered: []models.Server{
|
|
||||||
{Premium: true, VPN: vpn.OpenVPN, UDP: true},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
"filter by stream only": {
|
|
||||||
selection: settings.ServerSelection{
|
|
||||||
StreamOnly: boolPtr(true),
|
|
||||||
}.WithDefaults(providers.Surfshark),
|
|
||||||
servers: []models.Server{
|
|
||||||
{Stream: false, VPN: vpn.OpenVPN, UDP: true},
|
|
||||||
{Stream: true, VPN: vpn.OpenVPN, UDP: true},
|
|
||||||
{Stream: false, VPN: vpn.OpenVPN, UDP: true},
|
|
||||||
},
|
|
||||||
filtered: []models.Server{
|
|
||||||
{Stream: true, VPN: vpn.OpenVPN, UDP: true},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
"filter by secure core only": {
|
|
||||||
selection: settings.ServerSelection{
|
|
||||||
SecureCoreOnly: boolPtr(true),
|
|
||||||
}.WithDefaults(providers.Protonvpn),
|
|
||||||
servers: []models.Server{
|
|
||||||
{SecureCore: false, VPN: vpn.OpenVPN, UDP: true},
|
|
||||||
{SecureCore: true, VPN: vpn.OpenVPN, UDP: true},
|
|
||||||
{SecureCore: false, VPN: vpn.OpenVPN, UDP: true},
|
|
||||||
},
|
|
||||||
filtered: []models.Server{
|
|
||||||
{SecureCore: true, VPN: vpn.OpenVPN, UDP: true},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
"filter by tor only": {
|
|
||||||
selection: settings.ServerSelection{
|
|
||||||
TorOnly: boolPtr(true),
|
|
||||||
}.WithDefaults(providers.Protonvpn),
|
|
||||||
servers: []models.Server{
|
|
||||||
{Tor: false, VPN: vpn.OpenVPN, UDP: true},
|
|
||||||
{Tor: true, VPN: vpn.OpenVPN, UDP: true},
|
|
||||||
{Tor: false, VPN: vpn.OpenVPN, UDP: true},
|
|
||||||
},
|
|
||||||
filtered: []models.Server{
|
|
||||||
{Tor: true, VPN: vpn.OpenVPN, UDP: true},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
"filter by owned": {
|
|
||||||
selection: settings.ServerSelection{
|
|
||||||
OwnedOnly: boolPtr(true),
|
|
||||||
}.WithDefaults(providers.Mullvad),
|
|
||||||
servers: []models.Server{
|
|
||||||
{Owned: false, VPN: vpn.OpenVPN, UDP: true},
|
|
||||||
{Owned: true, VPN: vpn.OpenVPN, UDP: true},
|
|
||||||
{Owned: false, VPN: vpn.OpenVPN, UDP: true},
|
|
||||||
},
|
|
||||||
filtered: []models.Server{
|
|
||||||
{Owned: true, VPN: vpn.OpenVPN, UDP: true},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
"filter by port forwarding only": {
|
|
||||||
selection: settings.ServerSelection{
|
|
||||||
PortForwardOnly: boolPtr(true),
|
|
||||||
}.WithDefaults(providers.PrivateInternetAccess),
|
|
||||||
servers: []models.Server{
|
|
||||||
{PortForward: false, VPN: vpn.OpenVPN, UDP: true},
|
|
||||||
{PortForward: true, VPN: vpn.OpenVPN, UDP: true},
|
|
||||||
{PortForward: false, VPN: vpn.OpenVPN, UDP: true},
|
|
||||||
},
|
|
||||||
filtered: []models.Server{
|
|
||||||
{PortForward: true, VPN: vpn.OpenVPN, UDP: true},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
"filter by country": {
|
|
||||||
selection: settings.ServerSelection{
|
|
||||||
Countries: []string{"b"},
|
|
||||||
}.WithDefaults(providers.Mullvad),
|
|
||||||
servers: []models.Server{
|
|
||||||
{Country: "a", VPN: vpn.OpenVPN, UDP: true},
|
|
||||||
{Country: "b", VPN: vpn.OpenVPN, UDP: true},
|
|
||||||
{Country: "c", VPN: vpn.OpenVPN, UDP: true},
|
|
||||||
},
|
|
||||||
filtered: []models.Server{
|
|
||||||
{Country: "b", VPN: vpn.OpenVPN, UDP: true},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
"filter by region": {
|
|
||||||
selection: settings.ServerSelection{
|
|
||||||
Regions: []string{"b"},
|
|
||||||
}.WithDefaults(providers.Surfshark),
|
|
||||||
servers: []models.Server{
|
|
||||||
{Region: "a", VPN: vpn.OpenVPN, UDP: true},
|
|
||||||
{Region: "b", VPN: vpn.OpenVPN, UDP: true},
|
|
||||||
{Region: "c", VPN: vpn.OpenVPN, UDP: true},
|
|
||||||
},
|
|
||||||
filtered: []models.Server{
|
|
||||||
{Region: "b", VPN: vpn.OpenVPN, UDP: true},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
"filter by city": {
|
|
||||||
selection: settings.ServerSelection{
|
|
||||||
Cities: []string{"b"},
|
|
||||||
}.WithDefaults(providers.Mullvad),
|
|
||||||
servers: []models.Server{
|
|
||||||
{City: "a", VPN: vpn.OpenVPN, UDP: true},
|
|
||||||
{City: "b", VPN: vpn.OpenVPN, UDP: true},
|
|
||||||
{City: "c", VPN: vpn.OpenVPN, UDP: true},
|
|
||||||
},
|
|
||||||
filtered: []models.Server{
|
|
||||||
{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"},
|
|
||||||
}.WithDefaults(providers.Mullvad),
|
|
||||||
servers: []models.Server{
|
|
||||||
{ISP: "a", VPN: vpn.OpenVPN, UDP: true},
|
|
||||||
{ISP: "b", VPN: vpn.OpenVPN, UDP: true},
|
|
||||||
{ISP: "c", VPN: vpn.OpenVPN, UDP: true},
|
|
||||||
},
|
|
||||||
filtered: []models.Server{
|
|
||||||
{ISP: "b", VPN: vpn.OpenVPN, UDP: true},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
"filter by number": {
|
|
||||||
selection: settings.ServerSelection{
|
|
||||||
Numbers: []uint16{1},
|
|
||||||
}.WithDefaults(providers.Mullvad),
|
|
||||||
servers: []models.Server{
|
|
||||||
{Number: 0, VPN: vpn.OpenVPN, UDP: true},
|
|
||||||
{Number: 1, VPN: vpn.OpenVPN, UDP: true},
|
|
||||||
{Number: 2, VPN: vpn.OpenVPN, UDP: true},
|
|
||||||
},
|
|
||||||
filtered: []models.Server{
|
|
||||||
{Number: 1, VPN: vpn.OpenVPN, UDP: true},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
"filter by server name": {
|
|
||||||
selection: settings.ServerSelection{
|
|
||||||
Names: []string{"b"},
|
|
||||||
}.WithDefaults(providers.Mullvad),
|
|
||||||
servers: []models.Server{
|
|
||||||
{ServerName: "a", VPN: vpn.OpenVPN, UDP: true},
|
|
||||||
{ServerName: "b", VPN: vpn.OpenVPN, UDP: true},
|
|
||||||
{ServerName: "c", VPN: vpn.OpenVPN, UDP: true},
|
|
||||||
},
|
|
||||||
filtered: []models.Server{
|
|
||||||
{ServerName: "b", VPN: vpn.OpenVPN, UDP: true},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
"filter by hostname": {
|
|
||||||
selection: settings.ServerSelection{
|
|
||||||
Hostnames: []string{"b"},
|
|
||||||
}.WithDefaults(providers.Mullvad),
|
|
||||||
servers: []models.Server{
|
|
||||||
{Hostname: "a", VPN: vpn.OpenVPN, UDP: true},
|
|
||||||
{Hostname: "b", VPN: vpn.OpenVPN, UDP: true},
|
|
||||||
{Hostname: "c", VPN: vpn.OpenVPN, UDP: true},
|
|
||||||
},
|
|
||||||
filtered: []models.Server{
|
|
||||||
{Hostname: "b", VPN: vpn.OpenVPN, UDP: true},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
for name, testCase := range testCases {
|
|
||||||
t.Run(name, func(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
|
|
||||||
filtered := filterServers(testCase.servers, testCase.selection)
|
|
||||||
|
|
||||||
assert.Equal(t, testCase.filtered, filtered)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func Test_filterByPossibilities(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
testCases := map[string]struct {
|
|
||||||
value string
|
|
||||||
possibilities []string
|
|
||||||
filtered bool
|
|
||||||
}{
|
|
||||||
"no possibilities": {},
|
|
||||||
"value not in possibilities": {
|
|
||||||
value: "c",
|
|
||||||
possibilities: []string{"a", "b"},
|
|
||||||
filtered: true,
|
|
||||||
},
|
|
||||||
"value in possibilities": {
|
|
||||||
value: "c",
|
|
||||||
possibilities: []string{"a", "b", "c"},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
for name, testCase := range testCases {
|
|
||||||
t.Run(name, func(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
filtered := filterByPossibilities(testCase.value, testCase.possibilities)
|
|
||||||
assert.Equal(t, testCase.filtered, filtered)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -9,7 +9,6 @@ import (
|
|||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
func boolPtr(b bool) *bool { return &b }
|
|
||||||
func uint16Ptr(n uint16) *uint16 { return &n }
|
func uint16Ptr(n uint16) *uint16 { return &n }
|
||||||
|
|
||||||
func Test_GetPort(t *testing.T) {
|
func Test_GetPort(t *testing.T) {
|
||||||
|
|||||||
Reference in New Issue
Block a user