From 0f220c1a782bda680ed5916653941dafe0620028 Mon Sep 17 00:00:00 2001 From: Emile Nijssen Date: Sun, 23 May 2021 13:36:26 +0200 Subject: [PATCH] wip --- Dockerfile | 7 ++-- README.md | 2 +- {config => config_}/client1/presharedkey | 0 {config => config_}/client1/privatekey | 0 {config => config_}/client1/publickey | 0 {config => config_}/privatekey | 0 {config => config_}/publickey | 0 {config => config_}/wg0 copy.conf | 0 config_/wg0.conf | 27 ++++++++++++++++ config_/wg0.json | 41 ++++++++++++++++++++++++ package.json | 3 +- src/lib/WireGuard.js | 13 +++++++- src/package.json | 2 +- src/server.js | 13 +++++++- 14 files changed, 100 insertions(+), 8 deletions(-) rename {config => config_}/client1/presharedkey (100%) rename {config => config_}/client1/privatekey (100%) rename {config => config_}/client1/publickey (100%) rename {config => config_}/privatekey (100%) rename {config => config_}/publickey (100%) rename {config => config_}/wg0 copy.conf (100%) create mode 100644 config_/wg0.conf create mode 100644 config_/wg0.json diff --git a/Dockerfile b/Dockerfile index 800e42d..6d81afc 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,13 +1,14 @@ -FROM debian:bullseye +FROM node:16-buster # 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 curl -fsSL https://deb.nodesource.com/setup_16.x | bash - +# RUN apt-get install -y nodejs COPY src/ /app/ WORKDIR /app diff --git a/README.md b/README.md index cf657ec..8ea122a 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ ```bash $ docker run \ --name wg-easy \ - --mount type=bind,source=~/.wg-easy,target=/etc/wireguard \ + --mount type=bind,source="$(pwd)",target=/etc/wireguard \ --cap-add=NET_ADMIN \ --cap-add=SYS_MODULE \ --sysctl="net.ipv4.conf.all.src_valid_mark=1" \ diff --git a/config/client1/presharedkey b/config_/client1/presharedkey similarity index 100% rename from config/client1/presharedkey rename to config_/client1/presharedkey diff --git a/config/client1/privatekey b/config_/client1/privatekey similarity index 100% rename from config/client1/privatekey rename to config_/client1/privatekey diff --git a/config/client1/publickey b/config_/client1/publickey similarity index 100% rename from config/client1/publickey rename to config_/client1/publickey diff --git a/config/privatekey b/config_/privatekey similarity index 100% rename from config/privatekey rename to config_/privatekey diff --git a/config/publickey b/config_/publickey similarity index 100% rename from config/publickey rename to config_/publickey diff --git a/config/wg0 copy.conf b/config_/wg0 copy.conf similarity index 100% rename from config/wg0 copy.conf rename to config_/wg0 copy.conf diff --git a/config_/wg0.conf b/config_/wg0.conf new file mode 100644 index 0000000..cbeae7a --- /dev/null +++ b/config_/wg0.conf @@ -0,0 +1,27 @@ + +# Note: Do not edit this file directly. +# Your changes will be overwritten! + +# Server +[Interface] +PrivateKey = iOQJS7OUUGPYATsX6nqlL+sOODoiWiN5IOE8Msfw/0o= +Address = 10.8.0.1/24 +ListenPort = 51820 + +# Client: Emile (af3111a4-7343-4380-a293-ed498d9aa3b8) +[Peer] +PublicKey = i8xWKqicnDkNL14I4B+I1zlB8od/booA1joIosWn7X4= +PresharedKey = MzplKtOQ44/IaAKri2VKqCoIlg4XiVH7TCp5bcYRTQU= +AllowedIPs = 10.8.0.2/32 + +# Client: Test (2ca33a1c-ed49-4bdd-b84c-adc77f1f3b2d) +[Peer] +PublicKey = 563oiA0IuQqt8JPEXHGINT4mHYKzlLx9Ol2gcV1vKCk= +PresharedKey = Q6xGB4og5Sj6M0MsHzkD16VsniT3FCqOnGmiLLilsU8= +AllowedIPs = 10.8.0.3/32 + +# Client: Test 3 (f1d0280c-07e7-4927-94dd-000a1723872f) +[Peer] +PublicKey = +PresharedKey = +AllowedIPs = 10.8.0.4/32 \ No newline at end of file diff --git a/config_/wg0.json b/config_/wg0.json new file mode 100644 index 0000000..796a329 --- /dev/null +++ b/config_/wg0.json @@ -0,0 +1,41 @@ +{ + "server": { + "privateKey": "iOQJS7OUUGPYATsX6nqlL+sOODoiWiN5IOE8Msfw/0o=", + "publicKey": "BkdntwYazhYZzEEHhcYayq6TGw9/YUDQ251s+5bTgC0=", + "address": "10.8.0.1", + "port": "51820", + "dns": "1.1.1.1" + }, + "clients": { + "af3111a4-7343-4380-a293-ed498d9aa3b8": { + "name": "Emile", + "createdAt": "2021-05-22T20:02:45.372Z", + "updatedAt": "2021-05-22T20:02:45.372Z", + "privateKey": "sHUUDbaZBQshfOvvF8HeebhhXq3rDKWlW1Vm+6XMklU=", + "publicKey": "i8xWKqicnDkNL14I4B+I1zlB8od/booA1joIosWn7X4=", + "preSharedKey": "MzplKtOQ44/IaAKri2VKqCoIlg4XiVH7TCp5bcYRTQU=", + "address": "10.8.0.2", + "enabled": true + }, + "2ca33a1c-ed49-4bdd-b84c-adc77f1f3b2d": { + "name": "Test", + "address": "10.8.0.3", + "privateKey": "AJVOxJxEnbWyrj7SbhJxxiIIgBsRljs1fP2xrN76Kns=", + "publicKey": "563oiA0IuQqt8JPEXHGINT4mHYKzlLx9Ol2gcV1vKCk=", + "preSharedKey": "Q6xGB4og5Sj6M0MsHzkD16VsniT3FCqOnGmiLLilsU8=", + "createdAt": "2021-05-22T21:41:49.876Z", + "updatedAt": "2021-05-23T10:04:29.051Z", + "enabled": true + }, + "f1d0280c-07e7-4927-94dd-000a1723872f": { + "name": "Test 3", + "address": "10.8.0.4", + "privateKey": "", + "publicKey": "", + "preSharedKey": "", + "createdAt": "2021-05-23T10:21:24.607Z", + "updatedAt": "2021-05-23T10:21:24.607Z", + "enabled": true + } + } +} \ No newline at end of file diff --git a/package.json b/package.json index 74d2419..3c1c331 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,8 @@ { "version": "0.1.0", "scripts": { - "build": "docker build --tag wg-easy ." + "build": "docker build --tag wg-easy .", + "start": "docker run --env WG_HOST=0.0.0.0 --name wg-easy --cap-add=NET_ADMIN --cap-add=SYS_MODULE --sysctl=\"net.ipv4.conf.all.src_valid_mark=1\" --mount type=bind,source=\"$(pwd)\"/config,target=/etc/wireguard -p 51820:51820/udp -p 51821:51821/tcp wg-easy" }, "docker": { "production": { diff --git a/src/lib/WireGuard.js b/src/lib/WireGuard.js index 1adb1bc..4ff68bc 100644 --- a/src/lib/WireGuard.js +++ b/src/lib/WireGuard.js @@ -3,6 +3,7 @@ const fs = require('fs').promises; const path = require('path'); +const debug = require('debug')('WireGuard'); const uuid = require('uuid'); const QRCode = require('qrcode'); @@ -26,10 +27,12 @@ module.exports = class WireGuard { throw new Error('WG_HOST Environment Variable Not Set!'); } + debug('Loading configuration...'); let config; try { config = await fs.readFile(path.join(WG_PATH, 'wg0.json'), 'utf8'); config = JSON.parse(config); + debug('Configuration loaded'); } catch (err) { config = { server: { @@ -38,10 +41,14 @@ module.exports = class WireGuard { }, clients: {}, }; - await this.saveConfig(); + debug('New configuration saved'); } + await this.__saveConfig(config); + + debug('Starting...'); await Util.exec('wg-quick up wg0'); + debug('Started'); return config; }); @@ -52,6 +59,10 @@ module.exports = class WireGuard { async saveConfig() { const config = await this.getConfig(); + await this.__saveConfig(config); + } + + async __saveConfig(config) { let result = ` # Note: Do not edit this file directly. # Your changes will be overwritten! diff --git a/src/package.json b/src/package.json index c98556d..f5c9c1c 100644 --- a/src/package.json +++ b/src/package.json @@ -4,7 +4,7 @@ "description": "", "main": "server.js", "scripts": { - "serve": "DEBUG=Server WG_HOST=0.0.0.0 WG_PATH=../config/ nodemon server.js", + "serve": "DEBUG=Server,WireGuard WG_HOST=0.0.0.0 WG_PATH=../config/ nodemon server.js", "serve-with-password": "PASSWORD=wg npm run serve" }, "author": "Emile Nijssen", diff --git a/src/server.js b/src/server.js index c7b1cf6..ac26c6d 100644 --- a/src/server.js +++ b/src/server.js @@ -1,3 +1,14 @@ 'use strict'; -require('./services/Server'); \ No newline at end of file +require('./services/Server'); + +const WireGuard = require('./services/WireGuard'); + +WireGuard.getConfig() + .catch(err => { + // eslint-disable-next-line no-console + console.error(err); + + // eslint-disable-next-line no-process-exit + process.exit(1); + });