Browse Source

don't add ipv6 address

pull/1951/head
Bernd Storath 1 month ago
parent
commit
aa1dd3dfa4
  1. 16
      src/server/utils/WireGuard.ts
  2. 38
      src/server/utils/wgHelper.ts

16
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 }) {

38
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<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` : ''}

Loading…
Cancel
Save