From aa1dd3dfa428c804f08bd6d59eaa30f9ad1ea573 Mon Sep 17 00:00:00 2001 From: Bernd Storath <999999bst@gmail.com> Date: Thu, 26 Jun 2025 15:50:09 +0200 Subject: [PATCH] don't add ipv6 address --- src/server/utils/WireGuard.ts | 16 ++++++++++++--- src/server/utils/wgHelper.ts | 38 +++++++++++++++++++++++++++++------ 2 files changed, 45 insertions(+), 9 deletions(-) diff --git a/src/server/utils/WireGuard.ts b/src/server/utils/WireGuard.ts index a44a3b11..0cf44006 100644 --- a/src/server/utils/WireGuard.ts +++ b/src/server/utils/WireGuard.ts @@ -25,13 +25,21 @@ class WireGuard { const hooks = await Database.hooks.get(); const result = []; - result.push(wg.generateServerInterface(wgInterface, hooks)); + result.push( + wg.generateServerInterface(wgInterface, hooks, { + enableIpv6: !WG_ENV.DISABLE_IPV6, + }) + ); for (const client of clients) { if (!client.enabled) { continue; } - result.push(wg.generateServerPeer(client)); + result.push( + wg.generateServerPeer(client, { + enableIpv6: !WG_ENV.DISABLE_IPV6, + }) + ); } result.push(''); @@ -125,7 +133,9 @@ class WireGuard { throw new Error('Client not found'); } - return wg.generateClientConfig(wgInterface, userConfig, client); + return wg.generateClientConfig(wgInterface, userConfig, client, { + enableIpv6: !WG_ENV.DISABLE_IPV6, + }); } async getClientQRCodeSVG({ clientId }: { clientId: ID }) { diff --git a/src/server/utils/wgHelper.ts b/src/server/utils/wgHelper.ts index 7f1c44ec..6d096e99 100644 --- a/src/server/utils/wgHelper.ts +++ b/src/server/utils/wgHelper.ts @@ -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) => { + generateServerPeer: ( + client: Omit, + 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` : ''}