Feature: Private Internet Access custom port

This commit is contained in:
Quentin McGaw
2021-01-31 01:27:13 +00:00
parent 8f4354936c
commit 3c7dc9b9ad
5 changed files with 66 additions and 18 deletions

View File

@@ -33,7 +33,7 @@ type ServerSelection struct {
ISPs []string `json:"isps"` ISPs []string `json:"isps"`
Owned bool `json:"owned"` Owned bool `json:"owned"`
// Mullvad, Windscribe // Mullvad, Windscribe, PIA
CustomPort uint16 `json:"custom_port"` CustomPort uint16 `json:"custom_port"`
// NordVPN // NordVPN
@@ -92,6 +92,7 @@ func (p *ProviderSettings) String() string {
"Regions: "+commaJoin(p.ServerSelection.Regions), "Regions: "+commaJoin(p.ServerSelection.Regions),
"Encryption preset: "+p.ExtraConfigOptions.EncryptionPreset, "Encryption preset: "+p.ExtraConfigOptions.EncryptionPreset,
"Port forwarding: "+p.PortForwarding.String(), "Port forwarding: "+p.PortForwarding.String(),
"Custom port: "+customPort,
) )
case "mullvad": case "mullvad":
settingsList = append(settingsList, settingsList = append(settingsList,

View File

@@ -62,6 +62,7 @@ type Reader interface {
GetPortForwardingStatusFilepath() (filepath models.Filepath, err error) GetPortForwardingStatusFilepath() (filepath models.Filepath, err error)
GetPIAEncryptionPreset() (preset string, err error) GetPIAEncryptionPreset() (preset string, err error)
GetPIARegions() (regions []string, err error) GetPIARegions() (regions []string, err error)
GetPIAPort() (port uint16, err error)
// Mullvad getters // Mullvad getters
GetMullvadCountries() (countries []string, err error) GetMullvadCountries() (countries []string, err error)

View File

@@ -63,3 +63,10 @@ func (r *reader) GetPIAEncryptionPreset() (preset string, err error) {
func (r *reader) GetPIARegions() (regions []string, err error) { func (r *reader) GetPIARegions() (regions []string, err error) {
return r.env.CSVInside("REGION", constants.PIAGeoChoices()) return r.env.CSVInside("REGION", constants.PIAGeoChoices())
} }
// GetPIAPort obtains the port to reach the PIA server on from the
// environment variable PORT.
func (r *reader) GetPIAPort() (port uint16, err error) {
n, err := r.env.IntRange("PORT", 0, 65535, libparams.Default("0"))
return uint16(n), err
}

View File

@@ -6,6 +6,7 @@ import (
"crypto/x509" "crypto/x509"
"encoding/base64" "encoding/base64"
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"math/rand" "math/rand"
@@ -40,9 +41,12 @@ func newPrivateInternetAccess(servers []models.PIAServer, timeNow timeNowFunc) *
} }
} }
func (p *pia) GetOpenVPNConnection(selection models.ServerSelection) ( var (
connection models.OpenVPNConnection, err error) { ErrInvalidPort = errors.New("invalid port number")
var port uint16 )
func (p *pia) getPort(selection models.ServerSelection) (port uint16, err error) {
if selection.CustomPort == 0 {
switch selection.Protocol { switch selection.Protocol {
case constants.TCP: case constants.TCP:
switch selection.EncryptionPreset { switch selection.EncryptionPreset {
@@ -59,10 +63,41 @@ func (p *pia) GetOpenVPNConnection(selection models.ServerSelection) (
port = 1197 port = 1197
} }
} }
if port == 0 { if port == 0 {
return connection, fmt.Errorf( return 0, fmt.Errorf(
"combination of protocol %q and encryption %q does not yield any port number", "%w: combination of protocol %q and encryption %q does not yield any port number",
selection.Protocol, selection.EncryptionPreset) ErrInvalidPort, selection.Protocol, selection.EncryptionPreset)
}
return port, nil
}
port = selection.CustomPort
switch selection.Protocol {
case constants.TCP:
switch port {
case 80, 110, 443: //nolint:gomnd
default:
return 0, fmt.Errorf("%w: %d for protocol %s",
ErrInvalidPort, port, selection.Protocol)
}
case constants.UDP:
switch port {
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
}
func (p *pia) GetOpenVPNConnection(selection models.ServerSelection) (
connection models.OpenVPNConnection, err error) {
port, err := p.getPort(selection)
if err != nil {
return connection, err
} }
servers := p.servers servers := p.servers

View File

@@ -29,6 +29,10 @@ func GetPIASettings(paramsReader params.Reader) (settings models.ProviderSetting
if err != nil { if err != nil {
return settings, err return settings, err
} }
settings.ServerSelection.CustomPort, err = paramsReader.GetPIAPort()
if err != nil {
return settings, err
}
settings.PortForwarding.Enabled, err = paramsReader.GetPortForwarding() settings.PortForwarding.Enabled, err = paramsReader.GetPortForwarding()
if err != nil { if err != nil {
return settings, err return settings, err