From d9896398feeb55c42fa3bd15f70104dd02b13899 Mon Sep 17 00:00:00 2001 From: xream Date: Fri, 13 Nov 2026 23:42:23 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20Surge=20=E6=94=AF=E6=8C=81=20VLESS=20XH?= =?UTF-8?q?TTP=20=E5=92=8C=20Encryption?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/package.json | 2 +- .../core/proxy-utils/parsers/peggy/surge.js | 13 +++- .../src/core/proxy-utils/producers/surge.js | 27 ++++++- backend/src/test/proxy-producers/text.spec.js | 77 ++++++++++++++++++- backend/src/test/restful/download.spec.js | 33 +++++--- 5 files changed, 131 insertions(+), 21 deletions(-) diff --git a/backend/package.json b/backend/package.json index 389ab573..621f21f1 100644 --- a/backend/package.json +++ b/backend/package.json @@ -1,6 +1,6 @@ { "name": "sub-store", - "version": "2.44.10", + "version": "2.44.11", "description": "Advanced Subscription Manager for QX, Loon, Surge, Stash and Shadowrocket.", "main": "src/main.js", "packageManager": "pnpm@11.0.9", diff --git a/backend/src/core/proxy-utils/parsers/peggy/surge.js b/backend/src/core/proxy-utils/parsers/peggy/surge.js index c18a8840..9b7e93a4 100644 --- a/backend/src/core/proxy-utils/parsers/peggy/surge.js +++ b/backend/src/core/proxy-utils/parsers/peggy/surge.js @@ -19,6 +19,8 @@ const grammars = String.raw` const proxy = {}; const obfs = {}; const shadowTLS = {}; + const xhttpOpts = { mode: "auto" }; + let xhttpEnabled = false; const $ = {}; function handleWebsocket() { @@ -396,11 +398,16 @@ vmess = tag equals "vmess" address (vmess_uuid/vmess_aead/ws/ws_path/ws_headers/ handleWebsocket(); handleShadowTLS(); } -vless = tag equals "vless" address (vless_username/vless_option/reality_option/ws/ws_path/ws_headers/grpc/grpc_service_name/grpc_mode/grpc_authority/ip_version/underlying_proxy/tos/allow_other_interface/interface/test_url/test_udp/test_timeout/hybrid/no_error_alert/tls/sni/cert_verify_name/alpn/tls_fingerprint/tls_verification/client_cert/fast_open/tfo/shadow_tls_version/shadow_tls_sni/shadow_tls_password/block_quic/others)* { +vless = tag equals "vless" address (vless_username/vless_option/reality_option/ws/ws_path/ws_headers/grpc/grpc_service_name/grpc_mode/grpc_authority/xhttp/xhttp_option/ip_version/underlying_proxy/tos/allow_other_interface/interface/test_url/test_udp/test_timeout/hybrid/no_error_alert/tls/sni/cert_verify_name/alpn/tls_fingerprint/tls_verification/client_cert/fast_open/tfo/shadow_tls_version/shadow_tls_sni/shadow_tls_password/block_quic/others)* { proxy.type = "vless"; proxy["packet-encoding"] = "xudp"; if (proxy["reality-opts"] && proxy["reality-opts"]["public-key"]) proxy.tls = true; - handleWebsocket(); + if (xhttpEnabled) { + proxy.network = "xhttp"; + proxy["xhttp-opts"] = xhttpOpts; + } else { + handleWebsocket(); + } handleShadowTLS(); } trojan = tag equals "trojan" address (passwordk/ws/ws_path/ws_headers/tls/sni/cert_verify_name/alpn/tls_fingerprint/tls_verification/client_cert/ip_version/underlying_proxy/tos/allow_other_interface/interface/test_url/test_udp/test_timeout/hybrid/no_error_alert/fast_open/tfo/udp_relay/shadow_tls_version/shadow_tls_sni/shadow_tls_password/block_quic/others)* { @@ -593,6 +600,8 @@ grpc = comma "grpc" equals flag:bool { if (flag) proxy.network = "grpc"; } grpc_service_name = comma "grpc-service-name" equals value:quoted_value { $set(proxy, "grpc-opts.grpc-service-name", value.trim()); } grpc_mode = comma "grpc-mode" equals value:quoted_value { $set(proxy, "grpc-opts._grpc-type", value.trim()); } grpc_authority = comma "grpc-authority" equals value:quoted_value { $set(proxy, "grpc-opts._grpc-authority", value.trim()); } +xhttp = comma "xhttp" equals flag:bool { xhttpEnabled = flag; } +xhttp_option = comma "xhttp-" key:("path"/"host"/"mode") equals value:quoted_value { xhttpOpts[key] = value.trim(); } headers = comma "headers" equals & { const start = peg$currPos; const index = readHeadersEnd(input, start, ";"); diff --git a/backend/src/core/proxy-utils/producers/surge.js b/backend/src/core/proxy-utils/producers/surge.js index 92c12a87..539e6ea3 100644 --- a/backend/src/core/proxy-utils/producers/surge.js +++ b/backend/src/core/proxy-utils/producers/surge.js @@ -699,11 +699,11 @@ function appendVlessParams(result, proxy) { const network = proxy.network ?? 'tcp'; const reality = proxy['reality-opts']; const tls = proxy.tls || reality; - if (!['tcp', 'raw', 'none', 'ws', 'grpc'].includes(network)) { + if (!['tcp', 'raw', 'none', 'ws', 'grpc', 'xhttp'].includes(network)) { throw unsupported(`VLESS network ${network} is unsupported`); } - if (proxy.encryption && proxy.encryption !== 'none') { - throw unsupported('VLESS encryption is unsupported'); + if (proxy.encryption && /[,\r\n"']/.test(proxy.encryption)) { + throw unsupported('VLESS encryption contains unsupported characters'); } if ( (network === 'ws' && reality) || @@ -727,6 +727,27 @@ function appendVlessParams(result, proxy) { ]) { if (value) result.append(`,${key}=${value}`); } + } else if (network === 'xhttp') { + result.append(',xhttp=true'); + // shortcut: export path, host and mode, extend when more XHTTP fields have a Surge mapping. + const xhttp = proxy['xhttp-opts']; + for (const [key, field] of [ + ['path', xhttp?.path], + ['host', xhttp?.host ?? xhttp?.headers?.Host ?? xhttp?.headers?.host], + ['mode', xhttp?.mode], + ]) { + const value = stripSurgeQuotes(String(field ?? '')); + if (!value) continue; + if (/[\r\n"]/.test(value)) { + throw unsupported( + `VLESS xhttp-${key} contains unsupported characters`, + ); + } + const parameter = value.includes(',') + ? quoteSurgeValue(value) + : value; + result.append(`,xhttp-${key}=${parameter}`); + } } if (proxy.flow) result.append(`,flow=${proxy.flow}`); diff --git a/backend/src/test/proxy-producers/text.spec.js b/backend/src/test/proxy-producers/text.spec.js index 2f5e940e..e7ec389a 100644 --- a/backend/src/test/proxy-producers/text.spec.js +++ b/backend/src/test/proxy-producers/text.spec.js @@ -2396,7 +2396,7 @@ describe('Proxy text producers', function () { it('round-trips Surge VLESS with RAW, WebSocket, gRPC and REALITY', function () { const cases = [ [ - 'ws=false,tls=true,flow=xtls-rprx-vision-udp443,encryption=none', + 'ws=false,xhttp=false,xhttp-path=/unused,tls=true,flow=xtls-rprx-vision-udp443,encryption=none', { network: 'tcp', tls: true, @@ -2468,6 +2468,77 @@ describe('Proxy text producers', function () { ); }); + it('round-trips Surge VLESS XHTTP modes', function () { + const prefix = `XHTTP=vless,example.com,443,username=${UUID},xhttp=true,xhttp-path=/path,xhttp-host=cdn.example.com`; + for (const mode of [ + undefined, + 'auto', + 'packet-up', + 'stream-up', + 'stream-one', + ]) { + const [proxy] = ProxyUtils.parse( + `${prefix}${mode ? `,xhttp-mode=${mode}` : ''},tls=true`, + ); + const expected = { + network: 'xhttp', + tls: true, + 'xhttp-opts': { + path: '/path', + host: 'cdn.example.com', + mode: mode ?? 'auto', + }, + }; + expectSubset(proxy, expected); + + for (const target of ['Surge', 'SurgeMac']) { + const output = produceExternal(target, proxy, { + useMihomoExternal: true, + }); + expect(output).to.equal( + `${prefix},xhttp-mode=${mode ?? 'auto'},tls=true,sni="example.com"`, + ); + expectSubset(ProxyUtils.parse(output)[0], expected); + } + } + }); + + it('produces Surge VLESS XHTTP from a share link without a mode', function () { + const extra = encodeURIComponent( + '{"headers":{"Host":"cdn.example.com"}}', + ); + const [proxy] = ProxyUtils.parse( + `vless://${UUID}@example.com:443?type=xhttp&security=tls&path=%2Fpath%3Fa%3D1%2C2&extra=${extra}#XHTTP`, + ); + const output = produceExternal('Surge', proxy); + expect(output).to.include( + ',xhttp=true,xhttp-path="/path?a=1,2",xhttp-host=cdn.example.com,tls=true', + ); + expect(ProxyUtils.parse(output)[0]['xhttp-opts']).to.deep.equal({ + path: '/path?a=1,2', + host: 'cdn.example.com', + mode: 'auto', + }); + }); + + it('round-trips Surge VLESS encryption with XHTTP', function () { + const encryption = `mlkem768x25519plus.native.0rtt.100-111-1111.75-0-111.50-0-3333.${'Ab9_'.repeat(394)}a-A`; + const [proxy] = ProxyUtils.parse( + `enc = vless, a.com, 443, username=${UUID}, encryption=${encryption}, xhttp=true, xhttp-path=/path, xhttp-host=host.com, xhttp-mode=stream-one, tls=true`, + ); + expect(proxy.encryption).to.equal(encryption); + + for (const target of ['Surge', 'SurgeMac']) { + const output = produceExternal(target, proxy, { + useMihomoExternal: true, + }); + expect(output).to.include( + `,xhttp-mode=stream-one,encryption=${encryption},tls=true`, + ); + expect(ProxyUtils.parse(output)[0].encryption).to.equal(encryption); + } + }); + it('preserves gRPC authority from a VLESS share link without a service name', function () { const [proxy] = ProxyUtils.parse( `vless://${UUID}@edge.example.com:443?type=grpc&security=tls&authority=grpc.example.com#VLESS`, @@ -2488,8 +2559,8 @@ describe('Proxy text producers', function () { port: 443, uuid: UUID, tls: true, - network: 'xhttp', - 'xhttp-opts': { path: '/xhttp', host: 'cdn.example.com' }, + network: 'ws', + 'ws-opts': { path: '/upgrade', 'v2ray-http-upgrade': true }, }, { useMihomoExternal: true, localPort: 16666 }, ); diff --git a/backend/src/test/restful/download.spec.js b/backend/src/test/restful/download.spec.js index 66309dce..e4582804 100644 --- a/backend/src/test/restful/download.spec.js +++ b/backend/src/test/restful/download.spec.js @@ -6,6 +6,7 @@ import { COLLECTIONS_KEY, FILES_KEY, SUBS_KEY } from '@/constants'; const UUID = '11111111-1111-4111-8111-111111111111'; const VLESS_WS = `vless://${UUID}@1.1.1.1:443?security=tls&type=ws&host=cdn.example.com&path=%2Fws&sni=sni.example.com#VLESS%20WS`; const VLESS_XHTTP = `vless://${UUID}@192.0.2.1:443?security=tls&type=xhttp&path=%2Fxhttp&sni=sni.example.com#VLESS%20XHTTP`; +const VLESS_HTTP_UPGRADE = `vless://${UUID}@192.0.2.2:443?security=tls&type=httpupgrade&path=%2Fupgrade&sni=sni.example.com#VLESS%20HTTPUpgrade`; let $; let openApi; @@ -581,59 +582,67 @@ describe('download routes', function () { }); it('keeps SurgeMac Mihomo external output unmerged by default', async function () { - state[SUBS_KEY][0].content = [VLESS_WS, VLESS_XHTTP].join('\n'); + state[SUBS_KEY][0].content = [ + VLESS_WS, + VLESS_XHTTP, + VLESS_HTTP_UPGRADE, + ].join('\n'); const output = await downloadSubscription({ target: 'SurgeMac' }); expect(output).to.include('VLESS WS=vless,'); expect(output).to.include( - 'VLESS XHTTP=external,exec="/usr/local/bin/mihomo"', + `VLESS XHTTP=vless,192.0.2.1,443,username=${UUID},xhttp=true,xhttp-path=/xhttp,tls=true`, ); - expect(output).to.not.include('VLESS XHTTP=socks5'); + expect(output).to.include( + 'VLESS HTTPUpgrade=external,exec="/usr/local/bin/mihomo"', + ); + expect(output).to.not.include('VLESS HTTPUpgrade=socks5'); expect(output).to.not.include('mihomo merged=external'); }); it('enables merged Mihomo external output when the query parameter is present', async function () { - state[SUBS_KEY][0].content = VLESS_XHTTP; + state[SUBS_KEY][0].content = VLESS_HTTP_UPGRADE; const output = await downloadSubscription({ target: 'SurgeMac', mihomoMerge: 'true', mihomoMergeName: 'Shared Mihomo', }); - expect(output).to.include('VLESS XHTTP=socks5,127.0.0.1,65535'); + expect(output).to.include('VLESS HTTPUpgrade=socks5,127.0.0.1,65535'); expect(output).to.include( 'Shared Mihomo=external,exec="/usr/local/bin/mihomo"', ); expect(output).to.not.include( - 'VLESS XHTTP=external,exec="/usr/local/bin/mihomo"', + 'VLESS HTTPUpgrade=external,exec="/usr/local/bin/mihomo"', ); }); it('treats mihomoMerge=false as enabled because query flags are presence-based', async function () { - state[SUBS_KEY][0].content = VLESS_XHTTP; + state[SUBS_KEY][0].content = VLESS_HTTP_UPGRADE; const output = await downloadSubscription({ target: 'SurgeMac', mihomoMerge: 'false', mihomoMergeName: 'Shared Mihomo', }); - expect(output).to.include('VLESS XHTTP=socks5,127.0.0.1,65535'); + expect(output).to.include('VLESS HTTPUpgrade=socks5,127.0.0.1,65535'); expect(output).to.include( 'Shared Mihomo=external,exec="/usr/local/bin/mihomo"', ); expect(output).to.not.include( - 'VLESS XHTTP=external,exec="/usr/local/bin/mihomo"', + 'VLESS HTTPUpgrade=external,exec="/usr/local/bin/mihomo"', ); }); it('keeps merged Mihomo process external when mihomoExternal is enabled', async function () { + state[SUBS_KEY][0].content = VLESS_XHTTP; const output = await downloadSubscription({ target: 'SurgeMac', mihomoExternal: 'true', mihomoMerge: 'true', }); - expect(output).to.include('VLESS WS=socks5,127.0.0.1,65535'); + expect(output).to.include('VLESS XHTTP=socks5,127.0.0.1,65535'); expect(output).to.include( 'mihomo merged=external,exec="/usr/local/bin/mihomo",local-port=65534', ); @@ -641,14 +650,14 @@ describe('download routes', function () { }); it('enables merged Mihomo external output for collection downloads', async function () { - state[SUBS_KEY][0].content = VLESS_XHTTP; + state[SUBS_KEY][0].content = VLESS_HTTP_UPGRADE; const output = await downloadCollection({ target: 'SurgeMac', mihomoMerge: 'true', mihomoMergeName: 'Shared Mihomo', }); - expect(output).to.include('VLESS XHTTP=socks5,127.0.0.1,65535'); + expect(output).to.include('VLESS HTTPUpgrade=socks5,127.0.0.1,65535'); expect(output).to.include( 'Shared Mihomo=external,exec="/usr/local/bin/mihomo"', );