@ -4,6 +4,7 @@ const fs = require('fs').promises;
const path = require ( 'path' ) ;
const path = require ( 'path' ) ;
const debug = require ( 'debug' ) ( 'WireGuard' ) ;
const debug = require ( 'debug' ) ( 'WireGuard' ) ;
const ip = require ( 'ip' ) ;
const uuid = require ( 'uuid' ) ;
const uuid = require ( 'uuid' ) ;
const QRCode = require ( 'qrcode' ) ;
const QRCode = require ( 'qrcode' ) ;
@ -16,9 +17,12 @@ const {
WG_PORT ,
WG_PORT ,
WG_MTU ,
WG_MTU ,
WG_DEFAULT_DNS ,
WG_DEFAULT_DNS ,
WG_DEFAULT_ADDRESS ,
WG_DEFAULT_ADDRESS_RANGE ,
WG_PERSISTENT_KEEPALIVE ,
WG_PERSISTENT_KEEPALIVE ,
WG_ALLOWED_IPS ,
WG_ALLOWED_IPS ,
WG_SERVER_ADDRESS ,
WG_CLIENT_FIRST_ADDRESS ,
WG_CLIENT_LAST_ADDRESS ,
WG_PRE_UP ,
WG_PRE_UP ,
WG_POST_UP ,
WG_POST_UP ,
WG_PRE_DOWN ,
WG_PRE_DOWN ,
@ -45,13 +49,15 @@ 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 = WG_SERVER_ADDRESS ;
const cidrBlock = WG_DEFAULT_ADDRESS_RANGE ;
config = {
config = {
server : {
server : {
privateKey ,
privateKey ,
publicKey ,
publicKey ,
address ,
address ,
cidrBlock ,
} ,
} ,
clients : { } ,
clients : { } ,
} ;
} ;
@ -94,7 +100,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 } / $ { config . server . cidrBlock }
ListenPort = 51820
ListenPort = 51820
PreUp = $ { WG_PRE_UP }
PreUp = $ { WG_PRE_UP }
PostUp = $ { WG_POST_UP }
PostUp = $ { WG_POST_UP }
@ -229,15 +235,16 @@ Endpoint = ${WG_HOST}:${WG_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
// find next IP
let address ;
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 ) => {
const client = Object . values ( config . clients ) . find ( ( client ) => {
return client . address === WG_DEFAULT_ADDRESS . replace ( 'x' , i ) ;
return client . address === currentIp ;
} ) ;
} ) ;
if ( ! client ) {
if ( ! client ) {
address = WG_DEFAULT_ADDRESS . replace ( 'x' , i ) ;
address = currentIp ;
break ;
break ;
}
}
}
}