From dcc35d5058305494c98a6ab6d3ce4fecd7c70a74 Mon Sep 17 00:00:00 2001 From: kolsys Date: Fri, 1 Nov 2024 15:10:17 +0300 Subject: [PATCH] Add amneziawg-tools support --- src/lib/WireGuard.js | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/src/lib/WireGuard.js b/src/lib/WireGuard.js index 822915ef..bd0961aa 100644 --- a/src/lib/WireGuard.js +++ b/src/lib/WireGuard.js @@ -31,6 +31,8 @@ const { WG_ASC_H1, WG_ASC_H2, WG_ASC_H3, WG_ASC_H4, } = require('../config'); +const CMD = WG_ASC_JC ? 'awg' : 'wg'; + module.exports = class WireGuard { async __buildConfig() { @@ -46,9 +48,9 @@ module.exports = class WireGuard { config = JSON.parse(config); debug('Configuration loaded.'); } catch (err) { - const privateKey = await Util.exec('wg genkey'); - const publicKey = await Util.exec(`echo ${privateKey} | wg pubkey`, { - log: 'echo ***hidden*** | wg pubkey', + const privateKey = await Util.exec(`${CMD} genkey`); + const publicKey = await Util.exec(`echo ${privateKey} | ${CMD} pubkey`, { + log: `echo ***hidden*** | ${CMD} pubkey`, }); const address = WG_DEFAULT_ADDRESS.replace('x', '1'); @@ -72,10 +74,9 @@ module.exports = class WireGuard { async getConfig() { if (!this.__configPromise) { const config = await this.__buildConfig(); - await this.__saveConfig(config); - await Util.exec('wg-quick down wg0').catch(() => {}); - await Util.exec('wg-quick up wg0').catch((err) => { + await Util.exec(`${CMD}-quick down wg0`).catch(() => {}); + await Util.exec(`${CMD}-quick up wg0`).catch((err) => { if (err && err.message && err.message.includes('Cannot find device "wg0"')) { throw new Error('WireGuard exited with the error: Cannot find device "wg0"\nThis usually means that your host\'s kernel does not support WireGuard!'); } @@ -153,7 +154,7 @@ ${client.preSharedKey ? `PresharedKey = ${client.preSharedKey}\n` : '' async __syncConfig() { debug('Config syncing...'); - await Util.exec('wg syncconf wg0 <(wg-quick strip wg0)'); + await Util.exec(`${CMD} syncconf wg0 <(${CMD}-quick strip wg0)`); debug('Config synced.'); } @@ -182,7 +183,7 @@ ${client.preSharedKey ? `PresharedKey = ${client.preSharedKey}\n` : '' })); // Loop WireGuard status - const dump = await Util.exec('wg show wg0 dump', { + const dump = await Util.exec(`${CMD} show wg0 dump`, { log: false, }); dump @@ -270,11 +271,11 @@ Endpoint = ${WG_HOST}:${WG_CONFIG_PORT}`; const config = await this.getConfig(); - const privateKey = await Util.exec('wg genkey'); - const publicKey = await Util.exec(`echo ${privateKey} | wg pubkey`, { - log: 'echo ***hidden*** | wg pubkey', + const privateKey = await Util.exec(`${CMD} genkey`); + const publicKey = await Util.exec(`echo ${privateKey} | ${CMD} pubkey`, { + log: `echo ***hidden*** | ${CMD} pubkey`, }); - const preSharedKey = await Util.exec('wg genpsk'); + const preSharedKey = await Util.exec(`${CMD} genpsk`); // Calculate next IP let address; @@ -425,7 +426,7 @@ Endpoint = ${WG_HOST}:${WG_CONFIG_PORT}`; // Shutdown wireguard async Shutdown() { - await Util.exec('wg-quick down wg0').catch(() => {}); + await Util.exec(`${CMD}-quick down wg0`).catch(() => {}); } async cronJobEveryMinute() {