From 987b8206a7249a7421f55f1d78a9b9e7720c78f2 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 | 4 ++--
src/lib/WireGuard.js | 22 +++++++++++++---------
src/www/index.html | 18 ++++++++++++------
src/www/js/api.js | 4 ++--
src/www/js/app.js | 6 ++++--
5 files changed, 33 insertions(+), 21 deletions(-)
diff --git a/src/lib/Server.js b/src/lib/Server.js
index 7f06da50..664244dc 100644
--- a/src/lib/Server.js
+++ b/src/lib/Server.js
@@ -191,8 +191,8 @@ module.exports = class Server {
return config;
}))
.post('/api/wireguard/client', defineEventHandler(async (event) => {
- const { name } = await readBody(event);
- await WireGuard.createClient({ name });
+ const { name, allowedIps } = await readBody(event);
+ await WireGuard.createClient({ name, allowedIps });
return { success: true };
}))
.delete('/api/wireguard/client/:clientId', defineEventHandler(async (event) => {
diff --git a/src/lib/WireGuard.js b/src/lib/WireGuard.js
index adf6ca95..9a612ba5 100644
--- a/src/lib/WireGuard.js
+++ b/src/lib/WireGuard.js
@@ -225,7 +225,7 @@ Endpoint = ${WG_HOST}:${WG_CONFIG_PORT}`;
});
}
- async createClient({ name }) {
+ async createClient({ name, allowedIps }) {
if (!name) {
throw new Error('Missing: Name');
}
@@ -238,16 +238,20 @@ Endpoint = ${WG_HOST}:${WG_CONFIG_PORT}`;
});
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 66dc3697..4e941343 100644
--- a/src/www/index.html
+++ b/src/www/index.html
@@ -42,7 +42,7 @@
-
+
@@ -614,4 +620,4 @@