From 73a2e1cc962bc02e6d84308246ae06ee45fa6c1d Mon Sep 17 00:00:00 2001 From: Bernd Storath <999999bst@gmail.com> Date: Mon, 5 Aug 2024 14:55:30 +0200 Subject: [PATCH] cache config --- src/server/utils/WireGuard.ts | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/src/server/utils/WireGuard.ts b/src/server/utils/WireGuard.ts index a797f464..26d83d5b 100644 --- a/src/server/utils/WireGuard.ts +++ b/src/server/utils/WireGuard.ts @@ -38,6 +38,7 @@ type Config = { }; class WireGuard { + #configCache: Config | null = null; async __buildConfig() { if (!WG_HOST) { throw new Error('WG_HOST Environment Variable Not Set!'); @@ -70,10 +71,13 @@ class WireGuard { } async getConfig(): Promise { + if (this.#configCache !== null) { + return this.#configCache; + } const config = await this.__buildConfig(); await this.__saveConfig(config); - await exec('wg-quick down wg0').catch(() => {}); + await exec('wg-quick down wg0').catch(() => { }); await exec('wg-quick up wg0').catch((err) => { if ( err && @@ -92,7 +96,8 @@ class WireGuard { // await Util.exec('iptables -A FORWARD -i wg0 -j ACCEPT'); // await Util.exec('iptables -A FORWARD -o wg0 -j ACCEPT'); await this.__syncConfig(); - return config; + this.#configCache = config; + return this.#configCache; } async saveConfig() { @@ -125,9 +130,8 @@ PostDown = ${WG_POST_DOWN} # Client: ${client.name} (${clientId}) [Peer] PublicKey = ${client.publicKey} -${ - client.preSharedKey ? `PresharedKey = ${client.preSharedKey}\n` : '' -}AllowedIPs = ${client.address}/32`; +${client.preSharedKey ? `PresharedKey = ${client.preSharedKey}\n` : '' + }AllowedIPs = ${client.address}/32`; } debug('Config saving...'); @@ -228,9 +232,8 @@ ${WG_MTU ? `MTU = ${WG_MTU}\n` : ''}\ [Peer] PublicKey = ${config.server.publicKey} -${ - client.preSharedKey ? `PresharedKey = ${client.preSharedKey}\n` : '' -}AllowedIPs = ${WG_ALLOWED_IPS} +${client.preSharedKey ? `PresharedKey = ${client.preSharedKey}\n` : '' + }AllowedIPs = ${WG_ALLOWED_IPS} PersistentKeepalive = ${WG_PERSISTENT_KEEPALIVE} Endpoint = ${WG_HOST}:${WG_CONFIG_PORT}`; } @@ -380,7 +383,7 @@ Endpoint = ${WG_HOST}:${WG_CONFIG_PORT}`; // Shutdown wireguard async Shutdown() { - await exec('wg-quick down wg0').catch(() => {}); + await exec('wg-quick down wg0').catch(() => { }); } }