diff --git a/backend/package.json b/backend/package.json index 5f76b325..b96b25d6 100644 --- a/backend/package.json +++ b/backend/package.json @@ -1,6 +1,6 @@ { "name": "sub-store", - "version": "2.22.9", + "version": "2.22.10", "description": "Advanced Subscription Manager for QX, Loon, Surge, Stash and Shadowrocket.", "main": "src/main.js", "scripts": { diff --git a/backend/src/core/proxy-utils/parsers/index.js b/backend/src/core/proxy-utils/parsers/index.js index 1b2783e6..7f45a777 100644 --- a/backend/src/core/proxy-utils/parsers/index.js +++ b/backend/src/core/proxy-utils/parsers/index.js @@ -1036,9 +1036,20 @@ function URI_VLESS() { ); } + if ( + Object.prototype.hasOwnProperty.call(xhttpSettings, 'mode') && + !isNotBlank(xhttpSettings.mode) + ) { + setUnsupportedXhttpField( + unsupportedXhttpSettings, + 'mode', + xhttpSettings.mode, + ); + } + const inlineExtra = {}; for (const [key, value] of Object.entries(xhttpSettings)) { - if (['path', 'host', 'extra'].includes(key)) { + if (['path', 'host', 'mode', 'extra'].includes(key)) { continue; } inlineExtra[key] = value; @@ -1098,7 +1109,9 @@ function URI_VLESS() { break; case 'security': { const normalizedSecurity = - typeof value === 'string' ? value.toLowerCase() : ''; + typeof value === 'string' + ? value.toLowerCase() + : ''; if (!['tls', 'reality'].includes(normalizedSecurity)) { setUnsupportedXhttpField( unsupportedDownloadSettings, @@ -1119,7 +1132,9 @@ function URI_VLESS() { } const unsupportedTlsSettings = {}; - for (const [tlsKey, tlsValue] of Object.entries(value)) { + for (const [tlsKey, tlsValue] of Object.entries( + value, + )) { switch (tlsKey) { case 'serverName': case 'fingerprint': @@ -1238,7 +1253,9 @@ function URI_VLESS() { } case 'network': { const normalizedNetwork = - typeof value === 'string' ? value.toLowerCase() : ''; + typeof value === 'string' + ? value.toLowerCase() + : ''; if ( normalizedNetwork !== 'xhttp' && normalizedNetwork !== 'splithttp' @@ -1501,6 +1518,10 @@ function URI_VLESS() { parsedDownloadSettings.host = downloadSettings.xhttpSettings.host; } + if (isNotBlank(downloadSettings.xhttpSettings.mode)) { + parsedDownloadSettings.mode = + downloadSettings.xhttpSettings.mode; + } applyXhttpExtraFields( parsedDownloadSettings, downloadSettings.xhttpSettings, @@ -1651,6 +1672,13 @@ function URI_VLESS() { } else { opts.headers = { Host: host }; } + if (['xhttp'].includes(proxy.network) && opts.headers?.Host) { + opts.host = opts.headers.Host; + delete opts.headers.Host; + if (Object.keys(opts.headers).length === 0) { + delete opts.headers; + } + } } if (params.serviceName) { opts[`${proxy.network}-service-name`] = params.serviceName; @@ -1753,9 +1781,12 @@ function URI_VLESS() { // Supported fields must round-trip through the structured node // so later edits are reflected on export, while unsupported // fields still survive VLESS URI -> node -> VLESS URI flows. - const unsupportedExtra = collectUnsupportedRootXhttpExtra(extra, { - parsedDownloadSettings: downloadSettings, - }); + const unsupportedExtra = collectUnsupportedRootXhttpExtra( + extra, + { + parsedDownloadSettings: downloadSettings, + }, + ); if (unsupportedExtra) { proxy._extra_unsupported = unsupportedExtra; } diff --git a/backend/src/core/proxy-utils/producers/uri.js b/backend/src/core/proxy-utils/producers/uri.js index 65817b7a..c02e1aad 100644 --- a/backend/src/core/proxy-utils/producers/uri.js +++ b/backend/src/core/proxy-utils/producers/uri.js @@ -191,7 +191,7 @@ function applyStructuredXhttpExtraFields( } } -function buildXhttpDownloadSettings(downloadSettings) { +function buildXhttpDownloadSettings(downloadSettings, outerXhttpOpts = {}) { if (!isPlainObject(downloadSettings)) { return undefined; } @@ -271,13 +271,23 @@ function buildXhttpDownloadSettings(downloadSettings) { } const xhttpSettings = {}; - if (downloadSettings.path) { - xhttpSettings.path = downloadSettings.path; + // Mirror Mihomo's inheritance: path and host fall back to outer xhttp-opts + // when not explicitly set in download-settings. Mode is never a field in + // Mihomo's XHTTPDownloadSettings struct, so it always comes from outer. + const dsPath = downloadSettings.path ?? outerXhttpOpts.path; + if (dsPath) { + xhttpSettings.path = dsPath; } - const downloadHost = getTransportHost('xhttp', downloadSettings); + const downloadHost = + getTransportHost('xhttp', downloadSettings) ?? + getTransportHost('xhttp', outerXhttpOpts); if (downloadHost) { xhttpSettings.host = downloadHost; } + const mode = downloadSettings.mode ?? outerXhttpOpts.mode; + if (mode) { + xhttpSettings.mode = mode; + } applyStructuredXhttpExtraFields(xhttpSettings, downloadSettings, { excludeHostHeader: true, xmuxTarget: 'extra', @@ -298,11 +308,15 @@ function buildXhttpDownloadSettings(downloadSettings) { network: normalizedNetwork || 'xhttp', ...(result.port != null ? { port: result.port } : {}), ...(result.security != null ? { security: result.security } : {}), - ...(result.tlsSettings != null ? { tlsSettings: result.tlsSettings } : {}), + ...(result.tlsSettings != null + ? { tlsSettings: result.tlsSettings } + : {}), ...(result.realitySettings != null ? { realitySettings: result.realitySettings } : {}), - ...(result.xhttpSettings != null ? { xhttpSettings: result.xhttpSettings } : {}), + ...(result.xhttpSettings != null + ? { xhttpSettings: result.xhttpSettings } + : {}), }; } @@ -316,6 +330,7 @@ function buildStructuredVlessExtraObject(proxy) { const downloadSettings = buildXhttpDownloadSettings( xhttpOpts['download-settings'], + xhttpOpts, ); if (downloadSettings) { extra.downloadSettings = downloadSettings; diff --git a/backend/src/test/proxy-parsers/v2ray-and-platforms.spec.js b/backend/src/test/proxy-parsers/v2ray-and-platforms.spec.js index c1b4972a..bb67c382 100644 --- a/backend/src/test/proxy-parsers/v2ray-and-platforms.spec.js +++ b/backend/src/test/proxy-parsers/v2ray-and-platforms.spec.js @@ -657,9 +657,7 @@ describe('VMess and VLESS parser coverage', function () { 'xhttp-opts': { mode: 'stream-up', path: '/xhttp', - headers: { - Host: 'cdn.example.com', - }, + host: 'cdn.example.com', 'no-grpc-header': true, 'x-padding-bytes': '64-128', 'sc-max-each-post-bytes': 1000000, @@ -677,6 +675,32 @@ describe('VMess and VLESS parser coverage', function () { expect(proxy).to.not.have.property('_extra_unsupported'); }); + it('moves xhttp Host from obfsParam to xhttp-opts.host', function () { + const obfsParam = JSON.stringify({ + Host: 'header.example.com', + 'X-Test': 'demo', + }); + const proxy = parseOne( + `vless://${UUID}@vless-xhttp.example.com:443?type=xhttp&security=tls&obfsParam=${encodeURIComponent( + obfsParam, + )}&path=%2Fxhttp#VLESS%20XHTTP%20Host%20Header`, + ); + + expectSubset(proxy, { + network: 'xhttp', + 'xhttp-opts': { + host: 'header.example.com', + path: '/xhttp', + headers: { + 'X-Test': 'demo', + }, + }, + }); + expect(proxy['xhttp-opts']).to.not.have.nested.property( + 'headers.Host', + ); + }); + it('parses xhttp VLESS shares with downloadSettings extra', function () { const extra = JSON.stringify({ downloadSettings: { @@ -721,9 +745,7 @@ describe('VMess and VLESS parser coverage', function () { 'xhttp-opts': { mode: 'stream-up', path: '/xhttp', - headers: { - Host: 'cdn.example.com', - }, + host: 'cdn.example.com', 'download-settings': { server: 'download.example.com', port: 8443, @@ -777,9 +799,7 @@ describe('VMess and VLESS parser coverage', function () { network: 'xhttp', 'xhttp-opts': { path: '/xhttp', - headers: { - Host: 'cdn.example.com', - }, + host: 'cdn.example.com', 'download-settings': { server: 'download.example.com', port: 8443, @@ -817,9 +837,7 @@ describe('VMess and VLESS parser coverage', function () { 'xhttp-opts': { mode: 'stream-up', path: '/xhttp', - headers: { - Host: 'cdn.example.com', - }, + host: 'cdn.example.com', 'download-settings': { network: 'xhttp', }, @@ -860,9 +878,7 @@ describe('VMess and VLESS parser coverage', function () { 'xhttp-opts': { mode: 'stream-up', path: '/xhttp', - headers: { - Host: 'cdn.example.com', - }, + host: 'cdn.example.com', 'download-settings': { network: 'xhttp', }, @@ -906,9 +922,7 @@ describe('VMess and VLESS parser coverage', function () { 'xhttp-opts': { mode: 'stream-up', path: '/xhttp', - headers: { - Host: 'cdn.example.com', - }, + host: 'cdn.example.com', 'download-settings': { network: 'xhttp', server: 'download.example.com', @@ -943,9 +957,7 @@ describe('VMess and VLESS parser coverage', function () { 'xhttp-opts': { mode: 'stream-up', path: '/xhttp', - headers: { - Host: 'cdn.example.com', - }, + host: 'cdn.example.com', }, }); expect(proxy).to.not.have.property('_extra_unsupported'); @@ -981,9 +993,7 @@ describe('VMess and VLESS parser coverage', function () { 'xhttp-opts': { mode: 'stream-up', path: '/xhttp', - headers: { - Host: 'cdn.example.com', - }, + host: 'cdn.example.com', 'no-grpc-header': true, 'x-padding-bytes': '64-128', 'sc-min-posts-interval-ms': '100-300', @@ -1028,9 +1038,7 @@ describe('VMess and VLESS parser coverage', function () { 'xhttp-opts': { mode: 'stream-up', path: '/xhttp', - headers: { - Host: 'cdn.example.com', - }, + host: 'cdn.example.com', 'no-grpc-header': true, 'x-padding-bytes': '64-128', 'sc-min-posts-interval-ms': 300, @@ -1075,9 +1083,7 @@ describe('VMess and VLESS parser coverage', function () { 'xhttp-opts': { mode: 'stream-up', path: '/xhttp', - headers: { - Host: 'cdn.example.com', - }, + host: 'cdn.example.com', 'no-grpc-header': true, 'x-padding-bytes': '64-128', 'sc-min-posts-interval-ms': '0-300', @@ -1127,9 +1133,7 @@ describe('VMess and VLESS parser coverage', function () { 'xhttp-opts': { mode: 'stream-up', path: '/xhttp', - headers: { - Host: 'cdn.example.com', - }, + host: 'cdn.example.com', 'no-grpc-header': true, 'x-padding-bytes': '64-128', 'sc-max-each-post-bytes': 1000000, @@ -1182,9 +1186,7 @@ describe('VMess and VLESS parser coverage', function () { 'xhttp-opts': { mode: 'stream-up', path: '/xhttp', - headers: { - Host: 'cdn.example.com', - }, + host: 'cdn.example.com', 'no-grpc-header': true, 'x-padding-bytes': '64-128', 'sc-max-each-post-bytes': 1000000, @@ -1244,9 +1246,7 @@ describe('VMess and VLESS parser coverage', function () { 'xhttp-opts': { mode: 'stream-up', path: '/xhttp', - headers: { - Host: 'cdn.example.com', - }, + host: 'cdn.example.com', 'no-grpc-header': true, 'x-padding-bytes': '64-128', 'reuse-settings': { @@ -1301,9 +1301,7 @@ describe('VMess and VLESS parser coverage', function () { 'xhttp-opts': { mode: 'stream-up', path: '/xhttp', - headers: { - Host: 'cdn.example.com', - }, + host: 'cdn.example.com', 'download-settings': { server: 'download.example.com', port: 8443, @@ -1414,8 +1412,8 @@ describe('VMess and VLESS parser coverage', function () { 'xhttp-opts': { mode: 'stream-up', path: '/xhttp', + host: 'cdn.example.com', headers: { - Host: 'cdn.example.com', 'X-Test': 'demo', }, 'no-grpc-header': true, @@ -1512,18 +1510,16 @@ describe('VMess and VLESS parser coverage', function () { 'xhttp-opts': { mode: 'stream-up', path: '/xhttp', - headers: { - Host: 'cdn.example.com', - }, + host: 'cdn.example.com', 'reuse-settings': { 'max-connections': '8', 'max-concurrency': '8-16', }, }, }); - expect(proxy['xhttp-opts']?.['reuse-settings']).to.not.have.property( - 'h-keep-alive-period', - ); + expect( + proxy['xhttp-opts']?.['reuse-settings'], + ).to.not.have.property('h-keep-alive-period'); expect(proxy._extra_unsupported).to.deep.equal({ xmux: { hKeepAlivePeriod: '9007199254740993', @@ -1554,9 +1550,7 @@ describe('VMess and VLESS parser coverage', function () { 'xhttp-opts': { mode: 'stream-up', path: '/xhttp', - headers: { - Host: 'cdn.example.com', - }, + host: 'cdn.example.com', 'download-settings': { server: 'download.example.com', port: 8443, @@ -1593,9 +1587,7 @@ describe('VMess and VLESS parser coverage', function () { 'xhttp-opts': { mode: 'stream-up', path: '/xhttp', - headers: { - Host: 'cdn.example.com', - }, + host: 'cdn.example.com', 'download-settings': { server: 'download.example.com', port: 8443, @@ -1646,9 +1638,7 @@ describe('VMess and VLESS parser coverage', function () { 'xhttp-opts': { mode: 'stream-up', path: '/xhttp', - headers: { - Host: 'cdn.example.com', - }, + host: 'cdn.example.com', 'download-settings': { server: 'download.example.com', port: 8443, @@ -1699,9 +1689,7 @@ describe('VMess and VLESS parser coverage', function () { 'xhttp-opts': { mode: 'stream-up', path: '/xhttp', - headers: { - Host: 'cdn.example.com', - }, + host: 'cdn.example.com', 'no-grpc-header': true, 'x-padding-bytes': '64-128', 'sc-max-each-post-bytes': 1000000, @@ -1746,9 +1734,7 @@ describe('VMess and VLESS parser coverage', function () { 'xhttp-opts': { mode: 'stream-up', path: '/xhttp', - headers: { - Host: 'cdn.example.com', - }, + host: 'cdn.example.com', 'no-grpc-header': true, 'x-padding-bytes': '64-128', 'sc-max-each-post-bytes': 1000000, @@ -1793,9 +1779,7 @@ describe('VMess and VLESS parser coverage', function () { 'xhttp-opts': { mode: 'stream-up', path: '/xhttp', - headers: { - Host: 'cdn.example.com', - }, + host: 'cdn.example.com', 'no-grpc-header': true, 'x-padding-bytes': '64-128', 'sc-max-each-post-bytes': 1000000, @@ -1851,9 +1835,7 @@ describe('VMess and VLESS parser coverage', function () { 'xhttp-opts': { mode: 'stream-up', path: '/xhttp', - headers: { - Host: 'cdn.example.com', - }, + host: 'cdn.example.com', 'no-grpc-header': true, 'x-padding-bytes': '64-128', 'reuse-settings': { diff --git a/backend/src/test/proxy-producers/text.spec.js b/backend/src/test/proxy-producers/text.spec.js index e44c13e4..43430a17 100644 --- a/backend/src/test/proxy-producers/text.spec.js +++ b/backend/src/test/proxy-producers/text.spec.js @@ -1339,7 +1339,9 @@ describe('Proxy text producers', function () { expect(extra.xmux).to.deep.equal({ hKeepAlivePeriod: '9007199254740993', }); - expect(extra.downloadSettings?.address).to.equal('download.example.com'); + expect(extra.downloadSettings?.address).to.equal( + 'download.example.com', + ); expect(extra.downloadSettings?.port).to.equal(8443); expect(extra.downloadSettings?.network).to.equal('xhttp'); expect(extra.downloadSettings?.security).to.equal('tls'); @@ -1400,6 +1402,11 @@ describe('Proxy text producers', function () { const extra = JSON.parse(decodeURIComponent(encodedExtra)); expect(extra.downloadSettings).to.deep.equal({ network: 'xhttp', + xhttpSettings: { + path: '/xhttp', + host: 'cdn.example.com', + mode: 'stream-up', + }, sockopt: { mark: 255, }, @@ -1407,11 +1414,12 @@ describe('Proxy text producers', function () { const reparsed = ProxyUtils.parse(output); expect(reparsed, output).to.have.length(1); - expect(reparsed[0]['xhttp-opts']?.['download-settings']).to.deep.equal( - { - network: 'xhttp', - }, - ); + expect(reparsed[0]['xhttp-opts']?.['download-settings']).to.deep.equal({ + network: 'xhttp', + path: '/xhttp', + host: 'cdn.example.com', + mode: 'stream-up', + }); expect(reparsed[0]._extra_unsupported).to.deep.equal({ downloadSettings: { sockopt: { @@ -1525,12 +1533,12 @@ describe('Proxy text producers', function () { maxConcurrency: '8-16', hKeepAlivePeriod: 15, }); - expect(extra.downloadSettings?.xhttpSettings?.extra?.xmux).to.deep.equal( - { - hMaxRequestTimes: '4-8', - hKeepAlivePeriod: -1, - }, - ); + expect( + extra.downloadSettings?.xhttpSettings?.extra?.xmux, + ).to.deep.equal({ + hMaxRequestTimes: '4-8', + hKeepAlivePeriod: -1, + }); const reparsed = ProxyUtils.parse(output); expect(reparsed, output).to.have.length(1); @@ -1636,6 +1644,7 @@ describe('Proxy text producers', function () { xhttpSettings: { path: '/download', host: 'download-host.example.com', + mode: 'stream-up', headers: { 'X-Download': '1', }, @@ -1781,6 +1790,7 @@ describe('Proxy text producers', function () { xhttpSettings: { path: '/download', host: 'download-host.example.com', + mode: 'stream-up', scMaxEachPostBytes: 1000000, scMinPostsIntervalMs: '0-300', }, @@ -1853,6 +1863,7 @@ describe('Proxy text producers', function () { xhttpSettings: { path: '/download', host: 'download-host.example.com', + mode: 'stream-up', scMaxEachPostBytes: 1000000, scMinPostsIntervalMs: '0-300', }, @@ -1933,6 +1944,7 @@ describe('Proxy text producers', function () { xhttpSettings: { path: '/download', host: 'download-host.example.com', + mode: 'stream-up', noGRPCHeader: true, xPaddingBytes: '32-64', scMaxEachPostBytes: 1000000, @@ -2266,9 +2278,9 @@ describe('Proxy text producers', function () { const reparsed = ProxyUtils.parse(output); expect(reparsed, output).to.have.length(1); - expect( - reparsed[0]['xhttp-opts']?.['download-settings']?.host, - ).to.equal('download-host.example.com'); + expect(reparsed[0]['xhttp-opts']?.['download-settings']?.host).to.equal( + 'download-host.example.com', + ); expect( reparsed[0]['xhttp-opts']?.['download-settings']?.headers, ).to.deep.equal({ @@ -2317,9 +2329,9 @@ describe('Proxy text producers', function () { const reparsed = ProxyUtils.parse(output); expect(reparsed, output).to.have.length(1); - expect( - reparsed[0]['xhttp-opts']?.['download-settings']?.host, - ).to.equal('download-host.example.com'); + expect(reparsed[0]['xhttp-opts']?.['download-settings']?.host).to.equal( + 'download-host.example.com', + ); expect( reparsed[0]['xhttp-opts']?.['download-settings']?.headers, ).to.deep.equal({ @@ -2344,6 +2356,7 @@ describe('Proxy text producers', function () { xhttpSettings: { path: '/download', host: 'download-host.example.com', + mode: 'stream-up', scMaxEachPostBytes: 1000000, scMinPostsIntervalMs: '0-300', extra: { @@ -2451,6 +2464,8 @@ describe('Proxy text producers', function () { security: 'tls', xhttpSettings: { path: '/download', + host: 'cdn.example.com', + mode: 'stream-up', }, }); @@ -2464,6 +2479,8 @@ describe('Proxy text producers', function () { network: 'xhttp', tls: true, path: '/download', + host: 'cdn.example.com', + mode: 'stream-up', }); }); @@ -2482,6 +2499,7 @@ describe('Proxy text producers', function () { xhttpSettings: { path: '/download', host: 'download-host.example.com', + mode: 'stream-up', noGRPCHeader: true, xPaddingBytes: '32-64', extra: { @@ -2596,6 +2614,11 @@ describe('Proxy text producers', function () { network: 'xhttp', port: 8443, security: 'tls', + xhttpSettings: { + path: '/xhttp', + host: 'cdn.example.com', + mode: 'stream-up', + }, }, }); const output = produceExternal('URI', { @@ -2638,12 +2661,12 @@ describe('Proxy text producers', function () { expect( reparsed[0]['xhttp-opts']?.['download-settings'], ).to.not.have.property('sc-min-posts-interval-ms'); - expect( - reparsed[0]['xhttp-opts']?.['download-settings'], - ).to.not.have.property('path'); - expect( - reparsed[0]['xhttp-opts']?.['download-settings'], - ).to.not.have.property('host'); + expect(reparsed[0]['xhttp-opts']?.['download-settings']?.path).to.equal( + '/xhttp', + ); + expect(reparsed[0]['xhttp-opts']?.['download-settings']?.host).to.equal( + 'cdn.example.com', + ); }); it('produces URI Trojan websocket links with pcs from tls fingerprint', function () {