This commit is contained in:
Emile Nijssen 2021-05-23 12:02:56 +02:00
parent 18b6ce7c78
commit 9d355f67d8
12 changed files with 60 additions and 37 deletions

View file

@ -1,7 +1,7 @@
'use strict';
module.exports.PORT = process.env.PORT || 80;
module.exports.PASSWORD = process.env.PASSWORD || 'wireguard';
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 || '127.0.0.1';
module.exports.WG_PORT = process.env.WG_PORT || 51820;

View file

@ -31,8 +31,14 @@ module.exports = class Server {
// Authentication
.get('/api/session', Util.promisify(async req => {
const requiresPassword = !!process.env.PASSWORD;
const authenticated = requiresPassword
? !!(req.session && req.session.authenticated)
: true;
return {
authenticated: !!(req.session && req.session.authenticated),
requiresPassword,
authenticated,
};
}))
.post('/api/session', Util.promisify(async req => {
@ -55,7 +61,19 @@ module.exports = class Server {
}))
// WireGuard
.use(Util.requireSession)
.use((req, res, next) => {
if (!PASSWORD) {
return next();
}
if (req.session && req.session.authenticated) {
return next();
}
return res.status(401).json({
error: 'Not Logged In',
});
})
.delete('/api/session', Util.promisify(async req => {
const sessionId = req.session.id;

View file

@ -4,16 +4,6 @@ const childProcess = require('child_process');
module.exports = class Util {
static requireSession(req, res, next) {
if (req.session && req.session.authenticated) {
return next();
}
return res.status(401).json({
error: 'Not Logged In',
});
}
static promisify(fn) {
return function(req, res) {
Promise.resolve().then(async () => fn(req, res))
@ -53,7 +43,7 @@ module.exports = class Util {
return new Promise((resolve, reject) => {
childProcess.exec(cmd, (err, stdout) => {
if (err) return reject(err);
return resolve(stdout);
return resolve(String(stdout).trim());
});
});
}

View file

@ -34,8 +34,11 @@ module.exports = class WireGuard {
},
clients: {},
};
await this.saveConfig();
}
await Util.exec('wg-quick up wg0');
return config;
});
}
@ -99,7 +102,7 @@ AllowedIPs = ${client.address}/32`;
const [
publicKey,
preSharedKey, // eslint-disable-line no-unused-vars
endpoint,
endpoint, // eslint-disable-line no-unused-vars
allowedIps, // eslint-disable-line no-unused-vars
latestHandshakeAt,
transferRx,
@ -110,9 +113,6 @@ AllowedIPs = ${client.address}/32`;
const client = clients.find(client => client.publicKey === publicKey);
if (!client) return;
client.endpoint = endpoint === '(none)'
? null
: endpoint;
client.latestHandshakeAt = latestHandshakeAt === '0'
? null
: new Date(Number(`${latestHandshakeAt}000`));

View file

@ -4,7 +4,8 @@
"description": "",
"main": "server.js",
"scripts": {
"serve": "DEBUG=Server PASSWORD=p WG_PATH=../config/ nodemon server.js"
"serve": "DEBUG=Server WG_PATH=../config/ nodemon server.js",
"serve-with-password": "PASSWORD=wg npm run serve"
},
"author": "Emile Nijssen",
"license": "GPL",

View file

@ -15,7 +15,8 @@
<div id="app" class="container mx-auto">
<div v-if="authenticated === true">
<h1 class="text-4xl font-medium mt-10 mb-2">WireGuard</h1>
<h2 class="text-sm text-gray-400 mb-10"><span class="cursor-pointer hover:underline" @click="logout">
<h2 class="text-sm text-gray-400 mb-10"><span v-if="requiresPassword" class="cursor-pointer hover:underline"
@click="logout">
Logout
<svg class="h-3 inline" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24"
stroke="currentColor">

View file

@ -11,6 +11,7 @@ new Vue({
authenticated: null,
authenticating: false,
password: null,
requiresPassword: null,
clients: null,
clientDelete: null,
@ -55,6 +56,7 @@ new Vue({
.then(async () => {
const session = await this.api.getSession();
this.authenticated = session.authenticated;
this.requiresPassword = session.requiresPassword;
return this.refresh();
})
.catch(err => {