chore(env): getEnvWithRetro helper function
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/qdm12/gluetun/internal/configuration/settings"
|
"github.com/qdm12/gluetun/internal/configuration/settings"
|
||||||
|
"github.com/qdm12/govalid/binary"
|
||||||
"inet.af/netaddr"
|
"inet.af/netaddr"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -36,23 +37,18 @@ func (r *Reader) readDNSBlacklist() (blacklist settings.DNSBlacklist, err error)
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (r *Reader) readBlockSurveillance() (blocked *bool, err error) {
|
func (r *Reader) readBlockSurveillance() (blocked *bool, err error) {
|
||||||
blocked, err = envToBoolPtr("BLOCK_NSA")
|
key, value := r.getEnvWithRetro("BLOCK_SURVEILLANCE", "BLOCK_NSA")
|
||||||
if err != nil {
|
if value == "" {
|
||||||
r.onRetroActive("BLOCK_NSA", "BLOCK_SURVEILLANCE")
|
return nil, nil //nolint:nilnil
|
||||||
return nil, fmt.Errorf("environment variable BLOCK_NSA: %w", err)
|
|
||||||
} else if blocked != nil {
|
|
||||||
r.onRetroActive("BLOCK_NSA", "BLOCK_SURVEILLANCE")
|
|
||||||
return blocked, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
blocked, err = envToBoolPtr("BLOCK_SURVEILLANCE")
|
blocked = new(bool)
|
||||||
|
*blocked, err = binary.Validate(key)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("environment variable BLOCK_SURVEILLANCE: %w", err)
|
return nil, fmt.Errorf("environment variable %s: %w", key, err)
|
||||||
}
|
|
||||||
return blocked, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil, nil //nolint:nilnil
|
return blocked, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
|||||||
10
internal/configuration/sources/env/firewall.go
vendored
10
internal/configuration/sources/env/firewall.go
vendored
@@ -22,16 +22,8 @@ func (r *Reader) readFirewall() (firewall settings.Firewall, err error) {
|
|||||||
return firewall, fmt.Errorf("environment variable FIREWALL_INPUT_PORTS: %w", err)
|
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)
|
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)
|
firewall.OutboundSubnets, err = stringsToIPNets(outboundSubnetStrings)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return firewall, fmt.Errorf("environment variable %s: %w", outboundSubnetsKey, err)
|
return firewall, fmt.Errorf("environment variable %s: %w", outboundSubnetsKey, err)
|
||||||
|
|||||||
10
internal/configuration/sources/env/health.go
vendored
10
internal/configuration/sources/env/health.go
vendored
@@ -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) {
|
func (r *Reader) readDurationWithRetro(envKey, retroEnvKey string) (d *time.Duration, err error) {
|
||||||
s := os.Getenv(retroEnvKey)
|
envKey, s := r.getEnvWithRetro(envKey, retroEnvKey)
|
||||||
if s == "" {
|
if s == "" {
|
||||||
s = os.Getenv(envKey)
|
return nil, nil //nolint:nilnil
|
||||||
if s == "" {
|
|
||||||
return nil, nil //nolint:nilnil
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
r.onRetroActive(envKey, retroEnvKey)
|
|
||||||
envKey = retroEnvKey
|
|
||||||
}
|
}
|
||||||
|
|
||||||
d = new(time.Duration)
|
d = new(time.Duration)
|
||||||
|
|||||||
131
internal/configuration/sources/env/httproxy.go
vendored
131
internal/configuration/sources/env/httproxy.go
vendored
@@ -2,8 +2,6 @@ package env
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/qdm12/gluetun/internal/configuration/settings"
|
"github.com/qdm12/gluetun/internal/configuration/settings"
|
||||||
"github.com/qdm12/govalid/binary"
|
"github.com/qdm12/govalid/binary"
|
||||||
@@ -33,138 +31,61 @@ func (r *Reader) readHTTPProxy() (httpProxy settings.HTTPProxy, err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (r *Reader) readHTTProxyUser() (user *string) {
|
func (r *Reader) readHTTProxyUser() (user *string) {
|
||||||
s := os.Getenv("HTTPPROXY_USER")
|
_, s := r.getEnvWithRetro("HTTPPROXY_USER", "PROXY_USER", "TINYPROXY_USER")
|
||||||
if s != "" {
|
if s != "" {
|
||||||
return &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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Reader) readHTTProxyPassword() (user *string) {
|
func (r *Reader) readHTTProxyPassword() (user *string) {
|
||||||
s := os.Getenv("HTTPPROXY_PASSWORD")
|
_, s := r.getEnvWithRetro("HTTPPROXY_PASSWORD", "PROXY_PASSWORD", "TINYPROXY_PASSWORD")
|
||||||
if s != "" {
|
if s != "" {
|
||||||
return &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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Reader) readHTTProxyListeningAddress() (listeningAddress string) {
|
func (r *Reader) readHTTProxyListeningAddress() (listeningAddress string) {
|
||||||
// Retro-compatibility
|
key, value := r.getEnvWithRetro("HTTPPROXY_LISTENING_ADDRESS", "PROXY_PORT", "TINYPROXY_PORT", "HTTPPROXY_PORT")
|
||||||
retroKeys := []string{"PROXY_PORT", "TINYPROXY_PORT", "HTTPPROXY_PORT"}
|
if key == "HTTPPROXY_LISTENING_ADDRESS" {
|
||||||
for _, retroKey := range retroKeys {
|
return value
|
||||||
s := os.Getenv(retroKey)
|
|
||||||
if s != "" {
|
|
||||||
r.onRetroActive(retroKey, "HTTPPROXY_LISTENING_ADDRESS")
|
|
||||||
return ":" + s
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
return ":" + value
|
||||||
return os.Getenv("HTTPPROXY_LISTENING_ADDRESS")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Reader) readHTTProxyEnabled() (enabled *bool, err error) {
|
func (r *Reader) readHTTProxyEnabled() (enabled *bool, err error) {
|
||||||
// Retro-compatibility
|
key, s := r.getEnvWithRetro("HTTPPROXY", "PROXY", "TINYPROXY")
|
||||||
s := strings.ToLower(os.Getenv("PROXY"))
|
if s == "" {
|
||||||
if s != "" {
|
return nil, nil //nolint:nilnil
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Retro-compatibility
|
enabled = new(bool)
|
||||||
s = strings.ToLower(os.Getenv("TINYPROXY"))
|
*enabled, err = binary.Validate(s)
|
||||||
if s != "" {
|
if err != nil {
|
||||||
r.onRetroActive("TINYPROXY", "HTTPPROXY")
|
return nil, fmt.Errorf("environment variable %s: %w", key, err)
|
||||||
enabled = new(bool)
|
|
||||||
*enabled, err = binary.Validate(s)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("environment variable TINYPROXY: %w", err)
|
|
||||||
}
|
|
||||||
return enabled, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
s = strings.ToLower(os.Getenv("HTTPPROXY"))
|
return enabled, nil
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Reader) readHTTProxyLog() (enabled *bool, err error) {
|
func (r *Reader) readHTTProxyLog() (enabled *bool, err error) {
|
||||||
// Retro-compatibility
|
key, s := r.getEnvWithRetro("HTTPPROXY_LOG", "PROXY_LOG_LEVEL", "TINYPROXY_LOG")
|
||||||
retroOption := binary.OptionEnabled("on", "info", "connect", "notice")
|
if s == "" {
|
||||||
s := strings.ToLower(os.Getenv("PROXY_LOG_LEVEL"))
|
return nil, nil //nolint:nilnil
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Retro-compatibility
|
var binaryOptions []binary.Option
|
||||||
s = strings.ToLower(os.Getenv("TINYPROXY_LOG"))
|
if key != "HTTPROXY_LOG" {
|
||||||
if s != "" {
|
retroOption := binary.OptionEnabled("on", "info", "connect", "notice")
|
||||||
r.onRetroActive("TINYPROXY_LOG", "HTTPPROXY_LOG")
|
binaryOptions = append(binaryOptions, retroOption)
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
s = strings.ToLower(os.Getenv("HTTPPROXY_LOG"))
|
enabled = new(bool)
|
||||||
if s != "" {
|
*enabled, err = binary.Validate(s, binaryOptions...)
|
||||||
enabled = new(bool)
|
if err != nil {
|
||||||
*enabled, err = binary.Validate(s)
|
return nil, fmt.Errorf("environment variable %s: %w", key, err)
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("environment variable HTTPPROXY_LOG: %w", err)
|
|
||||||
}
|
|
||||||
return enabled, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil, nil //nolint:nilnil
|
return enabled, nil
|
||||||
}
|
}
|
||||||
|
|||||||
60
internal/configuration/sources/env/openvpn.go
vendored
60
internal/configuration/sources/env/openvpn.go
vendored
@@ -6,6 +6,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/qdm12/gluetun/internal/configuration/settings"
|
"github.com/qdm12/gluetun/internal/configuration/settings"
|
||||||
|
"github.com/qdm12/govalid/binary"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (r *Reader) readOpenVPN() (
|
func (r *Reader) readOpenVPN() (
|
||||||
@@ -66,29 +67,13 @@ func (r *Reader) readOpenVPN() (
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (r *Reader) readOpenVPNUser() (user string) {
|
func (r *Reader) readOpenVPNUser() (user string) {
|
||||||
user = os.Getenv("OPENVPN_USER")
|
_, user = r.getEnvWithRetro("OPENVPN_USER", "USER")
|
||||||
if user == "" {
|
|
||||||
// Retro-compatibility
|
|
||||||
user = os.Getenv("USER")
|
|
||||||
if user != "" {
|
|
||||||
r.onRetroActive("USER", "OPENVPN_USER")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Remove spaces in user ID to simplify user's life, thanks @JeordyR
|
// Remove spaces in user ID to simplify user's life, thanks @JeordyR
|
||||||
return strings.ReplaceAll(user, " ", "")
|
return strings.ReplaceAll(user, " ", "")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Reader) readOpenVPNPassword() (password string) {
|
func (r *Reader) readOpenVPNPassword() (password string) {
|
||||||
password = os.Getenv("OPENVPN_PASSWORD")
|
_, password = r.getEnvWithRetro("OPENVPN_PASSWORD", "PASSWORD")
|
||||||
if password != "" {
|
|
||||||
return password
|
|
||||||
}
|
|
||||||
|
|
||||||
// Retro-compatibility
|
|
||||||
password = os.Getenv("PASSWORD")
|
|
||||||
if password != "" {
|
|
||||||
r.onRetroActive("PASSWORD", "OPENVPN_PASSWORD")
|
|
||||||
}
|
|
||||||
return password
|
return password
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -107,35 +92,30 @@ func readBase64OrNil(envKey string) (valueOrNil *string, err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (r *Reader) readPIAEncryptionPreset() (presetPtr *string) {
|
func (r *Reader) readPIAEncryptionPreset() (presetPtr *string) {
|
||||||
preset := strings.ToLower(os.Getenv("PIA_ENCRYPTION"))
|
_, preset := r.getEnvWithRetro("PIA_ENCRYPTION", "ENCRYPTION")
|
||||||
if preset != "" {
|
if preset != "" {
|
||||||
return &preset
|
return &preset
|
||||||
}
|
}
|
||||||
|
|
||||||
// Retro-compatibility
|
|
||||||
preset = strings.ToLower(os.Getenv("ENCRYPTION"))
|
|
||||||
if preset != "" {
|
|
||||||
r.onRetroActive("ENCRYPTION", "PIA_ENCRYPTION")
|
|
||||||
return &preset
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Reader) readOpenVPNProcessUser() (processUser string, err error) {
|
func (r *Reader) readOpenVPNProcessUser() (processUser string, err error) {
|
||||||
// Retro-compatibility
|
key, value := r.getEnvWithRetro("OPENVPN_PROCESS_USER", "OPENVPN_ROOT")
|
||||||
root, err := envToBoolPtr("OPENVPN_ROOT")
|
if key == "OPENVPN_PROCESS_USER" {
|
||||||
if err != nil {
|
return value, 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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/qdm12/gluetun/internal/configuration/settings"
|
"github.com/qdm12/gluetun/internal/configuration/settings"
|
||||||
"github.com/qdm12/gluetun/internal/constants"
|
"github.com/qdm12/gluetun/internal/constants"
|
||||||
@@ -36,17 +35,7 @@ func (r *Reader) readOpenVPNSelection() (
|
|||||||
var ErrOpenVPNProtocolNotValid = errors.New("OpenVPN protocol is not valid")
|
var ErrOpenVPNProtocolNotValid = errors.New("OpenVPN protocol is not valid")
|
||||||
|
|
||||||
func (r *Reader) readOpenVPNProtocol() (tcp *bool, err error) {
|
func (r *Reader) readOpenVPNProtocol() (tcp *bool, err error) {
|
||||||
// Retro-compatibility
|
envKey, protocol := r.getEnvWithRetro("OPENVPN_PROTOCOL", "PROTOCOL")
|
||||||
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 {
|
switch protocol {
|
||||||
case "":
|
case "":
|
||||||
@@ -62,23 +51,9 @@ func (r *Reader) readOpenVPNProtocol() (tcp *bool, err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (r *Reader) readOpenVPNCustomPort() (customPort *uint16, err error) {
|
func (r *Reader) readOpenVPNCustomPort() (customPort *uint16, err error) {
|
||||||
const currentKey = "VPN_ENDPOINT_PORT"
|
key, s := r.getEnvWithRetro("VPN_ENDPOINT_PORT", "PORT", "OPENVPN_PORT")
|
||||||
key := "PORT"
|
|
||||||
s := os.Getenv(key) // Retro-compatibility
|
|
||||||
if s == "" {
|
if s == "" {
|
||||||
key = "OPENVPN_PORT" // Retro-compatibility
|
return nil, nil //nolint:nilnil
|
||||||
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 = new(uint16)
|
||||||
|
|||||||
10
internal/configuration/sources/env/publicip.go
vendored
10
internal/configuration/sources/env/publicip.go
vendored
@@ -35,17 +35,9 @@ func readPublicIPPeriod() (period *time.Duration, err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (r *Reader) readPublicIPFilepath() (filepath *string) {
|
func (r *Reader) readPublicIPFilepath() (filepath *string) {
|
||||||
// Retro-compatibility
|
_, s := r.getEnvWithRetro("PUBLICIP_FILE", "IP_STATUS_FILE")
|
||||||
s := os.Getenv("IP_STATUS_FILE")
|
|
||||||
if s != "" {
|
|
||||||
r.onRetroActive("IP_STATUS_FILE", "PUBLICIP_FILE")
|
|
||||||
return &s
|
|
||||||
}
|
|
||||||
|
|
||||||
s = os.Getenv("PUBLICIP_FILE")
|
|
||||||
if s != "" {
|
if s != "" {
|
||||||
return &s
|
return &s
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
23
internal/configuration/sources/env/reader.go
vendored
23
internal/configuration/sources/env/reader.go
vendored
@@ -1,6 +1,8 @@
|
|||||||
package env
|
package env
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"os"
|
||||||
|
|
||||||
"github.com/qdm12/gluetun/internal/configuration/settings"
|
"github.com/qdm12/gluetun/internal/configuration/settings"
|
||||||
"github.com/qdm12/gluetun/internal/configuration/sources"
|
"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 +
|
"You are using the old environment variable " + oldKey +
|
||||||
", please consider changing it to " + newKey)
|
", 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)
|
||||||
|
}
|
||||||
|
|||||||
20
internal/configuration/sources/env/server.go
vendored
20
internal/configuration/sources/env/server.go
vendored
@@ -34,18 +34,16 @@ func readControlServerLog() (enabled *bool, err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (r *Reader) readControlServerAddress() (address *string) {
|
func (r *Reader) readControlServerAddress() (address *string) {
|
||||||
// Retro-compatibility
|
key, s := r.getEnvWithRetro("HTTP_CONTROL_SERVER_ADDRESS", "HTTP_CONTROL_SERVER_PORT")
|
||||||
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")
|
|
||||||
if s == "" {
|
if s == "" {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return &s
|
|
||||||
|
if key == "HTTP_CONTROL_SERVER_ADDRESS" {
|
||||||
|
return &s
|
||||||
|
}
|
||||||
|
|
||||||
|
address = new(string)
|
||||||
|
*address = ":" + s
|
||||||
|
return address
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ func (r *Reader) readServerSelection(vpnProvider, vpnType string) (
|
|||||||
// Retro-compatibility
|
// Retro-compatibility
|
||||||
countriesCSV = os.Getenv("REGION")
|
countriesCSV = os.Getenv("REGION")
|
||||||
if countriesCSV != "" {
|
if countriesCSV != "" {
|
||||||
r.onRetroActive("REGION", "COUNTRY")
|
r.onRetroActive("REGION", "COUNTRY")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if countriesCSV != "" {
|
if countriesCSV != "" {
|
||||||
@@ -102,18 +102,7 @@ var (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func (r *Reader) readOpenVPNTargetIP() (ip net.IP, err error) {
|
func (r *Reader) readOpenVPNTargetIP() (ip net.IP, err error) {
|
||||||
envKey := "OPENVPN_TARGET_IP"
|
envKey, s := r.getEnvWithRetro("VPN_ENDPOINT_IP", "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")
|
|
||||||
}
|
|
||||||
|
|
||||||
ip = net.ParseIP(s)
|
ip = net.ParseIP(s)
|
||||||
if ip == nil {
|
if ip == nil {
|
||||||
return nil, fmt.Errorf("environment variable %s: %w: %s",
|
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) {
|
func (r *Reader) readOwnedOnly() (ownedOnly *bool, err error) {
|
||||||
// Retro-compatibility
|
envKey, _ := r.getEnvWithRetro("OWNED_ONLY", "OWNED")
|
||||||
ownedOnly, err = envToBoolPtr("OWNED")
|
ownedOnly, err = envToBoolPtr(envKey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
r.onRetroActive("OWNED", "OWNED_ONLY")
|
return nil, fmt.Errorf("environment variable %s: %w", envKey, err)
|
||||||
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 ownedOnly, nil
|
return ownedOnly, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ package env
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
|
||||||
|
|
||||||
"github.com/qdm12/gluetun/internal/configuration/settings"
|
"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) {
|
func (r *Reader) readShadowsocksAddress() (address string) {
|
||||||
// Retro-compatibility
|
key, value := r.getEnvWithRetro("SHADOWSOCKS_LISTENING_ADDRESS", "SHADOWSOCKS_PORT")
|
||||||
portString := os.Getenv("SHADOWSOCKS_PORT")
|
if value == "" {
|
||||||
if portString != "" {
|
return ""
|
||||||
r.onRetroActive("SHADOWSOCKS_PORT", "SHADOWSOCKS_LISTENING_ADDRESS")
|
|
||||||
return ":" + portString
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return os.Getenv("SHADOWSOCKS_LISTENING_ADDRESS")
|
if key == "SHADOWSOCKS_LISTENING_ADDRESS" {
|
||||||
|
return value
|
||||||
|
}
|
||||||
|
|
||||||
|
// Retro-compatibility
|
||||||
|
return ":" + value
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Reader) readShadowsocksCipher() (cipher string) {
|
func (r *Reader) readShadowsocksCipher() (cipher string) {
|
||||||
// Retro-compatibility
|
_, cipher = r.getEnvWithRetro("SHADOWSOCKS_CIPHER", "SHADOWSOCKS_METHOD")
|
||||||
cipher = os.Getenv("SHADOWSOCKS_METHOD")
|
|
||||||
if cipher != "" {
|
|
||||||
r.onRetroActive("SHADOWSOCKS_METHOD", "SHADOWSOCKS_CIPHER")
|
|
||||||
return cipher
|
return cipher
|
||||||
}
|
|
||||||
|
|
||||||
return os.Getenv("SHADOWSOCKS_CIPHER")
|
|
||||||
}
|
}
|
||||||
|
|||||||
12
internal/configuration/sources/env/system.go
vendored
12
internal/configuration/sources/env/system.go
vendored
@@ -35,17 +35,7 @@ var ErrSystemIDNotValid = errors.New("system ID is not valid")
|
|||||||
|
|
||||||
func (r *Reader) readID(key, retroKey string) (
|
func (r *Reader) readID(key, retroKey string) (
|
||||||
id *uint16, err error) {
|
id *uint16, err error) {
|
||||||
idEnvKey := key
|
idEnvKey, idString := r.getEnvWithRetro(key, retroKey)
|
||||||
idString := os.Getenv(key)
|
|
||||||
if idString == "" {
|
|
||||||
// retro-compatibility
|
|
||||||
idString = os.Getenv(retroKey)
|
|
||||||
if idString != "" {
|
|
||||||
idEnvKey = retroKey
|
|
||||||
r.onRetroActive(retroKey, key)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if idString == "" {
|
if idString == "" {
|
||||||
return nil, nil //nolint:nilnil
|
return nil, nil //nolint:nilnil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -30,19 +30,9 @@ func (r *Reader) readWireguardSelection() (
|
|||||||
var ErrIPAddressParse = errors.New("cannot parse IP address")
|
var ErrIPAddressParse = errors.New("cannot parse IP address")
|
||||||
|
|
||||||
func (r *Reader) readWireguardEndpointIP() (endpointIP net.IP, err error) {
|
func (r *Reader) readWireguardEndpointIP() (endpointIP net.IP, err error) {
|
||||||
const currentKey = "VPN_ENDPOINT_IP"
|
key, s := r.getEnvWithRetro("VPN_ENDPOINT_IP", "WIREGUARD_ENDPOINT_IP")
|
||||||
key := "WIREGUARD_ENDPOINT_IP"
|
|
||||||
s := os.Getenv(key) // Retro-compatibility
|
|
||||||
if s == "" {
|
if s == "" {
|
||||||
key = currentKey
|
return nil, nil
|
||||||
s = os.Getenv(key)
|
|
||||||
if s == "" {
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if key != currentKey {
|
|
||||||
r.onRetroActive(key, currentKey)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
endpointIP = net.ParseIP(s)
|
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) {
|
func (r *Reader) readWireguardCustomPort() (customPort *uint16, err error) {
|
||||||
const currentKey = "VPN_ENDPOINT_PORT"
|
key, s := r.getEnvWithRetro("VPN_ENDPOINT_PORT", "WIREGUARD_ENDPOINT_PORT")
|
||||||
key := "WIREGUARD_PORT" // Retro-compatibility
|
|
||||||
s := os.Getenv(key)
|
|
||||||
if s == "" {
|
if s == "" {
|
||||||
key = "WIREGUARD_ENDPOINT_PORT" // Retro-compatibility
|
return nil, nil //nolint:nilnil
|
||||||
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 = new(uint16)
|
||||||
|
|||||||
Reference in New Issue
Block a user