feat: 支持 sc-min-posts-interval-ms; 优化 VLESS URI 转换逻辑

This commit is contained in:
xream committed 2026-04-14 21:38:40 +08:00
1 parent 48cdcad8ee
commit 7e70ffd494
7 files changed
+1263 -33

No files matched your search

+1 -1
View File
@@ -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": {
+30 -25
View File
@@ -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);
+143 -2
View File
@@ -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);
@@ -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;
}
@@ -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({
@@ -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',
},
},
},
});
});
@@ -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 () {