From 6604e44ea3c955a477473799ef30cd30eeb86681 Mon Sep 17 00:00:00 2001 From: Chanchai Date: Wed, 21 Feb 2024 13:56:39 +0700 Subject: [PATCH 1/2] feat: support graceful shutdown --- src/config.js | 7 ++++++- src/lib/WireGuard.js | 4 ++++ src/server.js | 12 ++++++++++++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/config.js b/src/config.js index 4d71f96..33ff783 100644 --- a/src/config.js +++ b/src/config.js @@ -27,6 +27,11 @@ iptables -A FORWARD -o wg0 -j ACCEPT; `.split('\n').join(' '); module.exports.WG_PRE_DOWN = process.env.WG_PRE_DOWN || ''; -module.exports.WG_POST_DOWN = process.env.WG_POST_DOWN || ''; +module.exports.WG_POST_DOWN = process.env.WG_POST_DOWN || ` +iptables -t nat -D POSTROUTING -s ${module.exports.WG_DEFAULT_ADDRESS.replace('x', '0')}/24 -o ${module.exports.WG_DEVICE} -j MASQUERADE; +iptables -D INPUT -p udp -m udp --dport 51820 -j ACCEPT; +iptables -D FORWARD -i wg0 -j ACCEPT; +iptables -D FORWARD -o wg0 -j ACCEPT; +`.split('\n').join(' '); module.exports.LANG = process.env.LANG || 'en'; module.exports.UI_TRAFFIC_STATS = process.env.UI_TRAFFIC_STATS || 'false'; diff --git a/src/lib/WireGuard.js b/src/lib/WireGuard.js index f72c3a2..5d17028 100644 --- a/src/lib/WireGuard.js +++ b/src/lib/WireGuard.js @@ -318,4 +318,8 @@ Endpoint = ${WG_HOST}:${WG_PORT}`; await this.saveConfig(); } + // Shutdown wireguard + async Shutdown() { + await Util.exec('wg-quick down wg0').catch(() => { }); + } }; diff --git a/src/server.js b/src/server.js index 0bc2c98..1b1164e 100644 --- a/src/server.js +++ b/src/server.js @@ -12,3 +12,15 @@ WireGuard.getConfig() // eslint-disable-next-line no-process-exit process.exit(1); }); + +// Handle terminate signal +process.on('SIGTERM', async() => { + console.log('SIGTERM signal received.'); + await WireGuard.Shutdown(); + process.exit(0); +}); + +// Handle interupt signal +process.on('SIGINT', () => { + console.log('SIGINT signal received.'); +}); \ No newline at end of file From a6d092dd87dd7d880d73cdacae27f61c436e2deb Mon Sep 17 00:00:00 2001 From: Philip H <47042125+pheiduck@users.noreply.github.com> Date: Wed, 21 Feb 2024 08:59:37 +0000 Subject: [PATCH 2/2] fixup: lint errors --- src/lib/WireGuard.js | 1 + src/server.js | 7 +++++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/lib/WireGuard.js b/src/lib/WireGuard.js index 5d17028..8377f6f 100644 --- a/src/lib/WireGuard.js +++ b/src/lib/WireGuard.js @@ -322,4 +322,5 @@ Endpoint = ${WG_HOST}:${WG_PORT}`; async Shutdown() { await Util.exec('wg-quick down wg0').catch(() => { }); } + }; diff --git a/src/server.js b/src/server.js index 1b1164e..1ad06b3 100644 --- a/src/server.js +++ b/src/server.js @@ -14,13 +14,16 @@ WireGuard.getConfig() }); // Handle terminate signal -process.on('SIGTERM', async() => { +process.on('SIGTERM', async () => { + // eslint-disable-next-line no-console console.log('SIGTERM signal received.'); await WireGuard.Shutdown(); + // eslint-disable-next-line no-process-exit process.exit(0); }); // Handle interupt signal process.on('SIGINT', () => { + // eslint-disable-next-line no-console console.log('SIGINT signal received.'); -}); \ No newline at end of file +});