diff --git a/src/config.js b/src/config.js index a4ec3f01..40be5b05 100644 --- a/src/config.js +++ b/src/config.js @@ -39,22 +39,35 @@ if (!process.env.WG_POST_UP) { iptables -A FORWARD -o wg0 -j ACCEPT;`; if (modules.includes('ip6table_nat')) { - module.exports.WG_POST_UP += `ip6tables -t nat -A POSTROUTING -s ${module.exports.WG_DEFAULT_ADDRESS6.replace('x', '0')}/64 -o ${module.exports.WG_DEVICE} -j MASQUERADE; - ip6tables -A INPUT -p udp -m udp --dport 51820 -j ACCEPT; - ip6tables -A FORWARD -i wg0 -j ACCEPT; - ip6tables -A FORWARD -o wg0 -j ACCEPT;`; + module.exports.WG_POST_UP += ` + ip6tables -t nat -A POSTROUTING -s ${module.exports.WG_DEFAULT_ADDRESS6.replace('x', '0')}/64 -o ${module.exports.WG_DEVICE} -j MASQUERADE; + ip6tables -A INPUT -p udp -m udp --dport 51820 -j ACCEPT; + ip6tables -A FORWARD -i wg0 -j ACCEPT; + ip6tables -A FORWARD -o wg0 -j ACCEPT;`; } module.exports.WG_POST_UP = module.exports.WG_POST_UP.split('\n').join(' '); } module.exports.WG_PRE_DOWN = process.env.WG_PRE_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 ${module.exports.WG_PORT} -j ACCEPT; -iptables -D FORWARD -i wg0 -j ACCEPT; -iptables -D FORWARD -o wg0 -j ACCEPT; -`.split('\n').join(' '); +module.exports.WG_POST_DOWN = process.env.WG_POST_DOWN; +if (!process.env.WG_POST_DOWN) { + module.exports.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 ${module.exports.WG_PORT} -j ACCEPT; + iptables -D FORWARD -i wg0 -j ACCEPT; + iptables -D FORWARD -o wg0 -j ACCEPT;`; + + if (modules.includes('ip6table_nat')) { + module.exports.WG_POST_UP += ` + ip6tables -t nat -D POSTROUTING -s ${module.exports.WG_DEFAULT_ADDRESS6.replace('x', '0')}/64 -o ${module.exports.WG_DEVICE} -j MASQUERADE; + ip6tables -D INPUT -p udp -m udp --dport 51820 -j ACCEPT; + ip6tables -D FORWARD -i wg0 -j ACCEPT; + ip6tables -D FORWARD -o wg0 -j ACCEPT;`; + } + + module.exports.WG_POST_UP = module.exports.WG_POST_UP.split('\n').join(' '); +} module.exports.LANG = process.env.LANG || 'en'; module.exports.UI_TRAFFIC_STATS = process.env.UI_TRAFFIC_STATS || 'false'; module.exports.UI_CHART_TYPE = process.env.UI_CHART_TYPE || 0; diff --git a/src/lib/WireGuard.js b/src/lib/WireGuard.js index b56a3b5f..d29b90bf 100644 --- a/src/lib/WireGuard.js +++ b/src/lib/WireGuard.js @@ -47,12 +47,14 @@ module.exports = class WireGuard { log: 'echo ***hidden*** | wg pubkey', }); const address = WG_DEFAULT_ADDRESS.replace('x', '1'); + const address6 = WG_DEFAULT_ADDRESS6.replace('x', '1'); config = { server: { privateKey, publicKey, address, + address6, }, clients: {}, }; @@ -145,6 +147,7 @@ ${client.preSharedKey ? `PresharedKey = ${client.preSharedKey}\n` : '' name: client.name, enabled: client.enabled, address: client.address, + address6: client.address6, publicKey: client.publicKey, createdAt: new Date(client.createdAt), updatedAt: new Date(client.updatedAt), @@ -348,6 +351,19 @@ Endpoint = ${WG_HOST}:${WG_CONFIG_PORT}`; await this.saveConfig(); } + async updateClientAddress6({ clientId, address6 }) { + const client = await this.getClient({ clientId }); + + if (!Util.isValidIPv6(address6)) { + throw new ServerError(`Invalid Address: ${address6}`, 400); + } + + client.address6 = address6; + client.updatedAt = new Date(); + + await this.saveConfig(); + } + async __reloadConfig() { await this.__buildConfig(); await this.__syncConfig(); diff --git a/src/www/js/api.js b/src/www/js/api.js index 9006f5ab..239d7e1c 100644 --- a/src/www/js/api.js +++ b/src/www/js/api.js @@ -138,6 +138,14 @@ class API { }); } + async updateClientAddress6({ clientId, address6 }) { + return this.call({ + method: 'put', + path: `/wireguard/client/${clientId}/address6/`, + body: { address6 }, + }); + } + async restoreConfiguration(file) { return this.call({ method: 'put', diff --git a/src/www/js/app.js b/src/www/js/app.js index 60be75c3..fe3551c9 100644 --- a/src/www/js/app.js +++ b/src/www/js/app.js @@ -301,6 +301,11 @@ new Vue({ .catch((err) => alert(err.message || err.toString())) .finally(() => this.refresh().catch(console.error)); }, + updateClientAddress6(client, address6) { + this.api.updateClientAddress6({ clientId: client.id, address6 }) + .catch((err) => alert(err.message || err.toString())) + .finally(() => this.refresh().catch(console.error)); + }, restoreConfig(e) { e.preventDefault(); const file = e.currentTarget.files.item(0);