From 9f4a04152f3a412db00e511027916312573db50d Mon Sep 17 00:00:00 2001 From: Guillem Bonet Date: Mon, 13 Feb 2023 15:44:34 +0100 Subject: [PATCH] allow fetching public IP instead of using defined host Signed-off-by: Guillem Bonet --- Dockerfile | 4 ++-- README.md | 1 + src/config.js | 1 + src/lib/WireGuard.js | 17 ++++++++++++++--- 4 files changed, 18 insertions(+), 5 deletions(-) diff --git a/Dockerfile b/Dockerfile index 2695361f..2a8e735f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -9,7 +9,7 @@ # # # # -FROM docker.io/library/node:14-alpine@sha256:dc92f36e7cd917816fa2df041d4e9081453366381a00f40398d99e9392e78664 AS build_node_modules +FROM docker.io/library/node:18-alpine AS build_node_modules # Copy Web UI COPY src/ /app/ @@ -18,7 +18,7 @@ RUN npm ci --production # Copy build result to a new image. # This saves a lot of disk space. -FROM docker.io/library/node:14-alpine@sha256:dc92f36e7cd917816fa2df041d4e9081453366381a00f40398d99e9392e78664 +FROM docker.io/library/node:18-alpine COPY --from=build_node_modules /app /app # Move node_modules one directory up, so during development diff --git a/README.md b/README.md index 6daca1b2..f04d62e3 100644 --- a/README.md +++ b/README.md @@ -83,6 +83,7 @@ These options can be configured by setting environment variables using `-e KEY=" | - | - | - | - | | `PASSWORD` | - | `foobar123` | When set, requires a password when logging in to the Web UI. | | `WG_HOST` | - | `vpn.myserver.com` | The public hostname of your VPN server. | +| `IPIFY_URL` | - | `https://api.ipify.org?format=json` | API to fetch our public IP. | | `WG_PORT` | `51820` | `12345` | The public UDP port of your VPN server. WireGuard will always listen on `51820` inside the Docker container. | | `WG_MTU` | `null` | `1420` | The MTU the clients will use. Server uses default WG MTU. | | `WG_PERSISTENT_KEEPALIVE` | `0` | `25` | Value in seconds to keep the "connection" open. If this value is 0, then connections won't be kept alive. | diff --git a/src/config.js b/src/config.js index 28c9fc5a..f68c5120 100644 --- a/src/config.js +++ b/src/config.js @@ -7,6 +7,7 @@ module.exports.PORT = process.env.PORT || 51821; module.exports.PASSWORD = process.env.PASSWORD; module.exports.WG_PATH = process.env.WG_PATH || '/etc/wireguard/'; module.exports.WG_HOST = process.env.WG_HOST; +module.exports.IPIFY_URL = process.env.IPIFY_URL; module.exports.WG_PORT = process.env.WG_PORT || 51820; module.exports.WG_MTU = process.env.WG_MTU || null; module.exports.WG_PERSISTENT_KEEPALIVE = process.env.WG_PERSISTENT_KEEPALIVE || 0; diff --git a/src/lib/WireGuard.js b/src/lib/WireGuard.js index 7886ac91..2f29a88a 100644 --- a/src/lib/WireGuard.js +++ b/src/lib/WireGuard.js @@ -13,6 +13,7 @@ const ServerError = require('./ServerError'); const { WG_PATH, WG_HOST, + IPIFY_URL, WG_PORT, WG_MTU, WG_DEFAULT_DNS, @@ -30,8 +31,8 @@ module.exports = class WireGuard { async getConfig() { if (!this.__configPromise) { this.__configPromise = Promise.resolve().then(async () => { - if (!WG_HOST) { - throw new Error('WG_HOST Environment Variable Not Set!'); + if (!WG_HOST && !IPIFY_URL) { + throw new Error('WG_HOST or IPIFY_URL Environment Variable Not Set!'); } debug('Loading configuration...'); @@ -192,9 +193,19 @@ AllowedIPs = ${client.address}/32`; return client; } + async getPublicIp() { + if (IPIFY_URL == "") { + return WG_HOST + } + const response = await fetch(IPIFY_URL); + const data = await response.json(); + return data.ip; + } + async getClientConfiguration({ clientId }) { const config = await this.getConfig(); const client = await this.getClient({ clientId }); + const host = await this.getPublicIp(); return ` [Interface] @@ -208,7 +219,7 @@ PublicKey = ${config.server.publicKey} PresharedKey = ${client.preSharedKey} AllowedIPs = ${WG_ALLOWED_IPS} PersistentKeepalive = ${WG_PERSISTENT_KEEPALIVE} -Endpoint = ${WG_HOST}:${WG_PORT}`; +Endpoint = ${host}:${WG_PORT}`; } async getClientQRCodeSVG({ clientId }) {