From d412db1c2c632d69ece2bb4f4dc8767936f36a62 Mon Sep 17 00:00:00 2001 From: xream Date: Wed, 8 Apr 2026 14:34:13 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E8=BD=AC=E6=8D=A2=20scMaxEachPostBytes?= =?UTF-8?q?=20=E4=B8=BA=20mihomo=20=E5=85=BC=E5=AE=B9?= 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 | 28 +++- .../proxy-parsers/v2ray-and-platforms.spec.js | 144 ++++++++++++++++++ 3 files changed, 169 insertions(+), 5 deletions(-) diff --git a/backend/package.json b/backend/package.json index 578d59b1..d6fcba00 100644 --- a/backend/package.json +++ b/backend/package.json @@ -1,6 +1,6 @@ { "name": "sub-store", - "version": "2.21.81", + "version": "2.21.82", "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 862978b1..7456dbb3 100644 --- a/backend/src/core/proxy-utils/parsers/index.js +++ b/backend/src/core/proxy-utils/parsers/index.js @@ -873,12 +873,32 @@ function URI_VLESS() { if (isNotBlank(extra?.['xPaddingBytes'])) { xhttpOpts['x-padding-bytes'] = extra['xPaddingBytes']; } + const scMaxEachPostBytes = + extra?.['scMaxEachPostBytes']; if ( - typeof extra?.['scMaxEachPostBytes'] === 'number' && - Number.isFinite(extra['scMaxEachPostBytes']) + typeof scMaxEachPostBytes === 'string' || + typeof scMaxEachPostBytes === 'number' ) { - xhttpOpts['sc-max-each-post-bytes'] = - extra['scMaxEachPostBytes']; + 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; + } + } + } } if (isPlainObject(extra?.xmux)) { const reuseSettings = {}; 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 a633875a..a790fc4b 100644 --- a/backend/src/test/proxy-parsers/v2ray-and-platforms.spec.js +++ b/backend/src/test/proxy-parsers/v2ray-and-platforms.spec.js @@ -1,4 +1,5 @@ import { Base64 } from 'js-base64'; +import { expect } from 'chai'; import { describe, it } from 'mocha'; import { UUID, expectSubset, parseOne } from './helpers'; @@ -418,6 +419,149 @@ describe('VMess and VLESS parser coverage', function () { }); }); + it('parses xhttp VLESS shares with range-form scMaxEachPostBytes', function () { + const extra = JSON.stringify({ + noGRPCHeader: true, + xPaddingBytes: '64-128', + scMaxEachPostBytes: '500000 - 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%20Range`, + ); + + expectSubset(proxy, { + type: 'vless', + name: 'VLESS XHTTP 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-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('parses xhttp VLESS shares with string-form scMaxEachPostBytes', function () { + const extra = JSON.stringify({ + noGRPCHeader: true, + xPaddingBytes: '64-128', + scMaxEachPostBytes: '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%20String`, + ); + + expectSubset(proxy, { + type: 'vless', + name: 'VLESS XHTTP 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-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']; + + for (const value of invalidValues) { + const extra = JSON.stringify({ + noGRPCHeader: true, + xPaddingBytes: '64-128', + scMaxEachPostBytes: 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%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-max-each-post-bytes', + ); + } + }); + it('parses Shadowrocket VLESS shares', function () { const base = Base64.encode( `none:${UUID}@shadowrocket-vless.example.com:443`,