From 0c3217def06701c52a6b1e968857bbb089f066c4 Mon Sep 17 00:00:00 2001 From: Emile Nijssen Date: Sun, 23 May 2021 14:28:22 +0200 Subject: [PATCH] wip --- Dockerfile | 18 ++++++++---------- src/lib/Util.js | 4 +++- src/lib/WireGuard.js | 28 ++++++++++++++++++++-------- 3 files changed, 31 insertions(+), 19 deletions(-) diff --git a/Dockerfile b/Dockerfile index 6d81afc..b0d9b25 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,21 +1,19 @@ -FROM node:16-buster +FROM node:16-alpine # Install Linux packages -RUN apt-get clean -RUN echo "deb http://deb.debian.org/debian buster-backports main" > /etc/apt/sources.list.d/backports.list -RUN apt-get update -RUN apt-get install -y wireguard iproute2 openresolv curl - -# Install Node.js -# RUN curl -fsSL https://deb.nodesource.com/setup_16.x | bash - -# RUN apt-get install -y nodejs +RUN apk add -U wireguard-tools +# Copy Web UI COPY src/ /app/ WORKDIR /app RUN npm ci --production - +# Expose Ports EXPOSE 51820/udp EXPOSE 80/tcp + +# Set Environment ENV DEBUG=Server,WireGuard + +# Run Web UI CMD ["node", "server.js"] \ No newline at end of file diff --git a/src/lib/Util.js b/src/lib/Util.js index a1c1f5f..898e805 100644 --- a/src/lib/Util.js +++ b/src/lib/Util.js @@ -44,7 +44,9 @@ module.exports = class Util { } return new Promise((resolve, reject) => { - childProcess.exec(cmd, (err, stdout) => { + childProcess.exec(cmd, { + shell: 'bash', + }, (err, stdout) => { if (err) return reject(err); return resolve(String(stdout).trim()); }); diff --git a/src/lib/WireGuard.js b/src/lib/WireGuard.js index 4ff68bc..0a08e0a 100644 --- a/src/lib/WireGuard.js +++ b/src/lib/WireGuard.js @@ -32,23 +32,25 @@ module.exports = class WireGuard { try { config = await fs.readFile(path.join(WG_PATH, 'wg0.json'), 'utf8'); config = JSON.parse(config); - debug('Configuration loaded'); + debug('Configuration loaded.'); } catch (err) { + const privateKey = await Util.exec('wg genkey'); + const publicKey = await Util.exec(`echo ${privateKey} | wg pubkey`); + const address = WG_DEFAULT_ADDRESS.replace('x', '1'); + config = { server: { - privateKey: await Util.exec('wg genkey'), - address: `${WG_DEFAULT_ADDRESS.replace('x', '1')}/24`, + privateKey, + publicKey, + address, }, clients: {}, }; - debug('New configuration saved'); } await this.__saveConfig(config); - - debug('Starting...'); await Util.exec('wg-quick up wg0'); - debug('Started'); + await this.__syncConfig(); return config; }); @@ -60,6 +62,7 @@ module.exports = class WireGuard { async saveConfig() { const config = await this.getConfig(); await this.__saveConfig(config); + await this.__syncConfig(); } async __saveConfig(config) { @@ -85,8 +88,16 @@ PresharedKey = ${client.preSharedKey} AllowedIPs = ${client.address}/32`; } + debug('Saving config...'); await fs.writeFile(path.join(WG_PATH, 'wg0.json'), JSON.stringify(config, false, 2)); await fs.writeFile(path.join(WG_PATH, 'wg0.conf'), result); + debug('Config saved.'); + } + + async __syncConfig() { + debug('Syncing config...'); + await Util.exec('wg syncconf wg0 <(wg-quick strip wg0)'); + debug('Config synced.'); } async getClients() { @@ -150,6 +161,7 @@ AllowedIPs = ${client.address}/32`; } async getClientConfiguration({ clientId }) { + const config = await this.getConfig(); const client = await this.getClient({ clientId }); return ` @@ -159,7 +171,7 @@ Address = ${client.address}/24 DNS = ${WG_DEFAULT_DNS} [Peer] -PublicKey = ${client.publicKey} +PublicKey = ${config.server.publicKey} PresharedKey = ${client.preSharedKey} AllowedIPs = 0.0.0.0/0, ::/0 Endpoint = ${WG_HOST}:${WG_PORT}`;