chore(env): getEnvWithRetro helper function

This commit is contained in:
Quentin McGaw
2022-01-29 14:55:56 +00:00
parent e7e4cfca4c
commit 0d8cb66d43
13 changed files with 113 additions and 300 deletions

View File

@@ -5,6 +5,7 @@ import (
"fmt"
"github.com/qdm12/gluetun/internal/configuration/settings"
"github.com/qdm12/govalid/binary"
"inet.af/netaddr"
)
@@ -36,23 +37,18 @@ func (r *Reader) readDNSBlacklist() (blacklist settings.DNSBlacklist, err error)
}
func (r *Reader) readBlockSurveillance() (blocked *bool, err error) {
blocked, err = envToBoolPtr("BLOCK_NSA")
if err != nil {
r.onRetroActive("BLOCK_NSA", "BLOCK_SURVEILLANCE")
return nil, fmt.Errorf("environment variable BLOCK_NSA: %w", err)
} else if blocked != nil {
r.onRetroActive("BLOCK_NSA", "BLOCK_SURVEILLANCE")
return blocked, nil
key, value := r.getEnvWithRetro("BLOCK_SURVEILLANCE", "BLOCK_NSA")
if value == "" {
return nil, nil //nolint:nilnil
}
blocked, err = envToBoolPtr("BLOCK_SURVEILLANCE")
blocked = new(bool)
*blocked, err = binary.Validate(key)
if err != nil {
return nil, fmt.Errorf("environment variable BLOCK_SURVEILLANCE: %w", err)
}
return blocked, nil
return nil, fmt.Errorf("environment variable %s: %w", key, err)
}
return nil, nil //nolint:nilnil
return blocked, nil
}
var (

View File

@@ -22,16 +22,8 @@ func (r *Reader) readFirewall() (firewall settings.Firewall, err error) {
return firewall, fmt.Errorf("environment variable FIREWALL_INPUT_PORTS: %w", err)
}
outboundSubnetsKey := "FIREWALL_OUTBOUND_SUBNETS"
outboundSubnetsKey, _ := r.getEnvWithRetro("FIREWALL_OUTBOUND_SUBNETS", "EXTRA_SUBNETS")
outboundSubnetStrings := envToCSV(outboundSubnetsKey)
if len(outboundSubnetStrings) == 0 {
// Retro-compatibility
outboundSubnetStrings = envToCSV("EXTRA_SUBNETS")
if len(outboundSubnetStrings) > 0 {
outboundSubnetsKey = "EXTRA_SUBNETS"
r.onRetroActive("EXTRA_SUBNETS", "FIREWALL_OUTBOUND_SUBNETS")
}
}
firewall.OutboundSubnets, err = stringsToIPNets(outboundSubnetStrings)
if err != nil {
return firewall, fmt.Errorf("environment variable %s: %w", outboundSubnetsKey, err)

View File

@@ -30,15 +30,9 @@ func (r *Reader) ReadHealth() (health settings.Health, err error) {
}
func (r *Reader) readDurationWithRetro(envKey, retroEnvKey string) (d *time.Duration, err error) {
s := os.Getenv(retroEnvKey)
envKey, s := r.getEnvWithRetro(envKey, retroEnvKey)
if s == "" {
s = os.Getenv(envKey)
if s == "" {
return nil, nil //nolint:nilnil
}
} else {
r.onRetroActive(envKey, retroEnvKey)
envKey = retroEnvKey
return nil, nil //nolint:nilnil
}
d = new(time.Duration)

View File

@@ -2,8 +2,6 @@ package env
import (
"fmt"
"os"
"strings"
"github.com/qdm12/gluetun/internal/configuration/settings"
"github.com/qdm12/govalid/binary"
@@ -33,138 +31,61 @@ func (r *Reader) readHTTPProxy() (httpProxy settings.HTTPProxy, err error) {
}
func (r *Reader) readHTTProxyUser() (user *string) {
s := os.Getenv("HTTPPROXY_USER")
_, s := r.getEnvWithRetro("HTTPPROXY_USER", "PROXY_USER", "TINYPROXY_USER")
if s != "" {
return &s
}
// Retro-compatibility
s = os.Getenv("TINYPROXY_USER")
if s != "" {
r.onRetroActive("TINYPROXY_USER", "HTTPPROXY_USER")
return &s
}
// Retro-compatibility
s = os.Getenv("PROXY_USER")
if s != "" {
r.onRetroActive("PROXY_USER", "HTTPPROXY_USER")
return &s
}
return nil
}
func (r *Reader) readHTTProxyPassword() (user *string) {
s := os.Getenv("HTTPPROXY_PASSWORD")
_, s := r.getEnvWithRetro("HTTPPROXY_PASSWORD", "PROXY_PASSWORD", "TINYPROXY_PASSWORD")
if s != "" {
return &s
}
// Retro-compatibility
s = os.Getenv("TINYPROXY_PASSWORD")
if s != "" {
r.onRetroActive("TINYPROXY_PASSWORD", "HTTPPROXY_PASSWORD")
return &s
}
// Retro-compatibility
s = os.Getenv("PROXY_PASSWORD")
if s != "" {
r.onRetroActive("PROXY_PASSWORD", "HTTPPROXY_PASSWORD")
return &s
}
return nil
}
func (r *Reader) readHTTProxyListeningAddress() (listeningAddress string) {
// Retro-compatibility
retroKeys := []string{"PROXY_PORT", "TINYPROXY_PORT", "HTTPPROXY_PORT"}
for _, retroKey := range retroKeys {
s := os.Getenv(retroKey)
if s != "" {
r.onRetroActive(retroKey, "HTTPPROXY_LISTENING_ADDRESS")
return ":" + s
}
key, value := r.getEnvWithRetro("HTTPPROXY_LISTENING_ADDRESS", "PROXY_PORT", "TINYPROXY_PORT", "HTTPPROXY_PORT")
if key == "HTTPPROXY_LISTENING_ADDRESS" {
return value
}
return os.Getenv("HTTPPROXY_LISTENING_ADDRESS")
return ":" + value
}
func (r *Reader) readHTTProxyEnabled() (enabled *bool, err error) {
// Retro-compatibility
s := strings.ToLower(os.Getenv("PROXY"))
if s != "" {
r.onRetroActive("PROXY", "HTTPPROXY")
enabled = new(bool)
*enabled, err = binary.Validate(s)
if err != nil {
return nil, fmt.Errorf("environment variable PROXY: %w", err)
}
return enabled, nil
key, s := r.getEnvWithRetro("HTTPPROXY", "PROXY", "TINYPROXY")
if s == "" {
return nil, nil //nolint:nilnil
}
// Retro-compatibility
s = strings.ToLower(os.Getenv("TINYPROXY"))
if s != "" {
r.onRetroActive("TINYPROXY", "HTTPPROXY")
enabled = new(bool)
*enabled, err = binary.Validate(s)
if err != nil {
return nil, fmt.Errorf("environment variable TINYPROXY: %w", err)
}
return enabled, nil
enabled = new(bool)
*enabled, err = binary.Validate(s)
if err != nil {
return nil, fmt.Errorf("environment variable %s: %w", key, err)
}
s = strings.ToLower(os.Getenv("HTTPPROXY"))
if s != "" {
enabled = new(bool)
*enabled, err = binary.Validate(s)
if err != nil {
return nil, fmt.Errorf("environment variable HTTPPROXY: %w", err)
}
return enabled, nil
}
return nil, nil //nolint:nilnil
return enabled, nil
}
func (r *Reader) readHTTProxyLog() (enabled *bool, err error) {
// Retro-compatibility
retroOption := binary.OptionEnabled("on", "info", "connect", "notice")
s := strings.ToLower(os.Getenv("PROXY_LOG_LEVEL"))
if s != "" {
r.onRetroActive("PROXY_LOG_LEVEL", "HTTPPROXY_LOG")
enabled = new(bool)
*enabled, err = binary.Validate(s, retroOption)
if err != nil {
return nil, fmt.Errorf("environment variable PROXY_LOG_LEVEL: %w", err)
}
return enabled, nil
key, s := r.getEnvWithRetro("HTTPPROXY_LOG", "PROXY_LOG_LEVEL", "TINYPROXY_LOG")
if s == "" {
return nil, nil //nolint:nilnil
}
// Retro-compatibility
s = strings.ToLower(os.Getenv("TINYPROXY_LOG"))
if s != "" {
r.onRetroActive("TINYPROXY_LOG", "HTTPPROXY_LOG")
enabled = new(bool)
*enabled, err = binary.Validate(s, retroOption)
if err != nil {
return nil, fmt.Errorf("environment variable TINYPROXY_LOG: %w", err)
}
return enabled, nil
var binaryOptions []binary.Option
if key != "HTTPROXY_LOG" {
retroOption := binary.OptionEnabled("on", "info", "connect", "notice")
binaryOptions = append(binaryOptions, retroOption)
}
s = strings.ToLower(os.Getenv("HTTPPROXY_LOG"))
if s != "" {
enabled = new(bool)
*enabled, err = binary.Validate(s)
if err != nil {
return nil, fmt.Errorf("environment variable HTTPPROXY_LOG: %w", err)
}
return enabled, nil
enabled = new(bool)
*enabled, err = binary.Validate(s, binaryOptions...)
if err != nil {
return nil, fmt.Errorf("environment variable %s: %w", key, err)
}
return nil, nil //nolint:nilnil
return enabled, nil
}

View File

@@ -6,6 +6,7 @@ import (
"strings"
"github.com/qdm12/gluetun/internal/configuration/settings"
"github.com/qdm12/govalid/binary"
)
func (r *Reader) readOpenVPN() (
@@ -66,29 +67,13 @@ func (r *Reader) readOpenVPN() (
}
func (r *Reader) readOpenVPNUser() (user string) {
user = os.Getenv("OPENVPN_USER")
if user == "" {
// Retro-compatibility
user = os.Getenv("USER")
if user != "" {
r.onRetroActive("USER", "OPENVPN_USER")
}
}
_, user = r.getEnvWithRetro("OPENVPN_USER", "USER")
// Remove spaces in user ID to simplify user's life, thanks @JeordyR
return strings.ReplaceAll(user, " ", "")
}
func (r *Reader) readOpenVPNPassword() (password string) {
password = os.Getenv("OPENVPN_PASSWORD")
if password != "" {
return password
}
// Retro-compatibility
password = os.Getenv("PASSWORD")
if password != "" {
r.onRetroActive("PASSWORD", "OPENVPN_PASSWORD")
}
_, password = r.getEnvWithRetro("OPENVPN_PASSWORD", "PASSWORD")
return password
}
@@ -107,35 +92,30 @@ func readBase64OrNil(envKey string) (valueOrNil *string, err error) {
}
func (r *Reader) readPIAEncryptionPreset() (presetPtr *string) {
preset := strings.ToLower(os.Getenv("PIA_ENCRYPTION"))
_, preset := r.getEnvWithRetro("PIA_ENCRYPTION", "ENCRYPTION")
if preset != "" {
return &preset
}
// Retro-compatibility
preset = strings.ToLower(os.Getenv("ENCRYPTION"))
if preset != "" {
r.onRetroActive("ENCRYPTION", "PIA_ENCRYPTION")
return &preset
}
return nil
}
func (r *Reader) readOpenVPNProcessUser() (processUser string, err error) {
// Retro-compatibility
root, err := envToBoolPtr("OPENVPN_ROOT")
if err != nil {
r.onRetroActive("OPENVPN_ROOT", "OPENVPN_PROCESS_USER")
return "", fmt.Errorf("environment variable OPENVPN_ROOT: %w", err)
} else if root != nil {
r.onRetroActive("OPENVPN_ROOT", "OPENVPN_PROCESS_USER")
if *root {
return "root", nil
}
const defaultNonRootUser = "nonrootuser"
return defaultNonRootUser, nil
key, value := r.getEnvWithRetro("OPENVPN_PROCESS_USER", "OPENVPN_ROOT")
if key == "OPENVPN_PROCESS_USER" {
return value, nil
}
return os.Getenv("OPENVPN_PROCESS_USER"), nil
// Retro-compatibility
if value == "" {
return "", nil
}
root, err := binary.Validate(value)
if err != nil {
return "", fmt.Errorf("environment variable %s: %w", key, err)
}
if root {
return "root", nil
}
const defaultNonRootUser = "nonrootuser"
return defaultNonRootUser, nil
}

View File

@@ -4,7 +4,6 @@ import (
"errors"
"fmt"
"os"
"strings"
"github.com/qdm12/gluetun/internal/configuration/settings"
"github.com/qdm12/gluetun/internal/constants"
@@ -36,17 +35,7 @@ func (r *Reader) readOpenVPNSelection() (
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")
}
envKey, protocol := r.getEnvWithRetro("OPENVPN_PROTOCOL", "PROTOCOL")
switch protocol {
case "":
@@ -62,23 +51,9 @@ func (r *Reader) readOpenVPNProtocol() (tcp *bool, err error) {
}
func (r *Reader) readOpenVPNCustomPort() (customPort *uint16, err error) {
const currentKey = "VPN_ENDPOINT_PORT"
key := "PORT"
s := os.Getenv(key) // Retro-compatibility
key, s := r.getEnvWithRetro("VPN_ENDPOINT_PORT", "PORT", "OPENVPN_PORT")
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)
return nil, nil //nolint:nilnil
}
customPort = new(uint16)

View File

@@ -35,17 +35,9 @@ func readPublicIPPeriod() (period *time.Duration, err error) {
}
func (r *Reader) readPublicIPFilepath() (filepath *string) {
// Retro-compatibility
s := os.Getenv("IP_STATUS_FILE")
if s != "" {
r.onRetroActive("IP_STATUS_FILE", "PUBLICIP_FILE")
return &s
}
s = os.Getenv("PUBLICIP_FILE")
_, s := r.getEnvWithRetro("PUBLICIP_FILE", "IP_STATUS_FILE")
if s != "" {
return &s
}
return nil
}

View File

@@ -1,6 +1,8 @@
package env
import (
"os"
"github.com/qdm12/gluetun/internal/configuration/settings"
"github.com/qdm12/gluetun/internal/configuration/sources"
)
@@ -95,3 +97,24 @@ func (r *Reader) onRetroActive(oldKey, newKey string) {
"You are using the old environment variable " + oldKey +
", please consider changing it to " + newKey)
}
// getEnvWithRetro returns the first environment variable
// key and corresponding non empty value from the environment
// variable keys given. It first goes through the retroKeys
// and end on returning the value corresponding to the currentKey.
// Note retroKeys should be in order from oldest to most
// recent retro-compatibility key.
func (r *Reader) getEnvWithRetro(currentKey string,
retroKeys ...string) (key, value string) {
// We check retro-compatibility keys first since
// the current key might be set in the Dockerfile.
for _, key = range retroKeys {
value = os.Getenv(key)
if value != "" {
r.onRetroActive(key, currentKey)
return key, value
}
}
return currentKey, os.Getenv(currentKey)
}

View File

@@ -34,18 +34,16 @@ func readControlServerLog() (enabled *bool, err error) {
}
func (r *Reader) readControlServerAddress() (address *string) {
// Retro-compatibility
s := os.Getenv("HTTP_CONTROL_SERVER_PORT")
if s != "" {
r.onRetroActive("HTTP_CONTROL_SERVER_PORT", "HTTP_CONTROL_SERVER_ADDRESS")
address = new(string)
*address = ":" + s
return address
}
s = os.Getenv("HTTP_CONTROL_SERVER_ADDRESS")
key, s := r.getEnvWithRetro("HTTP_CONTROL_SERVER_ADDRESS", "HTTP_CONTROL_SERVER_PORT")
if s == "" {
return nil
}
return &s
if key == "HTTP_CONTROL_SERVER_ADDRESS" {
return &s
}
address = new(string)
*address = ":" + s
return address
}

View File

@@ -30,7 +30,7 @@ func (r *Reader) readServerSelection(vpnProvider, vpnType string) (
// Retro-compatibility
countriesCSV = os.Getenv("REGION")
if countriesCSV != "" {
r.onRetroActive("REGION", "COUNTRY")
r.onRetroActive("REGION", "COUNTRY")
}
}
if countriesCSV != "" {
@@ -102,18 +102,7 @@ var (
)
func (r *Reader) readOpenVPNTargetIP() (ip net.IP, err error) {
envKey := "OPENVPN_TARGET_IP"
s := os.Getenv(envKey) // Retro-compatibility
if s == "" {
envKey = "VPN_ENDPOINT_IP"
s = os.Getenv(envKey)
if s == "" {
return nil, nil
}
} else {
r.onRetroActive("OPENVPN_TARGET_IP", "VPN_ENDPOINT_IP")
}
envKey, s := r.getEnvWithRetro("VPN_ENDPOINT_IP", "OPENVPN_TARGET_IP")
ip = net.ParseIP(s)
if ip == nil {
return nil, fmt.Errorf("environment variable %s: %w: %s",
@@ -124,19 +113,10 @@ func (r *Reader) readOpenVPNTargetIP() (ip net.IP, err error) {
}
func (r *Reader) readOwnedOnly() (ownedOnly *bool, err error) {
// Retro-compatibility
ownedOnly, err = envToBoolPtr("OWNED")
envKey, _ := r.getEnvWithRetro("OWNED_ONLY", "OWNED")
ownedOnly, err = envToBoolPtr(envKey)
if err != nil {
r.onRetroActive("OWNED", "OWNED_ONLY")
return nil, fmt.Errorf("environment variable OWNED: %w", err)
} else if ownedOnly != nil {
r.onRetroActive("OWNED", "OWNED_ONLY")
return ownedOnly, nil
}
ownedOnly, err = envToBoolPtr("OWNED_ONLY")
if err != nil {
return nil, fmt.Errorf("environment variable OWNED_ONLY: %w", err)
return nil, fmt.Errorf("environment variable %s: %w", envKey, err)
}
return ownedOnly, nil
}

View File

@@ -2,7 +2,6 @@ package env
import (
"fmt"
"os"
"github.com/qdm12/gluetun/internal/configuration/settings"
)
@@ -25,23 +24,20 @@ func (r *Reader) readShadowsocks() (shadowsocks settings.Shadowsocks, err error)
}
func (r *Reader) readShadowsocksAddress() (address string) {
// Retro-compatibility
portString := os.Getenv("SHADOWSOCKS_PORT")
if portString != "" {
r.onRetroActive("SHADOWSOCKS_PORT", "SHADOWSOCKS_LISTENING_ADDRESS")
return ":" + portString
key, value := r.getEnvWithRetro("SHADOWSOCKS_LISTENING_ADDRESS", "SHADOWSOCKS_PORT")
if value == "" {
return ""
}
return os.Getenv("SHADOWSOCKS_LISTENING_ADDRESS")
if key == "SHADOWSOCKS_LISTENING_ADDRESS" {
return value
}
// Retro-compatibility
return ":" + value
}
func (r *Reader) readShadowsocksCipher() (cipher string) {
// Retro-compatibility
cipher = os.Getenv("SHADOWSOCKS_METHOD")
if cipher != "" {
r.onRetroActive("SHADOWSOCKS_METHOD", "SHADOWSOCKS_CIPHER")
_, cipher = r.getEnvWithRetro("SHADOWSOCKS_CIPHER", "SHADOWSOCKS_METHOD")
return cipher
}
return os.Getenv("SHADOWSOCKS_CIPHER")
}

View File

@@ -35,17 +35,7 @@ var ErrSystemIDNotValid = errors.New("system ID is not valid")
func (r *Reader) readID(key, retroKey string) (
id *uint16, err error) {
idEnvKey := key
idString := os.Getenv(key)
if idString == "" {
// retro-compatibility
idString = os.Getenv(retroKey)
if idString != "" {
idEnvKey = retroKey
r.onRetroActive(retroKey, key)
}
}
idEnvKey, idString := r.getEnvWithRetro(key, retroKey)
if idString == "" {
return nil, nil //nolint:nilnil
}

View File

@@ -30,19 +30,9 @@ func (r *Reader) readWireguardSelection() (
var ErrIPAddressParse = errors.New("cannot parse IP address")
func (r *Reader) readWireguardEndpointIP() (endpointIP net.IP, err error) {
const currentKey = "VPN_ENDPOINT_IP"
key := "WIREGUARD_ENDPOINT_IP"
s := os.Getenv(key) // Retro-compatibility
key, s := r.getEnvWithRetro("VPN_ENDPOINT_IP", "WIREGUARD_ENDPOINT_IP")
if s == "" {
key = currentKey
s = os.Getenv(key)
if s == "" {
return nil, nil
}
}
if key != currentKey {
r.onRetroActive(key, currentKey)
return nil, nil
}
endpointIP = net.ParseIP(s)
@@ -55,23 +45,9 @@ func (r *Reader) readWireguardEndpointIP() (endpointIP net.IP, err error) {
}
func (r *Reader) readWireguardCustomPort() (customPort *uint16, err error) {
const currentKey = "VPN_ENDPOINT_PORT"
key := "WIREGUARD_PORT" // Retro-compatibility
s := os.Getenv(key)
key, s := r.getEnvWithRetro("VPN_ENDPOINT_PORT", "WIREGUARD_ENDPOINT_PORT")
if s == "" {
key = "WIREGUARD_ENDPOINT_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)
return nil, nil //nolint:nilnil
}
customPort = new(uint16)