@ -4,6 +4,7 @@ const fs = require('node:fs/promises');
const path = require ( 'path' ) ;
const debug = require ( 'debug' ) ( 'WireGuard' ) ;
const crypto = require ( 'node:crypto' ) ;
const ip = require ( 'ip' ) ;
const QRCode = require ( 'qrcode' ) ;
const Util = require ( './Util' ) ;
@ -16,9 +17,12 @@ const {
WG_CONFIG_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 ,
@ -36,22 +40,32 @@ module.exports = class WireGuard {
debug ( 'Loading configuration...' ) ;
let config ;
const address = WG_SERVER_ADDRESS ;
const cidrBlock = WG_DEFAULT_ADDRESS_RANGE ;
try {
config = await fs . readFile ( path . join ( WG_PATH , 'wg0.json' ) , 'utf8' ) ;
config = JSON . parse ( config ) ;
config = {
... config ,
server : {
... config . server ,
address ,
cidrBlock ,
} ,
} ;
debug ( 'Configuration loaded.' ) ;
} catch ( err ) {
const privateKey = await Util . exec ( 'wg genkey' ) ;
const publicKey = await Util . exec ( ` echo ${ privateKey } | wg pubkey ` , {
log : 'echo ***hidden*** | wg pubkey' ,
} ) ;
const address = WG_DEFAULT_ADDRESS . replace ( 'x' , '1' ) ;
config = {
server : {
privateKey ,
publicKey ,
address ,
cidrBlock ,
} ,
clients : { } ,
} ;
@ -67,7 +81,7 @@ module.exports = class WireGuard {
throw err ;
} ) ;
// await Util.exec(`iptables -t nat -A POSTROUTING -s ${WG_DEFAULT_ADDRESS.replace('x', '0')}/24 -o ' + WG_DEVICE + ' -j MASQUERADE`);
// await Util.exec(`iptables -t nat -A POSTROUTING -s ${WG_SERVER_ADDRESS}/${WG_DEFAULT_ADDRESS_RANGE} -o ' + WG_DEVICE + ' -j MASQUERADE`);
// await Util.exec('iptables -A INPUT -p udp -m udp --dport 51820 -j ACCEPT');
// await Util.exec('iptables -A FORWARD -i wg0 -j ACCEPT');
// await Util.exec('iptables -A FORWARD -o wg0 -j ACCEPT');
@ -94,7 +108,7 @@ module.exports = class WireGuard {
# Server
[ Interface ]
PrivateKey = $ { config . server . privateKey }
Address = $ { config . server . address } / 24
Address = $ { config . server . address } / $ { config . server . cidrBlock }
ListenPort = $ { WG_PORT }
PreUp = $ { WG_PRE_UP }
PostUp = $ { WG_POST_UP }
@ -106,12 +120,12 @@ PostDown = ${WG_POST_DOWN}
if ( ! client . enabled ) continue ;
result += `
# Client : $ { client . name } ( $ { clientId } )
[ Peer ]
PublicKey = $ { client . publicKey }
$ { client . preSharedKey ? ` PresharedKey = ${ client . preSharedKey } \n ` : ''
} AllowedIPs = $ { client . address } / 32 ` ;
$ { client . preSharedKey ? ` PresharedKey = ${ client . preSharedKey } ` : '' }
AllowedIPs = $ { client . address } / 32
` ;
}
debug ( 'Config saving...' ) ;
@ -137,6 +151,7 @@ ${client.preSharedKey ? `PresharedKey = ${client.preSharedKey}\n` : ''
name : client . name ,
enabled : client . enabled ,
address : client . address ,
cidrBlock : client . cidrBlock ,
publicKey : client . publicKey ,
createdAt : new Date ( client . createdAt ) ,
updatedAt : new Date ( client . updatedAt ) ,
@ -199,14 +214,14 @@ ${client.preSharedKey ? `PresharedKey = ${client.preSharedKey}\n` : ''
return `
[ Interface ]
PrivateKey = $ { client . privateKey ? ` ${ client . privateKey } ` : 'REPLACE_ME' }
Address = $ { client . address } / 24
Address = $ { client . address } / $ { client . cidrBlock }
$ { WG_DEFAULT_DNS ? ` DNS = ${ WG_DEFAULT_DNS } \n ` : '' } \
$ { WG_MTU ? ` MTU = ${ WG_MTU } \n ` : '' } \
[ Peer ]
PublicKey = $ { config . server . publicKey }
$ { client . preSharedKey ? ` PresharedKey = ${ client . preSharedKey } \n ` : ''
} AllowedIPs = $ { WG_ALLOWED_IPS }
$ { client . preSharedKey ? ` PresharedKey = ${ client . preSharedKey } ` : '' }
AllowedIPs = $ { WG_ALLOWED_IPS }
PersistentKeepalive = $ { WG_PERSISTENT_KEEPALIVE }
Endpoint = $ { WG_HOST } : $ { WG_CONFIG_PORT } ` ;
}
@ -232,13 +247,14 @@ Endpoint = ${WG_HOST}:${WG_CONFIG_PORT}`;
// Calculate 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 ;
}
}
@ -249,10 +265,12 @@ Endpoint = ${WG_HOST}:${WG_CONFIG_PORT}`;
// Create Client
const id = crypto . randomUUID ( ) ;
const cidrBlock = WG_DEFAULT_ADDRESS_RANGE ;
const client = {
id ,
name ,
address ,
cidrBlock ,
privateKey ,
publicKey ,
preSharedKey ,
@ -309,7 +327,7 @@ Endpoint = ${WG_HOST}:${WG_CONFIG_PORT}`;
async updateClientAddress ( { clientId , address } ) {
const client = await this . getClient ( { clientId } ) ;
if ( ! Util . isValidIPv4 ( address ) ) {
if ( ! ip . isV4Format ( address ) ) {
throw new ServerError ( ` Invalid Address: ${ address } ` , 400 ) ;
}