From 236df35ee4544512f01afe5c135e20d7159efa87 Mon Sep 17 00:00:00 2001 From: yhl452493373 Date: Mon, 20 Jan 2025 22:24:54 +0800 Subject: [PATCH] In order to persist Peer's AllowedIPs, add allowedIPs for clients in wg0.json. --- build-wg-easy.sh | 2 ++ src/lib/WireGuard.js | 6 +++++- 2 files changed, 7 insertions(+), 1 deletion(-) create mode 100644 build-wg-easy.sh diff --git a/build-wg-easy.sh b/build-wg-easy.sh new file mode 100644 index 00000000..f80b2798 --- /dev/null +++ b/build-wg-easy.sh @@ -0,0 +1,2 @@ +#/bin/bash +docker build -t wg-easy . diff --git a/src/lib/WireGuard.js b/src/lib/WireGuard.js index 8ce325f7..dea6698c 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.allowedIPs? client.allowedIPs:(client.address+'/32')}`; } debug('Config saving...'); @@ -250,6 +250,7 @@ Endpoint = ${WG_HOST}:${WG_CONFIG_PORT}`; // Calculate next IP let address; + let allowedIps; for (let i = 2; i < 255; i++) { const client = Object.values(config.clients).find((client) => { return client.address === WG_DEFAULT_ADDRESS.replace('x', i); @@ -257,6 +258,7 @@ Endpoint = ${WG_HOST}:${WG_CONFIG_PORT}`; if (!client) { address = WG_DEFAULT_ADDRESS.replace('x', i); + allowedIps = address + '/32'; break; } } @@ -264,12 +266,14 @@ Endpoint = ${WG_HOST}:${WG_CONFIG_PORT}`; if (!address) { throw new Error('Maximum number of clients reached.'); } + // Create Client const id = crypto.randomUUID(); const client = { id, name, address, + allowedIps, privateKey, publicKey, preSharedKey,