Maint: common GetPort for OpenVPN+Wireguard providers

This commit is contained in:
Quentin McGaw (desktop)
2021-08-23 16:13:20 +00:00
parent dbf5c569ea
commit 8b52af0d03
8 changed files with 127 additions and 249 deletions

View File

@@ -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
}
}

View File

@@ -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)
})
}
}