Maintenance: Protocol selection as boolean in code

This commit is contained in:
Quentin McGaw
2021-05-10 18:18:12 +00:00
parent 810ff62c26
commit c59ea781e3
34 changed files with 221 additions and 233 deletions

View File

@@ -34,7 +34,7 @@ func (settings *Provider) cyberghostLines() (lines []string) {
func (settings *Provider) readCyberghost(r reader) (err error) { func (settings *Provider) readCyberghost(r reader) (err error) {
settings.Name = constants.Cyberghost settings.Name = constants.Cyberghost
settings.ServerSelection.Protocol, err = readProtocol(r.env) settings.ServerSelection.TCP, err = readProtocol(r.env)
if err != nil { if err != nil {
return err return err
} }

View File

@@ -19,7 +19,7 @@ func (settings *Provider) fastestvpnLines() (lines []string) {
func (settings *Provider) readFastestvpn(r reader) (err error) { func (settings *Provider) readFastestvpn(r reader) (err error) {
settings.Name = constants.Fastestvpn settings.Name = constants.Fastestvpn
settings.ServerSelection.Protocol, err = readProtocol(r.env) settings.ServerSelection.TCP, err = readProtocol(r.env)
if err != nil { if err != nil {
return err return err
} }

View File

@@ -27,7 +27,7 @@ func (settings *Provider) hideMyAssLines() (lines []string) {
func (settings *Provider) readHideMyAss(r reader) (err error) { func (settings *Provider) readHideMyAss(r reader) (err error) {
settings.Name = constants.HideMyAss settings.Name = constants.HideMyAss
settings.ServerSelection.Protocol, err = readProtocol(r.env) settings.ServerSelection.TCP, err = readProtocol(r.env)
if err != nil { if err != nil {
return err return err
} }

View File

@@ -38,7 +38,7 @@ func (settings *Provider) mullvadLines() (lines []string) {
func (settings *Provider) readMullvad(r reader) (err error) { func (settings *Provider) readMullvad(r reader) (err error) {
settings.Name = constants.Mullvad settings.Name = constants.Mullvad
settings.ServerSelection.Protocol, err = readProtocol(r.env) settings.ServerSelection.TCP, err = readProtocol(r.env)
if err != nil { if err != nil {
return err return err
} }
@@ -68,7 +68,7 @@ func (settings *Provider) readMullvad(r reader) (err error) {
return err return err
} }
settings.ServerSelection.CustomPort, err = readCustomPort(r.env, settings.ServerSelection.Protocol, settings.ServerSelection.CustomPort, err = readCustomPort(r.env, settings.ServerSelection.TCP,
[]uint16{80, 443, 1401}, []uint16{53, 1194, 1195, 1196, 1197, 1300, 1301, 1302, 1303, 1400}) []uint16{80, 443, 1401}, []uint16{53, 1194, 1195, 1196, 1197, 1300, 1301, 1302, 1303, 1400})
if err != nil { if err != nil {
return err return err

View File

@@ -35,7 +35,7 @@ func (settings *Provider) nordvpnLines() (lines []string) {
func (settings *Provider) readNordvpn(r reader) (err error) { func (settings *Provider) readNordvpn(r reader) (err error) {
settings.Name = constants.Nordvpn settings.Name = constants.Nordvpn
settings.ServerSelection.Protocol, err = readProtocol(r.env) settings.ServerSelection.TCP, err = readProtocol(r.env)
if err != nil { if err != nil {
return err return err
} }

View File

@@ -29,7 +29,7 @@ func Test_OpenVPN_JSON(t *testing.T) {
"provider": { "provider": {
"name": "name", "name": "name",
"server_selection": { "server_selection": {
"network_protocol": "", "tcp": false,
"regions": null, "regions": null,
"group": "", "group": "",
"countries": null, "countries": null,

View File

@@ -27,7 +27,7 @@ func (settings *Provider) privadoLines() (lines []string) {
func (settings *Provider) readPrivado(r reader) (err error) { func (settings *Provider) readPrivado(r reader) (err error) {
settings.Name = constants.Privado settings.Name = constants.Privado
settings.ServerSelection.Protocol, err = readProtocol(r.env) settings.ServerSelection.TCP, err = readProtocol(r.env)
if err != nil { if err != nil {
return err return err
} }

View File

@@ -37,7 +37,7 @@ func (settings *Provider) privateinternetaccessLines() (lines []string) {
func (settings *Provider) readPrivateInternetAccess(r reader) (err error) { func (settings *Provider) readPrivateInternetAccess(r reader) (err error) {
settings.Name = constants.PrivateInternetAccess settings.Name = constants.PrivateInternetAccess
settings.ServerSelection.Protocol, err = readProtocol(r.env) settings.ServerSelection.TCP, err = readProtocol(r.env)
if err != nil { if err != nil {
return err return err
} }

View File

@@ -23,7 +23,7 @@ func (settings *Provider) privatevpnLines() (lines []string) {
func (settings *Provider) readPrivatevpn(r reader) (err error) { func (settings *Provider) readPrivatevpn(r reader) (err error) {
settings.Name = constants.Privatevpn settings.Name = constants.Privatevpn
settings.ServerSelection.Protocol, err = readProtocol(r.env) settings.ServerSelection.TCP, err = readProtocol(r.env)
if err != nil { if err != nil {
return err return err
} }

View File

@@ -31,7 +31,7 @@ func (settings *Provider) protonvpnLines() (lines []string) {
func (settings *Provider) readProtonvpn(r reader) (err error) { func (settings *Provider) readProtonvpn(r reader) (err error) {
settings.Name = constants.Protonvpn settings.Name = constants.Protonvpn
settings.ServerSelection.Protocol, err = readProtocol(r.env) settings.ServerSelection.TCP, err = readProtocol(r.env)
if err != nil { if err != nil {
return err return err
} }

View File

@@ -1,7 +1,6 @@
package configuration package configuration
import ( import (
"errors"
"fmt" "fmt"
"net" "net"
"strings" "strings"
@@ -21,10 +20,12 @@ type Provider struct {
func (settings *Provider) lines() (lines []string) { func (settings *Provider) lines() (lines []string) {
lines = append(lines, lastIndent+strings.Title(settings.Name)+" settings:") lines = append(lines, lastIndent+strings.Title(settings.Name)+" settings:")
lines = append(lines, indent+lastIndent+"Network protocol: "+settings.ServerSelection.Protocol) selection := settings.ServerSelection
if settings.ServerSelection.TargetIP != nil { lines = append(lines, indent+lastIndent+"Network protocol: "+protoToString(selection.TCP))
lines = append(lines, indent+lastIndent+"Target IP address: "+settings.ServerSelection.TargetIP.String())
if selection.TargetIP != nil {
lines = append(lines, indent+lastIndent+"Target IP address: "+selection.TargetIP.String())
} }
var providerLines []string var providerLines []string
@@ -73,19 +74,26 @@ func commaJoin(slice []string) string {
return strings.Join(slice, ", ") return strings.Join(slice, ", ")
} }
func readProtocol(env params.Env) (protocol string, err error) { func readProtocol(env params.Env) (tcp bool, err error) {
return env.Inside("PROTOCOL", []string{constants.TCP, constants.UDP}, params.Default(constants.UDP)) protocol, err := env.Inside("PROTOCOL", []string{constants.TCP, constants.UDP}, params.Default(constants.UDP))
if err != nil {
return false, err
}
return protocol == constants.TCP, nil
}
func protoToString(tcp bool) string {
if tcp {
return constants.TCP
}
return constants.UDP
} }
func readTargetIP(env params.Env) (targetIP net.IP, err error) { func readTargetIP(env params.Env) (targetIP net.IP, err error) {
return readIP(env, "OPENVPN_TARGET_IP") return readIP(env, "OPENVPN_TARGET_IP")
} }
var ( func readCustomPort(env params.Env, tcp bool,
ErrInvalidProtocol = errors.New("invalid network protocol")
)
func readCustomPort(env params.Env, protocol string,
allowedTCP, allowedUDP []uint16) (port uint16, err error) { allowedTCP, allowedUDP []uint16) (port uint16, err error) {
port, err = readPortOrZero(env, "PORT") port, err = readPortOrZero(env, "PORT")
if err != nil { if err != nil {
@@ -94,22 +102,18 @@ func readCustomPort(env params.Env, protocol string,
return 0, nil return 0, nil
} }
switch protocol { if tcp {
case constants.TCP:
for i := range allowedTCP { for i := range allowedTCP {
if allowedTCP[i] == port { if allowedTCP[i] == port {
return port, nil return port, nil
} }
} }
return 0, fmt.Errorf("%w: port %d for TCP protocol", ErrInvalidPort, port) return 0, fmt.Errorf("%w: port %d for TCP protocol", ErrInvalidPort, port)
case constants.UDP: }
for i := range allowedUDP { for i := range allowedUDP {
if allowedUDP[i] == port { if allowedUDP[i] == port {
return port, nil return port, nil
} }
} }
return 0, fmt.Errorf("%w: port %d for UDP protocol", ErrInvalidPort, port) return 0, fmt.Errorf("%w: port %d for UDP protocol", ErrInvalidPort, port)
default:
return 0, fmt.Errorf("%w: %s", ErrInvalidProtocol, protocol)
}
} }

View File

@@ -24,7 +24,6 @@ func Test_Provider_lines(t *testing.T) {
settings: Provider{ settings: Provider{
Name: constants.Cyberghost, Name: constants.Cyberghost,
ServerSelection: ServerSelection{ ServerSelection: ServerSelection{
Protocol: constants.UDP,
Group: "group", Group: "group",
Regions: []string{"a", "El country"}, Regions: []string{"a", "El country"},
}, },
@@ -46,7 +45,6 @@ func Test_Provider_lines(t *testing.T) {
settings: Provider{ settings: Provider{
Name: constants.Fastestvpn, Name: constants.Fastestvpn,
ServerSelection: ServerSelection{ ServerSelection: ServerSelection{
Protocol: constants.UDP,
Hostnames: []string{"a", "b"}, Hostnames: []string{"a", "b"},
Countries: []string{"c", "d"}, Countries: []string{"c", "d"},
}, },
@@ -62,7 +60,6 @@ func Test_Provider_lines(t *testing.T) {
settings: Provider{ settings: Provider{
Name: constants.HideMyAss, Name: constants.HideMyAss,
ServerSelection: ServerSelection{ ServerSelection: ServerSelection{
Protocol: constants.UDP,
Countries: []string{"a", "b"}, Countries: []string{"a", "b"},
Cities: []string{"c", "d"}, Cities: []string{"c", "d"},
Hostnames: []string{"e", "f"}, Hostnames: []string{"e", "f"},
@@ -80,7 +77,6 @@ func Test_Provider_lines(t *testing.T) {
settings: Provider{ settings: Provider{
Name: constants.Mullvad, Name: constants.Mullvad,
ServerSelection: ServerSelection{ ServerSelection: ServerSelection{
Protocol: constants.UDP,
Countries: []string{"a", "b"}, Countries: []string{"a", "b"},
Cities: []string{"c", "d"}, Cities: []string{"c", "d"},
ISPs: []string{"e", "f"}, ISPs: []string{"e", "f"},
@@ -104,7 +100,6 @@ func Test_Provider_lines(t *testing.T) {
settings: Provider{ settings: Provider{
Name: constants.Nordvpn, Name: constants.Nordvpn,
ServerSelection: ServerSelection{ ServerSelection: ServerSelection{
Protocol: constants.UDP,
Regions: []string{"a", "b"}, Regions: []string{"a", "b"},
Numbers: []uint16{1, 2}, Numbers: []uint16{1, 2},
}, },
@@ -120,7 +115,6 @@ func Test_Provider_lines(t *testing.T) {
settings: Provider{ settings: Provider{
Name: constants.Privado, Name: constants.Privado,
ServerSelection: ServerSelection{ ServerSelection: ServerSelection{
Protocol: constants.UDP,
Hostnames: []string{"a", "b"}, Hostnames: []string{"a", "b"},
}, },
}, },
@@ -134,7 +128,6 @@ func Test_Provider_lines(t *testing.T) {
settings: Provider{ settings: Provider{
Name: constants.Privatevpn, Name: constants.Privatevpn,
ServerSelection: ServerSelection{ ServerSelection: ServerSelection{
Protocol: constants.UDP,
Hostnames: []string{"a", "b"}, Hostnames: []string{"a", "b"},
Countries: []string{"c", "d"}, Countries: []string{"c", "d"},
Cities: []string{"e", "f"}, Cities: []string{"e", "f"},
@@ -152,7 +145,6 @@ func Test_Provider_lines(t *testing.T) {
settings: Provider{ settings: Provider{
Name: constants.Protonvpn, Name: constants.Protonvpn,
ServerSelection: ServerSelection{ ServerSelection: ServerSelection{
Protocol: constants.UDP,
Countries: []string{"a", "b"}, Countries: []string{"a", "b"},
Regions: []string{"c", "d"}, Regions: []string{"c", "d"},
Cities: []string{"e", "f"}, Cities: []string{"e", "f"},
@@ -174,7 +166,6 @@ func Test_Provider_lines(t *testing.T) {
settings: Provider{ settings: Provider{
Name: constants.PrivateInternetAccess, Name: constants.PrivateInternetAccess,
ServerSelection: ServerSelection{ ServerSelection: ServerSelection{
Protocol: constants.UDP,
Regions: []string{"a", "b"}, Regions: []string{"a", "b"},
EncryptionPreset: constants.PIAEncryptionPresetStrong, EncryptionPreset: constants.PIAEncryptionPresetStrong,
CustomPort: 1, CustomPort: 1,
@@ -198,7 +189,6 @@ func Test_Provider_lines(t *testing.T) {
settings: Provider{ settings: Provider{
Name: constants.Purevpn, Name: constants.Purevpn,
ServerSelection: ServerSelection{ ServerSelection: ServerSelection{
Protocol: constants.UDP,
Regions: []string{"a", "b"}, Regions: []string{"a", "b"},
Countries: []string{"c", "d"}, Countries: []string{"c", "d"},
Cities: []string{"e", "f"}, Cities: []string{"e", "f"},
@@ -216,7 +206,6 @@ func Test_Provider_lines(t *testing.T) {
settings: Provider{ settings: Provider{
Name: constants.Surfshark, Name: constants.Surfshark,
ServerSelection: ServerSelection{ ServerSelection: ServerSelection{
Protocol: constants.UDP,
Regions: []string{"a", "b"}, Regions: []string{"a", "b"},
}, },
}, },
@@ -230,7 +219,6 @@ func Test_Provider_lines(t *testing.T) {
settings: Provider{ settings: Provider{
Name: constants.Torguard, Name: constants.Torguard,
ServerSelection: ServerSelection{ ServerSelection: ServerSelection{
Protocol: constants.UDP,
Countries: []string{"a", "b"}, Countries: []string{"a", "b"},
Cities: []string{"c", "d"}, Cities: []string{"c", "d"},
Hostnames: []string{"e"}, Hostnames: []string{"e"},
@@ -248,7 +236,6 @@ func Test_Provider_lines(t *testing.T) {
settings: Provider{ settings: Provider{
Name: constants.Vyprvpn, Name: constants.Vyprvpn,
ServerSelection: ServerSelection{ ServerSelection: ServerSelection{
Protocol: constants.UDP,
Regions: []string{"a", "b"}, Regions: []string{"a", "b"},
}, },
}, },
@@ -262,7 +249,6 @@ func Test_Provider_lines(t *testing.T) {
settings: Provider{ settings: Provider{
Name: constants.Windscribe, Name: constants.Windscribe,
ServerSelection: ServerSelection{ ServerSelection: ServerSelection{
Protocol: constants.UDP,
Regions: []string{"a", "b"}, Regions: []string{"a", "b"},
Cities: []string{"c", "d"}, Cities: []string{"c", "d"},
Hostnames: []string{"e", "f"}, Hostnames: []string{"e", "f"},
@@ -298,7 +284,7 @@ func Test_readProtocol(t *testing.T) {
testCases := map[string]struct { testCases := map[string]struct {
mockStr string mockStr string
mockErr error mockErr error
protocol string tcp bool
err error err error
}{ }{
"error": { "error": {
@@ -307,7 +293,7 @@ func Test_readProtocol(t *testing.T) {
}, },
"success": { "success": {
mockStr: "tcp", mockStr: "tcp",
protocol: constants.TCP, tcp: true,
}, },
} }
@@ -322,7 +308,7 @@ func Test_readProtocol(t *testing.T) {
Inside("PROTOCOL", []string{"tcp", "udp"}, gomock.Any()). Inside("PROTOCOL", []string{"tcp", "udp"}, gomock.Any()).
Return(testCase.mockStr, testCase.mockErr) Return(testCase.mockStr, testCase.mockErr)
protocol, err := readProtocol(env) tcp, err := readProtocol(env)
if testCase.err != nil { if testCase.err != nil {
require.Error(t, err) require.Error(t, err)
@@ -331,7 +317,7 @@ func Test_readProtocol(t *testing.T) {
assert.NoError(t, err) assert.NoError(t, err)
} }
assert.Equal(t, testCase.protocol, protocol) assert.Equal(t, testCase.tcp, tcp)
}) })
} }
} }

View File

@@ -27,7 +27,7 @@ func (settings *Provider) purevpnLines() (lines []string) {
func (settings *Provider) readPurevpn(r reader) (err error) { func (settings *Provider) readPurevpn(r reader) (err error) {
settings.Name = constants.Purevpn settings.Name = constants.Purevpn
settings.ServerSelection.Protocol, err = readProtocol(r.env) settings.ServerSelection.TCP, err = readProtocol(r.env)
if err != nil { if err != nil {
return err return err
} }

View File

@@ -4,9 +4,9 @@ import (
"net" "net"
) )
type ServerSelection struct { type ServerSelection struct { //nolint:maligned
// Common // Common
Protocol string `json:"network_protocol"` TCP bool `json:"tcp"` // UDP if TCP is false
TargetIP net.IP `json:"target_ip,omitempty"` TargetIP net.IP `json:"target_ip,omitempty"`
// TODO comments // TODO comments
// Cyberghost, PIA, Protonvpn, Surfshark, Windscribe, Vyprvpn, NordVPN // Cyberghost, PIA, Protonvpn, Surfshark, Windscribe, Vyprvpn, NordVPN

View File

@@ -28,7 +28,7 @@ func Test_Settings_lines(t *testing.T) {
" |--Verbosity level: 0", " |--Verbosity level: 0",
" |--Provider:", " |--Provider:",
" |--Mullvad settings:", " |--Mullvad settings:",
" |--Network protocol: ", " |--Network protocol: udp",
"|--DNS:", "|--DNS:",
"|--Firewall: disabled ⚠️", "|--Firewall: disabled ⚠️",
"|--System:", "|--System:",

View File

@@ -19,7 +19,7 @@ func (settings *Provider) surfsharkLines() (lines []string) {
func (settings *Provider) readSurfshark(r reader) (err error) { func (settings *Provider) readSurfshark(r reader) (err error) {
settings.Name = constants.Surfshark settings.Name = constants.Surfshark
settings.ServerSelection.Protocol, err = readProtocol(r.env) settings.ServerSelection.TCP, err = readProtocol(r.env)
if err != nil { if err != nil {
return err return err
} }

View File

@@ -23,7 +23,7 @@ func (settings *Provider) torguardLines() (lines []string) {
func (settings *Provider) readTorguard(r reader) (err error) { func (settings *Provider) readTorguard(r reader) (err error) {
settings.Name = constants.Torguard settings.Name = constants.Torguard
settings.ServerSelection.Protocol, err = readProtocol(r.env) settings.ServerSelection.TCP, err = readProtocol(r.env)
if err != nil { if err != nil {
return err return err
} }

View File

@@ -15,7 +15,7 @@ func (settings *Provider) vyprvpnLines() (lines []string) {
func (settings *Provider) readVyprvpn(r reader) (err error) { func (settings *Provider) readVyprvpn(r reader) (err error) {
settings.Name = constants.Vyprvpn settings.Name = constants.Vyprvpn
settings.ServerSelection.Protocol, err = readProtocol(r.env) settings.ServerSelection.TCP, err = readProtocol(r.env)
if err != nil { if err != nil {
return err return err
} }

View File

@@ -27,7 +27,7 @@ func (settings *Provider) windscribeLines() (lines []string) {
func (settings *Provider) readWindscribe(r reader) (err error) { func (settings *Provider) readWindscribe(r reader) (err error) {
settings.Name = constants.Windscribe settings.Name = constants.Windscribe
settings.ServerSelection.Protocol, err = readProtocol(r.env) settings.ServerSelection.TCP, err = readProtocol(r.env)
if err != nil { if err != nil {
return err return err
} }
@@ -52,7 +52,7 @@ func (settings *Provider) readWindscribe(r reader) (err error) {
return err return err
} }
settings.ServerSelection.CustomPort, err = readCustomPort(r.env, settings.ServerSelection.Protocol, settings.ServerSelection.CustomPort, err = readCustomPort(r.env, settings.ServerSelection.TCP,
[]uint16{21, 22, 80, 123, 143, 443, 587, 1194, 3306, 8080, 54783}, []uint16{21, 22, 80, 123, 143, 443, 587, 1194, 3306, 8080, 54783},
[]uint16{53, 80, 123, 443, 1194, 54783}) []uint16{53, 80, 123, 443, 1194, 54783})
if err != nil { if err != nil {

View File

@@ -45,8 +45,9 @@ func (c *cyberghost) filterServers(regions, hostnames []string, group string) (s
func (c *cyberghost) GetOpenVPNConnection(selection configuration.ServerSelection) ( func (c *cyberghost) GetOpenVPNConnection(selection configuration.ServerSelection) (
connection models.OpenVPNConnection, err error) { connection models.OpenVPNConnection, err error) {
const httpsPort = 443 const httpsPort = 443
protocol := tcpBoolToProtocol(selection.TCP)
if selection.TargetIP != nil { if selection.TargetIP != nil {
return models.OpenVPNConnection{IP: selection.TargetIP, Port: httpsPort, Protocol: selection.Protocol}, nil return models.OpenVPNConnection{IP: selection.TargetIP, Port: httpsPort, Protocol: protocol}, nil
} }
servers := c.filterServers(selection.Regions, selection.Hostnames, selection.Group) servers := c.filterServers(selection.Regions, selection.Hostnames, selection.Group)
@@ -58,7 +59,7 @@ func (c *cyberghost) GetOpenVPNConnection(selection configuration.ServerSelectio
var connections []models.OpenVPNConnection var connections []models.OpenVPNConnection
for _, server := range servers { for _, server := range servers {
for _, IP := range server.IPs { for _, IP := range server.IPs {
connections = append(connections, models.OpenVPNConnection{IP: IP, Port: httpsPort, Protocol: selection.Protocol}) connections = append(connections, models.OpenVPNConnection{IP: IP, Port: httpsPort, Protocol: protocol})
} }
} }

View File

@@ -28,20 +28,13 @@ func newFastestvpn(servers []models.FastestvpnServer, timeNow timeNowFunc) *fast
} }
} }
func (f *fastestvpn) filterServers(countries, hostnames []string, protocol string) (servers []models.FastestvpnServer) { func (f *fastestvpn) filterServers(countries, hostnames []string, tcp bool) (servers []models.FastestvpnServer) {
var tcp, udp bool
if protocol == "tcp" {
tcp = true
} else {
udp = true
}
for _, server := range f.servers { for _, server := range f.servers {
switch { switch {
case filterByPossibilities(server.Country, countries): case filterByPossibilities(server.Country, countries):
case filterByPossibilities(server.Hostname, hostnames): case filterByPossibilities(server.Hostname, hostnames):
case tcp && !server.TCP: case tcp && !server.TCP:
case udp && !server.UDP: case !tcp && !server.UDP:
default: default:
servers = append(servers, server) servers = append(servers, server)
} }
@@ -50,7 +43,7 @@ func (f *fastestvpn) filterServers(countries, hostnames []string, protocol strin
} }
func (f *fastestvpn) notFoundErr(selection configuration.ServerSelection) error { func (f *fastestvpn) notFoundErr(selection configuration.ServerSelection) error {
message := "no server found for protocol " + selection.Protocol message := "no server found for protocol " + tcpBoolToProtocol(selection.TCP)
if len(selection.Hostnames) > 0 { if len(selection.Hostnames) > 0 {
message += " + hostnames " + commaJoin(selection.Hostnames) message += " + hostnames " + commaJoin(selection.Hostnames)
@@ -66,12 +59,13 @@ func (f *fastestvpn) notFoundErr(selection configuration.ServerSelection) error
func (f *fastestvpn) GetOpenVPNConnection(selection configuration.ServerSelection) ( func (f *fastestvpn) GetOpenVPNConnection(selection configuration.ServerSelection) (
connection models.OpenVPNConnection, err error) { connection models.OpenVPNConnection, err error) {
var port uint16 = 4443 var port uint16 = 4443
protocol := tcpBoolToProtocol(selection.TCP)
if selection.TargetIP != nil { if selection.TargetIP != nil {
return models.OpenVPNConnection{IP: selection.TargetIP, Port: port, Protocol: selection.Protocol}, nil return models.OpenVPNConnection{IP: selection.TargetIP, Port: port, Protocol: protocol}, nil
} }
servers := f.filterServers(selection.Countries, selection.Hostnames, selection.Protocol) servers := f.filterServers(selection.Countries, selection.Hostnames, selection.TCP)
if len(servers) == 0 { if len(servers) == 0 {
return connection, f.notFoundErr(selection) return connection, f.notFoundErr(selection)
} }
@@ -82,7 +76,7 @@ func (f *fastestvpn) GetOpenVPNConnection(selection configuration.ServerSelectio
connection := models.OpenVPNConnection{ connection := models.OpenVPNConnection{
IP: IP, IP: IP,
Port: port, Port: port,
Protocol: selection.Protocol, Protocol: protocol,
} }
connections = append(connections, connection) connections = append(connections, connection)
} }

View File

@@ -30,15 +30,15 @@ func newHideMyAss(servers []models.HideMyAssServer, timeNow timeNowFunc) *hideMy
} }
func (h *hideMyAss) filterServers(countries, cities, hostnames []string, func (h *hideMyAss) filterServers(countries, cities, hostnames []string,
protocol string) (servers []models.HideMyAssServer) { tcp bool) (servers []models.HideMyAssServer) {
for _, server := range h.servers { for _, server := range h.servers {
switch { switch {
case case
filterByPossibilities(server.Country, countries), filterByPossibilities(server.Country, countries),
filterByPossibilities(server.City, cities), filterByPossibilities(server.City, cities),
filterByPossibilities(server.Hostname, hostnames), filterByPossibilities(server.Hostname, hostnames),
protocol == constants.TCP && !server.TCP, tcp && !server.TCP,
protocol == constants.UDP && !server.UDP: !tcp && !server.UDP:
default: default:
servers = append(servers, server) servers = append(servers, server)
} }
@@ -67,7 +67,9 @@ func (h *hideMyAss) notFoundErr(selection configuration.ServerSelection) error {
func (h *hideMyAss) GetOpenVPNConnection(selection configuration.ServerSelection) ( func (h *hideMyAss) GetOpenVPNConnection(selection configuration.ServerSelection) (
connection models.OpenVPNConnection, err error) { connection models.OpenVPNConnection, err error) {
var defaultPort uint16 = 553 var defaultPort uint16 = 553
if selection.Protocol == constants.TCP { protocol := constants.UDP
if selection.TCP {
protocol = constants.TCP
defaultPort = 8080 defaultPort = 8080
} }
port := defaultPort port := defaultPort
@@ -76,10 +78,14 @@ func (h *hideMyAss) GetOpenVPNConnection(selection configuration.ServerSelection
} }
if selection.TargetIP != nil { if selection.TargetIP != nil {
return models.OpenVPNConnection{IP: selection.TargetIP, Port: port, Protocol: selection.Protocol}, nil return models.OpenVPNConnection{
IP: selection.TargetIP,
Port: port,
Protocol: protocol,
}, nil
} }
servers := h.filterServers(selection.Countries, selection.Cities, selection.Hostnames, selection.Protocol) servers := h.filterServers(selection.Countries, selection.Cities, selection.Hostnames, selection.TCP)
if len(servers) == 0 { if len(servers) == 0 {
return models.OpenVPNConnection{}, h.notFoundErr(selection) return models.OpenVPNConnection{}, h.notFoundErr(selection)
} }
@@ -87,7 +93,11 @@ func (h *hideMyAss) GetOpenVPNConnection(selection configuration.ServerSelection
var connections []models.OpenVPNConnection var connections []models.OpenVPNConnection
for _, server := range servers { for _, server := range servers {
for _, IP := range server.IPs { for _, IP := range server.IPs {
connections = append(connections, models.OpenVPNConnection{IP: IP, Port: port, Protocol: selection.Protocol}) connections = append(connections, models.OpenVPNConnection{
IP: IP,
Port: port,
Protocol: protocol,
})
} }
} }

View File

@@ -48,8 +48,10 @@ func (m *mullvad) filterServers(countries, cities, hostnames,
func (m *mullvad) GetOpenVPNConnection(selection configuration.ServerSelection) ( func (m *mullvad) GetOpenVPNConnection(selection configuration.ServerSelection) (
connection models.OpenVPNConnection, err error) { connection models.OpenVPNConnection, err error) {
var defaultPort uint16 = 1194 var defaultPort uint16 = 1194
if selection.Protocol == constants.TCP { protocol := constants.UDP
if selection.TCP {
defaultPort = 443 defaultPort = 443
protocol = constants.TCP
} }
port := defaultPort port := defaultPort
if selection.CustomPort > 0 { if selection.CustomPort > 0 {
@@ -57,7 +59,7 @@ func (m *mullvad) GetOpenVPNConnection(selection configuration.ServerSelection)
} }
if selection.TargetIP != nil { if selection.TargetIP != nil {
return models.OpenVPNConnection{IP: selection.TargetIP, Port: port, Protocol: selection.Protocol}, nil return models.OpenVPNConnection{IP: selection.TargetIP, Port: port, Protocol: protocol}, nil
} }
servers := m.filterServers(selection.Countries, selection.Cities, servers := m.filterServers(selection.Countries, selection.Cities,
@@ -70,7 +72,7 @@ func (m *mullvad) GetOpenVPNConnection(selection configuration.ServerSelection)
var connections []models.OpenVPNConnection var connections []models.OpenVPNConnection
for _, server := range servers { for _, server := range servers {
for _, IP := range server.IPs { for _, IP := range server.IPs {
connections = append(connections, models.OpenVPNConnection{IP: IP, Port: port, Protocol: selection.Protocol}) connections = append(connections, models.OpenVPNConnection{IP: IP, Port: port, Protocol: protocol})
} }
} }

View File

@@ -29,7 +29,7 @@ func newNordvpn(servers []models.NordvpnServer, timeNow timeNowFunc) *nordvpn {
} }
} }
func (n *nordvpn) filterServers(regions, hostnames, names []string, numbers []uint16, protocol string) ( func (n *nordvpn) filterServers(regions, hostnames, names []string, numbers []uint16, tcp bool) (
servers []models.NordvpnServer) { servers []models.NordvpnServer) {
numbersStr := make([]string, len(numbers)) numbersStr := make([]string, len(numbers))
for i := range numbers { for i := range numbers {
@@ -39,8 +39,8 @@ func (n *nordvpn) filterServers(regions, hostnames, names []string, numbers []ui
numberStr := fmt.Sprintf("%d", server.Number) numberStr := fmt.Sprintf("%d", server.Number)
switch { switch {
case case
protocol == constants.TCP && !server.TCP, tcp && !server.TCP,
protocol == constants.UDP && !server.UDP, !tcp && !server.UDP,
filterByPossibilities(server.Region, regions), filterByPossibilities(server.Region, regions),
filterByPossibilities(server.Hostname, hostnames), filterByPossibilities(server.Hostname, hostnames),
filterByPossibilities(server.Name, names), filterByPossibilities(server.Name, names),
@@ -55,7 +55,7 @@ func (n *nordvpn) filterServers(regions, hostnames, names []string, numbers []ui
var errNoServerFound = errors.New("no server found") var errNoServerFound = errors.New("no server found")
func (n *nordvpn) notFoundErr(selection configuration.ServerSelection) error { func (n *nordvpn) notFoundErr(selection configuration.ServerSelection) error {
message := "for protocol " + selection.Protocol message := "for protocol " + tcpBoolToProtocol(selection.TCP)
if len(selection.Regions) > 0 { if len(selection.Regions) > 0 {
message += " + regions " + commaJoin(selection.Regions) message += " + regions " + commaJoin(selection.Regions)
@@ -82,29 +82,26 @@ func (n *nordvpn) notFoundErr(selection configuration.ServerSelection) error {
func (n *nordvpn) GetOpenVPNConnection(selection configuration.ServerSelection) ( func (n *nordvpn) GetOpenVPNConnection(selection configuration.ServerSelection) (
connection models.OpenVPNConnection, err error) { connection models.OpenVPNConnection, err error) {
var port uint16 var port uint16 = 1194
switch { protocol := constants.UDP
case selection.Protocol == constants.UDP: if selection.TCP {
port = 1194
case selection.Protocol == constants.TCP:
port = 443 port = 443
default: protocol = constants.TCP
return connection, fmt.Errorf("protocol %q is unknown", selection.Protocol)
} }
if selection.TargetIP != nil { if selection.TargetIP != nil {
return models.OpenVPNConnection{IP: selection.TargetIP, Port: port, Protocol: selection.Protocol}, nil return models.OpenVPNConnection{IP: selection.TargetIP, Port: port, Protocol: protocol}, nil
} }
servers := n.filterServers(selection.Regions, selection.Hostnames, servers := n.filterServers(selection.Regions, selection.Hostnames,
selection.Names, selection.Numbers, selection.Protocol) selection.Names, selection.Numbers, selection.TCP)
if len(servers) == 0 { if len(servers) == 0 {
return connection, n.notFoundErr(selection) return connection, n.notFoundErr(selection)
} }
connections := make([]models.OpenVPNConnection, len(servers)) connections := make([]models.OpenVPNConnection, len(servers))
for i := range servers { for i := range servers {
connections[i] = models.OpenVPNConnection{IP: servers[i].IP, Port: port, Protocol: selection.Protocol} connections[i] = models.OpenVPNConnection{IP: servers[i].IP, Port: port, Protocol: protocol}
} }
return pickRandomConnection(connections, n.randSource), nil return pickRandomConnection(connections, n.randSource), nil

View File

@@ -47,15 +47,14 @@ var (
func (p *pia) getPort(selection configuration.ServerSelection) (port uint16, err error) { func (p *pia) getPort(selection configuration.ServerSelection) (port uint16, err error) {
if selection.CustomPort == 0 { if selection.CustomPort == 0 {
switch selection.Protocol { if selection.TCP {
case constants.TCP:
switch selection.EncryptionPreset { switch selection.EncryptionPreset {
case constants.PIAEncryptionPresetNormal: case constants.PIAEncryptionPresetNormal:
port = 502 port = 502
case constants.PIAEncryptionPresetStrong: case constants.PIAEncryptionPresetStrong:
port = 501 port = 501
} }
case constants.UDP: } else {
switch selection.EncryptionPreset { switch selection.EncryptionPreset {
case constants.PIAEncryptionPresetNormal: case constants.PIAEncryptionPresetNormal:
port = 1198 port = 1198
@@ -63,38 +62,28 @@ func (p *pia) getPort(selection configuration.ServerSelection) (port uint16, err
port = 1197 port = 1197
} }
} }
if port == 0 {
return 0, fmt.Errorf(
"%w: combination of protocol %q and encryption %q does not yield any port number",
ErrInvalidPort, selection.Protocol, selection.EncryptionPreset)
}
return port, nil return port, nil
} }
port = selection.CustomPort port = selection.CustomPort
switch selection.Protocol { if selection.TCP {
case constants.TCP:
switch port { switch port {
case 80, 110, 443: //nolint:gomnd case 80, 110, 443: //nolint:gomnd
return port, nil
default: default:
return 0, fmt.Errorf("%w: %d for protocol %s", return 0, fmt.Errorf("%w: %d for protocol TCP", ErrInvalidPort, port)
ErrInvalidPort, port, selection.Protocol) }
} }
case constants.UDP:
switch port { switch port {
case 53, 1194, 1197, 1198, 8080, 9201: //nolint:gomnd case 53, 1194, 1197, 1198, 8080, 9201: //nolint:gomnd
default:
return 0, fmt.Errorf("%w: %d for protocol %s",
ErrInvalidPort, port, selection.Protocol)
}
}
return port, nil return port, nil
default:
return 0, fmt.Errorf("%w: %d for protocol UDP", ErrInvalidPort, port)
}
} }
func (p *pia) notFoundErr(regions, hostnames, names []string, protocol string) error { func (p *pia) notFoundErr(regions, hostnames, names []string, tcp bool) error {
message := "for protocol " + protocol message := "for protocol " + tcpBoolToProtocol(tcp)
if len(regions) > 0 { if len(regions) > 0 {
message += " + regions " + commaJoin(regions) message += " + regions " + commaJoin(regions)
@@ -118,15 +107,17 @@ func (p *pia) GetOpenVPNConnection(selection configuration.ServerSelection) (
return connection, err return connection, err
} }
protocol := tcpBoolToProtocol(selection.TCP)
servers := p.servers servers := p.servers
if selection.TargetIP != nil { if selection.TargetIP != nil {
connection = models.OpenVPNConnection{IP: selection.TargetIP, Port: port, Protocol: selection.Protocol} connection = models.OpenVPNConnection{IP: selection.TargetIP, Port: port, Protocol: protocol}
} else { } else {
servers := p.filterServers(selection.Regions, selection.Hostnames, servers := p.filterServers(selection.Regions, selection.Hostnames,
selection.Names, selection.Protocol) selection.Names, selection.TCP)
if len(servers) == 0 { if len(servers) == 0 {
return connection, p.notFoundErr(selection.Regions, selection.Hostnames, return connection, p.notFoundErr(selection.Regions, selection.Hostnames,
selection.Names, selection.Protocol) selection.Names, selection.TCP)
} }
var connections []models.OpenVPNConnection var connections []models.OpenVPNConnection
@@ -135,7 +126,7 @@ func (p *pia) GetOpenVPNConnection(selection configuration.ServerSelection) (
connection := models.OpenVPNConnection{ connection := models.OpenVPNConnection{
IP: ip, IP: ip,
Port: port, Port: port,
Protocol: selection.Protocol, Protocol: protocol,
} }
connections = append(connections, connection) connections = append(connections, connection)
} }
@@ -369,15 +360,15 @@ func (p *pia) PortForward(ctx context.Context, client *http.Client,
} }
} }
func (p *pia) filterServers(regions, hostnames, names []string, protocol string) ( func (p *pia) filterServers(regions, hostnames, names []string, tcp bool) (
filtered []models.PIAServer) { filtered []models.PIAServer) {
for _, server := range p.servers { for _, server := range p.servers {
switch { switch {
case filterByPossibilities(server.Region, regions), case filterByPossibilities(server.Region, regions),
filterByPossibilities(server.Hostname, hostnames), filterByPossibilities(server.Hostname, hostnames),
filterByPossibilities(server.ServerName, names), filterByPossibilities(server.ServerName, names),
protocol == constants.TCP && !server.TCP, tcp && !server.TCP,
protocol == constants.UDP && !server.UDP: !tcp && !server.UDP:
default: default:
filtered = append(filtered, server) filtered = append(filtered, server)
} }

View File

@@ -2,6 +2,7 @@ package provider
import ( import (
"context" "context"
"errors"
"fmt" "fmt"
"math/rand" "math/rand"
"net" "net"
@@ -67,17 +68,22 @@ func (p *privado) notFoundErr(countries, regions, cities, hostnames []string) er
return fmt.Errorf("%w: %s", errNoServerFound, message) return fmt.Errorf("%w: %s", errNoServerFound, message)
} }
var ErrProtocolUnsupported = errors.New("network protocol is not supported")
func (p *privado) GetOpenVPNConnection(selection configuration.ServerSelection) ( func (p *privado) GetOpenVPNConnection(selection configuration.ServerSelection) (
connection models.OpenVPNConnection, err error) { connection models.OpenVPNConnection, err error) {
var port uint16 = 1194 var port uint16 = 1194
switch selection.Protocol { const protocol = constants.UDP
case constants.UDP: if selection.TCP {
default: return connection, fmt.Errorf("%w: TCP for provider Privado", ErrProtocolUnsupported)
return connection, fmt.Errorf("protocol %q is not supported by Privado", selection.Protocol)
} }
if selection.TargetIP != nil { if selection.TargetIP != nil {
return models.OpenVPNConnection{IP: selection.TargetIP, Port: port, Protocol: selection.Protocol}, nil return models.OpenVPNConnection{
IP: selection.TargetIP,
Port: port,
Protocol: protocol,
}, nil
} }
servers := p.filterServers(selection.Countries, selection.Regions, servers := p.filterServers(selection.Countries, selection.Regions,
@@ -92,7 +98,7 @@ func (p *privado) GetOpenVPNConnection(selection configuration.ServerSelection)
connection := models.OpenVPNConnection{ connection := models.OpenVPNConnection{
IP: servers[i].IP, IP: servers[i].IP,
Port: port, Port: port,
Protocol: selection.Protocol, Protocol: protocol,
Hostname: servers[i].Hostname, Hostname: servers[i].Hostname,
} }
connections[i] = connection connections[i] = connection

View File

@@ -43,7 +43,7 @@ func (p *privatevpn) filterServers(countries, cities, hostnames []string) (serve
} }
func (p *privatevpn) notFoundErr(selection configuration.ServerSelection) error { func (p *privatevpn) notFoundErr(selection configuration.ServerSelection) error {
message := "no server found for protocol " + selection.Protocol message := "no server found for protocol " + tcpBoolToProtocol(selection.TCP)
if len(selection.Countries) > 0 { if len(selection.Countries) > 0 {
message += " + countries " + commaJoin(selection.Countries) message += " + countries " + commaJoin(selection.Countries)
@@ -63,14 +63,20 @@ func (p *privatevpn) notFoundErr(selection configuration.ServerSelection) error
func (p *privatevpn) GetOpenVPNConnection(selection configuration.ServerSelection) ( func (p *privatevpn) GetOpenVPNConnection(selection configuration.ServerSelection) (
connection models.OpenVPNConnection, err error) { connection models.OpenVPNConnection, err error) {
var port uint16 var port uint16
if selection.Protocol == constants.TCP { protocol := constants.TCP
if selection.TCP {
port = 443 port = 443
} else { } else {
protocol = constants.UDP
port = 1194 port = 1194
} }
if selection.TargetIP != nil { if selection.TargetIP != nil {
return models.OpenVPNConnection{IP: selection.TargetIP, Port: port, Protocol: selection.Protocol}, nil return models.OpenVPNConnection{
IP: selection.TargetIP,
Port: port,
Protocol: protocol,
}, nil
} }
servers := p.filterServers(selection.Countries, selection.Cities, selection.Hostnames) servers := p.filterServers(selection.Countries, selection.Cities, selection.Hostnames)
@@ -84,7 +90,7 @@ func (p *privatevpn) GetOpenVPNConnection(selection configuration.ServerSelectio
connection := models.OpenVPNConnection{ connection := models.OpenVPNConnection{
IP: ip, IP: ip,
Port: port, Port: port,
Protocol: selection.Protocol, Protocol: protocol,
} }
connections = append(connections, connection) connections = append(connections, connection)
} }

View File

@@ -35,11 +35,13 @@ func (p *protonvpn) GetOpenVPNConnection(selection configuration.ServerSelection
return connection, err return connection, err
} }
protocol := tcpBoolToProtocol(selection.TCP)
if selection.TargetIP != nil { if selection.TargetIP != nil {
return models.OpenVPNConnection{ return models.OpenVPNConnection{
IP: selection.TargetIP, IP: selection.TargetIP,
Port: port, Port: port,
Protocol: selection.Protocol, Protocol: protocol,
}, nil }, nil
} }
@@ -54,7 +56,7 @@ func (p *protonvpn) GetOpenVPNConnection(selection configuration.ServerSelection
connections[i] = models.OpenVPNConnection{ connections[i] = models.OpenVPNConnection{
IP: servers[i].EntryIP, IP: servers[i].EntryIP,
Port: port, Port: port,
Protocol: selection.Protocol, Protocol: protocol,
} }
} }
@@ -139,35 +141,29 @@ func (p *protonvpn) PortForward(ctx context.Context, client *http.Client,
func (p *protonvpn) getPort(selection configuration.ServerSelection) (port uint16, err error) { func (p *protonvpn) getPort(selection configuration.ServerSelection) (port uint16, err error) {
if selection.CustomPort == 0 { if selection.CustomPort == 0 {
switch selection.Protocol { if selection.TCP {
case constants.TCP:
const defaultTCPPort = 443 const defaultTCPPort = 443
return defaultTCPPort, nil return defaultTCPPort, nil
case constants.UDP: }
const defaultUDPPort = 1194 const defaultUDPPort = 1194
return defaultUDPPort, nil return defaultUDPPort, nil
} }
}
port = selection.CustomPort port = selection.CustomPort
switch selection.Protocol { if selection.TCP {
case constants.TCP:
switch port { switch port {
case 443, 5995, 8443: //nolint:gomnd case 443, 5995, 8443: //nolint:gomnd
return port, nil
default: default:
return 0, fmt.Errorf("%w: %d for protocol %s", return 0, fmt.Errorf("%w: %d for protocol TCP", ErrInvalidPort, port)
ErrInvalidPort, port, selection.Protocol) }
} }
case constants.UDP:
switch port { switch port {
case 80, 443, 1194, 4569, 5060: //nolint:gomnd case 80, 443, 1194, 4569, 5060: //nolint:gomnd
default:
return 0, fmt.Errorf("%w: %d for protocol %s",
ErrInvalidPort, port, selection.Protocol)
}
}
return port, nil return port, nil
default:
return 0, fmt.Errorf("%w: %d for protocol UDP", ErrInvalidPort, port)
}
} }
func (p *protonvpn) filterServers(countries, regions, cities, names, hostnames []string) ( func (p *protonvpn) filterServers(countries, regions, cities, names, hostnames []string) (
@@ -188,7 +184,7 @@ func (p *protonvpn) filterServers(countries, regions, cities, names, hostnames [
} }
func (p *protonvpn) notFoundErr(selection configuration.ServerSelection) error { func (p *protonvpn) notFoundErr(selection configuration.ServerSelection) error {
message := "no server found for protocol " + selection.Protocol message := "no server found for protocol " + tcpBoolToProtocol(selection.TCP)
if len(selection.Countries) > 0 { if len(selection.Countries) > 0 {
message += " + countries " + commaJoin(selection.Countries) message += " + countries " + commaJoin(selection.Countries)

View File

@@ -7,7 +7,6 @@ import (
"net" "net"
"net/http" "net/http"
"strconv" "strconv"
"strings"
"github.com/qdm12/gluetun/internal/configuration" "github.com/qdm12/gluetun/internal/configuration"
"github.com/qdm12/gluetun/internal/constants" "github.com/qdm12/gluetun/internal/constants"
@@ -30,7 +29,7 @@ func newPurevpn(servers []models.PurevpnServer, timeNow timeNowFunc) *purevpn {
} }
func (p *purevpn) filterServers(regions, countries, cities, hostnames []string, func (p *purevpn) filterServers(regions, countries, cities, hostnames []string,
protocol string) (servers []models.PurevpnServer) { tcp bool) (servers []models.PurevpnServer) {
for _, server := range p.servers { for _, server := range p.servers {
switch { switch {
case case
@@ -38,8 +37,8 @@ func (p *purevpn) filterServers(regions, countries, cities, hostnames []string,
filterByPossibilities(server.Country, countries), filterByPossibilities(server.Country, countries),
filterByPossibilities(server.City, cities), filterByPossibilities(server.City, cities),
filterByPossibilities(server.Hostname, hostnames), filterByPossibilities(server.Hostname, hostnames),
strings.EqualFold(protocol, "tcp") && !server.TCP, tcp && !server.TCP,
strings.EqualFold(protocol, "udp") && !server.UDP: !tcp && !server.UDP:
default: default:
servers = append(servers, server) servers = append(servers, server)
} }
@@ -49,22 +48,19 @@ func (p *purevpn) filterServers(regions, countries, cities, hostnames []string,
func (p *purevpn) GetOpenVPNConnection(selection configuration.ServerSelection) ( func (p *purevpn) GetOpenVPNConnection(selection configuration.ServerSelection) (
connection models.OpenVPNConnection, err error) { connection models.OpenVPNConnection, err error) {
var port uint16 var port uint16 = 53
switch { protocol := constants.UDP
case selection.Protocol == constants.UDP: if selection.TCP {
port = 53
case selection.Protocol == constants.TCP:
port = 80 port = 80
default: protocol = constants.TCP
return connection, fmt.Errorf("protocol %q is unknown", selection.Protocol)
} }
if selection.TargetIP != nil { if selection.TargetIP != nil {
return models.OpenVPNConnection{IP: selection.TargetIP, Port: port, Protocol: selection.Protocol}, nil return models.OpenVPNConnection{IP: selection.TargetIP, Port: port, Protocol: protocol}, nil
} }
servers := p.filterServers(selection.Regions, selection.Countries, servers := p.filterServers(selection.Regions, selection.Countries,
selection.Cities, selection.Hostnames, selection.Protocol) selection.Cities, selection.Hostnames, selection.TCP)
if len(servers) == 0 { if len(servers) == 0 {
return connection, fmt.Errorf("no server found for regions %s, countries %s and cities %s", return connection, fmt.Errorf("no server found for regions %s, countries %s and cities %s",
commaJoin(selection.Regions), commaJoin(selection.Countries), commaJoin(selection.Cities)) commaJoin(selection.Regions), commaJoin(selection.Countries), commaJoin(selection.Cities))
@@ -73,7 +69,7 @@ func (p *purevpn) GetOpenVPNConnection(selection configuration.ServerSelection)
var connections []models.OpenVPNConnection var connections []models.OpenVPNConnection
for _, server := range servers { for _, server := range servers {
for _, IP := range server.IPs { for _, IP := range server.IPs {
connections = append(connections, models.OpenVPNConnection{IP: IP, Port: port, Protocol: selection.Protocol}) connections = append(connections, models.OpenVPNConnection{IP: IP, Port: port, Protocol: protocol})
} }
} }

View File

@@ -7,7 +7,6 @@ import (
"net" "net"
"net/http" "net/http"
"strconv" "strconv"
"strings"
"github.com/qdm12/gluetun/internal/configuration" "github.com/qdm12/gluetun/internal/configuration"
"github.com/qdm12/gluetun/internal/constants" "github.com/qdm12/gluetun/internal/constants"
@@ -29,14 +28,14 @@ func newSurfshark(servers []models.SurfsharkServer, timeNow timeNowFunc) *surfsh
} }
} }
func (s *surfshark) filterServers(regions, hostnames []string, protocol string) (servers []models.SurfsharkServer) { func (s *surfshark) filterServers(regions, hostnames []string, tcp bool) (servers []models.SurfsharkServer) {
for _, server := range s.servers { for _, server := range s.servers {
switch { switch {
case case
filterByPossibilities(server.Region, regions), filterByPossibilities(server.Region, regions),
filterByPossibilities(server.Hostname, hostnames), filterByPossibilities(server.Hostname, hostnames),
strings.EqualFold(protocol, "tcp") && !server.TCP, tcp && !server.TCP,
strings.EqualFold(protocol, "udp") && !server.UDP: !tcp && !server.UDP:
default: default:
servers = append(servers, server) servers = append(servers, server)
} }
@@ -45,7 +44,7 @@ func (s *surfshark) filterServers(regions, hostnames []string, protocol string)
} }
func (s *surfshark) notFoundErr(selection configuration.ServerSelection) error { func (s *surfshark) notFoundErr(selection configuration.ServerSelection) error {
message := "for protocol " + selection.Protocol message := "for protocol " + tcpBoolToProtocol(selection.TCP)
if len(selection.Countries) > 0 { if len(selection.Countries) > 0 {
message += " + regions " + commaJoin(selection.Regions) message += " + regions " + commaJoin(selection.Regions)
@@ -60,21 +59,18 @@ func (s *surfshark) notFoundErr(selection configuration.ServerSelection) error {
func (s *surfshark) GetOpenVPNConnection(selection configuration.ServerSelection) ( func (s *surfshark) GetOpenVPNConnection(selection configuration.ServerSelection) (
connection models.OpenVPNConnection, err error) { connection models.OpenVPNConnection, err error) {
var port uint16 var port uint16 = 1194
switch { protocol := constants.UDP
case selection.Protocol == constants.TCP: if selection.TCP {
port = 1443 port = 1443
case selection.Protocol == constants.UDP: protocol = constants.TCP
port = 1194
default:
return connection, fmt.Errorf("protocol %q is unknown", selection.Protocol)
} }
if selection.TargetIP != nil { if selection.TargetIP != nil {
return models.OpenVPNConnection{IP: selection.TargetIP, Port: port, Protocol: selection.Protocol}, nil return models.OpenVPNConnection{IP: selection.TargetIP, Port: port, Protocol: protocol}, nil
} }
servers := s.filterServers(selection.Regions, selection.Hostnames, selection.Protocol) servers := s.filterServers(selection.Regions, selection.Hostnames, selection.TCP)
if len(servers) == 0 { if len(servers) == 0 {
return connection, s.notFoundErr(selection) return connection, s.notFoundErr(selection)
} }
@@ -82,7 +78,7 @@ func (s *surfshark) GetOpenVPNConnection(selection configuration.ServerSelection
var connections []models.OpenVPNConnection var connections []models.OpenVPNConnection
for _, server := range servers { for _, server := range servers {
for _, IP := range server.IPs { for _, IP := range server.IPs {
connections = append(connections, models.OpenVPNConnection{IP: IP, Port: port, Protocol: selection.Protocol}) connections = append(connections, models.OpenVPNConnection{IP: IP, Port: port, Protocol: protocol})
} }
} }

View File

@@ -7,7 +7,6 @@ import (
"net" "net"
"net/http" "net/http"
"strconv" "strconv"
"strings"
"github.com/qdm12/gluetun/internal/configuration" "github.com/qdm12/gluetun/internal/configuration"
"github.com/qdm12/gluetun/internal/constants" "github.com/qdm12/gluetun/internal/constants"
@@ -30,15 +29,15 @@ func newTorguard(servers []models.TorguardServer, timeNow timeNowFunc) *torguard
} }
func (t *torguard) filterServers(countries, cities, hostnames []string, func (t *torguard) filterServers(countries, cities, hostnames []string,
protocol string) (servers []models.TorguardServer) { tcp bool) (servers []models.TorguardServer) {
for _, server := range t.servers { for _, server := range t.servers {
switch { switch {
case case
filterByPossibilities(server.Country, countries), filterByPossibilities(server.Country, countries),
filterByPossibilities(server.City, cities), filterByPossibilities(server.City, cities),
filterByPossibilities(server.Hostname, hostnames), filterByPossibilities(server.Hostname, hostnames),
strings.EqualFold(protocol, "tcp") && !server.TCP, tcp && !server.TCP,
strings.EqualFold(protocol, "udp") && !server.UDP: !tcp && !server.UDP:
default: default:
servers = append(servers, server) servers = append(servers, server)
} }
@@ -47,7 +46,7 @@ func (t *torguard) filterServers(countries, cities, hostnames []string,
} }
func (t *torguard) notFoundErr(selection configuration.ServerSelection) error { func (t *torguard) notFoundErr(selection configuration.ServerSelection) error {
message := "no server found for protocol " + selection.Protocol message := "no server found for protocol " + tcpBoolToProtocol(selection.TCP)
if len(selection.Countries) > 0 { if len(selection.Countries) > 0 {
message += " + countries " + commaJoin(selection.Countries) message += " + countries " + commaJoin(selection.Countries)
@@ -71,12 +70,14 @@ func (t *torguard) GetOpenVPNConnection(selection configuration.ServerSelection)
port = selection.CustomPort port = selection.CustomPort
} }
protocol := tcpBoolToProtocol(selection.TCP)
if selection.TargetIP != nil { if selection.TargetIP != nil {
return models.OpenVPNConnection{IP: selection.TargetIP, Port: port, Protocol: selection.Protocol}, nil return models.OpenVPNConnection{IP: selection.TargetIP, Port: port, Protocol: protocol}, nil
} }
servers := t.filterServers(selection.Countries, selection.Cities, servers := t.filterServers(selection.Countries, selection.Cities,
selection.Hostnames, selection.Protocol) selection.Hostnames, selection.TCP)
if len(servers) == 0 { if len(servers) == 0 {
return connection, t.notFoundErr(selection) return connection, t.notFoundErr(selection)
} }
@@ -87,7 +88,7 @@ func (t *torguard) GetOpenVPNConnection(selection configuration.ServerSelection)
connection := models.OpenVPNConnection{ connection := models.OpenVPNConnection{
IP: ip, IP: ip,
Port: port, Port: port,
Protocol: selection.Protocol, Protocol: protocol,
} }
connections = append(connections, connection) connections = append(connections, connection)
} }

View File

@@ -52,3 +52,10 @@ func filterByPossibilities(value string, possibilities []string) (filtered bool)
func commaJoin(slice []string) string { func commaJoin(slice []string) string {
return strings.Join(slice, ",") return strings.Join(slice, ",")
} }
func tcpBoolToProtocol(tcp bool) (protocol string) {
if tcp {
return "tcp"
}
return "udp"
}

View File

@@ -7,7 +7,6 @@ import (
"net" "net"
"net/http" "net/http"
"strconv" "strconv"
"strings"
"github.com/qdm12/gluetun/internal/configuration" "github.com/qdm12/gluetun/internal/configuration"
"github.com/qdm12/gluetun/internal/constants" "github.com/qdm12/gluetun/internal/constants"
@@ -29,14 +28,14 @@ func newVyprvpn(servers []models.VyprvpnServer, timeNow timeNowFunc) *vyprvpn {
} }
} }
func (v *vyprvpn) filterServers(regions, hostnames []string, protocol string) (servers []models.VyprvpnServer) { func (v *vyprvpn) filterServers(regions, hostnames []string, tcp bool) (servers []models.VyprvpnServer) {
for _, server := range v.servers { for _, server := range v.servers {
switch { switch {
case case
filterByPossibilities(server.Region, regions), filterByPossibilities(server.Region, regions),
filterByPossibilities(server.Hostname, hostnames), filterByPossibilities(server.Hostname, hostnames),
strings.EqualFold(protocol, "tcp") && !server.TCP, tcp && !server.TCP,
strings.EqualFold(protocol, "udp") && !server.UDP: !tcp && !server.UDP:
default: default:
servers = append(servers, server) servers = append(servers, server)
} }
@@ -47,20 +46,17 @@ func (v *vyprvpn) filterServers(regions, hostnames []string, protocol string) (s
func (v *vyprvpn) GetOpenVPNConnection(selection configuration.ServerSelection) ( func (v *vyprvpn) GetOpenVPNConnection(selection configuration.ServerSelection) (
connection models.OpenVPNConnection, err error) { connection models.OpenVPNConnection, err error) {
var port uint16 var port uint16
switch { const protocol = constants.TCP
case selection.Protocol == constants.TCP: if selection.TCP {
return connection, fmt.Errorf("TCP protocol not supported by this VPN provider") return connection, fmt.Errorf("%w: TCP for provider VyprVPN",
case selection.Protocol == constants.UDP: ErrProtocolUnsupported)
port = 443
default:
return connection, fmt.Errorf("protocol %q is unknown", selection.Protocol)
} }
if selection.TargetIP != nil { if selection.TargetIP != nil {
return models.OpenVPNConnection{IP: selection.TargetIP, Port: port, Protocol: selection.Protocol}, nil return models.OpenVPNConnection{IP: selection.TargetIP, Port: port, Protocol: protocol}, nil
} }
servers := v.filterServers(selection.Regions, selection.Hostnames, selection.Protocol) servers := v.filterServers(selection.Regions, selection.Hostnames, selection.TCP)
if len(servers) == 0 { if len(servers) == 0 {
return connection, fmt.Errorf("no server found for region %s", commaJoin(selection.Regions)) return connection, fmt.Errorf("no server found for region %s", commaJoin(selection.Regions))
} }
@@ -68,7 +64,7 @@ func (v *vyprvpn) GetOpenVPNConnection(selection configuration.ServerSelection)
var connections []models.OpenVPNConnection var connections []models.OpenVPNConnection
for _, server := range servers { for _, server := range servers {
for _, IP := range server.IPs { for _, IP := range server.IPs {
connections = append(connections, models.OpenVPNConnection{IP: IP, Port: port, Protocol: selection.Protocol}) connections = append(connections, models.OpenVPNConnection{IP: IP, Port: port, Protocol: protocol})
} }
} }

View File

@@ -45,20 +45,19 @@ func (w *windscribe) filterServers(regions, cities, hostnames []string) (servers
//nolint:lll //nolint:lll
func (w *windscribe) GetOpenVPNConnection(selection configuration.ServerSelection) (connection models.OpenVPNConnection, err error) { func (w *windscribe) GetOpenVPNConnection(selection configuration.ServerSelection) (connection models.OpenVPNConnection, err error) {
var port uint16 var port uint16 = 443
switch { protocol := constants.UDP
case selection.CustomPort > 0: if selection.TCP {
port = selection.CustomPort
case selection.Protocol == constants.TCP:
port = 1194 port = 1194
case selection.Protocol == constants.UDP: protocol = constants.TCP
port = 443 }
default:
return connection, fmt.Errorf("protocol %q is unknown", selection.Protocol) if selection.CustomPort > 0 {
port = selection.CustomPort
} }
if selection.TargetIP != nil { if selection.TargetIP != nil {
return models.OpenVPNConnection{IP: selection.TargetIP, Port: port, Protocol: selection.Protocol}, nil return models.OpenVPNConnection{IP: selection.TargetIP, Port: port, Protocol: protocol}, nil
} }
servers := w.filterServers(selection.Regions, selection.Cities, selection.Hostnames) servers := w.filterServers(selection.Regions, selection.Cities, selection.Hostnames)
@@ -72,7 +71,7 @@ func (w *windscribe) GetOpenVPNConnection(selection configuration.ServerSelectio
connection := models.OpenVPNConnection{ connection := models.OpenVPNConnection{
IP: ip, IP: ip,
Port: port, Port: port,
Protocol: selection.Protocol, Protocol: protocol,
} }
connections = append(connections, connection) connections = append(connections, connection)
} }