Browse Source

allow fetching public IP instead of using defined host

Signed-off-by: Guillem Bonet <[email protected]>
pull/488/head
Guillem Bonet 3 years ago
parent
commit
9f4a04152f
No known key found for this signature in database GPG Key ID: 48408F6E3AC6F4B4
  1. 4
      Dockerfile
  2. 1
      README.md
  3. 1
      src/config.js
  4. 17
      src/lib/WireGuard.js

4
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

1
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. |

1
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;

17
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 }) {

Loading…
Cancel
Save