From 748e24a4a54f5fd8a19d5c347af96e0c5b032d04 Mon Sep 17 00:00:00 2001 From: David Calvo Date: Sat, 9 Jul 2022 02:37:08 -0400 Subject: [PATCH] add allowed IPs to client creation --- src/lib/Server.js | 6 +++--- src/lib/WireGuard.js | 24 ++++++++++++++---------- src/www/index.html | 10 ++++++++-- src/www/js/api.js | 4 ++-- src/www/js/app.js | 6 ++++-- 5 files changed, 31 insertions(+), 19 deletions(-) diff --git a/src/lib/Server.js b/src/lib/Server.js index b92ca7a2..1aaeb0b7 100644 --- a/src/lib/Server.js +++ b/src/lib/Server.js @@ -130,9 +130,9 @@ module.exports = class Server { res.header('Content-Type', 'text/plain'); res.send(config); })) - .post('/api/wireguard/client', Util.promisify(async (req) => { - const { name } = req.body; - return WireGuard.createClient({ name }); + .post('/api/wireguard/client', Util.promisify(async req => { + const { name, allowedIps } = req.body; + return WireGuard.createClient({ name, allowedIps }); })) .delete('/api/wireguard/client/:clientId', Util.promisify(async (req) => { const { clientId } = req.params; diff --git a/src/lib/WireGuard.js b/src/lib/WireGuard.js index fb66f22e..68590699 100644 --- a/src/lib/WireGuard.js +++ b/src/lib/WireGuard.js @@ -112,7 +112,7 @@ PostDown = ${WG_POST_DOWN} [Peer] PublicKey = ${client.publicKey} PresharedKey = ${client.preSharedKey} -AllowedIPs = ${client.address}/32`; +AllowedIPs = ${client.address}`; } debug('Config saving...'); @@ -220,7 +220,7 @@ Endpoint = ${WG_HOST}:${WG_PORT}`; }); } - async createClient({ name }) { + async createClient({ name, allowedIps }) { if (!name) { throw new Error('Missing: Name'); } @@ -231,16 +231,20 @@ Endpoint = ${WG_HOST}:${WG_PORT}`; const publicKey = await Util.exec(`echo ${privateKey} | wg pubkey`); const preSharedKey = await Util.exec('wg genpsk'); - // Calculate next IP let address; - for (let i = 2; i < 255; i++) { - const client = Object.values(config.clients).find((client) => { - return client.address === WG_DEFAULT_ADDRESS.replace('x', i); - }); + if (allowedIps) { + address = allowedIps + } else { + // Calculate next IP + for (let i = 2; i < 255; i++) { + const client = Object.values(config.clients).find(client => { + return client.address.includes(WG_DEFAULT_ADDRESS.replace('x', i)); + }); - if (!client) { - address = WG_DEFAULT_ADDRESS.replace('x', i); - break; + if (!client) { + address = `${WG_DEFAULT_ADDRESS.replace('x', i)}/32`; + break; + } } } diff --git a/src/www/index.html b/src/www/index.html index 4b3ba6ef..c78becb9 100644 --- a/src/www/index.html +++ b/src/www/index.html @@ -67,8 +67,8 @@

Clients

-
+
+

+ +

+
diff --git a/src/www/js/api.js b/src/www/js/api.js index 826a7652..576138cd 100644 --- a/src/www/js/api.js +++ b/src/www/js/api.js @@ -72,11 +72,11 @@ class API { }))); } - async createClient({ name }) { + async createClient({ name, allowedIps }) { return this.call({ method: 'post', path: '/wireguard/client', - body: { name }, + body: { name, allowedIps }, }); } diff --git a/src/www/js/app.js b/src/www/js/app.js index 4a258d95..3cc08d31 100644 --- a/src/www/js/app.js +++ b/src/www/js/app.js @@ -36,6 +36,7 @@ new Vue({ clientDelete: null, clientCreate: null, clientCreateName: '', + clientCreateAllowedIps: '', clientEditName: null, clientEditNameId: null, clientEditAddress: null, @@ -210,10 +211,11 @@ new Vue({ }, createClient() { const name = this.clientCreateName; + const allowedIps = this.clientCreateAllowedIps; if (!name) return; - this.api.createClient({ name }) - .catch((err) => alert(err.message || err.toString())) + this.api.createClient({ name, allowedIps }) + .catch(err => alert(err.message || err.toString())) .finally(() => this.refresh().catch(console.error)); }, deleteClient(client) {