diff --git a/src/lib/Server.js b/src/lib/Server.js index 1f00a6b4..1de7e6d5 100644 --- a/src/lib/Server.js +++ b/src/lib/Server.js @@ -128,9 +128,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 1d432a30..f05a7835 100644 --- a/src/lib/WireGuard.js +++ b/src/lib/WireGuard.js @@ -111,7 +111,7 @@ PostDown = ${WG_POST_DOWN} [Peer] PublicKey = ${client.publicKey} PresharedKey = ${client.preSharedKey} -AllowedIPs = ${client.address}/32`; +AllowedIPs = ${client.address}`; } debug('Config saving...'); @@ -219,7 +219,7 @@ Endpoint = ${WG_HOST}:${WG_PORT}`; }); } - async createClient({ name }) { + async createClient({ name, allowedIps }) { if (!name) { throw new Error('Missing: Name'); } @@ -230,16 +230,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 149aa69d..308ccb35 100644 --- a/src/www/index.html +++ b/src/www/index.html @@ -62,8 +62,8 @@
{{$t("clients")}}
+ +
+