Browse Source

add cidr

pull/1347/head
Bernd Storath 2 years ago
parent
commit
21ac724995
  1. 17
      src/server/utils/WireGuard.ts

17
src/server/utils/WireGuard.ts

@ -6,7 +6,7 @@ import QRCode from 'qrcode';
import CRC32 from 'crc-32'; import CRC32 from 'crc-32';
import type { NewClient } from '~~/services/database/repositories/client'; import type { NewClient } from '~~/services/database/repositories/client';
import { cidrSubnet, fromLong, isV4Format, toLong } from 'ip'; import ip from 'ip';
const DEBUG = debug('WireGuard'); const DEBUG = debug('WireGuard');
@ -42,9 +42,8 @@ PostDown = ${system.iptables.PostDown}
# Client: ${client.name} (${clientId}) # Client: ${client.name} (${clientId})
[Peer] [Peer]
PublicKey = ${client.publicKey} PublicKey = ${client.publicKey}
${ PresharedKey = ${client.preSharedKey}
client.preSharedKey ? `PresharedKey = ${client.preSharedKey}\n` : '' AllowedIPs = ${client.address}/32`;
}AllowedIPs = ${client.address}/32`;
} }
DEBUG('Config saving...'); DEBUG('Config saving...');
@ -178,14 +177,14 @@ Endpoint = ${system.wgHost}:${system.wgConfigPort}`;
// TODO: cidr // TODO: cidr
// Calculate next IP // Calculate next IP
const cidr = cidrSubnet(system.userConfig.addressRange); const cidr = ip.cidrSubnet(system.userConfig.addressRange);
let address; let address;
for ( for (
let i = toLong(cidr.firstAddress); let i = ip.toLong(cidr.firstAddress);
i <= toLong(cidr.lastAddress); i <= ip.toLong(cidr.lastAddress);
i++ i++
) { ) {
const currentIp = fromLong(i); const currentIp = ip.fromLong(i);
const client = Object.values(clients).find((client) => { const client = Object.values(clients).find((client) => {
return client.address === currentIp; return client.address === currentIp;
}); });
@ -285,7 +284,7 @@ Endpoint = ${system.wgHost}:${system.wgConfigPort}`;
clientId: string; clientId: string;
address: string; address: string;
}) { }) {
if (!isV4Format(address)) { if (!ip.isV4Format(address)) {
throw createError({ throw createError({
statusCode: 400, statusCode: 400,
statusMessage: `Invalid Address: ${address}`, statusMessage: `Invalid Address: ${address}`,

Loading…
Cancel
Save