Files
gluetun/internal/configuration/sources/env/openvpnselection.go
Quentin McGaw e7e4cfca4c fix(env): Retro-compatible precedence order for variables with defaults set in Dockerfile
- `BLOCK_NSA` has precedence over `BLOCK_SURVEILLANCE`
- `HEALTH_OPENVPN_DURATION_ADDITION` has precedence over `HEALTH_VPN_DURATION_ADDITION`
- `HEALTH_OPENVPN_DURATION_INITIAL` has precendence over `HEALTH_VPN_DURATION_INITIAL`
- Chain of precedence: `PROXY` > `TINYPROXY` > `HTTPPROXY`
- Chain of precedence: `PROXY_LOG_LEVEL` > `TINYPROXY_LOG` > `HTTPPROXY_LOG`
- `PROTOCOL` has precendence over `OPENVPN_PROTOCOL`
- `IP_STATUS_FILE` has precendence over `PUBLICIP_FILE`
- `SHADOWSOCKS_PORT` has precedence over `SHADOWSOCKS_LISTENING_ADDRESS`
- `SHADOWSOCKS_METHOD` has precedence over `SHADOWSOCKS_CIPHER`
2022-02-06 19:59:07 +00:00

92 lines
2.0 KiB
Go

package env
import (
"errors"
"fmt"
"os"
"strings"
"github.com/qdm12/gluetun/internal/configuration/settings"
"github.com/qdm12/gluetun/internal/constants"
"github.com/qdm12/govalid/port"
)
func (r *Reader) readOpenVPNSelection() (
selection settings.OpenVPNSelection, err error) {
confFile := os.Getenv("OPENVPN_CUSTOM_CONFIG")
if confFile != "" {
selection.ConfFile = &confFile
}
selection.TCP, err = r.readOpenVPNProtocol()
if err != nil {
return selection, err
}
selection.CustomPort, err = r.readOpenVPNCustomPort()
if err != nil {
return selection, err
}
selection.PIAEncPreset = r.readPIAEncryptionPreset()
return selection, nil
}
var ErrOpenVPNProtocolNotValid = errors.New("OpenVPN protocol is not valid")
func (r *Reader) readOpenVPNProtocol() (tcp *bool, err error) {
// Retro-compatibility
envKey := "PROTOCOL"
protocol := strings.ToLower(os.Getenv("PROTOCOL"))
if protocol == "" {
protocol = strings.ToLower(os.Getenv("OPENVPN_PROTOCOL"))
if protocol != "" {
envKey = "OPENVPN_PROTOCOL"
}
} else {
r.onRetroActive("PROTOCOL", "OPENVPN_PROTOCOL")
}
switch protocol {
case "":
return nil, nil //nolint:nilnil
case constants.UDP:
return boolPtr(false), nil
case constants.TCP:
return boolPtr(true), nil
default:
return nil, fmt.Errorf("environment variable %s: %w: %s",
envKey, ErrOpenVPNProtocolNotValid, protocol)
}
}
func (r *Reader) readOpenVPNCustomPort() (customPort *uint16, err error) {
const currentKey = "VPN_ENDPOINT_PORT"
key := "PORT"
s := os.Getenv(key) // Retro-compatibility
if s == "" {
key = "OPENVPN_PORT" // Retro-compatibility
s = os.Getenv(key)
if s == "" {
key = currentKey
s = os.Getenv(key)
if s == "" {
return nil, nil //nolint:nilnil
}
}
}
if key != currentKey {
r.onRetroActive(key, currentKey)
}
customPort = new(uint16)
*customPort, err = port.Validate(s)
if err != nil {
return nil, fmt.Errorf("environment variable %s: %w", key, err)
}
return customPort, nil
}