-
-
Notifications
You must be signed in to change notification settings - Fork 340
/
filtering.go
121 lines (94 loc) · 2.33 KB
/
filtering.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
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 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
}