diff --git a/internal/provider/ivpn/connection.go b/internal/provider/ivpn/connection.go index d507eab6..b1f2009b 100644 --- a/internal/provider/ivpn/connection.go +++ b/internal/provider/ivpn/connection.go @@ -2,7 +2,6 @@ package ivpn import ( "github.com/qdm12/gluetun/internal/configuration" - "github.com/qdm12/gluetun/internal/constants" "github.com/qdm12/gluetun/internal/models" "github.com/qdm12/gluetun/internal/provider/utils" ) @@ -40,23 +39,11 @@ func (i *Ivpn) GetConnection(selection configuration.ServerSelection) ( } func getPort(selection configuration.ServerSelection) (port uint16) { - switch selection.VPN { - case constants.Wireguard: - customPort := selection.Wireguard.CustomPort - if customPort > 0 { - return customPort - } - const defaultPort = 58237 - return defaultPort - default: // OpenVPN - customPort := selection.OpenVPN.CustomPort - if customPort > 0 { - return customPort - } - port = 1194 - if selection.OpenVPN.TCP { - port = 443 - } - return port - } + const ( + defaultOpenVPNTCP = 443 + defaultOpenVPNUDP = 1194 + defaultWireguard = 58237 + ) + return utils.GetPort(selection, defaultOpenVPNTCP, + defaultOpenVPNUDP, defaultWireguard) } diff --git a/internal/provider/ivpn/connection_test.go b/internal/provider/ivpn/connection_test.go index 3a45f25a..18e78233 100644 --- a/internal/provider/ivpn/connection_test.go +++ b/internal/provider/ivpn/connection_test.go @@ -95,66 +95,3 @@ func Test_Ivpn_GetConnection(t *testing.T) { }) } } - -func Test_getPort(t *testing.T) { - t.Parallel() - - testCases := map[string]struct { - selection configuration.ServerSelection - port uint16 - }{ - "default": { - port: 1194, - }, - "OpenVPN UDP": { - selection: configuration.ServerSelection{ - VPN: constants.OpenVPN, - }, - port: 1194, - }, - "OpenVPN TCP": { - selection: configuration.ServerSelection{ - VPN: constants.OpenVPN, - OpenVPN: configuration.OpenVPNSelection{ - TCP: true, - }, - }, - port: 443, - }, - "OpenVPN custom port": { - selection: configuration.ServerSelection{ - VPN: constants.OpenVPN, - OpenVPN: configuration.OpenVPNSelection{ - CustomPort: 1234, - }, - }, - port: 1234, - }, - "Wireguard": { - selection: configuration.ServerSelection{ - VPN: constants.Wireguard, - }, - port: 58237, - }, - "Wireguard custom port": { - selection: configuration.ServerSelection{ - VPN: constants.Wireguard, - Wireguard: configuration.WireguardSelection{ - CustomPort: 1234, - }, - }, - port: 1234, - }, - } - - for name, testCase := range testCases { - testCase := testCase - t.Run(name, func(t *testing.T) { - t.Parallel() - - port := getPort(testCase.selection) - - assert.Equal(t, testCase.port, port) - }) - } -} diff --git a/internal/provider/mullvad/connection.go b/internal/provider/mullvad/connection.go index 694d4a12..341c7d62 100644 --- a/internal/provider/mullvad/connection.go +++ b/internal/provider/mullvad/connection.go @@ -2,7 +2,6 @@ package mullvad import ( "github.com/qdm12/gluetun/internal/configuration" - "github.com/qdm12/gluetun/internal/constants" "github.com/qdm12/gluetun/internal/models" "github.com/qdm12/gluetun/internal/provider/utils" ) @@ -39,23 +38,11 @@ func (m *Mullvad) GetConnection(selection configuration.ServerSelection) ( } func getPort(selection configuration.ServerSelection) (port uint16) { - switch selection.VPN { - case constants.Wireguard: - customPort := selection.Wireguard.CustomPort - if customPort > 0 { - return customPort - } - const defaultPort = 51820 - return defaultPort - default: // OpenVPN - customPort := selection.OpenVPN.CustomPort - if customPort > 0 { - return customPort - } - port = 1194 - if selection.OpenVPN.TCP { - port = 443 - } - return port - } + const ( + defaultOpenVPNTCP = 443 + defaultOpenVPNUDP = 1194 + defaultWireguard = 51820 + ) + return utils.GetPort(selection, defaultOpenVPNTCP, + defaultOpenVPNUDP, defaultWireguard) } diff --git a/internal/provider/mullvad/connection_test.go b/internal/provider/mullvad/connection_test.go index 864e41d5..5556a4da 100644 --- a/internal/provider/mullvad/connection_test.go +++ b/internal/provider/mullvad/connection_test.go @@ -94,66 +94,3 @@ func Test_Mullvad_GetConnection(t *testing.T) { }) } } - -func Test_getPort(t *testing.T) { - t.Parallel() - - testCases := map[string]struct { - selection configuration.ServerSelection - port uint16 - }{ - "default": { - port: 1194, - }, - "OpenVPN UDP": { - selection: configuration.ServerSelection{ - VPN: constants.OpenVPN, - }, - port: 1194, - }, - "OpenVPN TCP": { - selection: configuration.ServerSelection{ - VPN: constants.OpenVPN, - OpenVPN: configuration.OpenVPNSelection{ - TCP: true, - }, - }, - port: 443, - }, - "OpenVPN custom port": { - selection: configuration.ServerSelection{ - VPN: constants.OpenVPN, - OpenVPN: configuration.OpenVPNSelection{ - CustomPort: 1234, - }, - }, - port: 1234, - }, - "Wireguard": { - selection: configuration.ServerSelection{ - VPN: constants.Wireguard, - }, - port: 51820, - }, - "Wireguard custom port": { - selection: configuration.ServerSelection{ - VPN: constants.Wireguard, - Wireguard: configuration.WireguardSelection{ - CustomPort: 1234, - }, - }, - port: 1234, - }, - } - - for name, testCase := range testCases { - testCase := testCase - t.Run(name, func(t *testing.T) { - t.Parallel() - - port := getPort(testCase.selection) - - assert.Equal(t, testCase.port, port) - }) - } -} diff --git a/internal/provider/utils/port.go b/internal/provider/utils/port.go new file mode 100644 index 00000000..22606d15 --- /dev/null +++ b/internal/provider/utils/port.go @@ -0,0 +1,27 @@ +package utils + +import ( + "github.com/qdm12/gluetun/internal/configuration" + "github.com/qdm12/gluetun/internal/constants" +) + +func GetPort(selection configuration.ServerSelection, + defaultOpenVPNTCP, defaultOpenVPNUDP, defaultWireguard uint16) (port uint16) { + switch selection.VPN { + case constants.Wireguard: + customPort := selection.Wireguard.CustomPort + if customPort > 0 { + return customPort + } + return defaultWireguard + default: // OpenVPN + customPort := selection.OpenVPN.CustomPort + if customPort > 0 { + return customPort + } + if selection.OpenVPN.TCP { + return defaultOpenVPNTCP + } + return defaultOpenVPNUDP + } +} diff --git a/internal/provider/utils/port_test.go b/internal/provider/utils/port_test.go new file mode 100644 index 00000000..efdb7208 --- /dev/null +++ b/internal/provider/utils/port_test.go @@ -0,0 +1,79 @@ +package utils + +import ( + "testing" + + "github.com/qdm12/gluetun/internal/configuration" + "github.com/qdm12/gluetun/internal/constants" + "github.com/stretchr/testify/assert" +) + +func Test_GetPort(t *testing.T) { + t.Parallel() + + const ( + defaultOpenVPNTCP = 443 + defaultOpenVPNUDP = 1194 + defaultWireguard = 51820 + ) + + testCases := map[string]struct { + selection configuration.ServerSelection + port uint16 + }{ + "default": { + port: defaultOpenVPNUDP, + }, + "OpenVPN UDP": { + selection: configuration.ServerSelection{ + VPN: constants.OpenVPN, + }, + port: defaultOpenVPNUDP, + }, + "OpenVPN TCP": { + selection: configuration.ServerSelection{ + VPN: constants.OpenVPN, + OpenVPN: configuration.OpenVPNSelection{ + TCP: true, + }, + }, + port: defaultOpenVPNTCP, + }, + "OpenVPN custom port": { + selection: configuration.ServerSelection{ + VPN: constants.OpenVPN, + OpenVPN: configuration.OpenVPNSelection{ + CustomPort: 1234, + }, + }, + port: 1234, + }, + "Wireguard": { + selection: configuration.ServerSelection{ + VPN: constants.Wireguard, + }, + port: defaultWireguard, + }, + "Wireguard custom port": { + selection: configuration.ServerSelection{ + VPN: constants.Wireguard, + Wireguard: configuration.WireguardSelection{ + CustomPort: 1234, + }, + }, + port: 1234, + }, + } + + for name, testCase := range testCases { + testCase := testCase + t.Run(name, func(t *testing.T) { + t.Parallel() + + port := GetPort(testCase.selection, + defaultOpenVPNTCP, defaultOpenVPNUDP, defaultWireguard) + + assert.Equal(t, testCase.port, port) + }) + } +} diff --git a/internal/provider/windscribe/connection.go b/internal/provider/windscribe/connection.go index bf589e03..5c0ea014 100644 --- a/internal/provider/windscribe/connection.go +++ b/internal/provider/windscribe/connection.go @@ -2,7 +2,6 @@ package windscribe import ( "github.com/qdm12/gluetun/internal/configuration" - "github.com/qdm12/gluetun/internal/constants" "github.com/qdm12/gluetun/internal/models" "github.com/qdm12/gluetun/internal/provider/utils" ) @@ -40,23 +39,11 @@ func (w *Windscribe) GetConnection(selection configuration.ServerSelection) ( } func getPort(selection configuration.ServerSelection) (port uint16) { - switch selection.VPN { - case constants.Wireguard: - customPort := selection.Wireguard.CustomPort - if customPort > 0 { - return customPort - } - const defaultPort = 1194 - return defaultPort - default: // OpenVPN - customPort := selection.OpenVPN.CustomPort - if customPort > 0 { - return customPort - } - port = 1194 - if selection.OpenVPN.TCP { - port = 443 - } - return port - } + const ( + defaultOpenVPNTCP = 443 + defaultOpenVPNUDP = 1194 + defaultWireguard = 1194 + ) + return utils.GetPort(selection, defaultOpenVPNTCP, + defaultOpenVPNUDP, defaultWireguard) } diff --git a/internal/provider/windscribe/connection_test.go b/internal/provider/windscribe/connection_test.go index aa4fafdc..4763d9f5 100644 --- a/internal/provider/windscribe/connection_test.go +++ b/internal/provider/windscribe/connection_test.go @@ -94,66 +94,3 @@ func Test_Windscribe_GetConnection(t *testing.T) { }) } } - -func Test_getPort(t *testing.T) { - t.Parallel() - - testCases := map[string]struct { - selection configuration.ServerSelection - port uint16 - }{ - "default": { - port: 1194, - }, - "OpenVPN UDP": { - selection: configuration.ServerSelection{ - VPN: constants.OpenVPN, - }, - port: 1194, - }, - "OpenVPN TCP": { - selection: configuration.ServerSelection{ - VPN: constants.OpenVPN, - OpenVPN: configuration.OpenVPNSelection{ - TCP: true, - }, - }, - port: 443, - }, - "OpenVPN custom port": { - selection: configuration.ServerSelection{ - VPN: constants.OpenVPN, - OpenVPN: configuration.OpenVPNSelection{ - CustomPort: 1234, - }, - }, - port: 1234, - }, - "Wireguard": { - selection: configuration.ServerSelection{ - VPN: constants.Wireguard, - }, - port: 1194, - }, - "Wireguard custom port": { - selection: configuration.ServerSelection{ - VPN: constants.Wireguard, - Wireguard: configuration.WireguardSelection{ - CustomPort: 1234, - }, - }, - port: 1234, - }, - } - - for name, testCase := range testCases { - testCase := testCase - t.Run(name, func(t *testing.T) { - t.Parallel() - - port := getPort(testCase.selection) - - assert.Equal(t, testCase.port, port) - }) - } -}