Browse Source

cache config

pull/1250/head
Bernd Storath 9 months ago
parent
commit
73a2e1cc96
  1. 21
      src/server/utils/WireGuard.ts

21
src/server/utils/WireGuard.ts

@ -38,6 +38,7 @@ type Config = {
}; };
class WireGuard { class WireGuard {
#configCache: Config | null = null;
async __buildConfig() { async __buildConfig() {
if (!WG_HOST) { if (!WG_HOST) {
throw new Error('WG_HOST Environment Variable Not Set!'); throw new Error('WG_HOST Environment Variable Not Set!');
@ -70,10 +71,13 @@ class WireGuard {
} }
async getConfig(): Promise<Config> { async getConfig(): Promise<Config> {
if (this.#configCache !== null) {
return this.#configCache;
}
const config = await this.__buildConfig(); const config = await this.__buildConfig();
await this.__saveConfig(config); 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) => { await exec('wg-quick up wg0').catch((err) => {
if ( if (
err && err &&
@ -92,7 +96,8 @@ class WireGuard {
// await Util.exec('iptables -A FORWARD -i wg0 -j ACCEPT'); // await Util.exec('iptables -A FORWARD -i wg0 -j ACCEPT');
// await Util.exec('iptables -A FORWARD -o wg0 -j ACCEPT'); // await Util.exec('iptables -A FORWARD -o wg0 -j ACCEPT');
await this.__syncConfig(); await this.__syncConfig();
return config; this.#configCache = config;
return this.#configCache;
} }
async saveConfig() { async saveConfig() {
@ -125,9 +130,8 @@ PostDown = ${WG_POST_DOWN}
# Client: ${client.name} (${clientId}) # Client: ${client.name} (${clientId})
[Peer] [Peer]
PublicKey = ${client.publicKey} PublicKey = ${client.publicKey}
${ ${client.preSharedKey ? `PresharedKey = ${client.preSharedKey}\n` : ''
client.preSharedKey ? `PresharedKey = ${client.preSharedKey}\n` : '' }AllowedIPs = ${client.address}/32`;
}AllowedIPs = ${client.address}/32`;
} }
debug('Config saving...'); debug('Config saving...');
@ -228,9 +232,8 @@ ${WG_MTU ? `MTU = ${WG_MTU}\n` : ''}\
[Peer] [Peer]
PublicKey = ${config.server.publicKey} PublicKey = ${config.server.publicKey}
${ ${client.preSharedKey ? `PresharedKey = ${client.preSharedKey}\n` : ''
client.preSharedKey ? `PresharedKey = ${client.preSharedKey}\n` : '' }AllowedIPs = ${WG_ALLOWED_IPS}
}AllowedIPs = ${WG_ALLOWED_IPS}
PersistentKeepalive = ${WG_PERSISTENT_KEEPALIVE} PersistentKeepalive = ${WG_PERSISTENT_KEEPALIVE}
Endpoint = ${WG_HOST}:${WG_CONFIG_PORT}`; Endpoint = ${WG_HOST}:${WG_CONFIG_PORT}`;
} }
@ -380,7 +383,7 @@ Endpoint = ${WG_HOST}:${WG_CONFIG_PORT}`;
// Shutdown wireguard // Shutdown wireguard
async Shutdown() { async Shutdown() {
await exec('wg-quick down wg0').catch(() => {}); await exec('wg-quick down wg0').catch(() => { });
} }
} }

Loading…
Cancel
Save