From 958be7033a777d45b17195b15fffa6c3cdf44fc9 Mon Sep 17 00:00:00 2001 From: xwvike Date: Sat, 13 Jan 2024 02:20:38 +0800 Subject: [PATCH] Added the capability for users to edit the AllowedIPs of the client --- src/lib/Server.js | 11 +++++- src/lib/WireGuard.js | 33 ++++++++++++++++-- src/www/index.html | 81 ++++++++++++++++++++++++++++++++++++++++++++ src/www/js/api.js | 8 +++++ src/www/js/app.js | 20 +++++++++++ 5 files changed, 149 insertions(+), 4 deletions(-) diff --git a/src/lib/Server.js b/src/lib/Server.js index 664244dc..cd00782a 100644 --- a/src/lib/Server.js +++ b/src/lib/Server.js @@ -233,7 +233,16 @@ module.exports = class Server { const { address } = await readBody(event); 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 { allowedIPs } = await readBody(event); + await WireGuard.updateClientAllowedIPs({ clientId, allowedIPs }); + return { success: true }; + })) const safePathJoin = (base, target) => { // Manage web root (edge case) diff --git a/src/lib/WireGuard.js b/src/lib/WireGuard.js index 1381db0a..aa902bd1 100644 --- a/src/lib/WireGuard.js +++ b/src/lib/WireGuard.js @@ -110,6 +110,15 @@ PostDown = ${WG_POST_DOWN} for (const [clientId, client] of Object.entries(config.clients)) { if (!client.enabled) continue; + let allowedIPs = ''; + if(client?.allowedIPs){ + for (const allowedIP of client.allowedIPs) { + allowedIPs += `,${allowedIP.address}/${allowedIP.cidr}`; + } + allowedIPs = allowedIPs.substring(1); + }else { + allowedIPs = client.address; + } result += ` @@ -146,8 +155,9 @@ ${client.preSharedKey ? `PresharedKey = ${client.preSharedKey}\n` : '' publicKey: client.publicKey, createdAt: new Date(client.createdAt), updatedAt: new Date(client.updatedAt), - allowedIPs: client.allowedIPs, downloadableConfig: 'privateKey' in client, + allowedIPs: client?.allowedIPs?client.allowedIPs:[{type:'ipv4', address:client.address, cidr: 32}], + persistentKeepalive: null, latestHandshakeAt: null, transferRx: null, @@ -249,7 +259,7 @@ Endpoint = ${WG_HOST}:${WG_CONFIG_PORT}`; }); if (!client) { - address = `${WG_DEFAULT_ADDRESS.replace('x', i)}/32`; + address = `${WG_DEFAULT_ADDRESS.replace('x', i)}`; break; } } @@ -271,6 +281,7 @@ Endpoint = ${WG_HOST}:${WG_CONFIG_PORT}`; createdAt: new Date(), updatedAt: new Date(), + allowedIPs: [{type:'ipv4', address, cidr: 32}], enabled: true, }; @@ -324,12 +335,28 @@ Endpoint = ${WG_HOST}:${WG_CONFIG_PORT}`; if (!Util.isValidIPv4(address)) { throw new ServerError(`Invalid Address: ${address}`, 400); } - + if(client?.allowedIPs){ + let allowedIPs = client.allowedIPs + client.allowedIPs[0].address = address; + } client.address = address; client.updatedAt = new Date(); await this.saveConfig(); } + async updateClientAllowedIPs({ clientId, allowedIPs }) { + const client = await this.getClient({ clientId }); + let ips = allowedIPs.map(item=>item.address) + ips.forEach(ip=>{ + if (!Util.isValidIPv4(ip)) { + throw new ServerError(`Invalid Address: ${ip}`, 400); + } + }) + client.allowedIPs = allowedIPs; + client.updatedAt = new Date(); + + await this.saveConfig(); + } async __reloadConfig() { await this.__buildConfig(); diff --git a/src/www/index.html b/src/www/index.html index 955a88cb..9e4986b3 100644 --- a/src/www/index.html +++ b/src/www/index.html @@ -305,6 +305,21 @@
+ + + + + + + +

diff --git a/src/www/js/api.js b/src/www/js/api.js index b2873f7e..d8e25eff 100644 --- a/src/www/js/api.js +++ b/src/www/js/api.js @@ -137,6 +137,14 @@ class API { body: { address }, }); } + async updateClientAllowedIPs({ clientId, allowedIPs }) { + return this.call({ + method: 'put', + path: `/wireguard/client/${clientId}/allowedIPs/`, + body: { allowedIPs }, + }); + } + async restoreConfiguration(file) { return this.call({ diff --git a/src/www/js/app.js b/src/www/js/app.js index 21545536..dd9aa6ec 100644 --- a/src/www/js/app.js +++ b/src/www/js/app.js @@ -64,6 +64,8 @@ new Vue({ clientEditNameId: null, clientEditAddress: null, clientEditAddressId: null, + clientEditAllowedIPs: null, + userInputIP:[0,0,0,0,0], qrcode: null, currentRelease: null, @@ -317,6 +319,24 @@ new Vue({ alert('Failed to load your file!'); } }, + updateClientAllowedIPs(client) { + this.api.updateClientAllowedIPs({ clientId: client.id, allowedIPs: client.allowedIPs }) + .catch((err) => alert(err.message || err.toString())) + .finally(() => this.refresh().catch(console.error)); + }, + addNewIP(){ + let address = this.userInputIP.slice(0,4).join('.'); + let allowedIPs = this.clientEditAllowedIPs.allowedIPs; + let obj = {type:'ipv4', address, cidr: this.userInputIP[4]}; + allowedIPs.push(obj); + this.clientEditAllowedIPs.allowedIPs = allowedIPs; + this.userInputIP = [0,0,0,0,0]; + }, + removeIP(index){ + let allowedIPs = this.clientEditAllowedIPs.allowedIPs; + allowedIPs.splice(index,1); + this.clientEditAllowedIPs.allowedIPs = allowedIPs; + }, toggleTheme() { const themes = ['light', 'dark', 'auto']; const currentIndex = themes.indexOf(this.uiTheme);