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 @@
+ + +