From 72ba79b5f2a636adab77ee116e0d11620c344bf3 Mon Sep 17 00:00:00 2001 From: tetuaoro Date: Tue, 9 Jul 2024 17:06:12 +0200 Subject: [PATCH 01/11] feat: load configuration from file * import json config file & update the config (restore) * export the config and save it to json file (backup) --- src/lib/Server.js | 19 +++++++- src/lib/WireGuard.js | 15 +++++- src/www/index.html | 106 ++++++++++++++++++++++++++++--------------- src/www/js/api.js | 15 ++++++ src/www/js/app.js | 16 +++++++ src/www/js/i18n.js | 2 + 6 files changed, 134 insertions(+), 39 deletions(-) diff --git a/src/lib/Server.js b/src/lib/Server.js index 40341ee..2c046d9 100644 --- a/src/lib/Server.js +++ b/src/lib/Server.js @@ -265,6 +265,23 @@ module.exports = class Server { }); }; + // import_export + const router3 = createRouter(); + app.use(router3); + + router3 + .get('/api/wireguard/dl', defineEventHandler((event) => { + const config = WireGuard.downloadConfiguration(); + setHeader(event, 'Content-Disposition', 'attachment; filename="wg0.json"'); + setHeader(event, 'Content-Type', 'text/json'); + return config; + })) + .put('/api/wireguard/upload', defineEventHandler(async (event) => { + const { file } = await readBody(event); + await WireGuard.uploadConfiguration(file); + return { success: true } + })); + // Static assets const publicDir = '/app/www'; app.use( @@ -276,7 +293,7 @@ module.exports = class Server { getMeta: async (id) => { const filePath = safePathJoin(publicDir, id); - const stats = await stat(filePath).catch(() => {}); + const stats = await stat(filePath).catch(() => { }); if (!stats || !stats.isFile()) { return; } diff --git a/src/lib/WireGuard.js b/src/lib/WireGuard.js index d4f32c5..ba4a224 100644 --- a/src/lib/WireGuard.js +++ b/src/lib/WireGuard.js @@ -111,7 +111,7 @@ PostDown = ${WG_POST_DOWN} [Peer] PublicKey = ${client.publicKey} ${client.preSharedKey ? `PresharedKey = ${client.preSharedKey}\n` : '' -}AllowedIPs = ${client.address}/32`; + }AllowedIPs = ${client.address}/32`; } debug('Config saving...'); @@ -206,7 +206,7 @@ ${WG_MTU ? `MTU = ${WG_MTU}\n` : ''}\ [Peer] PublicKey = ${config.server.publicKey} ${client.preSharedKey ? `PresharedKey = ${client.preSharedKey}\n` : '' -}AllowedIPs = ${WG_ALLOWED_IPS} + }AllowedIPs = ${WG_ALLOWED_IPS} PersistentKeepalive = ${WG_PERSISTENT_KEEPALIVE} Endpoint = ${WG_HOST}:${WG_CONFIG_PORT}`; } @@ -319,6 +319,17 @@ Endpoint = ${WG_HOST}:${WG_CONFIG_PORT}`; await this.saveConfig(); } + async uploadConfiguration(config) { + const _config = JSON.parse(config); + await this.__saveConfig(_config); + await this.__syncConfig(); + } + + async downloadConfiguration() { + const config = await this.getConfig(); + return JSON.stringify(config, null, 2); + } + // Shutdown wireguard async Shutdown() { await Util.exec('wg-quick down wg0').catch(() => { }); diff --git a/src/www/index.html b/src/www/index.html index 7204472..2fe8800 100644 --- a/src/www/index.html +++ b/src/www/index.html @@ -3,7 +3,7 @@ WireGuard - + @@ -23,45 +23,50 @@

- WireGuard + WireGuard

