diff --git a/assets/screenshot.png b/assets/screenshot.png index 6f87a647a..5f54cfe5a 100644 Binary files a/assets/screenshot.png and b/assets/screenshot.png differ diff --git a/src/lib/WireGuard.js b/src/lib/WireGuard.js index 215d0d5a1..adf6ca952 100644 --- a/src/lib/WireGuard.js +++ b/src/lib/WireGuard.js @@ -27,54 +27,60 @@ const { module.exports = class WireGuard { + async __buildConfig() { + this.__configPromise = Promise.resolve().then(async () => { + if (!WG_HOST) { + throw new Error('WG_HOST Environment Variable Not Set!'); + } + + debug('Loading configuration...'); + let config; + try { + config = await fs.readFile(path.join(WG_PATH, 'wg0.json'), 'utf8'); + 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 address = WG_DEFAULT_ADDRESS.replace('x', '1'); + + config = { + server: { + privateKey, + publicKey, + address, + }, + clients: {}, + }; + debug('Configuration generated.'); + } + + return config; + }); + + return this.__configPromise; + } + async getConfig() { if (!this.__configPromise) { - this.__configPromise = Promise.resolve().then(async () => { - if (!WG_HOST) { - throw new Error('WG_HOST Environment Variable Not Set!'); - } + const config = await this.__buildConfig(); - debug('Loading configuration...'); - let config; - try { - config = await fs.readFile(path.join(WG_PATH, 'wg0.json'), 'utf8'); - 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 address = WG_DEFAULT_ADDRESS.replace('x', '1'); - - config = { - server: { - privateKey, - publicKey, - address, - }, - clients: {}, - }; - debug('Configuration generated.'); + await this.__saveConfig(config); + await Util.exec('wg-quick down wg0').catch(() => {}); + await Util.exec('wg-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!'); } - await this.__saveConfig(config); - await Util.exec('wg-quick down wg0').catch(() => { }); - await Util.exec('wg-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!'); - } - - throw err; - }); - // await Util.exec(`iptables -t nat -A POSTROUTING -s ${WG_DEFAULT_ADDRESS.replace('x', '0')}/24 -o ' + WG_DEVICE + ' -j MASQUERADE`); - // await Util.exec('iptables -A INPUT -p udp -m udp --dport 51820 -j ACCEPT'); - // 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; + throw err; }); + // await Util.exec(`iptables -t nat -A POSTROUTING -s ${WG_DEFAULT_ADDRESS.replace('x', '0')}/24 -o ' + WG_DEVICE + ' -j MASQUERADE`); + // await Util.exec('iptables -A INPUT -p udp -m udp --dport 51820 -j ACCEPT'); + // await Util.exec('iptables -A FORWARD -i wg0 -j ACCEPT'); + // await Util.exec('iptables -A FORWARD -o wg0 -j ACCEPT'); + await this.__syncConfig(); } return this.__configPromise; @@ -227,7 +233,9 @@ 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`); + const publicKey = await Util.exec(`echo ${privateKey} | wg pubkey`, { + log: 'echo ***hidden*** | wg pubkey', + }); const preSharedKey = await Util.exec('wg genpsk'); // Calculate next IP @@ -319,25 +327,30 @@ Endpoint = ${WG_HOST}:${WG_CONFIG_PORT}`; await this.saveConfig(); } - async ___forceRestart() { - this.__configPromise = null; - await this.saveConfig(); + async __reloadConfig() { + await this.__buildConfig(); + await this.__syncConfig(); } async restoreConfiguration(config) { + debug('Starting configuration restore process.'); const _config = JSON.parse(config); await this.__saveConfig(_config); - await this.___forceRestart(); + await this.__reloadConfig(); + debug('Configuration restore process completed.'); } async backupConfiguration() { + debug('Starting configuration backup.'); const config = await this.getConfig(); - return JSON.stringify(config, null, 2); + const backup = JSON.stringify(config, null, 2); + debug('Configuration backup completed.'); + return backup; } // Shutdown wireguard async Shutdown() { - await Util.exec('wg-quick down wg0').catch(() => { }); + await Util.exec('wg-quick down wg0').catch(() => {}); } }; diff --git a/src/www/index.html b/src/www/index.html index 314e7fbe1..c68b18416 100644 --- a/src/www/index.html +++ b/src/www/index.html @@ -94,9 +94,8 @@