mirror of https://github.com/wg-easy/wg-easy
Browse Source
* add wireguard helpers * improve wireguard helpers * add server mtu * fix wg0.conf formatting * add ipv6 support to docker compose and readmepull/1618/head
committed by
Bernd Storath
7 changed files with 220 additions and 102 deletions
@ -0,0 +1,131 @@ |
|||||
|
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) => { |
||||
|
const allowedIps = [ |
||||
|
`${client.address4}/32`, |
||||
|
`${client.address6}/128`, |
||||
|
...(client.serverAllowedIPs ?? []), |
||||
|
]; |
||||
|
|
||||
|
return `# Client: ${client.name} (${client.id})
|
||||
|
[Peer] |
||||
|
PublicKey = ${client.publicKey} |
||||
|
PresharedKey = ${client.preSharedKey} |
||||
|
AllowedIPs = ${allowedIps.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} |
||||
|
MTU = ${system.userConfig.serverMtu} |
||||
|
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}`;
|
||||
|
}, |
||||
|
|
||||
|
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)'); |
||||
|
}, |
||||
|
|
||||
|
dump: async () => { |
||||
|
const rawDump = await exec('wg show wg0 dump', { |
||||
|
log: false, |
||||
|
}); |
||||
|
|
||||
|
type wgDumpLine = [ |
||||
|
string, |
||||
|
string, |
||||
|
string, |
||||
|
string, |
||||
|
string, |
||||
|
string, |
||||
|
string, |
||||
|
string, |
||||
|
]; |
||||
|
|
||||
|
return rawDump |
||||
|
.trim() |
||||
|
.split('\n') |
||||
|
.slice(1) |
||||
|
.map((line) => { |
||||
|
const splitLines = line.split('\t'); |
||||
|
const [ |
||||
|
publicKey, |
||||
|
preSharedKey, |
||||
|
endpoint, |
||||
|
allowedIPs, |
||||
|
latestHandshakeAt, |
||||
|
transferRx, |
||||
|
transferTx, |
||||
|
persistentKeepalive, |
||||
|
] = splitLines as wgDumpLine; |
||||
|
|
||||
|
return { |
||||
|
publicKey, |
||||
|
preSharedKey, |
||||
|
endpoint: endpoint === '(none)' ? null : endpoint, |
||||
|
allowedIPs, |
||||
|
latestHandshakeAt: |
||||
|
latestHandshakeAt === '0' |
||||
|
? null |
||||
|
: new Date(Number.parseInt(`${latestHandshakeAt}000`)), |
||||
|
transferRx: Number.parseInt(transferRx), |
||||
|
transferTx: Number.parseInt(transferTx), |
||||
|
persistentKeepalive: persistentKeepalive, |
||||
|
}; |
||||
|
}); |
||||
|
}, |
||||
|
}; |
Loading…
Reference in new issue