mirror of https://github.com/wg-easy/wg-easy
4 changed files with 147 additions and 77 deletions
@ -0,0 +1,111 @@ |
|||
import { parseCidr } from 'cidr-tools'; |
|||
import type { Client } from '~~/services/database/repositories/client'; |
|||
import type { System } from '~~/services/database/repositories/system'; |
|||
|
|||
export const wg = { |
|||
generateServerPeer: (client: Client) => { |
|||
return `# Client: ${client.name} (${client.id})
|
|||
[Peer] |
|||
PublicKey = ${client.publicKey} |
|||
PresharedKey = ${client.preSharedKey} |
|||
AllowedIPs = ${client.address4}/32, ${client.address6}/128${client.serverAllowedIPs ? ` ${client.serverAllowedIPs.join(', ')}` : ''}`;
|
|||
}, |
|||
|
|||
generateServerInterface: (system: System) => { |
|||
const cidr4Block = parseCidr(system.userConfig.address4Range).prefix; |
|||
const cidr6Block = parseCidr(system.userConfig.address6Range).prefix; |
|||
|
|||
return `# Note: Do not edit this file directly.
|
|||
# Your changes will be overwritten! |
|||
|
|||
# Server |
|||
[Interface] |
|||
PrivateKey = ${system.interface.privateKey} |
|||
Address = ${system.interface.address4}/${cidr4Block}, ${system.interface.address6}/${cidr6Block} |
|||
ListenPort = ${system.wgPort} |
|||
PreUp = ${system.iptables.PreUp} |
|||
PostUp = ${system.iptables.PostUp} |
|||
PreDown = ${system.iptables.PreDown} |
|||
PostDown = ${system.iptables.PostDown}`;
|
|||
}, |
|||
|
|||
generateClientConfig: (system: System, client: Client) => { |
|||
const cidr4Block = parseCidr(system.userConfig.address4Range).prefix; |
|||
const cidr6Block = parseCidr(system.userConfig.address6Range).prefix; |
|||
|
|||
return `[Interface]
|
|||
PrivateKey = ${client.privateKey} |
|||
Address = ${client.address4}/${cidr4Block}, ${client.address6}/${cidr6Block} |
|||
DNS = ${system.userConfig.defaultDns.join(', ')} |
|||
MTU = ${system.userConfig.mtu} |
|||
|
|||
[Peer] |
|||
PublicKey = ${system.interface.publicKey} |
|||
PresharedKey = ${client.preSharedKey} |
|||
AllowedIPs = ${client.allowedIPs.join(', ')} |
|||
PersistentKeepalive = ${client.persistentKeepalive} |
|||
Endpoint = ${system.wgHost}:${system.wgConfigPort}`;
|
|||
}, |
|||
|
|||
// TODO?: generate keys using plain javascript
|
|||
|
|||
generatePrivateKey: () => { |
|||
return exec('wg genkey'); |
|||
}, |
|||
|
|||
getPublicKey: (privateKey: string) => { |
|||
return exec(`echo ${privateKey} | wg pubkey`, { |
|||
log: 'echo ***hidden*** | wg pubkey', |
|||
}); |
|||
}, |
|||
|
|||
generatePresharedKey: () => { |
|||
return exec('wg genpsk'); |
|||
}, |
|||
|
|||
up: () => { |
|||
return exec('wg-quick up wg0'); |
|||
}, |
|||
|
|||
down: () => { |
|||
return exec('wg-quick down wg0'); |
|||
}, |
|||
|
|||
sync: () => { |
|||
return exec('wg syncconf wg0 <(wg-quick strip wg0)'); |
|||
}, |
|||
|
|||
// TODO: properly convert
|
|||
dump: async () => { |
|||
const rawDump = await exec('wg show wg0 dump', { |
|||
log: false, |
|||
}); |
|||
return rawDump |
|||
.trim() |
|||
.split('\n') |
|||
.slice(1) |
|||
.map((line) => { |
|||
const [ |
|||
publicKey, |
|||
preSharedKey, |
|||
endpoint, |
|||
allowedIPs, |
|||
latestHandshakeAt, |
|||
transferRx, |
|||
transferTx, |
|||
persistentKeepalive, |
|||
] = line.split('\t'); |
|||
|
|||
return { |
|||
publicKey, |
|||
preSharedKey, |
|||
endpoint, |
|||
allowedIPs, |
|||
latestHandshakeAt, |
|||
transferRx, |
|||
transferTx, |
|||
persistentKeepalive, |
|||
}; |
|||
}); |
|||
}, |
|||
}; |
Loading…
Reference in new issue