Maint: use OPENVPN_PORT instead of PORT

with retro-compatibility
This commit is contained in:
Quentin McGaw (desktop)
2021-09-18 16:09:21 +00:00
parent e2e218c74b
commit b8356b60a6
8 changed files with 89 additions and 54 deletions

View File

@@ -84,7 +84,7 @@ ENV VPNSP=pia \
OPENVPN_IPV6=off \ OPENVPN_IPV6=off \
OPENVPN_CUSTOM_CONFIG= \ OPENVPN_CUSTOM_CONFIG= \
OPENVPN_INTERFACE=tun0 \ OPENVPN_INTERFACE=tun0 \
PORT= \ OPENVPN_PORT= \
# Wireguard # Wireguard
WIREGUARD_PRIVATE_KEY= \ WIREGUARD_PRIVATE_KEY= \
WIREGUARD_PRESHARED_KEY= \ WIREGUARD_PRESHARED_KEY= \

View File

@@ -50,8 +50,11 @@ func (settings *OpenVPNSelection) readIVPN(r reader) (err error) {
return err return err
} }
settings.CustomPort, err = readOpenVPNCustomPort(r.env, settings.TCP, settings.CustomPort, err = readOpenVPNCustomPort(r, openvpnPortValidation{
[]uint16{80, 443, 1443}, []uint16{53, 1194, 2049, 2050}) tcp: settings.TCP,
allowedTCP: []uint16{80, 443, 1443},
allowedUDP: []uint16{53, 1194, 2049, 2050},
})
if err != nil { if err != nil {
return err return err
} }

View File

@@ -47,6 +47,7 @@ func Test_Provider_readIvpn(t *testing.T) { //nolint:gocognit
hostnames sliceStringCall hostnames sliceStringCall
protocol singleStringCall protocol singleStringCall
ovpnPort portCall ovpnPort portCall
ovpnOldPort portCall
wgPort portCall wgPort portCall
wgOldPort portCall wgOldPort portCall
settings Provider settings Provider
@@ -120,7 +121,7 @@ func Test_Provider_readIvpn(t *testing.T) { //nolint:gocognit
settings: Provider{ settings: Provider{
Name: constants.Ivpn, Name: constants.Ivpn,
}, },
err: errors.New("environment variable PORT: dummy test error"), err: errors.New("environment variable OPENVPN_PORT: dummy test error"),
}, },
"wireguard custom port error": { "wireguard custom port error": {
targetIP: singleStringCall{call: true}, targetIP: singleStringCall{call: true},
@@ -130,6 +131,7 @@ func Test_Provider_readIvpn(t *testing.T) { //nolint:gocognit
hostnames: sliceStringCall{call: true}, hostnames: sliceStringCall{call: true},
protocol: singleStringCall{call: true}, protocol: singleStringCall{call: true},
ovpnPort: portCall{getCall: true, getValue: "0"}, ovpnPort: portCall{getCall: true, getValue: "0"},
ovpnOldPort: portCall{getCall: true, getValue: "0"},
wgPort: portCall{getCall: true, getErr: errDummy}, wgPort: portCall{getCall: true, getErr: errDummy},
settings: Provider{ settings: Provider{
Name: constants.Ivpn, Name: constants.Ivpn,
@@ -144,6 +146,7 @@ func Test_Provider_readIvpn(t *testing.T) { //nolint:gocognit
hostnames: sliceStringCall{call: true}, hostnames: sliceStringCall{call: true},
protocol: singleStringCall{call: true}, protocol: singleStringCall{call: true},
ovpnPort: portCall{getCall: true, getValue: "0"}, ovpnPort: portCall{getCall: true, getValue: "0"},
ovpnOldPort: portCall{getCall: true, getValue: "0"},
wgPort: portCall{getCall: true, getValue: "0"}, wgPort: portCall{getCall: true, getValue: "0"},
wgOldPort: portCall{getCall: true, getValue: "0"}, wgOldPort: portCall{getCall: true, getValue: "0"},
settings: Provider{ settings: Provider{
@@ -218,13 +221,21 @@ func Test_Provider_readIvpn(t *testing.T) { //nolint:gocognit
Return(testCase.protocol.value, testCase.protocol.err) Return(testCase.protocol.value, testCase.protocol.err)
} }
if testCase.ovpnPort.getCall { if testCase.ovpnPort.getCall {
env.EXPECT().Get("PORT", gomock.Any()). env.EXPECT().Get("OPENVPN_PORT", gomock.Any()).
Return(testCase.ovpnPort.getValue, testCase.ovpnPort.getErr) Return(testCase.ovpnPort.getValue, testCase.ovpnPort.getErr)
} }
if testCase.ovpnPort.portCall { if testCase.ovpnPort.portCall {
env.EXPECT().Port("PORT"). env.EXPECT().Port("OPENVPN_PORT").
Return(testCase.ovpnPort.portValue, testCase.ovpnPort.portErr) Return(testCase.ovpnPort.portValue, testCase.ovpnPort.portErr)
} }
if testCase.ovpnOldPort.getCall {
env.EXPECT().Get("PORT", gomock.Any()).
Return(testCase.ovpnOldPort.getValue, testCase.ovpnOldPort.getErr)
}
if testCase.ovpnOldPort.portCall {
env.EXPECT().Port("PORT").
Return(testCase.ovpnOldPort.portValue, testCase.ovpnOldPort.portErr)
}
if testCase.wgPort.getCall { if testCase.wgPort.getCall {
env.EXPECT().Get("WIREGUARD_ENDPOINT_PORT", gomock.Any()). env.EXPECT().Get("WIREGUARD_ENDPOINT_PORT", gomock.Any()).
Return(testCase.wgPort.getValue, testCase.wgPort.getErr) Return(testCase.wgPort.getValue, testCase.wgPort.getErr)

View File

@@ -55,8 +55,11 @@ func (settings *OpenVPNSelection) readMullvad(r reader) (err error) {
return err return err
} }
settings.CustomPort, err = readOpenVPNCustomPort(r.env, settings.TCP, settings.CustomPort, err = readOpenVPNCustomPort(r, openvpnPortValidation{
[]uint16{80, 443, 1401}, []uint16{53, 1194, 1195, 1196, 1197, 1300, 1301, 1302, 1303, 1400}) tcp: settings.TCP,
allowedTCP: []uint16{80, 443, 1401},
allowedUDP: []uint16{53, 1194, 1195, 1196, 1197, 1300, 1301, 1302, 1303, 1400},
})
if err != nil { if err != nil {
return err return err
} }

View File

@@ -53,9 +53,9 @@ func (settings *OpenVPNSelection) readPrivateInternetAccess(r reader) (err error
return err return err
} }
settings.CustomPort, err = readPortOrZero(r.env, "PORT") settings.CustomPort, err = readOpenVPNCustomPort(r, openvpnPortValidation{allAllowed: true})
if err != nil { if err != nil {
return fmt.Errorf("environment variable PORT: %w", err) return err
} }
return nil return nil

View File

@@ -149,33 +149,49 @@ func readTargetIP(env params.Interface) (targetIP net.IP, err error) {
return targetIP, nil return targetIP, nil
} }
func readOpenVPNCustomPort(env params.Interface, tcp bool, type openvpnPortValidation struct {
allowedTCP, allowedUDP []uint16) (port uint16, err error) { allAllowed bool
port, err = readPortOrZero(env, "PORT") tcp bool
if err != nil { allowedTCP []uint16
return 0, fmt.Errorf("environment variable PORT: %w", err) allowedUDP []uint16
} else if port == 0 {
return 0, nil
} }
if tcp { func readOpenVPNCustomPort(r reader, validation openvpnPortValidation) (
for i := range allowedTCP { port uint16, err error) {
if allowedTCP[i] == port { port, err = readPortOrZero(r.env, "OPENVPN_PORT")
if err != nil {
return 0, fmt.Errorf("environment variable OPENVPN_PORT: %w", err)
} else if port == 0 {
// Try using old variable name
port, err = readPortOrZero(r.env, "PORT")
if err != nil {
r.onRetroActive("PORT", "OPENVPN_PORT")
return 0, fmt.Errorf("environment variable PORT: %w", err)
}
}
if port == 0 || validation.allAllowed {
return port, nil
}
if validation.tcp {
for _, allowedPort := range validation.allowedTCP {
if port == allowedPort {
return port, nil return port, nil
} }
} }
return 0, fmt.Errorf( return 0, fmt.Errorf(
"environment variable PORT: %w: port %d for TCP protocol, can only be one of %s", "environment variable PORT: %w: port %d for TCP protocol, can only be one of %s",
ErrInvalidPort, port, portsToString(allowedTCP)) ErrInvalidPort, port, portsToString(validation.allowedTCP))
} }
for i := range allowedUDP { for _, allowedPort := range validation.allowedUDP {
if allowedUDP[i] == port { if port == allowedPort {
return port, nil return port, nil
} }
} }
return 0, fmt.Errorf( return 0, fmt.Errorf(
"environment variable PORT: %w: port %d for UDP protocol, can only be one of %s", "environment variable PORT: %w: port %d for UDP protocol, can only be one of %s",
ErrInvalidPort, port, portsToString(allowedUDP)) ErrInvalidPort, port, portsToString(validation.allowedUDP))
} }
// note: set allowed to an empty slice to allow all valid ports // note: set allowed to an empty slice to allow all valid ports

View File

@@ -142,7 +142,7 @@ func (settings *OpenVPNSelection) readProtocolAndPort(r reader) (err error) {
return err return err
} }
settings.CustomPort, err = readPortOrZero(r.env, "PORT") settings.CustomPort, err = readOpenVPNCustomPort(r, openvpnPortValidation{allAllowed: true})
if err != nil { if err != nil {
return fmt.Errorf("environment variable PORT: %w", err) return fmt.Errorf("environment variable PORT: %w", err)
} }

View File

@@ -46,9 +46,11 @@ func (settings *OpenVPNSelection) readWindscribe(r reader) (err error) {
return err return err
} }
settings.CustomPort, err = readOpenVPNCustomPort(r.env, settings.TCP, settings.CustomPort, err = readOpenVPNCustomPort(r, openvpnPortValidation{
[]uint16{21, 22, 80, 123, 143, 443, 587, 1194, 3306, 8080, 54783}, tcp: settings.TCP,
[]uint16{53, 80, 123, 443, 1194, 54783}) allowedTCP: []uint16{21, 22, 80, 123, 143, 443, 587, 1194, 3306, 8080, 54783},
allowedUDP: []uint16{53, 80, 123, 443, 1194, 54783},
})
if err != nil { if err != nil {
return err return err
} }