diff --git a/assets/screenshot.png b/assets/screenshot.png index c5b73cc..6f87a64 100644 Binary files a/assets/screenshot.png and b/assets/screenshot.png differ diff --git a/src/lib/Server.js b/src/lib/Server.js index 40341ee..315679c 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/backup', defineEventHandler(async (event) => { + const config = await WireGuard.backupConfiguration(); + setHeader(event, 'Content-Disposition', 'attachment; filename="wg0.json"'); + setHeader(event, 'Content-Type', 'text/json'); + return config; + })) + .put('/api/wireguard/restore', defineEventHandler(async (event) => { + const { file } = await readBody(event); + await WireGuard.restoreConfiguration(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..215d0d5 100644 --- a/src/lib/WireGuard.js +++ b/src/lib/WireGuard.js @@ -319,6 +319,22 @@ Endpoint = ${WG_HOST}:${WG_CONFIG_PORT}`; await this.saveConfig(); } + async ___forceRestart() { + this.__configPromise = null; + await this.saveConfig(); + } + + async restoreConfiguration(config) { + const _config = JSON.parse(config); + await this.__saveConfig(_config); + await this.___forceRestart(); + } + + async backupConfiguration() { + 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..4c32ae0 100644 --- a/src/www/index.html +++ b/src/www/index.html @@ -3,7 +3,7 @@ WireGuard - + @@ -23,45 +23,50 @@

- WireGuard + WireGuard

-
+ + + + + + + + + {{$t("backup")}} + +