|
|
@ -11,7 +11,7 @@ const Util = require('./Util'); |
|
|
const ServerError = require('./ServerError'); |
|
|
const ServerError = require('./ServerError'); |
|
|
|
|
|
|
|
|
const { |
|
|
const { |
|
|
WG_SUDO_STRING, |
|
|
SUDO_STRING, |
|
|
WG_PATH, |
|
|
WG_PATH, |
|
|
WG_HOST, |
|
|
WG_HOST, |
|
|
WG_PORT, |
|
|
WG_PORT, |
|
|
@ -42,9 +42,9 @@ module.exports = class WireGuard { |
|
|
config = JSON.parse(config); |
|
|
config = JSON.parse(config); |
|
|
debug('Configuration loaded.'); |
|
|
debug('Configuration loaded.'); |
|
|
} catch (err) { |
|
|
} catch (err) { |
|
|
const privateKey = await Util.exec(`${WG_SUDO_STRING}wg genkey`); |
|
|
const privateKey = await Util.exec(`${SUDO_STRING}wg genkey`); |
|
|
const publicKey = await Util.exec(`echo ${privateKey} | ${WG_SUDO_STRING}wg pubkey`, { |
|
|
const publicKey = await Util.exec(`echo ${privateKey} | ${SUDO_STRING}wg pubkey`, { |
|
|
log: `echo ***hidden*** | ${WG_SUDO_STRING}wg pubkey`, |
|
|
log: `echo ***hidden*** | ${SUDO_STRING}wg pubkey`, |
|
|
}); |
|
|
}); |
|
|
const address = WG_DEFAULT_ADDRESS.replace('x', '1'); |
|
|
const address = WG_DEFAULT_ADDRESS.replace('x', '1'); |
|
|
|
|
|
|
|
|
@ -60,8 +60,8 @@ module.exports = class WireGuard { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
await this.__saveConfig(config); |
|
|
await this.__saveConfig(config); |
|
|
await Util.exec(`${WG_SUDO_STRING}wg-quick down wg0`).catch(() => { }); |
|
|
await Util.exec(`${SUDO_STRING}wg-quick down wg0`).catch(() => { }); |
|
|
await Util.exec(`${WG_SUDO_STRING}wg-quick up wg0`).catch(err => { |
|
|
await Util.exec(`${SUDO_STRING}wg-quick up wg0`).catch(err => { |
|
|
if (err && err.message && err.message.includes('Cannot find device "wg0"')) { |
|
|
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 new Error('WireGuard exited with the error: Cannot find device "wg0"\nThis usually means that your host\'s kernel does not support WireGuard!'); |
|
|
} |
|
|
} |
|
|
@ -127,7 +127,7 @@ AllowedIPs = ${client.address}/32`; |
|
|
|
|
|
|
|
|
async __syncConfig() { |
|
|
async __syncConfig() { |
|
|
debug('Config syncing...'); |
|
|
debug('Config syncing...'); |
|
|
await Util.exec(`${WG_SUDO_STRING}wg syncconf wg0 <(${WG_SUDO_STRING}wg-quick strip wg0)`); |
|
|
await Util.exec(`${SUDO_STRING}wg syncconf wg0 <(${SUDO_STRING}wg-quick strip wg0)`); |
|
|
debug('Config synced.'); |
|
|
debug('Config synced.'); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@ -150,7 +150,7 @@ AllowedIPs = ${client.address}/32`; |
|
|
})); |
|
|
})); |
|
|
|
|
|
|
|
|
// Loop WireGuard status
|
|
|
// Loop WireGuard status
|
|
|
const dump = await Util.exec(`${WG_SUDO_STRING}wg show wg0 dump`, { |
|
|
const dump = await Util.exec(`${SUDO_STRING}wg show wg0 dump`, { |
|
|
log: false, |
|
|
log: false, |
|
|
}); |
|
|
}); |
|
|
dump |
|
|
dump |
|
|
@ -227,11 +227,11 @@ Endpoint = ${WG_HOST}:${WG_PORT}`; |
|
|
|
|
|
|
|
|
const config = await this.getConfig(); |
|
|
const config = await this.getConfig(); |
|
|
|
|
|
|
|
|
const privateKey = await Util.exec(`${WG_SUDO_STRING}wg genkey`); |
|
|
const privateKey = await Util.exec(`${SUDO_STRING}wg genkey`); |
|
|
const publicKey = await Util.exec(`echo ${privateKey} | ${WG_SUDO_STRING}wg pubkey`, { |
|
|
const publicKey = await Util.exec(`echo ${privateKey} | ${SUDO_STRING}wg pubkey`, { |
|
|
log: `echo ***hidden*** | ${WG_SUDO_STRING}wg pubkey`, |
|
|
log: `echo ***hidden*** | ${SUDO_STRING}wg pubkey`, |
|
|
}); |
|
|
}); |
|
|
const preSharedKey = await Util.exec(`${WG_SUDO_STRING}wg genpsk`); |
|
|
const preSharedKey = await Util.exec(`${SUDO_STRING}wg genpsk`); |
|
|
|
|
|
|
|
|
// Calculate next IP
|
|
|
// Calculate next IP
|
|
|
let address; |
|
|
let address; |
|
|
|