Files
gluetun/internal/provider/privateinternetaccess/port.go
Quentin McGaw 7d824a5179 chore(settings): refactor settings processing (#756)
- Better settings tree structure logged using `qdm12/gotree`
- Read settings from environment variables, then files, then secret files
- Settings methods to default them, merge them and override them
- `DNS_PLAINTEXT_ADDRESS` default changed to `127.0.0.1` to use DoT. Warning added if set to something else.
- `HTTPPROXY_LISTENING_ADDRESS` instead of `HTTPPROXY_PORT` (with retro-compatibility)
2022-01-06 06:40:23 -05:00

63 lines
1.4 KiB
Go

package privateinternetaccess
import (
"errors"
"fmt"
"github.com/qdm12/gluetun/internal/configuration/settings"
"github.com/qdm12/gluetun/internal/constants"
)
func getPort(openvpnSelection settings.OpenVPNSelection) (
port uint16, err error) {
customPort := *openvpnSelection.CustomPort
tcp := *openvpnSelection.TCP
if customPort == 0 {
return getDefaultPort(tcp, *openvpnSelection.PIAEncPreset), nil
}
if err := checkPort(customPort, tcp); err != nil {
return 0, err
}
return customPort, nil
}
func getDefaultPort(tcp bool, encryptionPreset string) (port uint16) {
if tcp {
switch encryptionPreset {
case constants.PIAEncryptionPresetNone, constants.PIAEncryptionPresetNormal:
port = 502
case constants.PIAEncryptionPresetStrong:
port = 501
}
} else {
switch encryptionPreset {
case constants.PIAEncryptionPresetNone, constants.PIAEncryptionPresetNormal:
port = 1198
case constants.PIAEncryptionPresetStrong:
port = 1197
}
}
return port
}
var ErrInvalidPort = errors.New("invalid port number")
func checkPort(port uint16, tcp bool) (err error) {
if tcp {
switch port {
case 80, 110, 443: //nolint:gomnd
return nil
default:
return fmt.Errorf("%w: %d for protocol TCP", ErrInvalidPort, port)
}
}
switch port {
case 53, 1194, 1197, 1198, 8080, 9201: //nolint:gomnd
return nil
default:
return fmt.Errorf("%w: %d for protocol UDP", ErrInvalidPort, port)
}
}