Skip to content

Commit

Permalink
add WG_POST_UP, WG_POST_DOWN
Browse files Browse the repository at this point in the history
  • Loading branch information
Emile Nijssen committed Jan 11, 2022
1 parent e1582ec commit a97142b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
9 changes: 9 additions & 0 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,12 @@ module.exports.WG_DEFAULT_DNS = typeof process.env.WG_DEFAULT_DNS === 'string'
? process.env.WG_DEFAULT_DNS
: '1.1.1.1';
module.exports.WG_ALLOWED_IPS = process.env.WG_ALLOWED_IPS || '0.0.0.0/0, ::/0';

module.exports.WG_POST_UP = process.env.WG_POST_UP || `
iptables -t nat -A POSTROUTING -s ${module.exports.WG_DEFAULT_ADDRESS.replace('x', '0')}/24 -o eth0 -j MASQUERADE;
iptables -A INPUT -p udp -m udp --dport 51820 -j ACCEPT;
iptables -A FORWARD -i wg0 -j ACCEPT;
iptables -A FORWARD -o wg0 -j ACCEPT;
`.split('\n').join(' ');

module.exports.WG_POST_DOWN = process.env.WG_POST_DOWN || '';
19 changes: 12 additions & 7 deletions src/lib/WireGuard.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ const {
WG_DEFAULT_ADDRESS,
WG_PERSISTENT_KEEPALIVE,
WG_ALLOWED_IPS,
WG_POST_UP,
WG_POST_DOWN,
} = require('../config');

module.exports = class WireGuard {
Expand Down Expand Up @@ -63,10 +65,10 @@ module.exports = class WireGuard {

throw err;
});
await Util.exec(`iptables -t nat -A POSTROUTING -s ${WG_DEFAULT_ADDRESS.replace('x', '0')}/24 -o eth0 -j MASQUERADE`);
await Util.exec('iptables -A INPUT -p udp -m udp --dport 51820 -j ACCEPT');
await Util.exec('iptables -A FORWARD -i wg0 -j ACCEPT');
await Util.exec('iptables -A FORWARD -o wg0 -j ACCEPT');
// await Util.exec(`iptables -t nat -A POSTROUTING -s ${WG_DEFAULT_ADDRESS.replace('x', '0')}/24 -o eth0 -j MASQUERADE`);
// await Util.exec('iptables -A INPUT -p udp -m udp --dport 51820 -j ACCEPT');
// await Util.exec('iptables -A FORWARD -i wg0 -j ACCEPT');
// await Util.exec('iptables -A FORWARD -o wg0 -j ACCEPT');
await this.__syncConfig();

return config;
Expand All @@ -91,7 +93,10 @@ module.exports = class WireGuard {
[Interface]
PrivateKey = ${config.server.privateKey}
Address = ${config.server.address}/24
ListenPort = 51820`;
ListenPort = 51820
PostUp = ${WG_POST_UP}
PostDown = ${WG_POST_DOWN}
`;

for (const [clientId, client] of Object.entries(config.clients)) {
if (!client.enabled) continue;
Expand All @@ -105,7 +110,7 @@ PresharedKey = ${client.preSharedKey}
AllowedIPs = ${client.address}/32`;
}

debug('Saving config...');
debug('Config saving...');
await fs.writeFile(path.join(WG_PATH, 'wg0.json'), JSON.stringify(config, false, 2), {
mode: 0o660,
});
Expand All @@ -116,7 +121,7 @@ AllowedIPs = ${client.address}/32`;
}

async __syncConfig() {
debug('Syncing config...');
debug('Config syncing...');
await Util.exec('wg syncconf wg0 <(wg-quick strip wg0)');
debug('Config synced.');
}
Expand Down

0 comments on commit a97142b

Please sign in to comment.