forked from mirrors/amnezia-wg-easy
introduce WG_DEFAULT_ADDRESS_RANGE (CIDR notation)
This PR allows the use of Address Ranges using the CIDR notation. To make it backward compatible, i introduced a new env variable WG_DEFAULT_ADDRESS_RANGE (defaults to the previous default of 24). This allows the usage of smaller subnets (or possibly larger; but i didn't test that due to restrictions on my network). Client IPs will be calculated with correct IP addresses instead of making assumptions of the address space.
This commit is contained in:
parent
3a4564a508
commit
577af9947d
7 changed files with 33 additions and 8 deletions
|
@ -4,6 +4,7 @@ const fs = require('fs').promises;
|
|||
const path = require('path');
|
||||
|
||||
const debug = require('debug')('WireGuard');
|
||||
const ip = require('ip');
|
||||
const uuid = require('uuid');
|
||||
const QRCode = require('qrcode');
|
||||
|
||||
|
@ -16,9 +17,12 @@ const {
|
|||
WG_PORT,
|
||||
WG_MTU,
|
||||
WG_DEFAULT_DNS,
|
||||
WG_DEFAULT_ADDRESS,
|
||||
WG_DEFAULT_ADDRESS_RANGE,
|
||||
WG_PERSISTENT_KEEPALIVE,
|
||||
WG_ALLOWED_IPS,
|
||||
WG_SERVER_ADDRESS,
|
||||
WG_CLIENT_FIRST_ADDRESS,
|
||||
WG_CLIENT_LAST_ADDRESS,
|
||||
WG_PRE_UP,
|
||||
WG_POST_UP,
|
||||
WG_PRE_DOWN,
|
||||
|
@ -45,13 +49,15 @@ 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 = WG_SERVER_ADDRESS;
|
||||
const cidrBlock = WG_DEFAULT_ADDRESS_RANGE;
|
||||
|
||||
config = {
|
||||
server: {
|
||||
privateKey,
|
||||
publicKey,
|
||||
address,
|
||||
cidrBlock,
|
||||
},
|
||||
clients: {},
|
||||
};
|
||||
|
@ -94,7 +100,7 @@ module.exports = class WireGuard {
|
|||
# Server
|
||||
[Interface]
|
||||
PrivateKey = ${config.server.privateKey}
|
||||
Address = ${config.server.address}/24
|
||||
Address = ${config.server.address}/${config.server.cidrBlock}
|
||||
ListenPort = 51820
|
||||
PreUp = ${WG_PRE_UP}
|
||||
PostUp = ${WG_POST_UP}
|
||||
|
@ -229,15 +235,16 @@ Endpoint = ${WG_HOST}:${WG_PORT}`;
|
|||
const publicKey = await Util.exec(`echo ${privateKey} | wg pubkey`);
|
||||
const preSharedKey = await Util.exec('wg genpsk');
|
||||
|
||||
// Calculate next IP
|
||||
// find next IP
|
||||
let address;
|
||||
for (let i = 2; i < 255; i++) {
|
||||
for (let i = WG_CLIENT_FIRST_ADDRESS; i <= WG_CLIENT_LAST_ADDRESS; i++) {
|
||||
const currentIp = ip.fromLong(i);
|
||||
const client = Object.values(config.clients).find((client) => {
|
||||
return client.address === WG_DEFAULT_ADDRESS.replace('x', i);
|
||||
return client.address === currentIp;
|
||||
});
|
||||
|
||||
if (!client) {
|
||||
address = WG_DEFAULT_ADDRESS.replace('x', i);
|
||||
address = currentIp;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue