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