fix(wireguard): исправить endpoint и поля клиентского шаблона

This commit is contained in:
Denozordec committed 2026-10-08 23:21:45 +07:00
1 parent 206255b856
commit c82e8fa12b
13 files changed
+128 -29

No files matched your search

+34
View File
@@ -0,0 +1,34 @@
import assert from 'node:assert/strict'
import { test } from 'node:test'
import { resolveWgClientEndpoint } from '../packages/contracts/src/wireguard'
import { generatePeerClientConf as client } from './wg-config'
import { generatePeerClientConf as backend } from '../backend/src/services/wireguard-config'
test('client endpoint uses exporting server or explicit client metadata', () => {
assert.equal(resolveWgClientEndpoint(undefined, '192.0.2.1', 51879), '192.0.2.1:51879')
assert.equal(resolveWgClientEndpoint('vpn.example:443', '192.0.2.1', 51879), 'vpn.example:443')
assert.equal(resolveWgClientEndpoint('vpn.example', '192.0.2.1', 51879), 'vpn.example:51879')
assert.equal(resolveWgClientEndpoint(undefined, '2001:db8::1', 51879), '[2001:db8::1]:51879')
assert.equal(resolveWgClientEndpoint('[2001:db8::2]:443', '192.0.2.1', 51879), '[2001:db8::2]:443')
assert.equal(resolveWgClientEndpoint(undefined, undefined, 51879), undefined)
assert.equal(resolveWgClientEndpoint('vpn.example:0', '192.0.2.1', 51879), undefined)
assert.equal(resolveWgClientEndpoint('https://vpn.example', '192.0.2.1', 51879), undefined)
})
for (const [name, generate] of [['frontend', client], ['backend', backend]] as const) {
test(`${name}: incomplete client data stays a template without invented routes`, () => {
const code = generate({ serverPublicKey: 'fixture-server-key', endpoint: resolveWgClientEndpoint(undefined, '192.0.2.1', 51879) })
assert(code.includes('Endpoint = 192.0.2.1:51879'))
assert(code.includes('# Address = <'))
assert(code.includes('# AllowedIPs = <'))
assert(code.includes('# PrivateKey = <'))
assert(!/^AllowedIPs =|^Address =|^PrivateKey =/m.test(code))
assert(!code.includes('0.0.0.0/0'))
})
test(`${name}: explicit client parameters remain unchanged`, () => {
const code = generate({ serverPublicKey: 'fixture-server-key', peerAddress: '198.18.255.242/30', peerDns: '192.0.2.53', allowedIps: ['198.18.255.240/30'], endpoint: 'vpn.example:443', persistentKeepalive: 5 })
assert(code.includes('Address = 198.18.255.242/30'))
assert(code.includes('AllowedIPs = 198.18.255.240/30'))
assert(code.includes('DNS = 192.0.2.53'))
assert(code.includes('Endpoint = vpn.example:443'))
})
}
+5 -2
View File
@@ -324,13 +324,16 @@ export function generatePeerClientConf(args: {
const lines: string[] = []
lines.push(`[Interface]`)
lines.push(`# PrivateKey = <ключ клиента>`)
if (args.peerAddress) lines.push(`Address = ${args.peerAddress}`)
if (args.peerAddress?.trim()) lines.push(`Address = ${args.peerAddress.trim()}`)
else lines.push(`# Address = <укажите адрес клиента>`)
if (args.peerDns) lines.push(`DNS = ${args.peerDns}`)
lines.push(``)
lines.push(`[Peer]`)
lines.push(`PublicKey = ${args.serverPublicKey}`)
lines.push(`AllowedIPs = ${(args.allowedIps?.length ? args.allowedIps : ["0.0.0.0/0"]).join(", ")}`)
if (args.allowedIps?.length) lines.push(`AllowedIPs = ${args.allowedIps.join(", ")}`)
else lines.push(`# AllowedIPs = <укажите сети через туннель>`)
if (args.endpoint) lines.push(`Endpoint = ${args.endpoint}`)
else lines.push(`# Endpoint = <укажите адрес сервера и порт>`)
if (args.persistentKeepalive != null && args.persistentKeepalive > 0) {
lines.push(`PersistentKeepalive = ${args.persistentKeepalive}`)
}