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 @@