|
@ -6,6 +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 ip from 'ip'; |
|
|
|
|
|
|
|
|
const DEBUG = debug('WireGuard'); |
|
|
const DEBUG = debug('WireGuard'); |
|
|
|
|
|
|
|
@ -18,6 +19,9 @@ class WireGuard { |
|
|
async #saveWireguardConfig() { |
|
|
async #saveWireguardConfig() { |
|
|
const system = await Database.getSystem(); |
|
|
const system = await Database.getSystem(); |
|
|
const clients = await Database.getClients(); |
|
|
const clients = await Database.getClients(); |
|
|
|
|
|
const cidrBlock = ip.cidrSubnet( |
|
|
|
|
|
system.userConfig.addressRange |
|
|
|
|
|
).subnetMaskLength; |
|
|
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! |
|
@ -25,7 +29,7 @@ class WireGuard { |
|
|
# Server |
|
|
# Server |
|
|
[Interface] |
|
|
[Interface] |
|
|
PrivateKey = ${system.interface.privateKey} |
|
|
PrivateKey = ${system.interface.privateKey} |
|
|
Address = ${system.interface.address}/24 |
|
|
Address = ${system.interface.address}/${cidrBlock} |
|
|
ListenPort = ${system.wgPort} |
|
|
ListenPort = ${system.wgPort} |
|
|
PreUp = ${system.iptables.PreUp} |
|
|
PreUp = ${system.iptables.PreUp} |
|
|
PostUp = ${system.iptables.PostUp} |
|
|
PostUp = ${system.iptables.PostUp} |
|
@ -41,9 +45,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...'); |
|
@ -134,8 +137,8 @@ ${ |
|
|
|
|
|
|
|
|
return ` |
|
|
return ` |
|
|
[Interface] |
|
|
[Interface] |
|
|
PrivateKey = ${client.privateKey ? `${client.privateKey}` : 'REPLACE_ME'} |
|
|
PrivateKey = ${client.privateKey} |
|
|
Address = ${client.address}/24 |
|
|
Address = ${client.address} |
|
|
DNS = ${system.userConfig.defaultDns.join(',')} |
|
|
DNS = ${system.userConfig.defaultDns.join(',')} |
|
|
MTU = ${system.userConfig.mtu} |
|
|
MTU = ${system.userConfig.mtu} |
|
|
|
|
|
|
|
@ -175,19 +178,21 @@ Endpoint = ${system.wgHost}:${system.wgConfigPort}`; |
|
|
}); |
|
|
}); |
|
|
const preSharedKey = await exec('wg genpsk'); |
|
|
const preSharedKey = await exec('wg genpsk'); |
|
|
|
|
|
|
|
|
// TODO: cidr
|
|
|
|
|
|
// Calculate next IP
|
|
|
// Calculate next IP
|
|
|
|
|
|
const cidr = ip.cidrSubnet(system.userConfig.addressRange); |
|
|
let address; |
|
|
let address; |
|
|
for (let i = 2; i < 255; i++) { |
|
|
for ( |
|
|
|
|
|
let i = ip.toLong(cidr.firstAddress) + 1; |
|
|
|
|
|
i <= ip.toLong(cidr.lastAddress) - 1; |
|
|
|
|
|
i++ |
|
|
|
|
|
) { |
|
|
|
|
|
const currentIp = ip.fromLong(i); |
|
|
const client = Object.values(clients).find((client) => { |
|
|
const client = Object.values(clients).find((client) => { |
|
|
return ( |
|
|
return client.address === currentIp; |
|
|
client.address === |
|
|
|
|
|
system.userConfig.addressRange.replace('x', i.toString()) |
|
|
|
|
|
); |
|
|
|
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
if (!client) { |
|
|
if (!client) { |
|
|
address = system.userConfig.addressRange.replace('x', i.toString()); |
|
|
address = currentIp; |
|
|
break; |
|
|
break; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
@ -281,7 +286,7 @@ Endpoint = ${system.wgHost}:${system.wgConfigPort}`; |
|
|
clientId: string; |
|
|
clientId: string; |
|
|
address: string; |
|
|
address: string; |
|
|
}) { |
|
|
}) { |
|
|
if (!isValidIPv4(address)) { |
|
|
if (!ip.isV4Format(address)) { |
|
|
throw createError({ |
|
|
throw createError({ |
|
|
statusCode: 400, |
|
|
statusCode: 400, |
|
|
statusMessage: `Invalid Address: ${address}`, |
|
|
statusMessage: `Invalid Address: ${address}`, |
|
|