mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-10-01 03:32:09 +03:00
aee45ca3fe
Invariant: an inbound's enabled Hosts are the endpoints every client config for it advertises, whichever surface renders that config. WireGuard and AmneziaWG broke it. Their raw generators ignored the externalProxy entries Hosts are injected as and always emitted resolveInboundAddress, so the raw subscription, the sub page .conf, the clients links API and "export all links" gave out the panel address while the JSON and Clash formats of the same inbound used the Host. advertisedEndpoints now states the fan-out once for mtproto, wireguard and amneziawg. The browser-built configs had the same gap. The Clients page WireGuard and AmneziaWG config blocks and QR panels, and its TUIC Clash config, used the panel hostname next to server links that already used Hosts; the Inbounds page peer configs, QR and export ignored them too. withMtprotoHostEndpoints becomes withHostEndpoints over a shared hostEndpointsFor mirror of the backend, the tunnel fan-outs render one config per Host, and the clients page waits for the hosts list the way the inbounds page does, so an empty list means "no hosts" rather than "not loaded yet".
59 lines
2.2 KiB
TypeScript
59 lines
2.2 KiB
TypeScript
import type { HostEndpoint } from '@/lib/hosts/host-link';
|
|
import { formatInboundLabel } from '@/lib/inbounds/label';
|
|
import { preferPublicHost, resolveShareHost } from '@/lib/xray/inbound-link';
|
|
import type { ClientRecord, InboundOption } from '@/hooks/useClients';
|
|
|
|
export function isWireguardClient(client: ClientRecord | null | undefined): boolean {
|
|
if (!client) return false;
|
|
return !!(
|
|
client.privateKey ||
|
|
client.publicKey ||
|
|
client.allowedIPs ||
|
|
client.preSharedKey ||
|
|
client.keepAlive
|
|
);
|
|
}
|
|
|
|
export function findWireguardInbounds(
|
|
client: ClientRecord | null | undefined,
|
|
inboundsById: Record<number, InboundOption>,
|
|
): InboundOption[] {
|
|
return (client?.inboundIds || [])
|
|
.map((id) => inboundsById?.[id])
|
|
.filter((ib): ib is InboundOption => ib?.protocol === 'wireguard');
|
|
}
|
|
|
|
export function buildWireguardClientConfig(
|
|
client: ClientRecord,
|
|
inbound: InboundOption | undefined,
|
|
host = window.location.hostname,
|
|
publicHost = '',
|
|
addressOverride = '',
|
|
hostEndpoint?: HostEndpoint,
|
|
): string {
|
|
const endpointHost =
|
|
hostEndpoint?.dest ||
|
|
resolveShareHost(inbound ?? {}, inbound?.nodeAddress ?? '', preferPublicHost(host, publicHost));
|
|
const address = addressOverride || client.allowedIPs || '10.0.0.2/32';
|
|
const endpoint = `${endpointHost}:${hostEndpoint?.port || inbound?.port || ''}`;
|
|
const inboundName = inbound ? formatInboundLabel(inbound.tag, inbound.remark) : '';
|
|
const remark = [inboundName, hostEndpoint?.remark, client.email, client.comment]
|
|
.filter(Boolean)
|
|
.join(' - ');
|
|
const lines = [
|
|
'[Interface]',
|
|
`PrivateKey = ${client.privateKey || client.password || ''}`,
|
|
`Address = ${address}`,
|
|
`DNS = ${inbound?.wgDns || '1.1.1.1, 1.0.0.1'}`,
|
|
];
|
|
if (inbound?.wgMtu && inbound.wgMtu > 0) lines.push(`MTU = ${inbound.wgMtu}`);
|
|
lines.push('');
|
|
if (remark) lines.push(`# ${remark}`);
|
|
lines.push('[Peer]', `PublicKey = ${inbound?.wgPublicKey || ''}`);
|
|
if (client.preSharedKey) lines.push(`PresharedKey = ${client.preSharedKey}`);
|
|
lines.push('AllowedIPs = 0.0.0.0/0, ::/0', `Endpoint = ${endpoint}`);
|
|
if (client.keepAlive && client.keepAlive > 0)
|
|
lines.push(`PersistentKeepalive = ${client.keepAlive}`);
|
|
return lines.join('\n');
|
|
}
|