mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-07-21 20:16:08 +00:00
fix(links): bracket ipv6 hosts in share links and qr codes (#5310)
* fix(sub): bracket ipv6 hosts in share links * fix(frontend): bracket ipv6 hosts in share links
This commit is contained in:
@@ -23,6 +23,14 @@ import { getHeaderValue } from './headers';
|
||||
type ForceTls = 'same' | 'tls' | 'none';
|
||||
const SHARE_HOSTNAME_RE = /^[A-Za-z0-9]([A-Za-z0-9-]*[A-Za-z0-9])?(\.[A-Za-z0-9]([A-Za-z0-9-]*[A-Za-z0-9])?)*$/;
|
||||
|
||||
// Format a host for interpolation into a URL authority. IPv6 literals are
|
||||
// wrapped in square brackets per RFC 3986; IPv4 and hostnames are left as-is.
|
||||
// Any brackets already present are first stripped so the helper is idempotent.
|
||||
function formatUrlHost(address: string): string {
|
||||
const bare = address.replace(/^\[|\]$/g, '');
|
||||
return bare.includes(':') ? `[${bare}]` : bare;
|
||||
}
|
||||
|
||||
// xHTTP headers ship as Record<string, string> on the wire (Zod schema)
|
||||
// rather than the legacy class's HeaderEntry[]. Lookup by case-folded key.
|
||||
function xhttpHostFallback(xhttp: XHttpStreamSettings | undefined): string {
|
||||
@@ -400,7 +408,7 @@ export function genVlessLink(input: GenVlessLinkInput): string {
|
||||
params.set('security', 'none');
|
||||
}
|
||||
|
||||
const url = new URL(`vless://${clientId}@${address}:${port}`);
|
||||
const url = new URL(`vless://${clientId}@${formatUrlHost(address)}:${port}`);
|
||||
for (const [key, value] of params) url.searchParams.set(key, value);
|
||||
url.hash = encodeURIComponent(remark);
|
||||
return url.toString();
|
||||
@@ -524,7 +532,7 @@ export function genTrojanLink(input: GenTrojanLinkInput): string {
|
||||
params.set('security', 'none');
|
||||
}
|
||||
|
||||
const url = new URL(`trojan://${encodeURIComponent(clientPassword)}@${address}:${port}`);
|
||||
const url = new URL(`trojan://${encodeURIComponent(clientPassword)}@${formatUrlHost(address)}:${port}`);
|
||||
for (const [key, value] of params) url.searchParams.set(key, value);
|
||||
url.hash = encodeURIComponent(remark);
|
||||
return url.toString();
|
||||
@@ -583,7 +591,7 @@ export function genShadowsocksLink(input: GenShadowsocksLinkInput): string {
|
||||
if (isSSMultiUser) passwords.push(clientPassword);
|
||||
|
||||
const userinfo = Base64.encode(`${settings.method}:${passwords.join(':')}`, true);
|
||||
const url = new URL(`ss://${userinfo}@${address}:${port}`);
|
||||
const url = new URL(`ss://${userinfo}@${formatUrlHost(address)}:${port}`);
|
||||
for (const [key, value] of params) url.searchParams.set(key, value);
|
||||
url.hash = encodeURIComponent(remark);
|
||||
return url.toString();
|
||||
@@ -681,7 +689,7 @@ export function genHysteriaLink(input: GenHysteriaLinkInput): string {
|
||||
params.set('mport', hopPorts);
|
||||
}
|
||||
|
||||
const url = new URL(`${scheme}://${clientAuth}@${address}:${port}`);
|
||||
const url = new URL(`${scheme}://${clientAuth}@${formatUrlHost(address)}:${port}`);
|
||||
for (const [key, value] of params) url.searchParams.set(key, value);
|
||||
url.hash = encodeURIComponent(remark);
|
||||
return url.toString();
|
||||
@@ -724,7 +732,7 @@ export function genWireguardLink(input: GenWireguardLinkInput): string {
|
||||
const peer = settings.peers[peerIndex];
|
||||
if (!peer) return '';
|
||||
|
||||
const url = new URL(`wireguard://${address}:${port}`);
|
||||
const url = new URL(`wireguard://${formatUrlHost(address)}:${port}`);
|
||||
url.username = peer.privateKey ?? '';
|
||||
|
||||
const pubKey = settings.secretKey.length > 0
|
||||
|
||||
@@ -437,6 +437,91 @@ describe('genShadowsocksLink', () => {
|
||||
}
|
||||
});
|
||||
|
||||
describe('IPv6 bracket wrapping in share-link authority', () => {
|
||||
it('genVlessLink brackets a bare IPv6 address', () => {
|
||||
const [, raw] = fixturesForProtocol('vless')[0];
|
||||
const typed = InboundSchema.parse(raw);
|
||||
const clientId = (raw as { settings: { clients: Array<{ id: string }> } }).settings.clients[0].id;
|
||||
|
||||
const link = genVlessLink({
|
||||
inbound: typed,
|
||||
address: '2001:db8::1',
|
||||
port: 443,
|
||||
clientId,
|
||||
});
|
||||
expect(new URL(link).host).toBe('[2001:db8::1]:443');
|
||||
});
|
||||
|
||||
it('genTrojanLink brackets a bare IPv6 address', () => {
|
||||
const [, raw] = fixturesForProtocol('trojan')[0];
|
||||
const typed = InboundSchema.parse(raw);
|
||||
const clientPassword = (raw as { settings: { clients: Array<{ password: string }> } }).settings.clients[0].password;
|
||||
|
||||
const link = genTrojanLink({
|
||||
inbound: typed,
|
||||
address: '2001:db8::1',
|
||||
port: 443,
|
||||
clientPassword,
|
||||
});
|
||||
expect(new URL(link).host).toBe('[2001:db8::1]:443');
|
||||
});
|
||||
|
||||
it('genShadowsocksLink brackets a bare IPv6 address', () => {
|
||||
const [, raw] = fixturesForProtocol('shadowsocks')[0];
|
||||
const typed = InboundSchema.parse(raw);
|
||||
const clientPassword = (raw as { settings: { clients?: Array<{ password: string }> } }).settings.clients?.[0]?.password ?? '';
|
||||
|
||||
const link = genShadowsocksLink({
|
||||
inbound: typed,
|
||||
address: '2001:db8::1',
|
||||
port: 443,
|
||||
clientPassword,
|
||||
});
|
||||
expect(new URL(link).host).toBe('[2001:db8::1]:443');
|
||||
});
|
||||
|
||||
it('genHysteriaLink brackets a bare IPv6 address', () => {
|
||||
const [, raw] = fixturesForProtocol('hysteria')[0];
|
||||
const typed = InboundSchema.parse(raw);
|
||||
const clientAuth = (raw as { settings: { clients: Array<{ auth: string }> } }).settings.clients[0].auth;
|
||||
|
||||
const link = genHysteriaLink({
|
||||
inbound: typed,
|
||||
address: '2001:db8::1',
|
||||
port: 443,
|
||||
clientAuth,
|
||||
});
|
||||
expect(new URL(link).host).toBe('[2001:db8::1]:443');
|
||||
});
|
||||
|
||||
it('genWireguardLink brackets a bare IPv6 address', () => {
|
||||
const [, raw] = fixturesForProtocol('wireguard')[0];
|
||||
const typed = InboundSchema.parse(raw);
|
||||
if (typed.protocol !== 'wireguard') throw new Error('not a wireguard fixture');
|
||||
const settings = typed.settings as WireguardInboundSettings;
|
||||
|
||||
const link = genWireguardLink({
|
||||
settings,
|
||||
address: '2001:db8::1',
|
||||
port: 443,
|
||||
peerIndex: 0,
|
||||
});
|
||||
expect(new URL(link).host).toBe('[2001:db8::1]:443');
|
||||
});
|
||||
|
||||
it('does not bracket IPv4 addresses or hostnames', () => {
|
||||
const [, raw] = fixturesForProtocol('vless')[0];
|
||||
const typed = InboundSchema.parse(raw);
|
||||
const clientId = (raw as { settings: { clients: Array<{ id: string }> } }).settings.clients[0].id;
|
||||
|
||||
const v4 = genVlessLink({ inbound: typed, address: '203.0.113.7', port: 443, clientId });
|
||||
expect(new URL(v4).host).toBe('203.0.113.7:443');
|
||||
|
||||
const host = genVlessLink({ inbound: typed, address: 'example.test', port: 443, clientId });
|
||||
expect(new URL(host).host).toBe('example.test:443');
|
||||
});
|
||||
});
|
||||
|
||||
describe('external proxy pinned cert (pcs)', () => {
|
||||
const [, raw] = fixturesForProtocol('vless').find(([name]) => name === 'vless-ws-tls')!;
|
||||
const typed = InboundSchema.parse(raw);
|
||||
|
||||
Reference in New Issue
Block a user