|
|
@ -5,11 +5,20 @@ import type { InterfaceType } from '#db/repositories/interface/types'; |
|
|
|
import type { UserConfigType } from '#db/repositories/userConfig/types'; |
|
|
|
import type { HooksType } from '#db/repositories/hooks/types'; |
|
|
|
|
|
|
|
type Options = { |
|
|
|
enableIpv6?: boolean; |
|
|
|
}; |
|
|
|
|
|
|
|
export const wg = { |
|
|
|
generateServerPeer: (client: Omit<ClientType, 'createdAt' | 'updatedAt'>) => { |
|
|
|
generateServerPeer: ( |
|
|
|
client: Omit<ClientType, 'createdAt' | 'updatedAt'>, |
|
|
|
options: Options = {} |
|
|
|
) => { |
|
|
|
const { enableIpv6 = true } = options; |
|
|
|
|
|
|
|
const allowedIps = [ |
|
|
|
`${client.ipv4Address}/32`, |
|
|
|
`${client.ipv6Address}/128`, |
|
|
|
...(enableIpv6 ? [`${client.ipv6Address}/128`] : []), |
|
|
|
...(client.serverAllowedIps ?? []), |
|
|
|
]; |
|
|
|
|
|
|
@ -25,19 +34,29 @@ PresharedKey = ${client.preSharedKey} |
|
|
|
AllowedIPs = ${allowedIps.join(', ')}${extraLines.length ? `\n${extraLines.join('\n')}` : ''}`;
|
|
|
|
}, |
|
|
|
|
|
|
|
generateServerInterface: (wgInterface: InterfaceType, hooks: HooksType) => { |
|
|
|
generateServerInterface: ( |
|
|
|
wgInterface: InterfaceType, |
|
|
|
hooks: HooksType, |
|
|
|
options: Options = {} |
|
|
|
) => { |
|
|
|
const { enableIpv6 = true } = options; |
|
|
|
|
|
|
|
const cidr4 = parseCidr(wgInterface.ipv4Cidr); |
|
|
|
const cidr6 = parseCidr(wgInterface.ipv6Cidr); |
|
|
|
const ipv4Addr = stringifyIp({ number: cidr4.start + 1n, version: 4 }); |
|
|
|
const ipv6Addr = stringifyIp({ number: cidr6.start + 1n, version: 6 }); |
|
|
|
|
|
|
|
const address = |
|
|
|
`${ipv4Addr}/${cidr4.prefix}` + |
|
|
|
(enableIpv6 ? `, ${ipv6Addr}/${cidr6.prefix}` : ''); |
|
|
|
|
|
|
|
return `# Note: Do not edit this file directly.
|
|
|
|
# Your changes will be overwritten! |
|
|
|
|
|
|
|
# Server |
|
|
|
[Interface] |
|
|
|
PrivateKey = ${wgInterface.privateKey} |
|
|
|
Address = ${ipv4Addr}/${cidr4.prefix}, ${ipv6Addr}/${cidr6.prefix} |
|
|
|
Address = ${address} |
|
|
|
ListenPort = ${wgInterface.port} |
|
|
|
MTU = ${wgInterface.mtu} |
|
|
|
PreUp = ${iptablesTemplate(hooks.preUp, wgInterface)} |
|
|
@ -49,11 +68,18 @@ PostDown = ${iptablesTemplate(hooks.postDown, wgInterface)}`; |
|
|
|
generateClientConfig: ( |
|
|
|
wgInterface: InterfaceType, |
|
|
|
userConfig: UserConfigType, |
|
|
|
client: ClientType |
|
|
|
client: ClientType, |
|
|
|
options: Options = {} |
|
|
|
) => { |
|
|
|
const { enableIpv6 = true } = options; |
|
|
|
|
|
|
|
const cidr4Block = parseCidr(wgInterface.ipv4Cidr).prefix; |
|
|
|
const cidr6Block = parseCidr(wgInterface.ipv6Cidr).prefix; |
|
|
|
|
|
|
|
const address = |
|
|
|
`${client.ipv4Address}/${cidr4Block}` + |
|
|
|
(enableIpv6 ? `, ${client.ipv6Address}/${cidr6Block}` : ''); |
|
|
|
|
|
|
|
const hookLines = [ |
|
|
|
client.preUp ? `PreUp = ${client.preUp}` : null, |
|
|
|
client.postUp ? `PostUp = ${client.postUp}` : null, |
|
|
@ -63,7 +89,7 @@ PostDown = ${iptablesTemplate(hooks.postDown, wgInterface)}`; |
|
|
|
|
|
|
|
return `[Interface]
|
|
|
|
PrivateKey = ${client.privateKey} |
|
|
|
Address = ${client.ipv4Address}/${cidr4Block}, ${client.ipv6Address}/${cidr6Block} |
|
|
|
Address = ${address} |
|
|
|
DNS = ${(client.dns ?? userConfig.defaultDns).join(', ')} |
|
|
|
MTU = ${client.mtu} |
|
|
|
${hookLines.length ? `${hookLines.join('\n')}\n` : ''} |
|
|
|