Browse Source

add CIDR

pull/1087/head
gganster 2 years ago
committed by Philip H
parent
commit
f480a4180c
  1. 7
      src/config.js
  2. 4
      src/lib/Util.js
  3. 43
      src/lib/WireGuard.js

7
src/config.js

@ -1,6 +1,7 @@
'use strict';
const { release } = require('./package.json');
const Util = require('./lib/Util');
module.exports.RELEASE = release;
module.exports.PORT = process.env.PORT || '51821';
@ -15,14 +16,16 @@ module.exports.WG_CONFIG_PORT = process.env.WG_CONFIG_PORT || process.env.WG_POR
module.exports.WG_MTU = process.env.WG_MTU || null;
module.exports.WG_PERSISTENT_KEEPALIVE = process.env.WG_PERSISTENT_KEEPALIVE || '0';
module.exports.WG_DEFAULT_ADDRESS = process.env.WG_DEFAULT_ADDRESS || '10.8.0.x';
module.exports.WG_CIDR = 40 - (module.exports.WG_DEFAULT_ADDRESS.split('x').length * 8);
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_PRE_UP = process.env.WG_PRE_UP || '';
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 ${module.exports.WG_DEVICE} -j MASQUERADE;
iptables -t nat -A POSTROUTING -s ${Util.replaceAll(module.exports.WG_DEFAULT_ADDRESS, 'x', '0')}/${module.exports.WG_CIDR} -o ${module.exports.WG_DEVICE} -j MASQUERADE;
iptables -A INPUT -p udp -m udp --dport ${module.exports.WG_PORT} -j ACCEPT;
iptables -A FORWARD -i wg0 -j ACCEPT;
iptables -A FORWARD -o wg0 -j ACCEPT;
@ -30,7 +33,7 @@ iptables -A FORWARD -o wg0 -j ACCEPT;
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 -t nat -D POSTROUTING -s ${Util.replaceAll(module.exports.WG_DEFAULT_ADDRESS, 'x', '0')}/${module.exports.WG_CIDR} -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;

4
src/lib/Util.js

@ -77,4 +77,8 @@ module.exports = class Util {
});
}
static replaceAll(str, match, substitute) {
return str.split(match).join(substitute);
}
};

43
src/lib/WireGuard.js

@ -17,6 +17,7 @@ const {
WG_MTU,
WG_DEFAULT_DNS,
WG_DEFAULT_ADDRESS,
WG_CIDR,
WG_PERSISTENT_KEEPALIVE,
WG_ALLOWED_IPS,
WG_PRE_UP,
@ -45,7 +46,7 @@ module.exports = class WireGuard {
const publicKey = await Util.exec(`echo ${privateKey} | wg pubkey`, {
log: 'echo ***hidden*** | wg pubkey',
});
const address = WG_DEFAULT_ADDRESS.replace('x', '1');
const address = Util.replaceAll(WG_DEFAULT_ADDRESS, "x", "1");
config = {
server: {
@ -87,6 +88,7 @@ module.exports = class WireGuard {
}
async __saveConfig(config) {
let result = `
# Note: Do not edit this file directly.
# Your changes will be overwritten!
@ -94,7 +96,7 @@ module.exports = class WireGuard {
# Server
[Interface]
PrivateKey = ${config.server.privateKey}
Address = ${config.server.address}/24
Address = ${config.server.address}/${WG_CIDR}
ListenPort = ${WG_PORT}
PreUp = ${WG_PRE_UP}
PostUp = ${WG_POST_UP}
@ -199,7 +201,7 @@ ${client.preSharedKey ? `PresharedKey = ${client.preSharedKey}\n` : ''
return `
[Interface]
PrivateKey = ${client.privateKey ? `${client.privateKey}` : 'REPLACE_ME'}
Address = ${client.address}/24
Address = ${client.address}/${WG_CIDR}
${WG_DEFAULT_DNS ? `DNS = ${WG_DEFAULT_DNS}\n` : ''}\
${WG_MTU ? `MTU = ${WG_MTU}\n` : ''}\
@ -230,18 +232,39 @@ Endpoint = ${WG_HOST}:${WG_CONFIG_PORT}`;
const publicKey = await Util.exec(`echo ${privateKey} | wg pubkey`);
const preSharedKey = await Util.exec('wg genpsk');
// Calculate next IP
let address;
for (let i = 2; i < 255; i++) {
/* ------ compute next IP address ------*/
const recursiveFindIpAdress = (o1, o2, o3, o4) => {
if (o4 > 255) {
if (WG_CIDR === 24)
return null;
o3++;
o4 = 0;
}
if (o3 > 255) {
if (WG_CIDR === 16)
return null;
o2++;
o3 = 0;
}
if (o2 > 255) {
if (WG_CIDR === 8)
return null;
o1++;
o2 = 0;
}
const client = Object.values(config.clients).find((client) => {
return client.address === WG_DEFAULT_ADDRESS.replace('x', i);
return client.address === `${o1}.${o2}.${o3}.${o4}`;
});
if (!client) {
address = WG_DEFAULT_ADDRESS.replace('x', i);
break;
return `${o1}.${o2}.${o3}.${o4}`;
}
return recursiveFindIpAdress(o1, o2, o3, o4 + 1);
}
const [o1, o2, o3, o4] = WG_DEFAULT_ADDRESS.split('.').map(i => (
i === 'x' ? 0 : parseInt(i, 10)
));
const address = recursiveFindIpAdress(o1, o2, o3, 2);
/*------ end compute next IP address ------*/
if (!address) {
throw new Error('Maximum number of clients reached.');

Loading…
Cancel
Save