@ -27,54 +27,60 @@ const {
module . exports = class WireGuard {
async __ buildConfig ( ) {
this . __ configPromise = Promise . resolve ( ) . then ( async ( ) => {
if ( ! WG_HOST ) {
throw new Error ( 'WG_HOST Environment Variable Not Set!' ) ;
}
debug ( 'Loading configuration...' ) ;
let config ;
try {
config = await fs . readFile ( path . join ( WG_PATH , 'wg0.json' ) , 'utf8' ) ;
config = JSON . parse ( config ) ;
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 ,
} ,
clients : { } ,
} ;
debug ( 'Configuration generated.' ) ;
}
return config ;
} ) ;
return this . __ configPromise ;
}
async getConfig ( ) {
if ( ! this . __ configPromise ) {
this . __ configPromise = Promise . resolve ( ) . then ( async ( ) => {
if ( ! WG_HOST ) {
throw new Error ( 'WG_HOST Environment Variable Not Set!' ) ;
}
const config = await this . __ buildConfig ( ) ;
debug ( 'Loading configuration...' ) ;
let config ;
try {
config = await fs . readFile ( path . join ( WG_PATH , 'wg0.json' ) , 'utf8' ) ;
config = JSON . parse ( config ) ;
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 ,
} ,
clients : { } ,
} ;
debug ( 'Configuration generated.' ) ;
await this . __ saveConfig ( config ) ;
await Util . exec ( 'wg-quick down wg0' ) . catch ( ( ) => { } ) ;
await Util . exec ( 'wg-quick up wg0' ) . catch ( ( err ) => {
if ( err && err . message && err . message . includes ( 'Cannot find device "wg0"' ) ) {
throw new Error ( 'WireGuard exited with the error: Cannot find device "wg0"\nThis usually means that your host\'s kernel does not support WireGuard!' ) ;
}
await this . __ saveConfig ( config ) ;
await Util . exec ( 'wg-quick down wg0' ) . catch ( ( ) => { } ) ;
await Util . exec ( 'wg-quick up wg0' ) . catch ( ( err ) => {
if ( err && err . message && err . message . includes ( 'Cannot find device "wg0"' ) ) {
throw new Error ( 'WireGuard exited with the error: Cannot find device "wg0"\nThis usually means that your host\'s kernel does not support 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 -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');
await this . __ syncConfig ( ) ;
return config ;
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 -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');
await this . __ syncConfig ( ) ;
}
return this . __ configPromise ;
@ -227,7 +233,9 @@ Endpoint = ${WG_HOST}:${WG_CONFIG_PORT}`;
const config = await this . getConfig ( ) ;
const privateKey = await Util . exec ( 'wg genkey' ) ;
const publicKey = await Util . exec ( ` echo ${ privateKey } | wg pubkey ` ) ;
const publicKey = await Util . exec ( ` echo ${ privateKey } | wg pubkey ` , {
log : 'echo ***hidden*** | wg pubkey' ,
} ) ;
const preSharedKey = await Util . exec ( 'wg genpsk' ) ;
// Calculate next IP
@ -319,25 +327,30 @@ Endpoint = ${WG_HOST}:${WG_CONFIG_PORT}`;
await this . saveConfig ( ) ;
}
async ___ forceRestart ( ) {
this . __ configPromise = null ;
await this . save Config( ) ;
async __ reloadConfig ( ) {
await this . __ buildConfig ( ) ;
await this . __ sync Config( ) ;
}
async restoreConfiguration ( config ) {
debug ( 'Starting configuration restore process.' ) ;
const _ config = JSON . parse ( config ) ;
await this . __ saveConfig ( _ config ) ;
await this . ___ forceRestart ( ) ;
await this . __ reloadConfig ( ) ;
debug ( 'Configuration restore process completed.' ) ;
}
async backupConfiguration ( ) {
debug ( 'Starting configuration backup.' ) ;
const config = await this . getConfig ( ) ;
return JSON . stringify ( config , null , 2 ) ;
const backup = JSON . stringify ( config , null , 2 ) ;
debug ( 'Configuration backup completed.' ) ;
return backup ;
}
// Shutdown wireguard
async Shutdown ( ) {
await Util . exec ( 'wg-quick down wg0' ) . catch ( ( ) => { } ) ;
await Util . exec ( 'wg-quick down wg0' ) . catch ( ( ) => { } ) ;
}
} ;