Skip to content

Commit

Permalink
feat: support graceful shutdown
Browse files Browse the repository at this point in the history
  • Loading branch information
Chanchai authored and pheiduck committed Feb 29, 2024
1 parent c733ef4 commit 5977438
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,10 @@ 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';
4 changes: 4 additions & 0 deletions src/lib/WireGuard.js
Original file line number Diff line number Diff line change
Expand Up @@ -318,4 +318,8 @@ Endpoint = ${WG_HOST}:${WG_PORT}`;
await this.saveConfig();
}

// Shutdown wireguard
async Shutdown() {
await Util.exec('wg-quick down wg0').catch(() => { });
}
};
12 changes: 12 additions & 0 deletions src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
});

0 comments on commit 5977438

Please sign in to comment.