From 1de98692c2406218b21e6a2e8c216dc7f2c07df4 Mon Sep 17 00:00:00 2001 From: Vladislav Reshetnikov Date: Fri, 21 Apr 2023 14:14:20 +0300 Subject: [PATCH] Allow use WG_HOST=auto to resolve host programatically --- Dockerfile | 1 + README.md | 2 +- src/lib/WireGuard.js | 8 +++++++- 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index 2695361f..46059e1e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -35,6 +35,7 @@ RUN npm i -g nodemon # Install Linux packages RUN apk add -U --no-cache \ + curl \ wireguard-tools \ dumb-init diff --git a/README.md b/README.md index 6daca1b2..3e1d57d6 100644 --- a/README.md +++ b/README.md @@ -82,7 +82,7 @@ These options can be configured by setting environment variables using `-e KEY=" | Env | Default | Example | Description | | - | - | - | - | | `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. | +| `WG_HOST` | - | `vpn.myserver.com` | The public hostname of your VPN server. Set `auto`, to resolve it via `icanhazip.com` service | | `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/lib/WireGuard.js b/src/lib/WireGuard.js index 7886ac91..dad14f29 100644 --- a/src/lib/WireGuard.js +++ b/src/lib/WireGuard.js @@ -30,8 +30,13 @@ module.exports = class WireGuard { async getConfig() { if (!this.__configPromise) { this.__configPromise = Promise.resolve().then(async () => { + let wgHost; if (!WG_HOST) { throw new Error('WG_HOST Environment Variable Not Set!'); + } else if (WG_HOST === 'auto') { + wgHost = await Util.exec('curl -s icanhazip.com'); + } else { + wgHost = WG_HOST; } debug('Loading configuration...'); @@ -52,6 +57,7 @@ module.exports = class WireGuard { privateKey, publicKey, address, + wgHost, }, clients: {}, }; @@ -208,7 +214,7 @@ PublicKey = ${config.server.publicKey} PresharedKey = ${client.preSharedKey} AllowedIPs = ${WG_ALLOWED_IPS} PersistentKeepalive = ${WG_PERSISTENT_KEEPALIVE} -Endpoint = ${WG_HOST}:${WG_PORT}`; +Endpoint = ${config.server.wgHost || WG_HOST}:${WG_PORT}`; } async getClientQRCodeSVG({ clientId }) {