From a951110461274ce252fc9870589ac6f9ef95f0ae Mon Sep 17 00:00:00 2001 From: Quentin McGaw Date: Fri, 28 Jan 2022 00:09:58 +0000 Subject: [PATCH] feat(vpn): `VPN_ENDPOINT_IP` - Deprecate `OPENVPN_TARGET_IP` - Deprecate `WIREGUARD_ENDPOINT_IP` --- Dockerfile | 4 ++-- .../sources/env/serverselection.go | 19 ++++++++++----- .../sources/env/wireguardselection.go | 24 ++++++++++++++----- 3 files changed, 33 insertions(+), 14 deletions(-) diff --git a/Dockerfile b/Dockerfile index 695b4da4..c5a64707 100644 --- a/Dockerfile +++ b/Dockerfile @@ -68,6 +68,8 @@ LABEL \ org.opencontainers.image.description="VPN swiss-knife like client to tunnel to multiple VPN servers using OpenVPN, IPtables, DNS over TLS, Shadowsocks, an HTTP proxy and Alpine Linux" ENV VPNSP=pia \ VPN_TYPE=openvpn \ + # Common VPN options + VPN_ENDPOINT_IP= \ # OpenVPN OPENVPN_PROTOCOL=udp \ OPENVPN_USER= \ @@ -80,7 +82,6 @@ ENV VPNSP=pia \ OPENVPN_CIPHER= \ OPENVPN_AUTH= \ OPENVPN_PROCESS_USER= \ - OPENVPN_TARGET_IP= \ OPENVPN_IPV6=off \ OPENVPN_CUSTOM_CONFIG= \ OPENVPN_INTERFACE=tun0 \ @@ -90,7 +91,6 @@ ENV VPNSP=pia \ WIREGUARD_PRESHARED_KEY= \ WIREGUARD_PUBLIC_KEY= \ WIREGUARD_ADDRESS= \ - WIREGUARD_ENDPOINT_IP= \ WIREGUARD_ENDPOINT_PORT= \ WIREGUARD_INTERFACE=wg0 \ # VPN server filtering diff --git a/internal/configuration/sources/env/serverselection.go b/internal/configuration/sources/env/serverselection.go index 2264c4d9..8dd85d75 100644 --- a/internal/configuration/sources/env/serverselection.go +++ b/internal/configuration/sources/env/serverselection.go @@ -20,7 +20,7 @@ func (r *Reader) readServerSelection(vpnProvider, vpnType string) ( ss settings.ServerSelection, err error) { ss.VPN = vpnType - ss.TargetIP, err = readOpenVPNTargetIP() + ss.TargetIP, err = r.readOpenVPNTargetIP() if err != nil { return ss, err } @@ -99,16 +99,23 @@ var ( ErrInvalidIP = errors.New("invalid IP address") ) -func readOpenVPNTargetIP() (ip net.IP, err error) { - s := os.Getenv("OPENVPN_TARGET_IP") +func (r *Reader) readOpenVPNTargetIP() (ip net.IP, err error) { + envKey := "OPENVPN_TARGET_IP" + s := os.Getenv(envKey) // Retro-compatibility if s == "" { - return nil, nil + 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) if ip == nil { - return nil, fmt.Errorf("environment variable OPENVPN_TARGET_IP: %w: %s", - ErrInvalidIP, s) + return nil, fmt.Errorf("environment variable %s: %w: %s", + envKey, ErrInvalidIP, s) } return ip, nil diff --git a/internal/configuration/sources/env/wireguardselection.go b/internal/configuration/sources/env/wireguardselection.go index 044d928c..f297e3f4 100644 --- a/internal/configuration/sources/env/wireguardselection.go +++ b/internal/configuration/sources/env/wireguardselection.go @@ -12,7 +12,7 @@ import ( func (r *Reader) readWireguardSelection() ( selection settings.WireguardSelection, err error) { - selection.EndpointIP, err = readWireguardEndpointIP() + selection.EndpointIP, err = r.readWireguardEndpointIP() if err != nil { return selection, err } @@ -29,16 +29,28 @@ func (r *Reader) readWireguardSelection() ( var ErrIPAddressParse = errors.New("cannot parse IP address") -func readWireguardEndpointIP() (endpointIP net.IP, err error) { - s := os.Getenv("WIREGUARD_ENDPOINT_IP") +func (r *Reader) readWireguardEndpointIP() (endpointIP net.IP, err error) { + const currentKey = "VPN_ENDPOINT_IP" + key := "WIREGUARD_ENDPOINT_IP" + s := os.Getenv(key) // Retro-compatibility if s == "" { - return nil, nil + key = currentKey + s = os.Getenv(key) + if s == "" { + return nil, nil + } } + + if key != currentKey { + r.onRetroActive(key, currentKey) + } + endpointIP = net.ParseIP(s) if endpointIP == nil { - return nil, fmt.Errorf("environment variable WIREGUARD_ENDPOINT_IP: %w: %s", - ErrIPAddressParse, s) + return nil, fmt.Errorf("environment variable %s: %w: %s", + key, ErrIPAddressParse, s) } + return endpointIP, nil }