This commit is contained in:
Emile Nijssen 2021-05-23 13:36:26 +02:00
parent d9901aa8a3
commit 0f220c1a78
14 changed files with 100 additions and 8 deletions

View file

@ -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!

View file

@ -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",

View file

@ -1,3 +1,14 @@
'use strict';
require('./services/Server');
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);
});