|
|
@ -16,7 +16,9 @@ const { |
|
|
WG_PORT, |
|
|
WG_PORT, |
|
|
WG_MTU, |
|
|
WG_MTU, |
|
|
WG_DEFAULT_DNS, |
|
|
WG_DEFAULT_DNS, |
|
|
|
|
|
WG_DEFAULT_DNS6, |
|
|
WG_DEFAULT_ADDRESS, |
|
|
WG_DEFAULT_ADDRESS, |
|
|
|
|
|
WG_DEFAULT_ADDRESS6, |
|
|
WG_PERSISTENT_KEEPALIVE, |
|
|
WG_PERSISTENT_KEEPALIVE, |
|
|
WG_ALLOWED_IPS, |
|
|
WG_ALLOWED_IPS, |
|
|
WG_PRE_UP, |
|
|
WG_PRE_UP, |
|
|
@ -46,12 +48,14 @@ module.exports = class WireGuard { |
|
|
log: 'echo ***hidden*** | wg pubkey', |
|
|
log: 'echo ***hidden*** | wg pubkey', |
|
|
}); |
|
|
}); |
|
|
const address = WG_DEFAULT_ADDRESS.replace('x', '1'); |
|
|
const address = WG_DEFAULT_ADDRESS.replace('x', '1'); |
|
|
|
|
|
const address6 = WG_DEFAULT_ADDRESS6.replace('x', '1'); |
|
|
|
|
|
|
|
|
config = { |
|
|
config = { |
|
|
server: { |
|
|
server: { |
|
|
privateKey, |
|
|
privateKey, |
|
|
publicKey, |
|
|
publicKey, |
|
|
address, |
|
|
address, |
|
|
|
|
|
address6, |
|
|
}, |
|
|
}, |
|
|
clients: {}, |
|
|
clients: {}, |
|
|
}; |
|
|
}; |
|
|
@ -67,10 +71,6 @@ module.exports = class WireGuard { |
|
|
|
|
|
|
|
|
throw err; |
|
|
throw err; |
|
|
}); |
|
|
}); |
|
|
// await Util.exec(`iptables -t nat -A POSTROUTING -s ${WG_DEFAULT_ADDRESS.replace('x', '0')}/24 -o eth0 -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(); |
|
|
await this.__syncConfig(); |
|
|
|
|
|
|
|
|
return config; |
|
|
return config; |
|
|
@ -94,7 +94,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}/24, ${config.server.address6}/64 |
|
|
ListenPort = ${WG_PORT} |
|
|
ListenPort = ${WG_PORT} |
|
|
PreUp = ${WG_PRE_UP} |
|
|
PreUp = ${WG_PRE_UP} |
|
|
PostUp = ${WG_POST_UP} |
|
|
PostUp = ${WG_POST_UP} |
|
|
@ -111,7 +111,7 @@ PostDown = ${WG_POST_DOWN} |
|
|
[Peer] |
|
|
[Peer] |
|
|
PublicKey = ${client.publicKey} |
|
|
PublicKey = ${client.publicKey} |
|
|
PresharedKey = ${client.preSharedKey} |
|
|
PresharedKey = ${client.preSharedKey} |
|
|
AllowedIPs = ${client.address}/32`;
|
|
|
AllowedIPs = ${client.address}/32, ${client.address6}/128`;
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
debug('Config saving...'); |
|
|
debug('Config saving...'); |
|
|
@ -137,6 +137,7 @@ AllowedIPs = ${client.address}/32`; |
|
|
name: client.name, |
|
|
name: client.name, |
|
|
enabled: client.enabled, |
|
|
enabled: client.enabled, |
|
|
address: client.address, |
|
|
address: client.address, |
|
|
|
|
|
address6: client.address6, |
|
|
publicKey: client.publicKey, |
|
|
publicKey: client.publicKey, |
|
|
createdAt: new Date(client.createdAt), |
|
|
createdAt: new Date(client.createdAt), |
|
|
updatedAt: new Date(client.updatedAt), |
|
|
updatedAt: new Date(client.updatedAt), |
|
|
@ -195,12 +196,15 @@ AllowedIPs = ${client.address}/32`; |
|
|
async getClientConfiguration({ clientId }) { |
|
|
async getClientConfiguration({ clientId }) { |
|
|
const config = await this.getConfig(); |
|
|
const config = await this.getConfig(); |
|
|
const client = await this.getClient({ clientId }); |
|
|
const client = await this.getClient({ clientId }); |
|
|
|
|
|
const isDnsSet = WG_DEFAULT_DNS || WG_DEFAULT_DNS6; |
|
|
|
|
|
const dnsServers = [WG_DEFAULT_DNS, WG_DEFAULT_DNS6].filter(item => !!item).join(', ') |
|
|
|
|
|
|
|
|
return ` |
|
|
return ` |
|
|
[Interface] |
|
|
[Interface] |
|
|
PrivateKey = ${client.privateKey} |
|
|
PrivateKey = ${client.privateKey} |
|
|
Address = ${client.address}/24 |
|
|
Address = ${client.address}/24, ${client.address6}/64 |
|
|
${WG_DEFAULT_DNS ? `DNS = ${WG_DEFAULT_DNS}` : ''} |
|
|
ListenPort = ${WG_PORT} |
|
|
|
|
|
${isDnsSet ? `DNS = ${dnsServers}` : ''} |
|
|
${WG_MTU ? `MTU = ${WG_MTU}` : ''} |
|
|
${WG_MTU ? `MTU = ${WG_MTU}` : ''} |
|
|
|
|
|
|
|
|
[Peer] |
|
|
[Peer] |
|
|
@ -230,7 +234,7 @@ 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
|
|
|
// Calculate next IPv4
|
|
|
let address; |
|
|
let address; |
|
|
for (let i = 2; i < 255; i++) { |
|
|
for (let i = 2; i < 255; i++) { |
|
|
const client = Object.values(config.clients).find((client) => { |
|
|
const client = Object.values(config.clients).find((client) => { |
|
|
@ -247,12 +251,30 @@ Endpoint = ${WG_HOST}:${WG_PORT}`; |
|
|
throw new Error('Maximum number of clients reached.'); |
|
|
throw new Error('Maximum number of clients reached.'); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Calculate next IPv6
|
|
|
|
|
|
let address6; |
|
|
|
|
|
for (let i = 2; i < 255; i++) { |
|
|
|
|
|
const client = Object.values(config.clients).find((client) => { |
|
|
|
|
|
return client.address6 === WG_DEFAULT_ADDRESS6.replace('x', i); |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
if (!client) { |
|
|
|
|
|
address6 = WG_DEFAULT_ADDRESS6.replace('x', i); |
|
|
|
|
|
break; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (!address6) { |
|
|
|
|
|
throw new Error('Maximum number of clients reached.'); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
// Create Client
|
|
|
// Create Client
|
|
|
const id = uuid.v4(); |
|
|
const id = uuid.v4(); |
|
|
const client = { |
|
|
const client = { |
|
|
id, |
|
|
id, |
|
|
name, |
|
|
name, |
|
|
address, |
|
|
address, |
|
|
|
|
|
address6, |
|
|
privateKey, |
|
|
privateKey, |
|
|
publicKey, |
|
|
publicKey, |
|
|
preSharedKey, |
|
|
preSharedKey, |
|
|
@ -319,4 +341,17 @@ Endpoint = ${WG_HOST}:${WG_PORT}`; |
|
|
await this.saveConfig(); |
|
|
await this.saveConfig(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
async updateClientaddress6({ clientId, address6 }) { |
|
|
|
|
|
const client = await this.getClient({ clientId }); |
|
|
|
|
|
|
|
|
|
|
|
if (!Util.isValidIPv6(address6)) { |
|
|
|
|
|
throw new ServerError(`Invalid Address: ${address6}`, 400); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
client.address6 = address6; |
|
|
|
|
|
client.updatedAt = new Date(); |
|
|
|
|
|
|
|
|
|
|
|
await this.saveConfig(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
}; |
|
|
}; |
|
|
|