diff --git a/.gitignore b/.gitignore index ef1066e2..36dbbb18 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -/config \ No newline at end of file +/config +.idea diff --git a/src/lib/Server.js b/src/lib/Server.js index 08262e6e..80e24501 100644 --- a/src/lib/Server.js +++ b/src/lib/Server.js @@ -99,8 +99,8 @@ module.exports = class Server { res.send(config); })) .post('/api/wireguard/client', Util.promisify(async req => { - const { name } = req.body; - return WireGuard.createClient({ name }); + const { name, number } = req.body; + return WireGuard.createClient({ name, number }); })) .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 b6d4f9cf..9e4c4f0c 100644 --- a/src/lib/WireGuard.js +++ b/src/lib/WireGuard.js @@ -202,15 +202,17 @@ Endpoint = ${WG_HOST}:${WG_PORT}`; const publicKey = await Util.exec(`echo ${privateKey} | wg pubkey`); const preSharedKey = await Util.exec('wg genpsk'); - // Calculate next IP + // IP address operations let address; + if (!number) { + // Calculate next IP for (let i = 2; i < 255; i++) { const client = Object.values(config.clients).find(client => { - return client.address === WG_DEFAULT_ADDRESS.replace('x', i); + return client.address === WG_DEFAULT_ADDRESS.replace('x', i.toString()); }); if (!client) { - address = WG_DEFAULT_ADDRESS.replace('x', i); + address = WG_DEFAULT_ADDRESS.replace('x', i.toString()); break; } } @@ -218,6 +220,17 @@ Endpoint = ${WG_HOST}:${WG_PORT}`; if (!address) { throw new Error('Maximum number of clients reached.'); } + } else { + // Search & use selected number for IP + const client = Object.values(config.clients).find(client => { + return client.address === WG_DEFAULT_ADDRESS.replace('x', number); + }); + + if (client) { + throw new Error('Number in use, please select another or leave empty.'); + } + address = WG_DEFAULT_ADDRESS.replace('x', number); + } // Create Client const clientId = uuid.v4(); diff --git a/src/www/index.html b/src/www/index.html index c75e43de..aefa91d7 100644 --- a/src/www/index.html +++ b/src/www/index.html @@ -35,7 +35,7 @@

Clients

-

There are no clients yet.

-

+
+

+ +

+
-