From 7ca165850325a1918a04c68ed234b7c1bfcb22af Mon Sep 17 00:00:00 2001 From: RobHold Date: Tue, 29 Oct 2024 23:55:38 +0700 Subject: [PATCH] First commit to add AllowedIPs field, form and logic. --- src/lib/Server.js | 9 +++++++++ src/lib/WireGuard.js | 11 ++++++++++- src/www/index.html | 25 ++++++++++++++++++++++++- src/www/js/api.js | 8 ++++++++ 4 files changed, 51 insertions(+), 2 deletions(-) diff --git a/src/lib/Server.js b/src/lib/Server.js index da2f6c9d..bd79a03c 100644 --- a/src/lib/Server.js +++ b/src/lib/Server.js @@ -303,6 +303,15 @@ module.exports = class Server { await WireGuard.updateClientAddress({ clientId, address }); return { success: true }; })) + .put('/api/wireguard/client/:clientId/allowedIPs', defineEventHandler(async (event) => { + const clientId = getRouterParam(event, 'clientId'); + if (clientId === '__proto__' || clientId === 'constructor' || clientId === 'prototype') { + throw createError({ status: 403 }); + } + const { address } = await readBody(event); + await WireGuard.updateClientAllowedIPs({ clientId, allowedIPs }); + return { success: true }; + })) .put('/api/wireguard/client/:clientId/expireDate', defineEventHandler(async (event) => { const clientId = getRouterParam(event, 'clientId'); if (clientId === '__proto__' || clientId === 'constructor' || clientId === 'prototype') { diff --git a/src/lib/WireGuard.js b/src/lib/WireGuard.js index 8ce325f7..411954cd 100644 --- a/src/lib/WireGuard.js +++ b/src/lib/WireGuard.js @@ -120,7 +120,7 @@ PostDown = ${WG_POST_DOWN} [Peer] PublicKey = ${client.publicKey} ${client.preSharedKey ? `PresharedKey = ${client.preSharedKey}\n` : '' -}AllowedIPs = ${client.address}/32`; +}AllowedIPs = ${client.address}/32, ${client.allowedIPs}`; } debug('Config saving...'); @@ -344,6 +344,15 @@ Endpoint = ${WG_HOST}:${WG_CONFIG_PORT}`; await this.saveConfig(); } +// ip is an array + async updateClientAllowedIPs({ clientId, allowedIPs }) { + const client = await this.getClient({ clientId }); + + client.allowedIPs = allowedIPs.toString(); + client.updatedAt = new Date(); + + await this.saveConfig(); + } async updateClientAddress({ clientId, address }) { const client = await this.getClient({ clientId }); diff --git a/src/www/index.html b/src/www/index.html index 03f93565..5594ee67 100644 --- a/src/www/index.html +++ b/src/www/index.html @@ -205,8 +205,31 @@ - +
+ + + + {{client.allowedIPs}} + + + + + + + + +