diff --git a/internal/configuration/cyberghost.go b/internal/configuration/cyberghost.go index 905c3ab3..38b8faf0 100644 --- a/internal/configuration/cyberghost.go +++ b/internal/configuration/cyberghost.go @@ -17,17 +17,14 @@ func (settings *Provider) cyberghostLines() (lines []string) { lines = append(lines, lastIndent+"Hostnames: "+commaJoin(settings.ServerSelection.Hostnames)) } + lines = append(lines, settings.ServerSelection.OpenVPN.lines()...) + return lines } func (settings *Provider) readCyberghost(r reader) (err error) { settings.Name = constants.Cyberghost - settings.ServerSelection.TCP, err = readProtocol(r.env) - if err != nil { - return err - } - settings.ServerSelection.TargetIP, err = readTargetIP(r.env) if err != nil { return err @@ -49,7 +46,7 @@ func (settings *Provider) readCyberghost(r reader) (err error) { return fmt.Errorf("environment variable SERVER_HOSTNAME: %w", err) } - return nil + return settings.ServerSelection.OpenVPN.readProtocolAndPort(r.env) } func (settings *OpenVPN) readCyberghost(r reader) (err error) { diff --git a/internal/configuration/fastestvpn.go b/internal/configuration/fastestvpn.go index d53cf3d9..a30515b4 100644 --- a/internal/configuration/fastestvpn.go +++ b/internal/configuration/fastestvpn.go @@ -15,17 +15,14 @@ func (settings *Provider) fastestvpnLines() (lines []string) { lines = append(lines, lastIndent+"Countries: "+commaJoin(settings.ServerSelection.Countries)) } + lines = append(lines, settings.ServerSelection.OpenVPN.lines()...) + return lines } func (settings *Provider) readFastestvpn(r reader) (err error) { settings.Name = constants.Fastestvpn - settings.ServerSelection.TCP, err = readProtocol(r.env) - if err != nil { - return err - } - settings.ServerSelection.TargetIP, err = readTargetIP(r.env) if err != nil { return err @@ -41,5 +38,5 @@ func (settings *Provider) readFastestvpn(r reader) (err error) { return fmt.Errorf("environment variable COUNTRY: %w", err) } - return nil + return settings.ServerSelection.OpenVPN.readProtocolOnly(r.env) } diff --git a/internal/configuration/hidemyass.go b/internal/configuration/hidemyass.go index e0e211c5..b3366e2a 100644 --- a/internal/configuration/hidemyass.go +++ b/internal/configuration/hidemyass.go @@ -23,17 +23,14 @@ func (settings *Provider) hideMyAssLines() (lines []string) { lines = append(lines, lastIndent+"Hostnames: "+commaJoin(settings.ServerSelection.Hostnames)) } + lines = append(lines, settings.ServerSelection.OpenVPN.lines()...) + return lines } func (settings *Provider) readHideMyAss(r reader) (err error) { settings.Name = constants.HideMyAss - settings.ServerSelection.TCP, err = readProtocol(r.env) - if err != nil { - return err - } - settings.ServerSelection.TargetIP, err = readTargetIP(r.env) if err != nil { return err @@ -59,5 +56,5 @@ func (settings *Provider) readHideMyAss(r reader) (err error) { return fmt.Errorf("environment variable SERVER_HOSTNAME: %w", err) } - return nil + return settings.ServerSelection.OpenVPN.readProtocolAndPort(r.env) } diff --git a/internal/configuration/ipvanish.go b/internal/configuration/ipvanish.go index c33e251e..50396fb3 100644 --- a/internal/configuration/ipvanish.go +++ b/internal/configuration/ipvanish.go @@ -19,17 +19,14 @@ func (settings *Provider) ipvanishLines() (lines []string) { lines = append(lines, lastIndent+"Hostnames: "+commaJoin(settings.ServerSelection.Hostnames)) } + lines = append(lines, settings.ServerSelection.OpenVPN.lines()...) + return lines } func (settings *Provider) readIpvanish(r reader) (err error) { settings.Name = constants.Ipvanish - settings.ServerSelection.TCP, err = readProtocol(r.env) - if err != nil { - return err - } - settings.ServerSelection.TargetIP, err = readTargetIP(r.env) if err != nil { return err @@ -50,5 +47,5 @@ func (settings *Provider) readIpvanish(r reader) (err error) { return fmt.Errorf("environment variable SERVER_HOSTNAME: %w", err) } - return nil + return settings.ServerSelection.OpenVPN.readProtocolOnly(r.env) } diff --git a/internal/configuration/ipvanish_test.go b/internal/configuration/ipvanish_test.go index e3d9ffe4..65a38c18 100644 --- a/internal/configuration/ipvanish_test.go +++ b/internal/configuration/ipvanish_test.go @@ -19,7 +19,12 @@ func Test_Provider_ipvanishLines(t *testing.T) { settings Provider lines []string }{ - "empty settings": {}, + "empty settings": { + lines: []string{ + "|--OpenVPN selection:", + " |--Protocol: udp", + }, + }, "full settings": { settings: Provider{ ServerSelection: ServerSelection{ @@ -32,6 +37,8 @@ func Test_Provider_ipvanishLines(t *testing.T) { "|--Countries: A, B", "|--Cities: C, D", "|--Hostnames: E, F", + "|--OpenVPN selection:", + " |--Protocol: udp", }, }, } @@ -65,23 +72,15 @@ func Test_Provider_readIpvanish(t *testing.T) { } testCases := map[string]struct { - protocol singleStringCall targetIP singleStringCall countries sliceStringCall cities sliceStringCall hostnames sliceStringCall + protocol singleStringCall settings Provider err error }{ - "protocol error": { - protocol: singleStringCall{call: true, err: errDummy}, - settings: Provider{ - Name: constants.Ipvanish, - }, - err: errors.New("environment variable PROTOCOL: dummy test error"), - }, "target IP error": { - protocol: singleStringCall{call: true}, targetIP: singleStringCall{call: true, value: "something", err: errDummy}, settings: Provider{ Name: constants.Ipvanish, @@ -89,7 +88,6 @@ func Test_Provider_readIpvanish(t *testing.T) { err: errors.New("environment variable OPENVPN_TARGET_IP: dummy test error"), }, "countries error": { - protocol: singleStringCall{call: true}, targetIP: singleStringCall{call: true}, countries: sliceStringCall{call: true, err: errDummy}, settings: Provider{ @@ -98,7 +96,6 @@ func Test_Provider_readIpvanish(t *testing.T) { err: errors.New("environment variable COUNTRY: dummy test error"), }, "cities error": { - protocol: singleStringCall{call: true}, targetIP: singleStringCall{call: true}, countries: sliceStringCall{call: true}, cities: sliceStringCall{call: true, err: errDummy}, @@ -108,7 +105,6 @@ func Test_Provider_readIpvanish(t *testing.T) { err: errors.New("environment variable CITY: dummy test error"), }, "hostnames error": { - protocol: singleStringCall{call: true}, targetIP: singleStringCall{call: true}, countries: sliceStringCall{call: true}, cities: sliceStringCall{call: true}, @@ -118,26 +114,39 @@ func Test_Provider_readIpvanish(t *testing.T) { }, err: errors.New("environment variable SERVER_HOSTNAME: dummy test error"), }, - "default settings": { - protocol: singleStringCall{call: true}, + "protocol error": { targetIP: singleStringCall{call: true}, countries: sliceStringCall{call: true}, cities: sliceStringCall{call: true}, hostnames: sliceStringCall{call: true}, + protocol: singleStringCall{call: true, err: errDummy}, + settings: Provider{ + Name: constants.Ipvanish, + }, + err: errors.New("environment variable PROTOCOL: dummy test error"), + }, + "default settings": { + targetIP: singleStringCall{call: true}, + countries: sliceStringCall{call: true}, + cities: sliceStringCall{call: true}, + hostnames: sliceStringCall{call: true}, + protocol: singleStringCall{call: true}, settings: Provider{ Name: constants.Ipvanish, }, }, "set settings": { - protocol: singleStringCall{call: true, value: constants.TCP}, targetIP: singleStringCall{call: true, value: "1.2.3.4"}, countries: sliceStringCall{call: true, values: []string{"A", "B"}}, cities: sliceStringCall{call: true, values: []string{"C", "D"}}, hostnames: sliceStringCall{call: true, values: []string{"E", "F"}}, + protocol: singleStringCall{call: true, value: constants.TCP}, settings: Provider{ Name: constants.Ipvanish, ServerSelection: ServerSelection{ - TCP: true, + OpenVPN: OpenVPNSelection{ + TCP: true, + }, TargetIP: net.IPv4(1, 2, 3, 4), Countries: []string{"A", "B"}, Cities: []string{"C", "D"}, @@ -153,10 +162,6 @@ func Test_Provider_readIpvanish(t *testing.T) { ctrl := gomock.NewController(t) env := mock_params.NewMockEnv(ctrl) - if testCase.protocol.call { - env.EXPECT().Inside("PROTOCOL", []string{constants.TCP, constants.UDP}, gomock.Any()). - Return(testCase.protocol.value, testCase.protocol.err) - } if testCase.targetIP.call { env.EXPECT().Get("OPENVPN_TARGET_IP"). Return(testCase.targetIP.value, testCase.targetIP.err) @@ -173,6 +178,10 @@ func Test_Provider_readIpvanish(t *testing.T) { env.EXPECT().CSVInside("SERVER_HOSTNAME", constants.IpvanishHostnameChoices()). Return(testCase.hostnames.values, testCase.hostnames.err) } + if testCase.protocol.call { + env.EXPECT().Inside("PROTOCOL", []string{constants.TCP, constants.UDP}, gomock.Any()). + Return(testCase.protocol.value, testCase.protocol.err) + } r := reader{env: env} diff --git a/internal/configuration/ivpn.go b/internal/configuration/ivpn.go index 0f7f6dcb..abd75b24 100644 --- a/internal/configuration/ivpn.go +++ b/internal/configuration/ivpn.go @@ -19,17 +19,14 @@ func (settings *Provider) ivpnLines() (lines []string) { lines = append(lines, lastIndent+"Hostnames: "+commaJoin(settings.ServerSelection.Hostnames)) } + lines = append(lines, settings.ServerSelection.OpenVPN.lines()...) + return lines } func (settings *Provider) readIvpn(r reader) (err error) { settings.Name = constants.Ivpn - settings.ServerSelection.TCP, err = readProtocol(r.env) - if err != nil { - return err - } - settings.ServerSelection.TargetIP, err = readTargetIP(r.env) if err != nil { return err @@ -50,5 +47,5 @@ func (settings *Provider) readIvpn(r reader) (err error) { return fmt.Errorf("environment variable SERVER_HOSTNAME: %w", err) } - return nil + return settings.ServerSelection.OpenVPN.readProtocolOnly(r.env) } diff --git a/internal/configuration/ivpn_test.go b/internal/configuration/ivpn_test.go index 54e29388..f790b67e 100644 --- a/internal/configuration/ivpn_test.go +++ b/internal/configuration/ivpn_test.go @@ -19,7 +19,12 @@ func Test_Provider_ivpnLines(t *testing.T) { settings Provider lines []string }{ - "empty settings": {}, + "empty settings": { + lines: []string{ + "|--OpenVPN selection:", + " |--Protocol: udp", + }, + }, "full settings": { settings: Provider{ ServerSelection: ServerSelection{ @@ -32,6 +37,8 @@ func Test_Provider_ivpnLines(t *testing.T) { "|--Countries: A, B", "|--Cities: C, D", "|--Hostnames: E, F", + "|--OpenVPN selection:", + " |--Protocol: udp", }, }, } @@ -73,15 +80,7 @@ func Test_Provider_readIvpn(t *testing.T) { settings Provider err error }{ - "protocol error": { - protocol: singleStringCall{call: true, err: errDummy}, - settings: Provider{ - Name: constants.Ivpn, - }, - err: errors.New("environment variable PROTOCOL: dummy test error"), - }, "target IP error": { - protocol: singleStringCall{call: true}, targetIP: singleStringCall{call: true, value: "something", err: errDummy}, settings: Provider{ Name: constants.Ivpn, @@ -89,7 +88,6 @@ func Test_Provider_readIvpn(t *testing.T) { err: errors.New("environment variable OPENVPN_TARGET_IP: dummy test error"), }, "countries error": { - protocol: singleStringCall{call: true}, targetIP: singleStringCall{call: true}, countries: sliceStringCall{call: true, err: errDummy}, settings: Provider{ @@ -98,7 +96,6 @@ func Test_Provider_readIvpn(t *testing.T) { err: errors.New("environment variable COUNTRY: dummy test error"), }, "cities error": { - protocol: singleStringCall{call: true}, targetIP: singleStringCall{call: true}, countries: sliceStringCall{call: true}, cities: sliceStringCall{call: true, err: errDummy}, @@ -108,7 +105,6 @@ func Test_Provider_readIvpn(t *testing.T) { err: errors.New("environment variable CITY: dummy test error"), }, "hostnames error": { - protocol: singleStringCall{call: true}, targetIP: singleStringCall{call: true}, countries: sliceStringCall{call: true}, cities: sliceStringCall{call: true}, @@ -118,26 +114,39 @@ func Test_Provider_readIvpn(t *testing.T) { }, err: errors.New("environment variable SERVER_HOSTNAME: dummy test error"), }, - "default settings": { - protocol: singleStringCall{call: true}, + "protocol error": { targetIP: singleStringCall{call: true}, countries: sliceStringCall{call: true}, cities: sliceStringCall{call: true}, hostnames: sliceStringCall{call: true}, + protocol: singleStringCall{call: true, err: errDummy}, + settings: Provider{ + Name: constants.Ivpn, + }, + err: errors.New("environment variable PROTOCOL: dummy test error"), + }, + "default settings": { + targetIP: singleStringCall{call: true}, + countries: sliceStringCall{call: true}, + cities: sliceStringCall{call: true}, + hostnames: sliceStringCall{call: true}, + protocol: singleStringCall{call: true}, settings: Provider{ Name: constants.Ivpn, }, }, "set settings": { - protocol: singleStringCall{call: true, value: constants.TCP}, targetIP: singleStringCall{call: true, value: "1.2.3.4"}, countries: sliceStringCall{call: true, values: []string{"A", "B"}}, cities: sliceStringCall{call: true, values: []string{"C", "D"}}, hostnames: sliceStringCall{call: true, values: []string{"E", "F"}}, + protocol: singleStringCall{call: true, value: constants.TCP}, settings: Provider{ Name: constants.Ivpn, ServerSelection: ServerSelection{ - TCP: true, + OpenVPN: OpenVPNSelection{ + TCP: true, + }, TargetIP: net.IPv4(1, 2, 3, 4), Countries: []string{"A", "B"}, Cities: []string{"C", "D"}, diff --git a/internal/configuration/mullvad.go b/internal/configuration/mullvad.go index 3c52eea2..d0605978 100644 --- a/internal/configuration/mullvad.go +++ b/internal/configuration/mullvad.go @@ -2,7 +2,6 @@ package configuration import ( "fmt" - "strconv" "github.com/qdm12/gluetun/internal/constants" "github.com/qdm12/golibs/params" @@ -25,9 +24,7 @@ func (settings *Provider) mullvadLines() (lines []string) { lines = append(lines, lastIndent+"ISPs: "+commaJoin(settings.ServerSelection.ISPs)) } - if settings.ServerSelection.CustomPort > 0 { - lines = append(lines, lastIndent+"Custom port: "+strconv.Itoa(int(settings.ServerSelection.CustomPort))) - } + lines = append(lines, settings.ServerSelection.OpenVPN.lines()...) return lines } @@ -35,11 +32,6 @@ func (settings *Provider) mullvadLines() (lines []string) { func (settings *Provider) readMullvad(r reader) (err error) { settings.Name = constants.Mullvad - settings.ServerSelection.TCP, err = readProtocol(r.env) - if err != nil { - return err - } - settings.ServerSelection.TargetIP, err = readTargetIP(r.env) if err != nil { return err @@ -65,16 +57,25 @@ func (settings *Provider) readMullvad(r reader) (err error) { return fmt.Errorf("environment variable ISP: %w", err) } - 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}) - if err != nil { - return err - } - settings.ServerSelection.Owned, err = r.env.YesNo("OWNED", params.Default("no")) if err != nil { return fmt.Errorf("environment variable OWNED: %w", err) } + return settings.ServerSelection.OpenVPN.readMullvad(r.env) +} + +func (settings *OpenVPNSelection) readMullvad(env params.Env) (err error) { + settings.TCP, err = readProtocol(env) + if err != nil { + return err + } + + settings.CustomPort, err = readCustomPort(env, settings.TCP, + []uint16{80, 443, 1401}, []uint16{53, 1194, 1195, 1196, 1197, 1300, 1301, 1302, 1303, 1400}) + if err != nil { + return err + } + return nil } diff --git a/internal/configuration/nordvpn.go b/internal/configuration/nordvpn.go index d7ef5721..a7952736 100644 --- a/internal/configuration/nordvpn.go +++ b/internal/configuration/nordvpn.go @@ -29,17 +29,14 @@ func (settings *Provider) nordvpnLines() (lines []string) { lines = append(lines, lastIndent+"Numbers: "+commaJoin(numbersString)) } + lines = append(lines, settings.ServerSelection.OpenVPN.lines()...) + return lines } func (settings *Provider) readNordvpn(r reader) (err error) { settings.Name = constants.Nordvpn - settings.ServerSelection.TCP, err = readProtocol(r.env) - if err != nil { - return err - } - settings.ServerSelection.TargetIP, err = readTargetIP(r.env) if err != nil { return err @@ -65,7 +62,7 @@ func (settings *Provider) readNordvpn(r reader) (err error) { return err } - return nil + return settings.ServerSelection.OpenVPN.readProtocolOnly(r.env) } func readNordVPNServerNumbers(env params.Env) (numbers []uint16, err error) { diff --git a/internal/configuration/openvpn.go b/internal/configuration/openvpn.go index 53b50e4d..55bf64b4 100644 --- a/internal/configuration/openvpn.go +++ b/internal/configuration/openvpn.go @@ -150,8 +150,6 @@ func (settings *OpenVPN) read(r reader, serviceProvider string) (err error) { switch serviceProvider { case constants.Cyberghost: err = settings.readCyberghost(r) - case constants.PrivateInternetAccess: - err = settings.readPrivateInternetAccess(r) case constants.VPNUnlimited: err = settings.readVPNUnlimited(r) } @@ -161,3 +159,11 @@ func (settings *OpenVPN) read(r reader, serviceProvider string) (err error) { return nil } + +func readProtocol(env params.Env) (tcp bool, err error) { + protocol, err := env.Inside("PROTOCOL", []string{constants.TCP, constants.UDP}, params.Default(constants.UDP)) + if err != nil { + return false, fmt.Errorf("environment variable PROTOCOL: %w", err) + } + return protocol == constants.TCP, nil +} diff --git a/internal/configuration/privado.go b/internal/configuration/privado.go index 231d1af7..e8277a11 100644 --- a/internal/configuration/privado.go +++ b/internal/configuration/privado.go @@ -23,17 +23,14 @@ func (settings *Provider) privadoLines() (lines []string) { lines = append(lines, lastIndent+"Hostnames: "+commaJoin(settings.ServerSelection.Hostnames)) } + lines = append(lines, settings.ServerSelection.OpenVPN.lines()...) + return lines } func (settings *Provider) readPrivado(r reader) (err error) { settings.Name = constants.Privado - settings.ServerSelection.TCP, err = readProtocol(r.env) - if err != nil { - return err - } - settings.ServerSelection.TargetIP, err = readTargetIP(r.env) if err != nil { return err diff --git a/internal/configuration/privateinternetaccess.go b/internal/configuration/privateinternetaccess.go index b8692c38..a1d6c838 100644 --- a/internal/configuration/privateinternetaccess.go +++ b/internal/configuration/privateinternetaccess.go @@ -2,7 +2,6 @@ package configuration import ( "fmt" - "strconv" "github.com/qdm12/gluetun/internal/constants" "github.com/qdm12/golibs/params" @@ -21,9 +20,7 @@ func (settings *Provider) privateinternetaccessLines() (lines []string) { lines = append(lines, lastIndent+"Names: "+commaJoin(settings.ServerSelection.Names)) } - if settings.ServerSelection.CustomPort > 0 { - lines = append(lines, lastIndent+"Custom port: "+strconv.Itoa(int(settings.ServerSelection.CustomPort))) - } + lines = append(lines, settings.ServerSelection.OpenVPN.lines()...) if settings.PortForwarding.Enabled { lines = append(lines, lastIndent+"Port forwarding:") @@ -38,11 +35,6 @@ func (settings *Provider) privateinternetaccessLines() (lines []string) { func (settings *Provider) readPrivateInternetAccess(r reader) (err error) { settings.Name = constants.PrivateInternetAccess - settings.ServerSelection.TCP, err = readProtocol(r.env) - if err != nil { - return err - } - settings.ServerSelection.TargetIP, err = readTargetIP(r.env) if err != nil { return err @@ -63,16 +55,6 @@ func (settings *Provider) readPrivateInternetAccess(r reader) (err error) { return fmt.Errorf("environment variable SERVER_NAME: %w", err) } - settings.ServerSelection.CustomPort, err = readPortOrZero(r.env, "PORT") - if err != nil { - return fmt.Errorf("environment variable PORT: %w", err) - } - - settings.ServerSelection.EncryptionPreset, err = getPIAEncryptionPreset(r) - if err != nil { - return err - } - settings.PortForwarding.Enabled, err = r.env.OnOff("PORT_FORWARDING", params.Default("off")) if err != nil { return fmt.Errorf("environment variable PORT_FORWARDING: %w", err) @@ -86,12 +68,21 @@ func (settings *Provider) readPrivateInternetAccess(r reader) (err error) { } } - return nil + return settings.ServerSelection.OpenVPN.readPrivateInternetAccess(r) } -func (settings *OpenVPN) readPrivateInternetAccess(r reader) (err error) { +func (settings *OpenVPNSelection) readPrivateInternetAccess(r reader) (err error) { settings.EncPreset, err = getPIAEncryptionPreset(r) - return err + if err != nil { + return err + } + + settings.CustomPort, err = readPortOrZero(r.env, "PORT") + if err != nil { + return fmt.Errorf("environment variable PORT: %w", err) + } + + return nil } func getPIAEncryptionPreset(r reader) (encryptionPreset string, err error) { @@ -103,5 +94,6 @@ func getPIAEncryptionPreset(r reader) (encryptionPreset string, err error) { if err != nil { return "", fmt.Errorf("environment variable PIA_ENCRYPTION: %w", err) } + return encryptionPreset, nil } diff --git a/internal/configuration/privatevpn.go b/internal/configuration/privatevpn.go index 37c01874..78b476cf 100644 --- a/internal/configuration/privatevpn.go +++ b/internal/configuration/privatevpn.go @@ -19,17 +19,14 @@ func (settings *Provider) privatevpnLines() (lines []string) { lines = append(lines, lastIndent+"Hostnames: "+commaJoin(settings.ServerSelection.Hostnames)) } + lines = append(lines, settings.ServerSelection.OpenVPN.lines()...) + return lines } func (settings *Provider) readPrivatevpn(r reader) (err error) { settings.Name = constants.Privatevpn - settings.ServerSelection.TCP, err = readProtocol(r.env) - if err != nil { - return err - } - settings.ServerSelection.TargetIP, err = readTargetIP(r.env) if err != nil { return err @@ -50,5 +47,5 @@ func (settings *Provider) readPrivatevpn(r reader) (err error) { return fmt.Errorf("environment variable SERVER_HOSTNAME: %w", err) } - return nil + return settings.ServerSelection.OpenVPN.readProtocolAndPort(r.env) } diff --git a/internal/configuration/protonvpn.go b/internal/configuration/protonvpn.go index 1812d02d..a5e6f248 100644 --- a/internal/configuration/protonvpn.go +++ b/internal/configuration/protonvpn.go @@ -32,27 +32,19 @@ func (settings *Provider) protonvpnLines() (lines []string) { lines = append(lines, lastIndent+"Free only: yes") } + lines = append(lines, settings.ServerSelection.OpenVPN.lines()...) + return lines } func (settings *Provider) readProtonvpn(r reader) (err error) { settings.Name = constants.Protonvpn - settings.ServerSelection.TCP, err = readProtocol(r.env) - if err != nil { - return err - } - settings.ServerSelection.TargetIP, err = readTargetIP(r.env) if err != nil { return err } - settings.ServerSelection.CustomPort, err = readPortOrZero(r.env, "PORT") - if err != nil { - return fmt.Errorf("environment variable PORT: %w", err) - } - settings.ServerSelection.Countries, err = r.env.CSVInside("COUNTRY", constants.ProtonvpnCountryChoices()) if err != nil { return fmt.Errorf("environment variable COUNTRY: %w", err) @@ -83,5 +75,5 @@ func (settings *Provider) readProtonvpn(r reader) (err error) { return fmt.Errorf("environment variable FREE_ONLY: %w", err) } - return nil + return settings.ServerSelection.OpenVPN.readProtocolAndPort(r.env) } diff --git a/internal/configuration/provider.go b/internal/configuration/provider.go index 8c95f2a0..370a21ac 100644 --- a/internal/configuration/provider.go +++ b/internal/configuration/provider.go @@ -18,12 +18,14 @@ type Provider struct { } func (settings *Provider) lines() (lines []string) { + if settings.Name == "" { // custom OpenVPN configuration + return nil + } + lines = append(lines, lastIndent+strings.Title(settings.Name)+" settings:") selection := settings.ServerSelection - lines = append(lines, indent+lastIndent+"Network protocol: "+protoToString(selection.TCP)) - if selection.TargetIP != nil { lines = append(lines, indent+lastIndent+"Target IP address: "+selection.TargetIP.String()) } @@ -153,14 +155,6 @@ func commaJoin(slice []string) string { return strings.Join(slice, ", ") } -func readProtocol(env params.Env) (tcp bool, err error) { - protocol, err := env.Inside("PROTOCOL", []string{constants.TCP, constants.UDP}, params.Default(constants.UDP)) - if err != nil { - return false, fmt.Errorf("environment variable PROTOCOL: %w", err) - } - return protocol == constants.TCP, nil -} - func protoToString(tcp bool) string { if tcp { return constants.TCP diff --git a/internal/configuration/provider_test.go b/internal/configuration/provider_test.go index d2079061..88c54de7 100644 --- a/internal/configuration/provider_test.go +++ b/internal/configuration/provider_test.go @@ -30,9 +30,10 @@ func Test_Provider_lines(t *testing.T) { }, lines: []string{ "|--Cyberghost settings:", - " |--Network protocol: udp", " |--Server groups: group", " |--Regions: a, El country", + " |--OpenVPN selection:", + " |--Protocol: udp", }, }, "fastestvpn": { @@ -45,9 +46,10 @@ func Test_Provider_lines(t *testing.T) { }, lines: []string{ "|--Fastestvpn settings:", - " |--Network protocol: udp", " |--Hostnames: a, b", " |--Countries: c, d", + " |--OpenVPN selection:", + " |--Protocol: udp", }, }, "hidemyass": { @@ -61,10 +63,11 @@ func Test_Provider_lines(t *testing.T) { }, lines: []string{ "|--Hidemyass settings:", - " |--Network protocol: udp", " |--Countries: a, b", " |--Cities: c, d", " |--Hostnames: e, f", + " |--OpenVPN selection:", + " |--Protocol: udp", }, }, "ipvanish": { @@ -78,10 +81,11 @@ func Test_Provider_lines(t *testing.T) { }, lines: []string{ "|--Ipvanish settings:", - " |--Network protocol: udp", " |--Countries: a, b", " |--Cities: c, d", " |--Hostnames: e, f", + " |--OpenVPN selection:", + " |--Protocol: udp", }, }, "ivpn": { @@ -95,29 +99,33 @@ func Test_Provider_lines(t *testing.T) { }, lines: []string{ "|--Ivpn settings:", - " |--Network protocol: udp", " |--Countries: a, b", " |--Cities: c, d", " |--Hostnames: e, f", + " |--OpenVPN selection:", + " |--Protocol: udp", }, }, "mullvad": { settings: Provider{ Name: constants.Mullvad, ServerSelection: ServerSelection{ - Countries: []string{"a", "b"}, - Cities: []string{"c", "d"}, - ISPs: []string{"e", "f"}, - CustomPort: 1, + Countries: []string{"a", "b"}, + Cities: []string{"c", "d"}, + ISPs: []string{"e", "f"}, + OpenVPN: OpenVPNSelection{ + CustomPort: 1, + }, }, }, lines: []string{ "|--Mullvad settings:", - " |--Network protocol: udp", " |--Countries: a, b", " |--Cities: c, d", " |--ISPs: e, f", - " |--Custom port: 1", + " |--OpenVPN selection:", + " |--Protocol: udp", + " |--Custom port: 1", }, }, "nordvpn": { @@ -130,9 +138,10 @@ func Test_Provider_lines(t *testing.T) { }, lines: []string{ "|--Nordvpn settings:", - " |--Network protocol: udp", " |--Regions: a, b", " |--Numbers: 1, 2", + " |--OpenVPN selection:", + " |--Protocol: udp", }, }, "privado": { @@ -144,8 +153,9 @@ func Test_Provider_lines(t *testing.T) { }, lines: []string{ "|--Privado settings:", - " |--Network protocol: udp", " |--Hostnames: a, b", + " |--OpenVPN selection:", + " |--Protocol: udp", }, }, "privatevpn": { @@ -159,10 +169,11 @@ func Test_Provider_lines(t *testing.T) { }, lines: []string{ "|--Privatevpn settings:", - " |--Network protocol: udp", " |--Countries: c, d", " |--Cities: e, f", " |--Hostnames: a, b", + " |--OpenVPN selection:", + " |--Protocol: udp", }, }, "protonvpn": { @@ -178,20 +189,23 @@ func Test_Provider_lines(t *testing.T) { }, lines: []string{ "|--Protonvpn settings:", - " |--Network protocol: udp", " |--Countries: a, b", " |--Regions: c, d", " |--Cities: e, f", " |--Names: g, h", " |--Hostnames: i, j", + " |--OpenVPN selection:", + " |--Protocol: udp", }, }, "private internet access": { settings: Provider{ Name: constants.PrivateInternetAccess, ServerSelection: ServerSelection{ - Regions: []string{"a", "b"}, - CustomPort: 1, + Regions: []string{"a", "b"}, + OpenVPN: OpenVPNSelection{ + CustomPort: 1, + }, }, PortForwarding: PortForwarding{ Enabled: true, @@ -200,9 +214,10 @@ func Test_Provider_lines(t *testing.T) { }, lines: []string{ "|--Private Internet Access settings:", - " |--Network protocol: udp", " |--Regions: a, b", - " |--Custom port: 1", + " |--OpenVPN selection:", + " |--Protocol: udp", + " |--Custom port: 1", " |--Port forwarding:", " |--File path: /here", }, @@ -218,10 +233,11 @@ func Test_Provider_lines(t *testing.T) { }, lines: []string{ "|--Purevpn settings:", - " |--Network protocol: udp", " |--Regions: a, b", " |--Countries: c, d", " |--Cities: e, f", + " |--OpenVPN selection:", + " |--Protocol: udp", }, }, "surfshark": { @@ -233,8 +249,9 @@ func Test_Provider_lines(t *testing.T) { }, lines: []string{ "|--Surfshark settings:", - " |--Network protocol: udp", " |--Regions: a, b", + " |--OpenVPN selection:", + " |--Protocol: udp", }, }, "torguard": { @@ -248,10 +265,11 @@ func Test_Provider_lines(t *testing.T) { }, lines: []string{ "|--Torguard settings:", - " |--Network protocol: udp", " |--Countries: a, b", " |--Cities: c, d", " |--Hostnames: e", + " |--OpenVPN selection:", + " |--Protocol: udp", }, }, constants.VPNUnlimited: { @@ -267,12 +285,13 @@ func Test_Provider_lines(t *testing.T) { }, lines: []string{ "|--Vpn Unlimited settings:", - " |--Network protocol: udp", " |--Countries: a, b", " |--Cities: c, d", " |--Hostnames: e, f", " |--Free servers only", " |--Stream servers only", + " |--OpenVPN selection:", + " |--Protocol: udp", }, }, "vyprvpn": { @@ -284,27 +303,31 @@ func Test_Provider_lines(t *testing.T) { }, lines: []string{ "|--Vyprvpn settings:", - " |--Network protocol: udp", " |--Regions: a, b", + " |--OpenVPN selection:", + " |--Protocol: udp", }, }, "windscribe": { settings: Provider{ Name: constants.Windscribe, ServerSelection: ServerSelection{ - Regions: []string{"a", "b"}, - Cities: []string{"c", "d"}, - Hostnames: []string{"e", "f"}, - CustomPort: 1, + Regions: []string{"a", "b"}, + Cities: []string{"c", "d"}, + Hostnames: []string{"e", "f"}, + OpenVPN: OpenVPNSelection{ + CustomPort: 1, + }, }, }, lines: []string{ "|--Windscribe settings:", - " |--Network protocol: udp", " |--Regions: a, b", " |--Cities: c, d", " |--Hostnames: e, f", - " |--Custom port: 1", + " |--OpenVPN selection:", + " |--Protocol: udp", + " |--Custom port: 1", }, }, } diff --git a/internal/configuration/purevpn.go b/internal/configuration/purevpn.go index e8b728c9..84bfbf2f 100644 --- a/internal/configuration/purevpn.go +++ b/internal/configuration/purevpn.go @@ -23,17 +23,14 @@ func (settings *Provider) purevpnLines() (lines []string) { lines = append(lines, lastIndent+"Hostnames: "+commaJoin(settings.ServerSelection.Hostnames)) } + lines = append(lines, settings.ServerSelection.OpenVPN.lines()...) + return lines } func (settings *Provider) readPurevpn(r reader) (err error) { settings.Name = constants.Purevpn - settings.ServerSelection.TCP, err = readProtocol(r.env) - if err != nil { - return err - } - settings.ServerSelection.TargetIP, err = readTargetIP(r.env) if err != nil { return err @@ -59,5 +56,5 @@ func (settings *Provider) readPurevpn(r reader) (err error) { return fmt.Errorf("environment variable SERVER_HOSTNAME: %w", err) } - return nil + return settings.ServerSelection.OpenVPN.readProtocolOnly(r.env) } diff --git a/internal/configuration/selection.go b/internal/configuration/selection.go index e0c1f4ee..45d3dadc 100644 --- a/internal/configuration/selection.go +++ b/internal/configuration/selection.go @@ -1,12 +1,14 @@ package configuration import ( + "fmt" "net" + + "github.com/qdm12/golibs/params" ) type ServerSelection struct { //nolint:maligned // Common - TCP bool `json:"tcp"` // UDP if TCP is false TargetIP net.IP `json:"target_ip,omitempty"` // TODO comments // Cyberghost, PIA, Protonvpn, Surfshark, Windscribe, Vyprvpn, NordVPN @@ -27,20 +29,57 @@ type ServerSelection struct { //nolint:maligned ISPs []string `json:"isps"` Owned bool `json:"owned"` - // Mullvad, Windscribe, PIA - CustomPort uint16 `json:"custom_port"` - // NordVPN Numbers []uint16 `json:"numbers"` - // PIA - needed to get the port number - EncryptionPreset string `json:"encryption_preset"` - // ProtonVPN FreeOnly bool `json:"free_only"` // VPNUnlimited StreamOnly bool `json:"stream_only"` + + OpenVPN OpenVPNSelection `json:"openvpn"` +} + +type OpenVPNSelection struct { + TCP bool `json:"tcp"` // UDP if TCP is false + CustomPort uint16 `json:"custom_port"` // HideMyAss, Mullvad, PIA, ProtonVPN, Windscribe + EncPreset string `json:"encryption_preset"` // PIA - needed to get the port number +} + +func (settings *OpenVPNSelection) lines() (lines []string) { + lines = append(lines, lastIndent+"OpenVPN selection:") + + lines = append(lines, indent+lastIndent+"Protocol: "+protoToString(settings.TCP)) + + if settings.CustomPort != 0 { + lines = append(lines, indent+lastIndent+"Custom port: "+fmt.Sprint(settings.CustomPort)) + } + + if settings.EncPreset != "" { + lines = append(lines, indent+lastIndent+"PIA encryption preset: "+settings.EncPreset) + } + + return lines +} + +func (settings *OpenVPNSelection) readProtocolOnly(env params.Env) (err error) { + settings.TCP, err = readProtocol(env) + return err +} + +func (settings *OpenVPNSelection) readProtocolAndPort(env params.Env) (err error) { + settings.TCP, err = readProtocol(env) + if err != nil { + return err + } + + settings.CustomPort, err = readPortOrZero(env, "PORT") + if err != nil { + return fmt.Errorf("environment variable PORT: %w", err) + } + + return nil } // PortForwarding contains settings for port forwarding. diff --git a/internal/configuration/settings_test.go b/internal/configuration/settings_test.go index fafa8ac9..64253285 100644 --- a/internal/configuration/settings_test.go +++ b/internal/configuration/settings_test.go @@ -34,7 +34,8 @@ func Test_Settings_lines(t *testing.T) { " |--Version: 2.5", " |--Verbosity level: 0", " |--Mullvad settings:", - " |--Network protocol: udp", + " |--OpenVPN selection:", + " |--Protocol: udp", "|--DNS:", "|--Firewall: disabled ⚠️", "|--System:", diff --git a/internal/configuration/surfshark.go b/internal/configuration/surfshark.go index 020bb6e9..700ce4fc 100644 --- a/internal/configuration/surfshark.go +++ b/internal/configuration/surfshark.go @@ -15,17 +15,14 @@ func (settings *Provider) surfsharkLines() (lines []string) { lines = append(lines, lastIndent+"Hostnames: "+commaJoin(settings.ServerSelection.Hostnames)) } + lines = append(lines, settings.ServerSelection.OpenVPN.lines()...) + return lines } func (settings *Provider) readSurfshark(r reader) (err error) { settings.Name = constants.Surfshark - settings.ServerSelection.TCP, err = readProtocol(r.env) - if err != nil { - return err - } - settings.ServerSelection.TargetIP, err = readTargetIP(r.env) if err != nil { return err @@ -41,5 +38,5 @@ func (settings *Provider) readSurfshark(r reader) (err error) { return fmt.Errorf("environment variable SERVER_HOSTNAME: %w", err) } - return nil + return settings.ServerSelection.OpenVPN.readProtocolOnly(r.env) } diff --git a/internal/configuration/torguard.go b/internal/configuration/torguard.go index 7e2b96a2..8824e5a2 100644 --- a/internal/configuration/torguard.go +++ b/internal/configuration/torguard.go @@ -19,17 +19,14 @@ func (settings *Provider) torguardLines() (lines []string) { lines = append(lines, lastIndent+"Hostnames: "+commaJoin(settings.ServerSelection.Hostnames)) } + lines = append(lines, settings.ServerSelection.OpenVPN.lines()...) + return lines } func (settings *Provider) readTorguard(r reader) (err error) { settings.Name = constants.Torguard - settings.ServerSelection.TCP, err = readProtocol(r.env) - if err != nil { - return err - } - settings.ServerSelection.TargetIP, err = readTargetIP(r.env) if err != nil { return err @@ -50,5 +47,5 @@ func (settings *Provider) readTorguard(r reader) (err error) { return fmt.Errorf("environment variable SERVER_HOSTNAME: %w", err) } - return nil + return settings.ServerSelection.OpenVPN.readProtocolAndPort(r.env) } diff --git a/internal/configuration/vpnunlimited.go b/internal/configuration/vpnunlimited.go index 0aadbf54..ed293060 100644 --- a/internal/configuration/vpnunlimited.go +++ b/internal/configuration/vpnunlimited.go @@ -28,17 +28,14 @@ func (settings *Provider) vpnUnlimitedLines() (lines []string) { lines = append(lines, lastIndent+"Stream servers only") } + lines = append(lines, settings.ServerSelection.OpenVPN.lines()...) + return lines } func (settings *Provider) readVPNUnlimited(r reader) (err error) { settings.Name = constants.VPNUnlimited - settings.ServerSelection.TCP, err = readProtocol(r.env) - if err != nil { - return err - } - settings.ServerSelection.TargetIP, err = readTargetIP(r.env) if err != nil { return err @@ -69,7 +66,7 @@ func (settings *Provider) readVPNUnlimited(r reader) (err error) { return fmt.Errorf("environment variable STREAM_ONLY: %w", err) } - return nil + return settings.ServerSelection.OpenVPN.readProtocolOnly(r.env) } func (settings *OpenVPN) readVPNUnlimited(r reader) (err error) { diff --git a/internal/configuration/vpnunlimited_test.go b/internal/configuration/vpnunlimited_test.go index 5622463e..9e08b4b4 100644 --- a/internal/configuration/vpnunlimited_test.go +++ b/internal/configuration/vpnunlimited_test.go @@ -13,7 +13,12 @@ func Test_Provider_vpnUnlimitedLines(t *testing.T) { settings Provider lines []string }{ - "empty settings": {}, + "empty settings": { + lines: []string{ + "|--OpenVPN selection:", + " |--Protocol: udp", + }, + }, "full settings": { settings: Provider{ ServerSelection: ServerSelection{ @@ -26,6 +31,8 @@ func Test_Provider_vpnUnlimitedLines(t *testing.T) { "|--Countries: A, B", "|--Cities: C, D", "|--Hostnames: E, F", + "|--OpenVPN selection:", + " |--Protocol: udp", }, }, } diff --git a/internal/configuration/vyprvpn.go b/internal/configuration/vyprvpn.go index 28f47866..88378bff 100644 --- a/internal/configuration/vyprvpn.go +++ b/internal/configuration/vyprvpn.go @@ -11,17 +11,14 @@ func (settings *Provider) vyprvpnLines() (lines []string) { lines = append(lines, lastIndent+"Regions: "+commaJoin(settings.ServerSelection.Regions)) } + lines = append(lines, settings.ServerSelection.OpenVPN.lines()...) + return lines } func (settings *Provider) readVyprvpn(r reader) (err error) { settings.Name = constants.Vyprvpn - settings.ServerSelection.TCP, err = readProtocol(r.env) - if err != nil { - return err - } - settings.ServerSelection.TargetIP, err = readTargetIP(r.env) if err != nil { return err diff --git a/internal/configuration/windscribe.go b/internal/configuration/windscribe.go index f30eabc4..dd4f01e9 100644 --- a/internal/configuration/windscribe.go +++ b/internal/configuration/windscribe.go @@ -2,9 +2,9 @@ package configuration import ( "fmt" - "strconv" "github.com/qdm12/gluetun/internal/constants" + "github.com/qdm12/golibs/params" ) func (settings *Provider) windscribeLines() (lines []string) { @@ -20,9 +20,7 @@ func (settings *Provider) windscribeLines() (lines []string) { lines = append(lines, lastIndent+"Hostnames: "+commaJoin(settings.ServerSelection.Hostnames)) } - if settings.ServerSelection.CustomPort > 0 { - lines = append(lines, lastIndent+"Custom port: "+strconv.Itoa(int(settings.ServerSelection.CustomPort))) - } + lines = append(lines, settings.ServerSelection.OpenVPN.lines()...) return lines } @@ -30,11 +28,6 @@ func (settings *Provider) windscribeLines() (lines []string) { func (settings *Provider) readWindscribe(r reader) (err error) { settings.Name = constants.Windscribe - settings.ServerSelection.TCP, err = readProtocol(r.env) - if err != nil { - return err - } - settings.ServerSelection.TargetIP, err = readTargetIP(r.env) if err != nil { return err @@ -55,7 +48,16 @@ func (settings *Provider) readWindscribe(r reader) (err error) { return fmt.Errorf("environment variable SERVER_HOSTNAME: %w", err) } - settings.ServerSelection.CustomPort, err = readCustomPort(r.env, settings.ServerSelection.TCP, + return settings.ServerSelection.OpenVPN.readWindscribe(r.env) +} + +func (settings *OpenVPNSelection) readWindscribe(env params.Env) (err error) { + settings.TCP, err = readProtocol(env) + if err != nil { + return err + } + + settings.CustomPort, err = readCustomPort(env, settings.TCP, []uint16{21, 22, 80, 123, 143, 443, 587, 1194, 3306, 8080, 54783}, []uint16{53, 80, 123, 443, 1194, 54783}) if err != nil { diff --git a/internal/provider/cyberghost/connection.go b/internal/provider/cyberghost/connection.go index 2bbd39d6..c97e4693 100644 --- a/internal/provider/cyberghost/connection.go +++ b/internal/provider/cyberghost/connection.go @@ -11,7 +11,7 @@ func (c *Cyberghost) GetOpenVPNConnection(selection configuration.ServerSelectio connection models.OpenVPNConnection, err error) { const port = 443 protocol := constants.UDP - if selection.TCP { + if selection.OpenVPN.TCP { protocol = constants.TCP } diff --git a/internal/provider/cyberghost/filter.go b/internal/provider/cyberghost/filter.go index c9d92d03..b1ed9348 100644 --- a/internal/provider/cyberghost/filter.go +++ b/internal/provider/cyberghost/filter.go @@ -16,7 +16,7 @@ var ErrGroupMismatchesProtocol = errors.New("server group does not match protoco func (c *Cyberghost) filterServers(selection configuration.ServerSelection) ( servers []models.CyberghostServer, err error) { if len(selection.Groups) == 0 { - if selection.TCP { + if selection.OpenVPN.TCP { selection.Groups = tcpGroupChoices() } else { selection.Groups = udpGroupChoices() @@ -25,7 +25,7 @@ func (c *Cyberghost) filterServers(selection configuration.ServerSelection) ( // Check each group match the protocol groupsCheckFn := groupsAreAllUDP - if selection.TCP { + if selection.OpenVPN.TCP { groupsCheckFn = groupsAreAllTCP } if err := groupsCheckFn(selection.Groups); err != nil { diff --git a/internal/provider/cyberghost/filter_test.go b/internal/provider/cyberghost/filter_test.go index 64389d1a..7ec15865 100644 --- a/internal/provider/cyberghost/filter_test.go +++ b/internal/provider/cyberghost/filter_test.go @@ -41,7 +41,9 @@ func Test_Cyberghost_filterServers(t *testing.T) { {Region: "d", Group: "Premium UDP Europe"}, }, selection: configuration.ServerSelection{ - TCP: true, + OpenVPN: configuration.OpenVPNSelection{ + TCP: true, + }, }, filteredServers: []models.CyberghostServer{ {Region: "a", Group: "Premium TCP Asia"}, diff --git a/internal/provider/fastestvpn/connection.go b/internal/provider/fastestvpn/connection.go index 2158f608..f13deebb 100644 --- a/internal/provider/fastestvpn/connection.go +++ b/internal/provider/fastestvpn/connection.go @@ -11,7 +11,7 @@ func (f *Fastestvpn) GetOpenVPNConnection(selection configuration.ServerSelectio connection models.OpenVPNConnection, err error) { const port = 4443 protocol := constants.UDP - if selection.TCP { + if selection.OpenVPN.TCP { protocol = constants.TCP } diff --git a/internal/provider/fastestvpn/filter.go b/internal/provider/fastestvpn/filter.go index dea2fb85..93e5c8c6 100644 --- a/internal/provider/fastestvpn/filter.go +++ b/internal/provider/fastestvpn/filter.go @@ -13,8 +13,8 @@ func (f *Fastestvpn) filterServers(selection configuration.ServerSelection) ( case utils.FilterByPossibilities(server.Country, selection.Countries), utils.FilterByPossibilities(server.Hostname, selection.Hostnames), - selection.TCP && !server.TCP, - !selection.TCP && !server.UDP: + selection.OpenVPN.TCP && !server.TCP, + !selection.OpenVPN.TCP && !server.UDP: default: servers = append(servers, server) } diff --git a/internal/provider/hidemyass/connection.go b/internal/provider/hidemyass/connection.go index 0bb7a790..d4b4ba61 100644 --- a/internal/provider/hidemyass/connection.go +++ b/internal/provider/hidemyass/connection.go @@ -11,13 +11,13 @@ func (h *HideMyAss) GetOpenVPNConnection(selection configuration.ServerSelection connection models.OpenVPNConnection, err error) { var port uint16 = 553 protocol := constants.UDP - if selection.TCP { + if selection.OpenVPN.TCP { protocol = constants.TCP port = 8080 } - if selection.CustomPort > 0 { - port = selection.CustomPort + if selection.OpenVPN.CustomPort > 0 { + port = selection.OpenVPN.CustomPort } servers, err := h.filterServers(selection) diff --git a/internal/provider/hidemyass/filter.go b/internal/provider/hidemyass/filter.go index 85911238..2a721f46 100644 --- a/internal/provider/hidemyass/filter.go +++ b/internal/provider/hidemyass/filter.go @@ -14,8 +14,8 @@ func (h *HideMyAss) filterServers(selection configuration.ServerSelection) ( utils.FilterByPossibilities(server.Country, selection.Countries), utils.FilterByPossibilities(server.City, selection.Cities), utils.FilterByPossibilities(server.Hostname, selection.Hostnames), - selection.TCP && !server.TCP, - !selection.TCP && !server.UDP: + selection.OpenVPN.TCP && !server.TCP, + !selection.OpenVPN.TCP && !server.UDP: default: servers = append(servers, server) } diff --git a/internal/provider/ipvanish/connection.go b/internal/provider/ipvanish/connection.go index f765530b..e19e52ea 100644 --- a/internal/provider/ipvanish/connection.go +++ b/internal/provider/ipvanish/connection.go @@ -15,7 +15,7 @@ func (i *Ipvanish) GetOpenVPNConnection(selection configuration.ServerSelection) connection models.OpenVPNConnection, err error) { const port = 443 const protocol = constants.UDP - if selection.TCP { + if selection.OpenVPN.TCP { return connection, ErrProtocolUnsupported } diff --git a/internal/provider/ipvanish/filter.go b/internal/provider/ipvanish/filter.go index 2fe01e71..393ea0c6 100644 --- a/internal/provider/ipvanish/filter.go +++ b/internal/provider/ipvanish/filter.go @@ -14,8 +14,8 @@ func (i *Ipvanish) filterServers(selection configuration.ServerSelection) ( utils.FilterByPossibilities(server.Country, selection.Countries), utils.FilterByPossibilities(server.City, selection.Cities), utils.FilterByPossibilities(server.Hostname, selection.Hostnames), - selection.TCP && !server.TCP, - !selection.TCP && !server.UDP: + selection.OpenVPN.TCP && !server.TCP, + !selection.OpenVPN.TCP && !server.UDP: default: servers = append(servers, server) } diff --git a/internal/provider/ivpn/connection.go b/internal/provider/ivpn/connection.go index 202862ff..9ec16c9d 100644 --- a/internal/provider/ivpn/connection.go +++ b/internal/provider/ivpn/connection.go @@ -15,7 +15,7 @@ func (i *Ivpn) GetOpenVPNConnection(selection configuration.ServerSelection) ( connection models.OpenVPNConnection, err error) { const port = 2049 const protocol = constants.UDP - if selection.TCP { + if selection.OpenVPN.TCP { return connection, ErrProtocolUnsupported } diff --git a/internal/provider/ivpn/filter.go b/internal/provider/ivpn/filter.go index ae4d9da2..385680d7 100644 --- a/internal/provider/ivpn/filter.go +++ b/internal/provider/ivpn/filter.go @@ -14,8 +14,8 @@ func (i *Ivpn) filterServers(selection configuration.ServerSelection) ( utils.FilterByPossibilities(server.Country, selection.Countries), utils.FilterByPossibilities(server.City, selection.Cities), utils.FilterByPossibilities(server.Hostname, selection.Hostnames), - selection.TCP && !server.TCP, - !selection.TCP && !server.UDP: + selection.OpenVPN.TCP && !server.TCP, + !selection.OpenVPN.TCP && !server.UDP: default: servers = append(servers, server) } diff --git a/internal/provider/mullvad/connection.go b/internal/provider/mullvad/connection.go index 9d8c5e35..adaeb078 100644 --- a/internal/provider/mullvad/connection.go +++ b/internal/provider/mullvad/connection.go @@ -11,13 +11,13 @@ func (m *Mullvad) GetOpenVPNConnection(selection configuration.ServerSelection) connection models.OpenVPNConnection, err error) { var port uint16 = 1194 protocol := constants.UDP - if selection.TCP { + if selection.OpenVPN.TCP { port = 443 protocol = constants.TCP } - if selection.CustomPort > 0 { - port = selection.CustomPort + if selection.OpenVPN.CustomPort > 0 { + port = selection.OpenVPN.CustomPort } servers, err := m.filterServers(selection) diff --git a/internal/provider/nordvpn/connection.go b/internal/provider/nordvpn/connection.go index 29b81c62..6fb09369 100644 --- a/internal/provider/nordvpn/connection.go +++ b/internal/provider/nordvpn/connection.go @@ -11,7 +11,7 @@ func (n *Nordvpn) GetOpenVPNConnection(selection configuration.ServerSelection) connection models.OpenVPNConnection, err error) { var port uint16 = 1194 protocol := constants.UDP - if selection.TCP { + if selection.OpenVPN.TCP { port = 443 protocol = constants.TCP } diff --git a/internal/provider/nordvpn/filter.go b/internal/provider/nordvpn/filter.go index 7b49a299..6942fff1 100644 --- a/internal/provider/nordvpn/filter.go +++ b/internal/provider/nordvpn/filter.go @@ -23,8 +23,8 @@ func (n *Nordvpn) filterServers(selection configuration.ServerSelection) ( utils.FilterByPossibilities(server.Hostname, selection.Hostnames), utils.FilterByPossibilities(server.Name, selection.Names), utils.FilterByPossibilities(serverNumber, selectedNumbers), - selection.TCP && !server.TCP, - !selection.TCP && !server.UDP: + selection.OpenVPN.TCP && !server.TCP, + !selection.OpenVPN.TCP && !server.UDP: default: servers = append(servers, server) } diff --git a/internal/provider/privado/connection.go b/internal/provider/privado/connection.go index 77f507e6..c308c797 100644 --- a/internal/provider/privado/connection.go +++ b/internal/provider/privado/connection.go @@ -16,7 +16,7 @@ func (p *Privado) GetOpenVPNConnection(selection configuration.ServerSelection) connection models.OpenVPNConnection, err error) { const port = 1194 const protocol = constants.UDP - if selection.TCP { + if selection.OpenVPN.TCP { return connection, fmt.Errorf("%w: TCP for provider Privado", ErrProtocolUnsupported) } diff --git a/internal/provider/privateinternetaccess/connection.go b/internal/provider/privateinternetaccess/connection.go index d0725f0e..656bcdd4 100644 --- a/internal/provider/privateinternetaccess/connection.go +++ b/internal/provider/privateinternetaccess/connection.go @@ -10,11 +10,11 @@ import ( func (p *PIA) GetOpenVPNConnection(selection configuration.ServerSelection) ( connection models.OpenVPNConnection, err error) { protocol := constants.UDP - if selection.TCP { + if selection.OpenVPN.TCP { protocol = constants.TCP } - port, err := getPort(selection.TCP, selection.EncryptionPreset, selection.CustomPort) + port, err := getPort(selection.OpenVPN) if err != nil { return connection, err } diff --git a/internal/provider/privateinternetaccess/filter.go b/internal/provider/privateinternetaccess/filter.go index 1f1b3501..abd10936 100644 --- a/internal/provider/privateinternetaccess/filter.go +++ b/internal/provider/privateinternetaccess/filter.go @@ -14,8 +14,8 @@ func (p *PIA) filterServers(selection configuration.ServerSelection) ( utils.FilterByPossibilities(server.Region, selection.Regions), utils.FilterByPossibilities(server.Hostname, selection.Hostnames), utils.FilterByPossibilities(server.ServerName, selection.Names), - selection.TCP && !server.TCP, - !selection.TCP && !server.UDP: + selection.OpenVPN.TCP && !server.TCP, + !selection.OpenVPN.TCP && !server.UDP: default: servers = append(servers, server) } diff --git a/internal/provider/privateinternetaccess/port.go b/internal/provider/privateinternetaccess/port.go index 2544c623..1f7a77a1 100644 --- a/internal/provider/privateinternetaccess/port.go +++ b/internal/provider/privateinternetaccess/port.go @@ -4,20 +4,21 @@ import ( "errors" "fmt" + "github.com/qdm12/gluetun/internal/configuration" "github.com/qdm12/gluetun/internal/constants" ) -func getPort(tcp bool, encryptionPreset string, customPort uint16) ( +func getPort(openvpnSelection configuration.OpenVPNSelection) ( port uint16, err error) { - if customPort == 0 { - return getDefaultPort(tcp, encryptionPreset), nil + if openvpnSelection.CustomPort == 0 { + return getDefaultPort(openvpnSelection.TCP, openvpnSelection.EncPreset), nil } - if err := checkPort(customPort, tcp); err != nil { + if err := checkPort(openvpnSelection.CustomPort, openvpnSelection.TCP); err != nil { return 0, err } - return customPort, nil + return openvpnSelection.CustomPort, nil } func getDefaultPort(tcp bool, encryptionPreset string) (port uint16) { diff --git a/internal/provider/privatevpn/connection.go b/internal/provider/privatevpn/connection.go index 36aac372..6d6b5dd1 100644 --- a/internal/provider/privatevpn/connection.go +++ b/internal/provider/privatevpn/connection.go @@ -11,7 +11,7 @@ func (p *Privatevpn) GetOpenVPNConnection(selection configuration.ServerSelectio connection models.OpenVPNConnection, err error) { protocol := constants.UDP var port uint16 = 1194 - if selection.TCP { + if selection.OpenVPN.TCP { protocol = constants.TCP port = 443 } diff --git a/internal/provider/protonvpn/connection.go b/internal/provider/protonvpn/connection.go index 92bc6d59..1f89b83a 100644 --- a/internal/provider/protonvpn/connection.go +++ b/internal/provider/protonvpn/connection.go @@ -10,11 +10,11 @@ import ( func (p *Protonvpn) GetOpenVPNConnection(selection configuration.ServerSelection) ( connection models.OpenVPNConnection, err error) { protocol := constants.UDP - if selection.TCP { + if selection.OpenVPN.TCP { protocol = constants.TCP } - port, err := getPort(selection.TCP, selection.CustomPort) + port, err := getPort(selection.OpenVPN.TCP, selection.OpenVPN.CustomPort) if err != nil { return connection, err } diff --git a/internal/provider/purevpn/connection.go b/internal/provider/purevpn/connection.go index 32486239..5fc4ce74 100644 --- a/internal/provider/purevpn/connection.go +++ b/internal/provider/purevpn/connection.go @@ -11,7 +11,7 @@ func (p *Purevpn) GetOpenVPNConnection(selection configuration.ServerSelection) connection models.OpenVPNConnection, err error) { protocol := constants.UDP var port uint16 = 53 - if selection.TCP { + if selection.OpenVPN.TCP { protocol = constants.TCP port = 80 } diff --git a/internal/provider/purevpn/filter.go b/internal/provider/purevpn/filter.go index 30c481ba..40b5fc49 100644 --- a/internal/provider/purevpn/filter.go +++ b/internal/provider/purevpn/filter.go @@ -15,8 +15,8 @@ func (p *Purevpn) filterServers(selection configuration.ServerSelection) ( utils.FilterByPossibilities(server.Country, selection.Countries), utils.FilterByPossibilities(server.City, selection.Cities), utils.FilterByPossibilities(server.Hostname, selection.Hostnames), - selection.TCP && !server.TCP, - !selection.TCP && !server.UDP: + selection.OpenVPN.TCP && !server.TCP, + !selection.OpenVPN.TCP && !server.UDP: default: servers = append(servers, server) } diff --git a/internal/provider/surfshark/connection.go b/internal/provider/surfshark/connection.go index cd9a9e41..eb084866 100644 --- a/internal/provider/surfshark/connection.go +++ b/internal/provider/surfshark/connection.go @@ -11,7 +11,7 @@ func (s *Surfshark) GetOpenVPNConnection(selection configuration.ServerSelection connection models.OpenVPNConnection, err error) { protocol := constants.UDP var port uint16 = 1194 - if selection.TCP { + if selection.OpenVPN.TCP { protocol = constants.TCP port = 1443 } diff --git a/internal/provider/surfshark/filter.go b/internal/provider/surfshark/filter.go index a80b93cb..a6c00183 100644 --- a/internal/provider/surfshark/filter.go +++ b/internal/provider/surfshark/filter.go @@ -13,8 +13,8 @@ func (s *Surfshark) filterServers(selection configuration.ServerSelection) ( case utils.FilterByPossibilities(server.Region, selection.Regions), utils.FilterByPossibilities(server.Hostname, selection.Hostnames), - selection.TCP && !server.TCP, - !selection.TCP && !server.UDP: + selection.OpenVPN.TCP && !server.TCP, + !selection.OpenVPN.TCP && !server.UDP: default: servers = append(servers, server) } diff --git a/internal/provider/torguard/connection.go b/internal/provider/torguard/connection.go index 33984b17..658e0313 100644 --- a/internal/provider/torguard/connection.go +++ b/internal/provider/torguard/connection.go @@ -10,13 +10,13 @@ import ( func (t *Torguard) GetOpenVPNConnection(selection configuration.ServerSelection) ( connection models.OpenVPNConnection, err error) { protocol := constants.UDP - if selection.TCP { + if selection.OpenVPN.TCP { protocol = constants.TCP } var port uint16 = 1912 - if selection.CustomPort > 0 { - port = selection.CustomPort + if selection.OpenVPN.CustomPort > 0 { + port = selection.OpenVPN.CustomPort } servers, err := t.filterServers(selection) diff --git a/internal/provider/torguard/filter.go b/internal/provider/torguard/filter.go index d89ccaa4..782b70d5 100644 --- a/internal/provider/torguard/filter.go +++ b/internal/provider/torguard/filter.go @@ -14,8 +14,8 @@ func (t *Torguard) filterServers(selection configuration.ServerSelection) ( utils.FilterByPossibilities(server.Country, selection.Countries), utils.FilterByPossibilities(server.City, selection.Cities), utils.FilterByPossibilities(server.Hostname, selection.Hostnames), - selection.TCP && !server.TCP, - !selection.TCP && !server.UDP: + selection.OpenVPN.TCP && !server.TCP, + !selection.OpenVPN.TCP && !server.UDP: default: servers = append(servers, server) } diff --git a/internal/provider/utils/formatting.go b/internal/provider/utils/formatting.go index b2827707..96de8da4 100644 --- a/internal/provider/utils/formatting.go +++ b/internal/provider/utils/formatting.go @@ -20,7 +20,7 @@ func NoServerFoundError(selection configuration.ServerSelection) (err error) { var messageParts []string protocol := constants.UDP - if selection.TCP { + if selection.OpenVPN.TCP { protocol = constants.TCP } messageParts = append(messageParts, "protocol "+protocol) @@ -113,8 +113,8 @@ func NoServerFoundError(selection configuration.ServerSelection) (err error) { messageParts = append(messageParts, part) } - if selection.EncryptionPreset != "" { - part := "encryption preset " + selection.EncryptionPreset + if selection.OpenVPN.EncPreset != "" { + part := "encryption preset " + selection.OpenVPN.EncPreset messageParts = append(messageParts, part) } diff --git a/internal/provider/vpnunlimited/connection.go b/internal/provider/vpnunlimited/connection.go index dbfa79af..7faba67f 100644 --- a/internal/provider/vpnunlimited/connection.go +++ b/internal/provider/vpnunlimited/connection.go @@ -15,7 +15,7 @@ func (p *Provider) GetOpenVPNConnection(selection configuration.ServerSelection) connection models.OpenVPNConnection, err error) { const port = 1194 const protocol = constants.UDP - if selection.TCP { + if selection.OpenVPN.TCP { return connection, ErrProtocolUnsupported } diff --git a/internal/provider/vpnunlimited/filter.go b/internal/provider/vpnunlimited/filter.go index ac724522..d1ed7044 100644 --- a/internal/provider/vpnunlimited/filter.go +++ b/internal/provider/vpnunlimited/filter.go @@ -16,8 +16,8 @@ func (p *Provider) filterServers(selection configuration.ServerSelection) ( utils.FilterByPossibilities(server.Hostname, selection.Hostnames), selection.FreeOnly && !server.Free, selection.StreamOnly && !server.Stream, - selection.TCP && !server.TCP, - !selection.TCP && !server.UDP: + selection.OpenVPN.TCP && !server.TCP, + !selection.OpenVPN.TCP && !server.UDP: default: servers = append(servers, server) } diff --git a/internal/provider/vyprvpn/connection.go b/internal/provider/vyprvpn/connection.go index 8f9d86da..76983ffa 100644 --- a/internal/provider/vyprvpn/connection.go +++ b/internal/provider/vyprvpn/connection.go @@ -16,7 +16,7 @@ func (v *Vyprvpn) GetOpenVPNConnection(selection configuration.ServerSelection) connection models.OpenVPNConnection, err error) { const port = 443 const protocol = constants.UDP - if selection.TCP { + if selection.OpenVPN.TCP { return connection, fmt.Errorf("%w: TCP for provider VyprVPN", ErrProtocolUnsupported) } diff --git a/internal/provider/vyprvpn/filter.go b/internal/provider/vyprvpn/filter.go index cacc5dd7..c4d0db27 100644 --- a/internal/provider/vyprvpn/filter.go +++ b/internal/provider/vyprvpn/filter.go @@ -13,8 +13,8 @@ func (v *Vyprvpn) filterServers(selection configuration.ServerSelection) ( case utils.FilterByPossibilities(server.Region, selection.Regions), utils.FilterByPossibilities(server.Hostname, selection.Hostnames), - selection.TCP && !server.TCP, - !selection.TCP && !server.UDP: + selection.OpenVPN.TCP && !server.TCP, + !selection.OpenVPN.TCP && !server.UDP: default: servers = append(servers, server) } diff --git a/internal/provider/windscribe/connection.go b/internal/provider/windscribe/connection.go index b619fa04..db7501ff 100644 --- a/internal/provider/windscribe/connection.go +++ b/internal/provider/windscribe/connection.go @@ -11,13 +11,13 @@ func (w *Windscribe) GetOpenVPNConnection(selection configuration.ServerSelectio connection models.OpenVPNConnection, err error) { protocol := constants.UDP var port uint16 = 443 - if selection.TCP { + if selection.OpenVPN.TCP { protocol = constants.TCP port = 1194 } - if selection.CustomPort > 0 { - port = selection.CustomPort + if selection.OpenVPN.CustomPort > 0 { + port = selection.OpenVPN.CustomPort } servers, err := w.filterServers(selection)