From 7e70ffd49416011ea9c8b66b7583fe9f5af6a2db Mon Sep 17 00:00:00 2001 From: xream Date: Tue, 14 Apr 2026 21:38:40 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=94=AF=E6=8C=81=20sc-min-posts-inter?= =?UTF-8?q?val-ms;=20=E4=BC=98=E5=8C=96=20VLESS=20URI=20=E8=BD=AC=E6=8D=A2?= =?UTF-8?q?=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/package.json | 2 +- backend/src/core/proxy-utils/parsers/index.js | 55 +- backend/src/core/proxy-utils/producers/uri.js | 145 ++++- backend/src/core/proxy-utils/xhttp-utils.js | 37 ++ .../proxy-parsers/v2ray-and-platforms.spec.js | 439 ++++++++++++++- .../test/proxy-producers/structured.spec.js | 90 ++- backend/src/test/proxy-producers/text.spec.js | 528 ++++++++++++++++++ 7 files changed, 1263 insertions(+), 33 deletions(-) create mode 100644 backend/src/core/proxy-utils/xhttp-utils.js diff --git a/backend/package.json b/backend/package.json index 9130801a..7f7116f7 100644 --- a/backend/package.json +++ b/backend/package.json @@ -1,6 +1,6 @@ { "name": "sub-store", - "version": "2.21.94", + "version": "2.21.95", "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 50b464c4..13d1a474 100644 --- a/backend/src/core/proxy-utils/parsers/index.js +++ b/backend/src/core/proxy-utils/parsers/index.js @@ -18,6 +18,7 @@ import YAML from '@/utils/yaml'; import _ from 'lodash'; import { Base64 } from 'js-base64'; +import { normalizeXhttpScalarUpperBound } from '../xhttp-utils'; function surge_port_hopping(raw) { const [parts, port_hopping] = @@ -800,6 +801,22 @@ function URI_VLESS() { parsedDownloadSettings['x-padding-bytes'] = downloadSettings.xhttpSettings.xPaddingBytes; } + const scMaxEachPostBytes = + normalizeXhttpScalarUpperBound( + downloadSettings.xhttpSettings.scMaxEachPostBytes, + ); + if (scMaxEachPostBytes != null) { + parsedDownloadSettings['sc-max-each-post-bytes'] = + scMaxEachPostBytes; + } + const scMinPostsIntervalMs = + normalizeXhttpScalarUpperBound( + downloadSettings.xhttpSettings.scMinPostsIntervalMs, + ); + if (scMinPostsIntervalMs != null) { + parsedDownloadSettings['sc-min-posts-interval-ms'] = + scMinPostsIntervalMs; + } const reuseSettings = mapXmuxToReuseSettings( downloadSettings.xhttpSettings.extra?.xmux, @@ -1032,31 +1049,19 @@ function URI_VLESS() { if (isNotBlank(extra?.['xPaddingBytes'])) { xhttpOpts['x-padding-bytes'] = extra['xPaddingBytes']; } - const scMaxEachPostBytes = extra?.['scMaxEachPostBytes']; - if ( - typeof scMaxEachPostBytes === 'string' || - typeof scMaxEachPostBytes === 'number' - ) { - const normalizedValue = `${scMaxEachPostBytes}`; - if (/^\s*[1-9]\d*\s*$/.test(normalizedValue)) { - xhttpOpts['sc-max-each-post-bytes'] = parseInt( - normalizedValue, - 10, - ); - } else { - const rangeMatch = normalizedValue.match( - /^\s*([1-9]\d*)\s*-\s*([1-9]\d*)\s*$/, - ); - if (rangeMatch) { - const lowerBound = parseInt(rangeMatch[1], 10); - const upperBound = parseInt(rangeMatch[2], 10); - - if (upperBound >= lowerBound) { - xhttpOpts['sc-max-each-post-bytes'] = - upperBound; - } - } - } + const scMaxEachPostBytes = + normalizeXhttpScalarUpperBound(extra?.['scMaxEachPostBytes']); + if (scMaxEachPostBytes != null) { + xhttpOpts['sc-max-each-post-bytes'] = + scMaxEachPostBytes; + } + const scMinPostsIntervalMs = + normalizeXhttpScalarUpperBound( + extra?.['scMinPostsIntervalMs'], + ); + if (scMinPostsIntervalMs != null) { + xhttpOpts['sc-min-posts-interval-ms'] = + scMinPostsIntervalMs; } if (isPlainObject(extra?.xmux)) { const reuseSettings = mapXmuxToReuseSettings(extra.xmux); diff --git a/backend/src/core/proxy-utils/producers/uri.js b/backend/src/core/proxy-utils/producers/uri.js index a87dd550..abe20235 100644 --- a/backend/src/core/proxy-utils/producers/uri.js +++ b/backend/src/core/proxy-utils/producers/uri.js @@ -2,6 +2,7 @@ import { Base64 } from 'js-base64'; import { isIPv6 } from '@/utils'; import { normalizePluginMuxValue } from './utils'; +import { normalizeXhttpScalarUpperBound } from '../xhttp-utils'; function isObject(value) { return value != null && typeof value === 'object' && !Array.isArray(value); @@ -103,6 +104,22 @@ function buildXhttpDownloadSettings(downloadSettings) { if (downloadSettings['x-padding-bytes']) { xhttpSettings.xPaddingBytes = downloadSettings['x-padding-bytes']; } + if (downloadSettings['sc-max-each-post-bytes'] != null) { + const scMaxEachPostBytes = normalizeXhttpScalarUpperBound( + downloadSettings['sc-max-each-post-bytes'], + ); + if (scMaxEachPostBytes != null) { + xhttpSettings.scMaxEachPostBytes = scMaxEachPostBytes; + } + } + if (downloadSettings['sc-min-posts-interval-ms'] != null) { + const scMinPostsIntervalMs = normalizeXhttpScalarUpperBound( + downloadSettings['sc-min-posts-interval-ms'], + ); + if (scMinPostsIntervalMs != null) { + xhttpSettings.scMinPostsIntervalMs = scMinPostsIntervalMs; + } + } const xmux = mapReuseSettingsToXmux(downloadSettings['reuse-settings']); if (xmux) { @@ -126,7 +143,20 @@ function buildStructuredVlessExtra(proxy) { extra.xPaddingBytes = xhttpOpts['x-padding-bytes']; } if (xhttpOpts['sc-max-each-post-bytes'] != null) { - extra.scMaxEachPostBytes = xhttpOpts['sc-max-each-post-bytes']; + const scMaxEachPostBytes = normalizeXhttpScalarUpperBound( + xhttpOpts['sc-max-each-post-bytes'], + ); + if (scMaxEachPostBytes != null) { + extra.scMaxEachPostBytes = scMaxEachPostBytes; + } + } + if (xhttpOpts['sc-min-posts-interval-ms'] != null) { + const scMinPostsIntervalMs = normalizeXhttpScalarUpperBound( + xhttpOpts['sc-min-posts-interval-ms'], + ); + if (scMinPostsIntervalMs != null) { + extra.scMinPostsIntervalMs = scMinPostsIntervalMs; + } } const xmux = mapReuseSettingsToXmux(xhttpOpts['reuse-settings']); @@ -144,13 +174,124 @@ function buildStructuredVlessExtra(proxy) { return Object.keys(extra).length > 0 ? JSON.stringify(extra) : ''; } +function applyNormalizedXhttpScalar(target, rawKey, value) { + if (!isObject(target)) { + return false; + } + + const normalizedValue = normalizeXhttpScalarUpperBound(value); + if (normalizedValue != null) { + if (target[rawKey] === normalizedValue) { + return false; + } + target[rawKey] = normalizedValue; + return true; + } + + if (Object.prototype.hasOwnProperty.call(target, rawKey)) { + delete target[rawKey]; + return true; + } + + return false; +} + +function normalizeRawXhttpExtra(proxy) { + if (!proxy._extra) { + return ''; + } + + let rawExtra; + try { + rawExtra = JSON.parse(proxy._extra); + } catch (e) { + return proxy._extra; + } + + if (!isObject(rawExtra) || !isObject(proxy['xhttp-opts'])) { + return proxy._extra; + } + + let didChange = false; + const xhttpOpts = proxy['xhttp-opts']; + + if (xhttpOpts['sc-max-each-post-bytes'] != null) { + didChange = + applyNormalizedXhttpScalar( + rawExtra, + 'scMaxEachPostBytes', + xhttpOpts['sc-max-each-post-bytes'], + ) || didChange; + } + if (xhttpOpts['sc-min-posts-interval-ms'] != null) { + didChange = + applyNormalizedXhttpScalar( + rawExtra, + 'scMinPostsIntervalMs', + xhttpOpts['sc-min-posts-interval-ms'], + ) || didChange; + } + + const downloadSettings = xhttpOpts['download-settings']; + if (isObject(downloadSettings)) { + const rawDownloadSettings = isObject(rawExtra.downloadSettings) + ? rawExtra.downloadSettings + : undefined; + + let rawXhttpSettings = isObject(rawDownloadSettings?.xhttpSettings) + ? rawDownloadSettings.xhttpSettings + : undefined; + if ( + !rawXhttpSettings && + (downloadSettings['sc-max-each-post-bytes'] != null || + downloadSettings['sc-min-posts-interval-ms'] != null) + ) { + rawExtra.downloadSettings = isObject(rawExtra.downloadSettings) + ? rawExtra.downloadSettings + : {}; + rawExtra.downloadSettings.xhttpSettings = {}; + rawXhttpSettings = rawExtra.downloadSettings.xhttpSettings; + didChange = true; + } + + if (rawXhttpSettings) { + if (downloadSettings['sc-max-each-post-bytes'] != null) { + didChange = + applyNormalizedXhttpScalar( + rawXhttpSettings, + 'scMaxEachPostBytes', + downloadSettings['sc-max-each-post-bytes'], + ) || didChange; + } + if (downloadSettings['sc-min-posts-interval-ms'] != null) { + didChange = + applyNormalizedXhttpScalar( + rawXhttpSettings, + 'scMinPostsIntervalMs', + downloadSettings['sc-min-posts-interval-ms'], + ) || didChange; + } + + if (Object.keys(rawXhttpSettings).length === 0) { + delete rawExtra.downloadSettings.xhttpSettings; + didChange = true; + if (Object.keys(rawExtra.downloadSettings).length === 0) { + delete rawExtra.downloadSettings; + } + } + } + } + + return didChange ? JSON.stringify(rawExtra) : proxy._extra; +} + function buildVlessExtra(proxy) { if (proxy.network !== 'xhttp') { return proxy._extra || ''; } if (proxy._extra) { - return proxy._extra; + return normalizeRawXhttpExtra(proxy); } return buildStructuredVlessExtra(proxy); diff --git a/backend/src/core/proxy-utils/xhttp-utils.js b/backend/src/core/proxy-utils/xhttp-utils.js new file mode 100644 index 00000000..733f203b --- /dev/null +++ b/backend/src/core/proxy-utils/xhttp-utils.js @@ -0,0 +1,37 @@ +export function normalizeXhttpScalarUpperBound(value) { + if (typeof value !== 'string' && typeof value !== 'number') { + return undefined; + } + + // Mirror Mihomo's ParseRange/GetNormalizedSc* behavior: accept explicit + // positive signs, leading zeros, and ascending ranges, but reject + // zero-valued finals because sc-* values must resolve to > 0. + const parseUnsignedIntegerToken = (token) => { + const normalizedToken = token.trim(); + if (!/^\+?\d+$/.test(normalizedToken)) { + return undefined; + } + + const parsedInteger = parseInt(normalizedToken, 10); + return Number.isSafeInteger(parsedInteger) ? parsedInteger : undefined; + }; + + const normalizedValue = `${value}`.trim(); + const rangeParts = normalizedValue.split('-'); + if (rangeParts.length === 1) { + const normalizedInteger = parseUnsignedIntegerToken(rangeParts[0]); + return normalizedInteger > 0 ? normalizedInteger : undefined; + } + + if (rangeParts.length !== 2) { + return undefined; + } + + const lowerBound = parseUnsignedIntegerToken(rangeParts[0]); + const upperBound = parseUnsignedIntegerToken(rangeParts[1]); + if (lowerBound == null || upperBound == null) { + return undefined; + } + + return upperBound > 0 && upperBound >= lowerBound ? upperBound : undefined; +} 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 ce17f6ed..87d2a361 100644 --- a/backend/src/test/proxy-parsers/v2ray-and-platforms.spec.js +++ b/backend/src/test/proxy-parsers/v2ray-and-platforms.spec.js @@ -629,6 +629,7 @@ describe('VMess and VLESS parser coverage', function () { noGRPCHeader: true, xPaddingBytes: '64-128', scMaxEachPostBytes: 1000000, + scMinPostsIntervalMs: 300, xmux: { maxConnections: 0, maxConcurrency: '16-32', @@ -661,6 +662,7 @@ describe('VMess and VLESS parser coverage', function () { 'no-grpc-header': true, 'x-padding-bytes': '64-128', 'sc-max-each-post-bytes': 1000000, + 'sc-min-posts-interval-ms': 300, 'reuse-settings': { 'max-connections': '0', 'max-concurrency': '16-32', @@ -688,6 +690,8 @@ describe('VMess and VLESS parser coverage', function () { host: 'download-host.example.com', noGRPCHeader: true, xPaddingBytes: '32-64', + scMaxEachPostBytes: '500000-1000000', + scMinPostsIntervalMs: '0-300', extra: { xmux: { maxConnections: '8', @@ -729,6 +733,8 @@ describe('VMess and VLESS parser coverage', function () { host: 'download-host.example.com', 'no-grpc-header': true, 'x-padding-bytes': '32-64', + 'sc-max-each-post-bytes': 1000000, + 'sc-min-posts-interval-ms': 300, 'reuse-settings': { 'max-connections': '8', 'h-max-reusable-secs': '900', @@ -780,6 +786,380 @@ describe('VMess and VLESS parser coverage', function () { expect(proxy['xhttp-opts']).to.not.have.property('mode'); }); + it('parses xhttp VLESS shares with range-form scMinPostsIntervalMs', function () { + const extra = JSON.stringify({ + noGRPCHeader: true, + xPaddingBytes: '64-128', + scMinPostsIntervalMs: '100 - 300', + xmux: { + maxConnections: 0, + maxConcurrency: '16-32', + cMaxReuseTimes: '64-128', + hMaxRequestTimes: '600-900', + hMaxReusableSecs: '1800-3000', + }, + }); + const proxy = parseOne( + `vless://${UUID}@vless-xhttp.example.com:443?type=xhttp&security=tls&host=cdn.example.com&path=%2Fxhttp&mode=stream-up&extra=${encodeURIComponent( + extra, + )}#VLESS%20XHTTP%20Min%20Interval%20Range`, + ); + + expectSubset(proxy, { + type: 'vless', + name: 'VLESS XHTTP Min Interval Range', + server: 'vless-xhttp.example.com', + port: 443, + uuid: UUID, + tls: true, + network: 'xhttp', + _extra: extra, + 'xhttp-opts': { + mode: 'stream-up', + path: '/xhttp', + headers: { + Host: 'cdn.example.com', + }, + 'no-grpc-header': true, + 'x-padding-bytes': '64-128', + 'sc-min-posts-interval-ms': 300, + 'reuse-settings': { + 'max-connections': '0', + 'max-concurrency': '16-32', + 'c-max-reuse-times': '64-128', + 'h-max-request-times': '600-900', + 'h-max-reusable-secs': '1800-3000', + }, + }, + }); + }); + + it('parses xhttp VLESS shares with string-form scMinPostsIntervalMs', function () { + const extra = JSON.stringify({ + noGRPCHeader: true, + xPaddingBytes: '64-128', + scMinPostsIntervalMs: '300', + xmux: { + maxConnections: 0, + maxConcurrency: '16-32', + cMaxReuseTimes: '64-128', + hMaxRequestTimes: '600-900', + hMaxReusableSecs: '1800-3000', + }, + }); + const proxy = parseOne( + `vless://${UUID}@vless-xhttp.example.com:443?type=xhttp&security=tls&host=cdn.example.com&path=%2Fxhttp&mode=stream-up&extra=${encodeURIComponent( + extra, + )}#VLESS%20XHTTP%20Min%20Interval%20String`, + ); + + expectSubset(proxy, { + type: 'vless', + name: 'VLESS XHTTP Min Interval String', + server: 'vless-xhttp.example.com', + port: 443, + uuid: UUID, + tls: true, + network: 'xhttp', + _extra: extra, + 'xhttp-opts': { + mode: 'stream-up', + path: '/xhttp', + headers: { + Host: 'cdn.example.com', + }, + 'no-grpc-header': true, + 'x-padding-bytes': '64-128', + 'sc-min-posts-interval-ms': 300, + 'reuse-settings': { + 'max-connections': '0', + 'max-concurrency': '16-32', + 'c-max-reuse-times': '64-128', + 'h-max-request-times': '600-900', + 'h-max-reusable-secs': '1800-3000', + }, + }, + }); + }); + + it('parses xhttp VLESS shares with zero-lower-bound scMinPostsIntervalMs range', function () { + const extra = JSON.stringify({ + noGRPCHeader: true, + xPaddingBytes: '64-128', + scMinPostsIntervalMs: '0-300', + xmux: { + maxConnections: 0, + maxConcurrency: '16-32', + cMaxReuseTimes: '64-128', + hMaxRequestTimes: '600-900', + hMaxReusableSecs: '1800-3000', + }, + }); + const proxy = parseOne( + `vless://${UUID}@vless-xhttp.example.com:443?type=xhttp&security=tls&host=cdn.example.com&path=%2Fxhttp&mode=stream-up&extra=${encodeURIComponent( + extra, + )}#VLESS%20XHTTP%20Min%20Interval%20Zero%20Lower%20Bound`, + ); + + expectSubset(proxy, { + type: 'vless', + name: 'VLESS XHTTP Min Interval Zero Lower Bound', + server: 'vless-xhttp.example.com', + port: 443, + uuid: UUID, + tls: true, + network: 'xhttp', + _extra: extra, + 'xhttp-opts': { + mode: 'stream-up', + path: '/xhttp', + headers: { + Host: 'cdn.example.com', + }, + 'no-grpc-header': true, + 'x-padding-bytes': '64-128', + 'sc-min-posts-interval-ms': 300, + 'reuse-settings': { + 'max-connections': '0', + 'max-concurrency': '16-32', + 'c-max-reuse-times': '64-128', + 'h-max-request-times': '600-900', + 'h-max-reusable-secs': '1800-3000', + }, + }, + }); + }); + + it('parses xhttp VLESS shares with Mihomo-style leading-zero sc scalars', function () { + const extra = JSON.stringify({ + noGRPCHeader: true, + xPaddingBytes: '64-128', + scMaxEachPostBytes: '000-1000000', + scMinPostsIntervalMs: '0300', + downloadSettings: { + address: 'download.example.com', + port: 8443, + security: 'tls', + xhttpSettings: { + path: '/download', + host: 'download-host.example.com', + scMaxEachPostBytes: '000-1000000', + scMinPostsIntervalMs: '000-300', + }, + }, + }); + const proxy = parseOne( + `vless://${UUID}@vless-xhttp.example.com:443?type=xhttp&security=tls&host=cdn.example.com&path=%2Fxhttp&mode=stream-up&extra=${encodeURIComponent( + extra, + )}#VLESS%20XHTTP%20Leading%20Zero%20Scalars`, + ); + + expectSubset(proxy, { + type: 'vless', + name: 'VLESS XHTTP Leading Zero Scalars', + server: 'vless-xhttp.example.com', + port: 443, + uuid: UUID, + tls: true, + network: 'xhttp', + _extra: extra, + 'xhttp-opts': { + mode: 'stream-up', + path: '/xhttp', + headers: { + Host: 'cdn.example.com', + }, + 'no-grpc-header': true, + 'x-padding-bytes': '64-128', + 'sc-max-each-post-bytes': 1000000, + 'sc-min-posts-interval-ms': 300, + 'download-settings': { + server: 'download.example.com', + port: 8443, + tls: true, + path: '/download', + host: 'download-host.example.com', + 'sc-max-each-post-bytes': 1000000, + 'sc-min-posts-interval-ms': 300, + }, + }, + }); + }); + + it('parses xhttp VLESS shares with Mihomo-style explicit-plus sc scalars', function () { + const extra = JSON.stringify({ + noGRPCHeader: true, + xPaddingBytes: '64-128', + scMaxEachPostBytes: '+500000-+1000000', + scMinPostsIntervalMs: '+300', + downloadSettings: { + address: 'download.example.com', + port: 8443, + security: 'tls', + xhttpSettings: { + path: '/download', + host: 'download-host.example.com', + scMaxEachPostBytes: '+0-+1000000', + scMinPostsIntervalMs: '+0-+300', + }, + }, + }); + const proxy = parseOne( + `vless://${UUID}@vless-xhttp.example.com:443?type=xhttp&security=tls&host=cdn.example.com&path=%2Fxhttp&mode=stream-up&extra=${encodeURIComponent( + extra, + )}#VLESS%20XHTTP%20Explicit%20Plus%20Scalars`, + ); + + expectSubset(proxy, { + type: 'vless', + name: 'VLESS XHTTP Explicit Plus Scalars', + server: 'vless-xhttp.example.com', + port: 443, + uuid: UUID, + tls: true, + network: 'xhttp', + _extra: extra, + 'xhttp-opts': { + mode: 'stream-up', + path: '/xhttp', + headers: { + Host: 'cdn.example.com', + }, + 'no-grpc-header': true, + 'x-padding-bytes': '64-128', + 'sc-max-each-post-bytes': 1000000, + 'sc-min-posts-interval-ms': 300, + 'download-settings': { + server: 'download.example.com', + port: 8443, + tls: true, + path: '/download', + host: 'download-host.example.com', + 'sc-max-each-post-bytes': 1000000, + 'sc-min-posts-interval-ms': 300, + }, + }, + }); + }); + + it('ignores invalid xhttp VLESS scMinPostsIntervalMs values', function () { + const invalidValues = [ + '1.5', + 1.5, + '0', + 0, + '0-0', + 'fast', + '10-1', + '9007199254740993', + '1-9007199254740993', + ]; + + for (const value of invalidValues) { + const extra = JSON.stringify({ + noGRPCHeader: true, + xPaddingBytes: '64-128', + scMinPostsIntervalMs: value, + xmux: { + maxConnections: 0, + maxConcurrency: '16-32', + cMaxReuseTimes: '64-128', + hMaxRequestTimes: '600-900', + hMaxReusableSecs: '1800-3000', + }, + }); + const proxy = parseOne( + `vless://${UUID}@vless-xhttp.example.com:443?type=xhttp&security=tls&host=cdn.example.com&path=%2Fxhttp&mode=stream-up&extra=${encodeURIComponent( + extra, + )}#VLESS%20XHTTP%20Min%20Interval%20Invalid`, + ); + + expectSubset(proxy, { + type: 'vless', + server: 'vless-xhttp.example.com', + port: 443, + uuid: UUID, + tls: true, + network: 'xhttp', + _extra: extra, + 'xhttp-opts': { + mode: 'stream-up', + path: '/xhttp', + headers: { + Host: 'cdn.example.com', + }, + 'no-grpc-header': true, + 'x-padding-bytes': '64-128', + 'reuse-settings': { + 'max-connections': '0', + 'max-concurrency': '16-32', + 'c-max-reuse-times': '64-128', + 'h-max-request-times': '600-900', + 'h-max-reusable-secs': '1800-3000', + }, + }, + }); + expect(proxy['xhttp-opts']).to.not.have.property( + 'sc-min-posts-interval-ms', + ); + } + }); + + it('ignores invalid nested xhttp VLESS scMinPostsIntervalMs values in downloadSettings extra', function () { + const extra = JSON.stringify({ + downloadSettings: { + address: 'download.example.com', + port: 8443, + security: 'tls', + tlsSettings: { + serverName: 'download-sni.example.com', + }, + xhttpSettings: { + path: '/download', + host: 'download-host.example.com', + noGRPCHeader: true, + xPaddingBytes: '32-64', + scMinPostsIntervalMs: '0-0', + }, + }, + }); + const proxy = parseOne( + `vless://${UUID}@vless-xhttp.example.com:443?type=xhttp&security=tls&host=cdn.example.com&path=%2Fxhttp&mode=stream-up&extra=${encodeURIComponent( + extra, + )}#VLESS%20XHTTP%20Download%20Invalid%20Min%20Interval`, + ); + + expectSubset(proxy, { + type: 'vless', + server: 'vless-xhttp.example.com', + port: 443, + uuid: UUID, + tls: true, + network: 'xhttp', + _extra: extra, + 'xhttp-opts': { + mode: 'stream-up', + path: '/xhttp', + headers: { + Host: 'cdn.example.com', + }, + 'download-settings': { + server: 'download.example.com', + port: 8443, + tls: true, + servername: 'download-sni.example.com', + path: '/download', + host: 'download-host.example.com', + 'no-grpc-header': true, + 'x-padding-bytes': '32-64', + }, + }, + }); + expect( + proxy['xhttp-opts']?.['download-settings'], + ).to.not.have.property('sc-min-posts-interval-ms'); + }); + it('parses xhttp VLESS shares with range-form scMaxEachPostBytes', function () { const extra = JSON.stringify({ noGRPCHeader: true, @@ -876,8 +1256,65 @@ describe('VMess and VLESS parser coverage', function () { }); }); + it('parses xhttp VLESS shares with zero-lower-bound scMaxEachPostBytes range', function () { + const extra = JSON.stringify({ + noGRPCHeader: true, + xPaddingBytes: '64-128', + scMaxEachPostBytes: '0-1000000', + xmux: { + maxConnections: 0, + maxConcurrency: '16-32', + cMaxReuseTimes: '64-128', + hMaxRequestTimes: '600-900', + hMaxReusableSecs: '1800-3000', + }, + }); + const proxy = parseOne( + `vless://${UUID}@vless-xhttp.example.com:443?type=xhttp&security=tls&host=cdn.example.com&path=%2Fxhttp&mode=stream-up&extra=${encodeURIComponent( + extra, + )}#VLESS%20XHTTP%20Zero%20Lower%20Bound`, + ); + + expectSubset(proxy, { + type: 'vless', + name: 'VLESS XHTTP Zero Lower Bound', + server: 'vless-xhttp.example.com', + port: 443, + uuid: UUID, + tls: true, + network: 'xhttp', + _extra: extra, + 'xhttp-opts': { + mode: 'stream-up', + path: '/xhttp', + headers: { + Host: 'cdn.example.com', + }, + 'no-grpc-header': true, + 'x-padding-bytes': '64-128', + 'sc-max-each-post-bytes': 1000000, + 'reuse-settings': { + 'max-connections': '0', + 'max-concurrency': '16-32', + 'c-max-reuse-times': '64-128', + 'h-max-request-times': '600-900', + 'h-max-reusable-secs': '1800-3000', + }, + }, + }); + }); + it('ignores invalid xhttp VLESS scMaxEachPostBytes values', function () { - const invalidValues = ['1.5', 1.5, '0', 0, 'fast', '10-1']; + const invalidValues = [ + '1.5', + 1.5, + '0', + 0, + 'fast', + '10-1', + '9007199254740993', + '1-9007199254740993', + ]; for (const value of invalidValues) { const extra = JSON.stringify({ diff --git a/backend/src/test/proxy-producers/structured.spec.js b/backend/src/test/proxy-producers/structured.spec.js index 5948590a..01a2ed0e 100644 --- a/backend/src/test/proxy-producers/structured.spec.js +++ b/backend/src/test/proxy-producers/structured.spec.js @@ -163,11 +163,8 @@ describe('Proxy structured producers', function () { mode: 'stream-up', 'no-grpc-header': true, 'x-padding-bytes': '64-128', + 'sc-min-posts-interval-ms': 300, }, - _extra: JSON.stringify({ - noGRPCHeader: true, - xPaddingBytes: '64-128', - }), }; const internal = produceInternal('Mihomo', proxy); @@ -188,6 +185,7 @@ describe('Proxy structured producers', function () { mode: 'stream-up', 'no-grpc-header': true, 'x-padding-bytes': '64-128', + 'sc-min-posts-interval-ms': 300, }, }); expectSubset(external.proxies[0], { @@ -195,6 +193,90 @@ describe('Proxy structured producers', function () { name: 'XHTTP', network: 'xhttp', servername: 'sni.example.com', + 'xhttp-opts': { + 'sc-min-posts-interval-ms': 300, + }, + }); + }); + + it('emits Mihomo VLESS xhttp download settings with scMinPostsIntervalMs', function () { + const proxy = { + type: 'vless', + name: 'XHTTP Download', + server: 'vless-xhttp.example.com', + port: 443, + uuid: UUID, + tls: true, + sni: 'sni.example.com', + network: 'xhttp', + 'xhttp-opts': { + path: '/xhttp', + headers: { + Host: 'cdn.example.com', + }, + mode: 'stream-up', + 'download-settings': { + server: 'download.example.com', + port: 8443, + tls: true, + servername: 'download-sni.example.com', + path: '/download', + host: 'download-host.example.com', + 'sc-min-posts-interval-ms': 300, + 'reuse-settings': { + 'max-connections': '8', + 'h-max-reusable-secs': '900', + }, + }, + }, + }; + + const internal = produceInternal('Mihomo', proxy); + const external = loadProducedYaml('Mihomo', proxy); + + expect(internal).to.have.length(1); + expect(external.proxies).to.have.length(1); + expectSubset(internal[0], { + type: 'vless', + name: 'XHTTP Download', + network: 'xhttp', + servername: 'sni.example.com', + 'xhttp-opts': { + 'download-settings': { + server: 'download.example.com', + port: 8443, + tls: true, + servername: 'download-sni.example.com', + path: '/download', + host: 'download-host.example.com', + 'sc-min-posts-interval-ms': 300, + 'reuse-settings': { + 'max-connections': '8', + 'h-max-reusable-secs': '900', + }, + }, + }, + }); + expectSubset(external.proxies[0], { + type: 'vless', + name: 'XHTTP Download', + network: 'xhttp', + servername: 'sni.example.com', + 'xhttp-opts': { + 'download-settings': { + server: 'download.example.com', + port: 8443, + tls: true, + servername: 'download-sni.example.com', + path: '/download', + host: 'download-host.example.com', + 'sc-min-posts-interval-ms': 300, + 'reuse-settings': { + 'max-connections': '8', + 'h-max-reusable-secs': '900', + }, + }, + }, }); }); diff --git a/backend/src/test/proxy-producers/text.spec.js b/backend/src/test/proxy-producers/text.spec.js index c1880f33..11c7ff92 100644 --- a/backend/src/test/proxy-producers/text.spec.js +++ b/backend/src/test/proxy-producers/text.spec.js @@ -896,6 +896,261 @@ describe('Proxy text producers', function () { ); }); + it('normalizes raw xhttp sc scalars from _extra when structured xhttp fields exist', function () { + const rawExtra = JSON.stringify({ + customField: 'keep-me', + scMaxEachPostBytes: '000-1000000', + scMinPostsIntervalMs: '0300', + downloadSettings: { + address: 'download.example.com', + port: 8443, + security: 'tls', + xhttpSettings: { + scMaxEachPostBytes: '000-1000000', + scMinPostsIntervalMs: '000-300', + }, + }, + }); + const output = produceExternal('URI', { + type: 'vless', + name: 'URI XHTTP Raw Extra Scalars', + server: 'vless-xhttp.example.com', + port: 443, + uuid: UUID, + tls: true, + sni: 'sni.example.com', + network: 'xhttp', + _extra: rawExtra, + 'xhttp-opts': { + path: '/xhttp', + mode: 'stream-up', + headers: { + Host: 'cdn.example.com', + }, + 'sc-max-each-post-bytes': 1000000, + 'sc-min-posts-interval-ms': 300, + 'download-settings': { + server: 'download.example.com', + port: 8443, + tls: true, + 'sc-max-each-post-bytes': 1000000, + 'sc-min-posts-interval-ms': 300, + }, + }, + }); + + const [, encodedExtra] = output.match(/[?&]extra=([^#]+)/); + const extra = JSON.parse(decodeURIComponent(encodedExtra)); + expect(extra.customField).to.equal('keep-me'); + expect(extra.scMaxEachPostBytes).to.equal(1000000); + expect(extra.scMinPostsIntervalMs).to.equal(300); + expect(extra.downloadSettings?.xhttpSettings?.scMaxEachPostBytes).to.equal( + 1000000, + ); + expect( + extra.downloadSettings?.xhttpSettings?.scMinPostsIntervalMs, + ).to.equal(300); + + const reparsed = ProxyUtils.parse(output); + expect(reparsed, output).to.have.length(1); + expect(reparsed[0]['xhttp-opts']?.['sc-max-each-post-bytes']).to.equal( + 1000000, + ); + expect(reparsed[0]['xhttp-opts']?.['sc-min-posts-interval-ms']).to.equal( + 300, + ); + }); + + it('produces URI VLESS xhttp links with structured scMinPostsIntervalMs', function () { + const extra = JSON.stringify({ + noGRPCHeader: true, + xPaddingBytes: '64-128', + scMaxEachPostBytes: 1000000, + scMinPostsIntervalMs: 300, + xmux: { + maxConnections: '8', + }, + }); + const output = produceExternal('URI', { + type: 'vless', + name: 'URI XHTTP Min Interval', + server: 'vless-xhttp.example.com', + port: 443, + uuid: UUID, + tls: true, + sni: 'sni.example.com', + network: 'xhttp', + 'xhttp-opts': { + path: '/xhttp', + mode: 'stream-up', + headers: { + Host: 'cdn.example.com', + }, + 'no-grpc-header': true, + 'x-padding-bytes': '64-128', + 'sc-max-each-post-bytes': 1000000, + 'sc-min-posts-interval-ms': 300, + 'reuse-settings': { + 'max-connections': '8', + }, + }, + }); + + expect(output).to.equal( + `vless://${UUID}@vless-xhttp.example.com:443?security=tls&type=xhttp&path=%2Fxhttp&host=cdn.example.com&sni=sni.example.com&mode=stream-up&extra=${encodeURIComponent( + extra, + )}#URI%20XHTTP%20Min%20Interval`, + ); + + const reparsed = ProxyUtils.parse(output); + expect(reparsed, output).to.have.length(1); + expect(reparsed[0]['xhttp-opts']?.['sc-min-posts-interval-ms']).to.equal( + 300, + ); + }); + + it('normalizes Mihomo leading-zero structured xhttp scalars when producing URI VLESS links', function () { + const extra = JSON.stringify({ + scMaxEachPostBytes: 1000000, + scMinPostsIntervalMs: 300, + downloadSettings: { + address: 'download.example.com', + port: 8443, + security: 'tls', + xhttpSettings: { + path: '/download', + host: 'download-host.example.com', + scMaxEachPostBytes: 1000000, + scMinPostsIntervalMs: 300, + }, + }, + }); + const output = produceExternal('URI', { + type: 'vless', + name: 'URI XHTTP Leading Zero Scalars', + server: 'vless-xhttp.example.com', + port: 443, + uuid: UUID, + tls: true, + sni: 'sni.example.com', + network: 'xhttp', + 'xhttp-opts': { + path: '/xhttp', + mode: 'stream-up', + headers: { + Host: 'cdn.example.com', + }, + 'sc-max-each-post-bytes': '000-1000000', + 'sc-min-posts-interval-ms': '0300', + 'download-settings': { + server: 'download.example.com', + port: 8443, + tls: true, + path: '/download', + host: 'download-host.example.com', + 'sc-max-each-post-bytes': '000-1000000', + 'sc-min-posts-interval-ms': '000-300', + }, + }, + }); + + expect(output).to.equal( + `vless://${UUID}@vless-xhttp.example.com:443?security=tls&type=xhttp&path=%2Fxhttp&host=cdn.example.com&sni=sni.example.com&mode=stream-up&extra=${encodeURIComponent( + extra, + )}#URI%20XHTTP%20Leading%20Zero%20Scalars`, + ); + + const reparsed = ProxyUtils.parse(output); + expect(reparsed, output).to.have.length(1); + expect( + reparsed[0]['xhttp-opts']?.['sc-max-each-post-bytes'], + ).to.equal(1000000); + expect(reparsed[0]['xhttp-opts']?.['sc-min-posts-interval-ms']).to.equal( + 300, + ); + expect( + reparsed[0]['xhttp-opts']?.['download-settings']?.[ + 'sc-max-each-post-bytes' + ], + ).to.equal(1000000); + expect( + reparsed[0]['xhttp-opts']?.['download-settings']?.[ + 'sc-min-posts-interval-ms' + ], + ).to.equal(300); + }); + + it('normalizes Mihomo explicit-plus structured xhttp scalars when producing URI VLESS links', function () { + const extra = JSON.stringify({ + scMaxEachPostBytes: 1000000, + scMinPostsIntervalMs: 300, + downloadSettings: { + address: 'download.example.com', + port: 8443, + security: 'tls', + xhttpSettings: { + path: '/download', + host: 'download-host.example.com', + scMaxEachPostBytes: 1000000, + scMinPostsIntervalMs: 300, + }, + }, + }); + const output = produceExternal('URI', { + type: 'vless', + name: 'URI XHTTP Explicit Plus Scalars', + server: 'vless-xhttp.example.com', + port: 443, + uuid: UUID, + tls: true, + sni: 'sni.example.com', + network: 'xhttp', + 'xhttp-opts': { + path: '/xhttp', + mode: 'stream-up', + headers: { + Host: 'cdn.example.com', + }, + 'sc-max-each-post-bytes': '+500000-+1000000', + 'sc-min-posts-interval-ms': '+300', + 'download-settings': { + server: 'download.example.com', + port: 8443, + tls: true, + path: '/download', + host: 'download-host.example.com', + 'sc-max-each-post-bytes': '+0-+1000000', + 'sc-min-posts-interval-ms': '+0-+300', + }, + }, + }); + + expect(output).to.equal( + `vless://${UUID}@vless-xhttp.example.com:443?security=tls&type=xhttp&path=%2Fxhttp&host=cdn.example.com&sni=sni.example.com&mode=stream-up&extra=${encodeURIComponent( + extra, + )}#URI%20XHTTP%20Explicit%20Plus%20Scalars`, + ); + + const reparsed = ProxyUtils.parse(output); + expect(reparsed, output).to.have.length(1); + expect( + reparsed[0]['xhttp-opts']?.['sc-max-each-post-bytes'], + ).to.equal(1000000); + expect(reparsed[0]['xhttp-opts']?.['sc-min-posts-interval-ms']).to.equal( + 300, + ); + expect( + reparsed[0]['xhttp-opts']?.['download-settings']?.[ + 'sc-max-each-post-bytes' + ], + ).to.equal(1000000); + expect( + reparsed[0]['xhttp-opts']?.['download-settings']?.[ + 'sc-min-posts-interval-ms' + ], + ).to.equal(300); + }); + it('produces URI VLESS xhttp links from structured download settings', function () { const extra = JSON.stringify({ noGRPCHeader: true, @@ -917,6 +1172,8 @@ describe('Proxy text producers', function () { host: 'download-host.example.com', noGRPCHeader: true, xPaddingBytes: '32-64', + scMaxEachPostBytes: 1000000, + scMinPostsIntervalMs: 300, extra: { xmux: { maxConcurrency: '16-32', @@ -956,6 +1213,8 @@ describe('Proxy text producers', function () { host: 'download-host.example.com', 'no-grpc-header': true, 'x-padding-bytes': '32-64', + 'sc-max-each-post-bytes': 1000000, + 'sc-min-posts-interval-ms': 300, 'reuse-settings': { 'max-concurrency': '16-32', }, @@ -968,6 +1227,275 @@ describe('Proxy text producers', function () { extra, )}#URI%20XHTTP%20Download`, ); + + const reparsed = ProxyUtils.parse(output); + expect(reparsed, output).to.have.length(1); + expect( + reparsed[0]['xhttp-opts']?.['download-settings']?.[ + 'sc-max-each-post-bytes' + ], + ).to.equal(1000000); + expect( + reparsed[0]['xhttp-opts']?.['download-settings']?.[ + 'sc-min-posts-interval-ms' + ], + ).to.equal(300); + }); + + it('normalizes structured xhttp range-form scalars when producing URI VLESS links', function () { + const extra = JSON.stringify({ + noGRPCHeader: true, + xPaddingBytes: '64-128', + scMaxEachPostBytes: 1000000, + scMinPostsIntervalMs: 300, + xmux: { + maxConnections: '8', + }, + downloadSettings: { + address: 'download.example.com', + port: 8443, + security: 'tls', + xhttpSettings: { + path: '/download', + host: 'download-host.example.com', + scMaxEachPostBytes: 1000000, + scMinPostsIntervalMs: 300, + extra: { + xmux: { + maxConcurrency: '16-32', + }, + }, + }, + }, + }); + const output = produceExternal('URI', { + type: 'vless', + name: 'URI XHTTP Range Scalars', + server: 'vless-xhttp.example.com', + port: 443, + uuid: UUID, + tls: true, + sni: 'sni.example.com', + network: 'xhttp', + 'xhttp-opts': { + path: '/xhttp', + mode: 'stream-up', + headers: { + Host: 'cdn.example.com', + }, + 'no-grpc-header': true, + 'x-padding-bytes': '64-128', + 'sc-max-each-post-bytes': '0-1000000', + 'sc-min-posts-interval-ms': '0-300', + 'reuse-settings': { + 'max-connections': '8', + }, + 'download-settings': { + server: 'download.example.com', + port: 8443, + tls: true, + path: '/download', + host: 'download-host.example.com', + 'sc-max-each-post-bytes': '500000 - 1000000', + 'sc-min-posts-interval-ms': '0 - 300', + 'reuse-settings': { + 'max-concurrency': '16-32', + }, + }, + }, + }); + + expect(output).to.equal( + `vless://${UUID}@vless-xhttp.example.com:443?security=tls&type=xhttp&path=%2Fxhttp&host=cdn.example.com&sni=sni.example.com&mode=stream-up&extra=${encodeURIComponent( + extra, + )}#URI%20XHTTP%20Range%20Scalars`, + ); + + const reparsed = ProxyUtils.parse(output); + expect(reparsed, output).to.have.length(1); + expect( + reparsed[0]['xhttp-opts']?.['sc-max-each-post-bytes'], + ).to.equal(1000000); + expect(reparsed[0]['xhttp-opts']?.['sc-min-posts-interval-ms']).to.equal( + 300, + ); + expect( + reparsed[0]['xhttp-opts']?.['download-settings']?.[ + 'sc-max-each-post-bytes' + ], + ).to.equal(1000000); + expect( + reparsed[0]['xhttp-opts']?.['download-settings']?.[ + 'sc-min-posts-interval-ms' + ], + ).to.equal(300); + }); + + it('drops invalid structured xhttp scalars when producing URI VLESS links', function () { + const extra = JSON.stringify({ + noGRPCHeader: true, + xPaddingBytes: '64-128', + xmux: { + maxConnections: '8', + }, + downloadSettings: { + address: 'download.example.com', + port: 8443, + security: 'tls', + xhttpSettings: { + path: '/download', + host: 'download-host.example.com', + noGRPCHeader: true, + xPaddingBytes: '32-64', + extra: { + xmux: { + maxConcurrency: '16-32', + }, + }, + }, + }, + }); + const output = produceExternal('URI', { + type: 'vless', + name: 'URI XHTTP Invalid Scalars', + server: 'vless-xhttp.example.com', + port: 443, + uuid: UUID, + tls: true, + sni: 'sni.example.com', + network: 'xhttp', + 'xhttp-opts': { + path: '/xhttp', + mode: 'stream-up', + headers: { + Host: 'cdn.example.com', + }, + 'no-grpc-header': true, + 'x-padding-bytes': '64-128', + 'sc-max-each-post-bytes': '9007199254740993', + 'sc-min-posts-interval-ms': 0, + 'reuse-settings': { + 'max-connections': '8', + }, + 'download-settings': { + server: 'download.example.com', + port: 8443, + tls: true, + path: '/download', + host: 'download-host.example.com', + 'no-grpc-header': true, + 'x-padding-bytes': '32-64', + 'sc-max-each-post-bytes': '1-9007199254740993', + 'sc-min-posts-interval-ms': 'fast', + 'reuse-settings': { + 'max-concurrency': '16-32', + }, + }, + }, + }); + + expect(output).to.equal( + `vless://${UUID}@vless-xhttp.example.com:443?security=tls&type=xhttp&path=%2Fxhttp&host=cdn.example.com&sni=sni.example.com&mode=stream-up&extra=${encodeURIComponent( + extra, + )}#URI%20XHTTP%20Invalid%20Scalars`, + ); + + const reparsed = ProxyUtils.parse(output); + expect(reparsed, output).to.have.length(1); + expect(reparsed[0]['xhttp-opts']).to.not.have.property( + 'sc-max-each-post-bytes', + ); + expect(reparsed[0]['xhttp-opts']).to.not.have.property( + 'sc-min-posts-interval-ms', + ); + expect( + reparsed[0]['xhttp-opts']?.['download-settings'], + ).to.not.have.property('sc-max-each-post-bytes'); + expect( + reparsed[0]['xhttp-opts']?.['download-settings'], + ).to.not.have.property('sc-min-posts-interval-ms'); + }); + + it('omits top-level URI xhttp extra when only invalid structured scalars remain', function () { + const output = produceExternal('URI', { + type: 'vless', + name: 'URI XHTTP Invalid Scalars Only', + server: 'vless-xhttp.example.com', + port: 443, + uuid: UUID, + tls: true, + sni: 'sni.example.com', + network: 'xhttp', + 'xhttp-opts': { + path: '/xhttp', + mode: 'stream-up', + headers: { + Host: 'cdn.example.com', + }, + 'sc-max-each-post-bytes': '9007199254740993', + 'sc-min-posts-interval-ms': 0, + }, + }); + + expect(output).to.equal( + `vless://${UUID}@vless-xhttp.example.com:443?security=tls&type=xhttp&path=%2Fxhttp&host=cdn.example.com&sni=sni.example.com&mode=stream-up#URI%20XHTTP%20Invalid%20Scalars%20Only`, + ); + expect(output).to.not.include('&extra='); + }); + + it('omits nested xhttp download settings when only invalid structured scalars remain', function () { + const extra = JSON.stringify({ + downloadSettings: { + address: 'download.example.com', + port: 8443, + security: 'tls', + }, + }); + const output = produceExternal('URI', { + type: 'vless', + name: 'URI XHTTP Invalid Download Scalars Only', + server: 'vless-xhttp.example.com', + port: 443, + uuid: UUID, + tls: true, + sni: 'sni.example.com', + network: 'xhttp', + 'xhttp-opts': { + path: '/xhttp', + mode: 'stream-up', + headers: { + Host: 'cdn.example.com', + }, + 'download-settings': { + server: 'download.example.com', + port: 8443, + tls: true, + 'sc-max-each-post-bytes': '1-9007199254740993', + 'sc-min-posts-interval-ms': 'fast', + }, + }, + }); + + expect(output).to.equal( + `vless://${UUID}@vless-xhttp.example.com:443?security=tls&type=xhttp&path=%2Fxhttp&host=cdn.example.com&sni=sni.example.com&mode=stream-up&extra=${encodeURIComponent( + extra, + )}#URI%20XHTTP%20Invalid%20Download%20Scalars%20Only`, + ); + + const reparsed = ProxyUtils.parse(output); + expect(reparsed, output).to.have.length(1); + expect(reparsed[0]['xhttp-opts']?.['download-settings']).to.not.have.property( + 'sc-max-each-post-bytes', + ); + 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', + ); }); it('produces URI Trojan websocket links with pcs from tls fingerprint', function () {