-
+ + + + + + + + {{$t("download")}} + + -
- - - - - - - - - {{$t("backup")}} - - - -
+
-
+
@@ -225,24 +220,20 @@ - + · - + {{client.transferTxCurrent | bytes}}/s - + - + · - + @@ -250,8 +241,7 @@ {{client.transferRxCurrent | bytes}}/s - {{!uiTrafficStats ? " · " : ""}}{{new Date(client.latestHandshakeAt) | timeago}} @@ -327,7 +317,8 @@ :class="{ 'hover:bg-red-800 dark:hover:bg-red-800 hover:text-white dark:hover:text-white': client.downloadableConfig, 'is-disabled': !client.downloadableConfig - }" :title="!client.downloadableConfig ? $t('noPrivKey') : $t('showQR')" + }" + :title="!client.downloadableConfig ? $t('noPrivKey') : $t('showQR')" @click="qrcode = `./api/wireguard/client/${client.id}/qrcode.svg`"> @@ -344,7 +335,8 @@ :class="{ 'hover:bg-red-800 dark:hover:bg-red-800 hover:text-white dark:hover:text-white': client.downloadableConfig, 'is-disabled': !client.downloadableConfig - }" :title="!client.downloadableConfig ? $t('noPrivKey') : $t('downloadConfig')" + }" + :title="!client.downloadableConfig ? $t('noPrivKey') : $t('downloadConfig')" @click="if(!client.downloadableConfig) { $event.preventDefault(); }"> @@ -448,8 +440,8 @@
- + @@ -566,8 +558,8 @@ class="shadow rounded-md bg-white dark:bg-neutral-700 mx-auto w-64 p-5 overflow-hidden mt-10">
- +
@@ -608,10 +600,9 @@
-

WireGuard Easy © 2021-2024 by Emile Nijssen is - licensed under WireGuard Easy © 2021-2024 by Emile Nijssen is licensed under CC BY-NC-SA 4.0 · {{$t("donate")}}

From 39d32b0a1cb5990ff3a36ea64f7b639f970d121d Mon Sep 17 00:00:00 2001 From: tetuaoro Date: Wed, 10 Jul 2024 14:44:40 +0200 Subject: [PATCH 10/11] Revert "i18n.js: german translation" This reverts commit e4a7ff08c6c76bf9b1449d520d458be9f57102bc. --- src/www/js/i18n.js | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/www/js/i18n.js b/src/www/js/i18n.js index ec914b8..267f8c3 100644 --- a/src/www/js/i18n.js +++ b/src/www/js/i18n.js @@ -229,10 +229,6 @@ const messages = { // eslint-disable-line no-unused-vars downloadConfig: 'Konfiguration herunterladen', madeBy: 'Erstellt von', donate: 'Spenden', - restore: 'Wiederherstellen', - backup: 'Sichern', - titleRestoreConfig: 'Stelle deine Konfiguration wieder her', - titleBackupConfig: 'Sichere deine Konfiguraion', }, ca: { // github.com/guillembonet name: 'Nom', From 45087a9683cf43a6a76d77f637c1b19644da0ca1 Mon Sep 17 00:00:00 2001 From: tetuaoro Date: Wed, 10 Jul 2024 15:01:39 +0200 Subject: [PATCH 11/11] fix conficts --- src/lib/Server.js | 2 +- src/www/index.html | 81 +++++++++++++++++++++++++--------------------- 2 files changed, 46 insertions(+), 37 deletions(-) diff --git a/src/lib/Server.js b/src/lib/Server.js index d995bb6..12c4207 100644 --- a/src/lib/Server.js +++ b/src/lib/Server.js @@ -265,7 +265,7 @@ module.exports = class Server { }); }; - // backup_restore + // import_export const router3 = createRouter(); app.use(router3); diff --git a/src/www/index.html b/src/www/index.html index 927d269..345ccb7 100644 --- a/src/www/index.html +++ b/src/www/index.html @@ -3,7 +3,7 @@ WireGuard - + @@ -23,45 +23,50 @@

- WireGuard + WireGuard

-