feat: 升级 VLESS xHTTP 的解析/导出兼容性,支持 session-id 表达式字段并强化范围校验

- 新增 strict-positive range 归一化逻辑,xPaddingBytes、sc-max-each-post-bytes、session-length 改为严格正数/范围处理,边界行为更准确
- parser 与 producer 同步支持 sessionIDTable/sessionIDLength,兼容 xhttp 中 session-table/session-length 的双向映射
- 将会话位置/密钥字段名统一为 sessionIDPlacement/sessionIDKey,避免与旧字段冲突
- 保留 xhttp headers 中的空字符串值,修复 URI VLESS round-trip 时空 header 被丢弃的问题
- 增强不合法值降级到 `_extra_unsupported` 的处理,并补充 parser/producers 结构化与文本导出测试(含空表名、范围合法性、非法值过滤)
This commit is contained in:
xream committed 2026-06-10 10:47:30 +08:00
1 parent 3c9a33d2e1
commit 6db17e7f2f
7 files changed
+607 -117

No files matched your search

+78 -10
View File
@@ -22,7 +22,8 @@ import {
normalizeXhttpIntegerValue,
normalizeXhttpNonNegativeRange,
normalizeXhttpPositiveRange,
normalizeXhttpScalarUpperBound,
normalizeXhttpStrictPositiveRangeString,
normalizeXhttpStrictPositiveRangeValue,
} from '../xhttp-utils';
import { extractPathQueryParam, getPathQueryParam } from '../transport-path';
import {
@@ -952,7 +953,7 @@ function URI_VLESS() {
const parsedHeaders = {};
for (const [key, value] of Object.entries(headers)) {
if (typeof value === 'string' && value !== '') {
if (typeof value === 'string') {
parsedHeaders[key] = value;
}
}
@@ -1023,7 +1024,7 @@ function URI_VLESS() {
const unsupportedHeaders = {};
for (const [key, value] of Object.entries(headers)) {
if (typeof value === 'string' && value !== '') {
if (typeof value === 'string') {
continue;
}
@@ -1105,18 +1106,47 @@ function URI_VLESS() {
}
break;
case 'xPaddingBytes':
if (
normalizeXhttpStrictPositiveRangeString(value) ==
null
) {
setUnsupportedXhttpField(
unsupportedExtra,
key,
value,
);
}
break;
case 'xPaddingKey':
case 'xPaddingHeader':
case 'xPaddingPlacement':
case 'xPaddingMethod':
case 'uplinkHTTPMethod':
case 'sessionIDPlacement':
case 'sessionPlacement':
case 'sessionIDKey':
case 'sessionKey':
case 'seqPlacement':
case 'seqKey':
case 'uplinkDataPlacement':
case 'uplinkDataKey':
if (!isNotBlank(value)) {
if (typeof value !== 'string') {
setUnsupportedXhttpField(
unsupportedExtra,
key,
value,
);
}
break;
case 'sessionIDTable':
// NOTE: Xray-core and mihomo both validate this field
// together with sessionIDLength when the table is
// non-empty: the table must stay ASCII, the length
// range must stay > 0, and the combined ID space must
// remain large enough. We only do local shape checks
// here for now, so type-valid values are not
// automatically upstream-valid yet.
if (typeof value !== 'string') {
setUnsupportedXhttpField(
unsupportedExtra,
key,
@@ -1134,7 +1164,10 @@ function URI_VLESS() {
}
break;
case 'scMaxEachPostBytes':
if (normalizeXhttpScalarUpperBound(value) == null) {
if (
normalizeXhttpStrictPositiveRangeString(value) ==
null
) {
setUnsupportedXhttpField(
unsupportedExtra,
key,
@@ -1151,6 +1184,23 @@ function URI_VLESS() {
);
}
break;
case 'sessionIDLength':
// NOTE: Xray-core and mihomo treat this as a coupled
// session-table/session-length constraint rather than a
// standalone positive range. Keeping only the local
// range normalization here does not guarantee the pair
// will pass upstream validation.
if (
normalizeXhttpStrictPositiveRangeString(value) ==
null
) {
setUnsupportedXhttpField(
unsupportedExtra,
key,
value,
);
}
break;
case 'xmux': {
const unsupportedXmux = collectUnsupportedXmux(value);
if (unsupportedXmux !== undefined) {
@@ -1543,8 +1593,11 @@ function URI_VLESS() {
if (extra.noGRPCHeader === true) {
target['no-grpc-header'] = true;
}
if (isNotBlank(extra.xPaddingBytes)) {
target['x-padding-bytes'] = extra.xPaddingBytes;
const xPaddingBytes = normalizeXhttpStrictPositiveRangeString(
extra.xPaddingBytes,
);
if (xPaddingBytes != null) {
target['x-padding-bytes'] = xPaddingBytes;
}
if (extra.xPaddingObfsMode === true) {
target['x-padding-obfs-mode'] = true;
@@ -1564,12 +1617,27 @@ function URI_VLESS() {
if (isNotBlank(extra.uplinkHTTPMethod)) {
target['uplink-http-method'] = extra.uplinkHTTPMethod;
}
if (isNotBlank(extra.sessionPlacement)) {
if (isNotBlank(extra.sessionIDPlacement)) {
target['session-placement'] = extra.sessionIDPlacement;
} else if (isNotBlank(extra.sessionPlacement)) {
target['session-placement'] = extra.sessionPlacement;
}
if (isNotBlank(extra.sessionKey)) {
if (isNotBlank(extra.sessionIDKey)) {
target['session-key'] = extra.sessionIDKey;
} else if (isNotBlank(extra.sessionKey)) {
target['session-key'] = extra.sessionKey;
}
if (typeof extra.sessionIDTable === 'string') {
target['session-table'] = extra.sessionIDTable;
}
const sessionIDLength = normalizeXhttpStrictPositiveRangeString(
extra.sessionIDLength,
);
if (sessionIDLength != null) {
target['session-length'] = sessionIDLength;
}
if (isNotBlank(extra.seqPlacement)) {
target['seq-placement'] = extra.seqPlacement;
}
@@ -1590,7 +1658,7 @@ function URI_VLESS() {
target['uplink-chunk-size'] = uplinkChunkSize;
}
const scMaxEachPostBytes = normalizeXhttpScalarUpperBound(
const scMaxEachPostBytes = normalizeXhttpStrictPositiveRangeValue(
extra.scMaxEachPostBytes,
);
if (scMaxEachPostBytes != null) {
+24 -8
View File
@@ -7,7 +7,7 @@ import {
normalizeXhttpIntegerValue,
normalizeXhttpNonNegativeRange,
normalizeXhttpPositiveRange,
normalizeXhttpScalarUpperBound,
normalizeXhttpStrictPositiveRangeValue,
} from '../xhttp-utils';
import {
extractPathQueryParam,
@@ -27,7 +27,7 @@ function toStringHeaderMap(headers, { excludeHost = false } = {}) {
const parsedHeaders = {};
for (const [key, value] of Object.entries(headers)) {
if (typeof value !== 'string' || value === '') {
if (typeof value !== 'string') {
continue;
}
if (excludeHost && /^host$/i.test(key)) {
@@ -209,10 +209,28 @@ function applyStructuredXhttpExtraFields(
target.uplinkHTTPMethod = xhttpOpts['uplink-http-method'];
}
if (xhttpOpts['session-placement']) {
target.sessionPlacement = xhttpOpts['session-placement'];
target.sessionIDPlacement = xhttpOpts['session-placement'];
}
if (xhttpOpts['session-key']) {
target.sessionKey = xhttpOpts['session-key'];
target.sessionIDKey = xhttpOpts['session-key'];
}
if (typeof xhttpOpts['session-table'] === 'string') {
// NOTE: This mirrors the current structured field mapping only.
// Xray-core/mihomo still apply coupled validation with
// session-length when the table is non-empty: ASCII-only table,
// strictly positive length range, and enough total ID space.
target.sessionIDTable = xhttpOpts['session-table'];
}
if (xhttpOpts['session-length'] != null) {
// NOTE: The normalized range here is only a local serialization check.
// Upstream compatibility still depends on the session-table/session-
// length pair satisfying the extra Xray-core/mihomo constraints.
const sessionIDLength = normalizeXhttpStrictPositiveRangeValue(
xhttpOpts['session-length'],
);
if (sessionIDLength != null) {
target.sessionIDLength = sessionIDLength;
}
}
if (xhttpOpts['seq-placement']) {
target.seqPlacement = xhttpOpts['seq-placement'];
@@ -235,7 +253,7 @@ function applyStructuredXhttpExtraFields(
}
if (xhttpOpts['sc-max-each-post-bytes'] != null) {
const scMaxEachPostBytes = normalizeXhttpScalarUpperBound(
const scMaxEachPostBytes = normalizeXhttpStrictPositiveRangeValue(
xhttpOpts['sc-max-each-post-bytes'],
);
if (scMaxEachPostBytes != null) {
@@ -526,9 +544,7 @@ function buildVlessExtra(proxy) {
// structured Mihomo node so later edits are reflected on export, while
// `_extra_unsupported` fills the holes needed for VLESS URI -> node ->
// VLESS URI lossless round-trips. That also means supported-field format
// conflicts are resolved by the structured emitters here, e.g.
// sc-max-each-post-bytes still emits the compatibility upper bound while
// sc-min-posts-interval-ms keeps range.
// conflicts are resolved by the structured emitters here.
const mergedExtra = mergeUnsupportedXhttpExtraObject(
structuredExtra,
proxy._extra_unsupported,
+35 -19
View File
@@ -1,6 +1,6 @@
function parseNormalizedXhttpRangeBounds(
value,
{ allowZeroUpperBound = true } = {},
{ allowZeroLowerBound = true, allowZeroUpperBound = true } = {},
) {
if (typeof value !== 'string' && typeof value !== 'number') {
return undefined;
@@ -22,9 +22,14 @@ function parseNormalizedXhttpRangeBounds(
const normalizedValue = `${value}`.trim();
const rangeParts = normalizedValue.split('-');
const minimumAllowedLowerBound = allowZeroLowerBound ? 0 : 1;
const minimumAllowedUpperBound = allowZeroUpperBound ? 0 : 1;
if (rangeParts.length === 1) {
const normalizedInteger = parseUnsignedIntegerToken(rangeParts[0]);
const minimumAllowedValue = allowZeroUpperBound ? 0 : 1;
const minimumAllowedValue = Math.max(
minimumAllowedLowerBound,
minimumAllowedUpperBound,
);
return normalizedInteger >= minimumAllowedValue
? {
lowerBound: normalizedInteger,
@@ -43,8 +48,9 @@ function parseNormalizedXhttpRangeBounds(
return undefined;
}
const minimumAllowedUpperBound = allowZeroUpperBound ? 0 : 1;
return upperBound >= minimumAllowedUpperBound && upperBound >= lowerBound
return lowerBound >= minimumAllowedLowerBound &&
upperBound >= minimumAllowedUpperBound &&
upperBound >= lowerBound
? {
lowerBound,
upperBound,
@@ -58,24 +64,14 @@ function parseNormalizedXhttpPositiveRangeBounds(value) {
});
}
export function normalizeXhttpScalarUpperBound(value) {
// IMPORTANT: the legacy-client compatibility reason for collapsing ranges
// to an upper bound specifically applies to sc-max-each-post-bytes.
// Mihomo first shipped sc-max-each-post-bytes as an int-only field, then
// only later added true range support, so emitting `lower-upper` here can
// still break older clients that predate that change. Once the new
// official Mihomo stable release with ranged sc-max-each-post-bytes is
// broadly deployed, switch that field back to preserving full ranges and
// remove the upper-bound compatibility logic tied to it.
const normalizedBounds = parseNormalizedXhttpPositiveRangeBounds(value);
return normalizedBounds?.upperBound;
function parseNormalizedXhttpStrictPositiveRangeBounds(value) {
return parseNormalizedXhttpRangeBounds(value, {
allowZeroLowerBound: false,
allowZeroUpperBound: false,
});
}
export function normalizeXhttpPositiveRange(value) {
// IMPORTANT: unlike sc-max-each-post-bytes, sc-min-posts-interval-ms does
// not need an old-client compatibility shim. Mihomo introduced
// sc-min-posts-interval-ms with range semantics from day one, so we should
// keep exporting its full normalized range form instead of collapsing it.
const normalizedBounds = parseNormalizedXhttpPositiveRangeBounds(value);
if (!normalizedBounds) {
return undefined;
@@ -85,6 +81,26 @@ export function normalizeXhttpPositiveRange(value) {
return lowerBound === upperBound ? upperBound : `${lowerBound}-${upperBound}`;
}
export function normalizeXhttpStrictPositiveRangeString(value) {
const normalizedBounds = parseNormalizedXhttpStrictPositiveRangeBounds(value);
if (!normalizedBounds) {
return undefined;
}
const { lowerBound, upperBound } = normalizedBounds;
return lowerBound === upperBound ? `${upperBound}` : `${lowerBound}-${upperBound}`;
}
export function normalizeXhttpStrictPositiveRangeValue(value) {
const normalizedBounds = parseNormalizedXhttpStrictPositiveRangeBounds(value);
if (!normalizedBounds) {
return undefined;
}
const { lowerBound, upperBound } = normalizedBounds;
return lowerBound === upperBound ? upperBound : `${lowerBound}-${upperBound}`;
}
export function normalizeXhttpNonNegativeRange(value) {
const normalizedBounds = parseNormalizedXhttpRangeBounds(value);
if (!normalizedBounds) {