fix: 统一 WebSocket ed 参数在解析与导出的处理逻辑

- 新增 transport-path 工具文件,集中处理 ws path 的 `ed` 查询参数提取/设置与安全整数校验
- SS/VMess/VLESS/Trojan 解析器改为从 path 查询中提取早期数据参数,并区分 httpupgrade 与普通 ws 的转换规则
- 解决 path 中 `ed` 可能被重复解码或保留在路径里的问题
- httpupgrade 场景改为用 `_v2ray-http-upgrade-ed` 记录 early data 元数据,普通 ws 用 `max-early-data` + `Sec-WebSocket-Protocol`
- Clash / ClashMeta / Shadowrocket / Stash / Sing-box 生成器统一复用标准化逻辑,补齐默认 ws path 并清理临时元数据
- 更新并扩展 parser 与 producer 测试:覆盖 SS、VMess、VLESS、Trojan 的 path early data、重复值优先级、非法值和默认值行为
This commit is contained in:
xream committed 2026-04-30 15:08:32 +08:00
1 parent 12fec7c8a3
commit 60ea29fccf
14 files changed
+1625 -159

No files matched your search

+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "sub-store",
"version": "2.22.13",
"version": "2.22.14",
"description": "Advanced Subscription Manager for QX, Loon, Surge, Stash and Shadowrocket.",
"main": "src/main.js",
"scripts": {
+113 -24
View File
@@ -24,6 +24,7 @@ import {
normalizeXhttpPositiveRange,
normalizeXhttpScalarUpperBound,
} from '../xhttp-utils';
import { extractPathQueryParam, getPathQueryParam } from '../transport-path';
function surge_port_hopping(raw) {
const [parts, port_hopping] =
@@ -63,6 +64,39 @@ function decodeShadowsocksUserInfo(rawUserInfoStr) {
return Base64.decode(decodedUserInfoStr);
}
function isNumericEarlyData(value) {
if (value == null || !/^\d+$/.test(`${value}`)) return false;
return Number.isSafeInteger(parseInt(`${value}`, 10));
}
function extractEarlyDataFromPath(path) {
const ed = getPathQueryParam(path, 'ed');
if (!isNumericEarlyData(ed)) {
return {
path,
ed: '',
};
}
return {
path: extractPathQueryParam(path, 'ed').path,
ed,
};
}
function parseEarlyDataSize(value) {
const raw = `${value}`;
if (!/^\d+$/.test(raw)) {
throw new Error(`bad WebSocket max early data size: ${value}`);
}
const parsed = parseInt(raw, 10);
if (!Number.isSafeInteger(parsed)) {
throw new Error(`bad WebSocket max early data size: ${value}`);
}
return parsed;
}
function parseWireGuardURIAddressValue(value) {
if (value == null) return null;
const raw = `${value}`.trim();
@@ -253,6 +287,8 @@ function URI_SS() {
if (params['type']) {
let httpupgrade;
let httpUpgradeEd = '';
let pathEarlyData = '';
proxy.network = params['type'];
if (proxy.network === 'httpupgrade') {
proxy.network = 'ws';
@@ -266,11 +302,18 @@ function URI_SS() {
};
} else {
if (params['path']) {
_.set(
proxy,
proxy.network + '-opts.path',
decodeURIComponent(params['path']),
);
let transportPath = params['path'];
if (proxy.network === 'ws') {
const extracted =
extractEarlyDataFromPath(transportPath);
transportPath = extracted.path;
if (httpupgrade) {
httpUpgradeEd = extracted.ed;
} else {
pathEarlyData = extracted.ed;
}
}
_.set(proxy, proxy.network + '-opts.path', transportPath);
}
if (params['host']) {
_.set(
@@ -280,15 +323,37 @@ function URI_SS() {
);
}
if (httpupgrade) {
httpUpgradeEd =
httpUpgradeEd ||
(isNumericEarlyData(params.ed) ? `${params.ed}` : '');
_.set(
proxy,
proxy.network + '-opts.v2ray-http-upgrade',
true,
);
if (httpUpgradeEd !== '') {
_.set(
proxy,
proxy.network +
'-opts.v2ray-http-upgrade-fast-open',
true,
);
_.set(
proxy,
proxy.network + '-opts._v2ray-http-upgrade-ed',
httpUpgradeEd,
);
}
} else if (proxy.network === 'ws' && pathEarlyData !== '') {
_.set(
proxy,
proxy.network + '-opts.v2ray-http-upgrade-fast-open',
true,
proxy.network + '-opts.max-early-data',
parseEarlyDataSize(pathEarlyData),
);
_.set(
proxy,
proxy.network + '-opts.early-data-header-name',
'Sec-WebSocket-Protocol',
);
}
}
@@ -673,6 +738,17 @@ function URI_VMess() {
// eslint-disable-next-line no-empty
} catch (e) {}
let transportPath = params.path;
let httpUpgradeEd = '';
let pathEarlyData = '';
if (proxy.network === 'ws' && transportPath) {
const extracted = extractEarlyDataFromPath(transportPath);
transportPath = extracted.path;
if (httpupgrade) {
httpUpgradeEd = extracted.ed;
} else {
pathEarlyData = extracted.ed;
}
}
// 补上默认 path
if (['ws'].includes(proxy.network)) {
@@ -727,7 +803,23 @@ function URI_VMess() {
};
if (httpupgrade) {
opts['v2ray-http-upgrade'] = true;
opts['v2ray-http-upgrade-fast-open'] = true;
httpUpgradeEd =
httpUpgradeEd ||
(isNumericEarlyData(params.ed)
? `${params.ed}`
: '');
if (httpUpgradeEd !== '') {
opts['v2ray-http-upgrade-fast-open'] = true;
opts['_v2ray-http-upgrade-ed'] = httpUpgradeEd;
}
} else if (
proxy.network === 'ws' &&
pathEarlyData !== ''
) {
opts['max-early-data'] =
parseEarlyDataSize(pathEarlyData);
opts['early-data-header-name'] =
'Sec-WebSocket-Protocol';
}
proxy[`${proxy.network}-opts`] = opts;
}
@@ -1671,6 +1763,7 @@ function URI_VLESS() {
if (proxy.network && !['tcp', 'none'].includes(proxy.network)) {
const opts = {};
let pathEarlyData = '';
const host = params.host ?? params.obfsParam;
if (host) {
if (params.obfsParam) {
@@ -1703,7 +1796,13 @@ function URI_VLESS() {
}
}
if (params.path) {
opts.path = params.path;
let transportPath = params.path;
if (proxy.network === 'ws') {
const extracted = extractEarlyDataFromPath(transportPath);
transportPath = extracted.path;
pathEarlyData = extracted.ed;
}
opts.path = transportPath;
}
if (proxy.network === 'http' && params.method) {
opts.method = params.method;
@@ -1714,23 +1813,13 @@ function URI_VLESS() {
}
if (httpupgrade) {
opts['v2ray-http-upgrade'] = true;
opts['v2ray-http-upgrade-fast-open'] = true;
}
if (params.ed) {
const maxEarlyDataRaw = `${params.ed}`;
if (!/^\d+$/.test(maxEarlyDataRaw)) {
throw new Error(
`bad WebSocket max early data size: ${params.ed}`,
);
}
const maxEarlyData = parseInt(maxEarlyDataRaw, 10);
if (!Number.isSafeInteger(maxEarlyData)) {
throw new Error(
`bad WebSocket max early data size: ${params.ed}`,
);
}
const earlyDataRaw = pathEarlyData || params.ed;
if (earlyDataRaw) {
const maxEarlyData = parseEarlyDataSize(earlyDataRaw);
if (httpupgrade) {
opts['max-early-data'] = maxEarlyData;
opts['v2ray-http-upgrade-fast-open'] = true;
opts['_v2ray-http-upgrade-ed'] = `${earlyDataRaw}`;
} else if (proxy.network === 'ws') {
opts['max-early-data'] = maxEarlyData;
opts['early-data-header-name'] =
@@ -17,6 +17,91 @@ const grammars = String.raw`
if (typeof str === 'undefined' || str === null) return undefined;
return /(TRUE)|1/i.test(str);
}
function decodeQueryComponent(value) {
try {
return decodeURIComponent(String(value).replace(/\+/g, '%20'));
} catch (e) {
return value;
}
}
function splitQueryPart(part) {
const separatorIndex = part.indexOf('=');
if (separatorIndex === -1) {
return {
key: decodeQueryComponent(part),
value: '',
};
}
return {
key: decodeQueryComponent(part.slice(0, separatorIndex)),
value: decodeQueryComponent(part.slice(separatorIndex + 1)),
};
}
function getPathQueryParam(path, paramName) {
const queryIndex = path.indexOf('?');
if (queryIndex === -1) return '';
const query = path.slice(queryIndex + 1);
for (const part of query.split('&')) {
if (part === '') continue;
const parsed = splitQueryPart(part);
if (parsed.key === paramName && parsed.value !== '') {
return parsed.value;
}
}
return '';
}
function extractPathQueryParam(path, paramName) {
const queryIndex = path.indexOf('?');
if (queryIndex === -1) {
return {
path,
value: '',
};
}
const basePath = path.slice(0, queryIndex);
const query = path.slice(queryIndex + 1);
const keptParts = [];
let value = '';
for (const part of query.split('&')) {
if (part === '') continue;
const parsed = splitQueryPart(part);
if (parsed.key === paramName) {
if (value === '' && parsed.value !== '') {
value = parsed.value;
}
continue;
}
keptParts.push(part);
}
return {
path: keptParts.length > 0 ? basePath + '?' + keptParts.join('&') : basePath,
value,
};
}
function parseEarlyDataSize(value) {
if (value == null || !/^\d+$/.test(String(value))) return null;
const parsed = parseInt(String(value), 10);
return Number.isSafeInteger(parsed) ? parsed : null;
}
function isNumericEarlyData(value) {
return parseEarlyDataSize(value) != null;
}
}}
{
@@ -96,6 +181,8 @@ params = "?" head:param tail:("&"@param)* {
if (params["type"]) {
let httpupgrade
let httpUpgradeEd = ''
let pathEarlyData = ''
proxy.network = params["type"]
if(proxy.network === 'httpupgrade') {
proxy.network = 'ws'
@@ -109,14 +196,33 @@ params = "?" head:param tail:("&"@param)* {
};
} else {
if (params["path"]) {
$set(proxy, proxy.network+"-opts.path", decodeURIComponent(params["path"]));
let transportPath = params["path"]
if (proxy.network === 'ws') {
const pathEd = getPathQueryParam(transportPath, 'ed')
if (isNumericEarlyData(pathEd)) {
transportPath = extractPathQueryParam(transportPath, 'ed').path
if (httpupgrade) {
httpUpgradeEd = pathEd
} else {
pathEarlyData = pathEd
}
}
}
$set(proxy, proxy.network+"-opts.path", transportPath);
}
if (params["host"]) {
$set(proxy, proxy.network+"-opts.headers.Host", decodeURIComponent(params["host"]));
}
if (httpupgrade) {
httpUpgradeEd = httpUpgradeEd || (isNumericEarlyData(params.ed) ? String(params.ed) : '')
$set(proxy, proxy.network+"-opts.v2ray-http-upgrade", true);
$set(proxy, proxy.network+"-opts.v2ray-http-upgrade-fast-open", true);
if (httpUpgradeEd !== '') {
$set(proxy, proxy.network+"-opts.v2ray-http-upgrade-fast-open", true);
$set(proxy, proxy.network+"-opts._v2ray-http-upgrade-ed", httpUpgradeEd);
}
} else if (proxy.network === 'ws' && pathEarlyData !== '') {
$set(proxy, proxy.network+"-opts.max-early-data", parseEarlyDataSize(pathEarlyData));
$set(proxy, proxy.network+"-opts.early-data-header-name", 'Sec-WebSocket-Protocol');
}
}
if (['reality'].includes(params.security)) {
+12 -18
View File
@@ -2,6 +2,10 @@ import {
isPresent,
produceProxyListOutput,
} from '@/core/proxy-utils/producers/utils';
import {
deleteHttpUpgradeEarlyDataMetadata,
normalizeWebSocketEarlyDataPath,
} from '../transport-path';
import $ from '@/core/app';
export default function Clash_Producer() {
@@ -143,25 +147,12 @@ export default function Clash_Producer() {
}
}
if (['ws'].includes(proxy.network)) {
const networkPath = proxy[`${proxy.network}-opts`]?.path;
if (networkPath) {
const reg = /^(.*?)(?:\?ed=(\d+))?$/;
// eslint-disable-next-line no-unused-vars
const [_, path = '', ed = ''] = reg.exec(networkPath);
proxy[`${proxy.network}-opts`].path = path;
if (ed !== '') {
proxy['ws-opts']['early-data-header-name'] =
'Sec-WebSocket-Protocol';
proxy['ws-opts']['max-early-data'] = parseInt(
ed,
10,
);
}
} else {
proxy[`${proxy.network}-opts`] =
proxy[`${proxy.network}-opts`] || {};
proxy[`${proxy.network}-opts`].path = '/';
const networkOptsKey = `${proxy.network}-opts`;
proxy[networkOptsKey] = proxy[networkOptsKey] || {};
if (!proxy[networkOptsKey].path) {
proxy[networkOptsKey].path = '/';
}
normalizeWebSocketEarlyDataPath(proxy[networkOptsKey]);
}
if (proxy['plugin-opts']?.tls) {
@@ -208,6 +199,9 @@ export default function Clash_Producer() {
delete proxy[key];
}
}
deleteHttpUpgradeEarlyDataMetadata(
proxy[`${proxy.network}-opts`],
);
}
if (
['grpc'].includes(proxy.network) &&
@@ -4,6 +4,10 @@ import {
produceProxyListOutput,
supportsShadowsocksV2rayPluginMode,
} from '@/core/proxy-utils/producers/utils';
import {
deleteHttpUpgradeEarlyDataMetadata,
normalizeWebSocketEarlyDataPath,
} from '../transport-path';
import $ from '@/core/app';
const ipVersions = {
@@ -255,25 +259,12 @@ export default function ClashMeta_Producer() {
}
}
if (['ws'].includes(proxy.network)) {
const networkPath = proxy[`${proxy.network}-opts`]?.path;
if (networkPath) {
const reg = /^(.*?)(?:\?ed=(\d+))?$/;
// eslint-disable-next-line no-unused-vars
const [_, path = '', ed = ''] = reg.exec(networkPath);
proxy[`${proxy.network}-opts`].path = path;
if (ed !== '') {
proxy['ws-opts']['early-data-header-name'] =
'Sec-WebSocket-Protocol';
proxy['ws-opts']['max-early-data'] = parseInt(
ed,
10,
);
}
} else {
proxy[`${proxy.network}-opts`] =
proxy[`${proxy.network}-opts`] || {};
proxy[`${proxy.network}-opts`].path = '/';
const networkOptsKey = `${proxy.network}-opts`;
proxy[networkOptsKey] = proxy[networkOptsKey] || {};
if (!proxy[networkOptsKey].path) {
proxy[networkOptsKey].path = '/';
}
normalizeWebSocketEarlyDataPath(proxy[networkOptsKey]);
}
if (proxy['plugin-opts']?.tls) {
@@ -324,6 +315,9 @@ export default function ClashMeta_Producer() {
delete proxy[key];
}
}
deleteHttpUpgradeEarlyDataMetadata(
proxy[`${proxy.network}-opts`],
);
}
if (
['grpc'].includes(proxy.network) &&
@@ -5,6 +5,10 @@ import {
produceProxyListOutput,
supportsShadowsocksV2rayPluginMode,
} from '@/core/proxy-utils/producers/utils';
import {
deleteHttpUpgradeEarlyDataMetadata,
normalizeWebSocketEarlyDataPath,
} from '../transport-path';
import $ from '@/core/app';
export default function Shadowrocket_Producer() {
@@ -222,25 +226,12 @@ export default function Shadowrocket_Producer() {
}
}
if (['ws'].includes(proxy.network)) {
const networkPath = proxy[`${proxy.network}-opts`]?.path;
if (networkPath) {
const reg = /^(.*?)(?:\?ed=(\d+))?$/;
// eslint-disable-next-line no-unused-vars
const [_, path = '', ed = ''] = reg.exec(networkPath);
proxy[`${proxy.network}-opts`].path = path;
if (ed !== '') {
proxy['ws-opts']['early-data-header-name'] =
'Sec-WebSocket-Protocol';
proxy['ws-opts']['max-early-data'] = parseInt(
ed,
10,
);
}
} else {
proxy[`${proxy.network}-opts`] =
proxy[`${proxy.network}-opts`] || {};
proxy[`${proxy.network}-opts`].path = '/';
const networkOptsKey = `${proxy.network}-opts`;
proxy[networkOptsKey] = proxy[networkOptsKey] || {};
if (!proxy[networkOptsKey].path) {
proxy[networkOptsKey].path = '/';
}
normalizeWebSocketEarlyDataPath(proxy[networkOptsKey]);
}
if (proxy['plugin-opts']?.tls) {
@@ -291,6 +282,9 @@ export default function Shadowrocket_Producer() {
delete proxy[key];
}
}
deleteHttpUpgradeEarlyDataMetadata(
proxy[`${proxy.network}-opts`],
);
}
if (
['grpc'].includes(proxy.network) &&
@@ -2,6 +2,10 @@ import ClashMeta_Producer from './clashmeta';
import $ from '@/core/app';
import { isPlainObject } from '@/utils';
import { getWireGuardAddressWithCIDR, normalizePluginMuxValue } from './utils';
import {
extractPathQueryParam,
getSafeIntegerPathQueryParam,
} from '../transport-path';
const ipVersions = {
ipv4: 'ipv4_only',
@@ -130,13 +134,12 @@ const wsParser = (proxy, parsedProxy) => {
if (proxy['ws-path'] && proxy['ws-path'] !== '')
transport.path = `${proxy['ws-path']}`;
if (transport.path) {
const reg = /^(.*?)(?:\?ed=(\d+))?$/;
// eslint-disable-next-line no-unused-vars
const [_, path = '', ed = ''] = reg.exec(transport.path);
transport.path = path;
const { value: ed, parsed: maxEarlyData } =
getSafeIntegerPathQueryParam(transport.path, 'ed');
if (ed !== '') {
transport.path = extractPathQueryParam(transport.path, 'ed').path;
transport.early_data_header_name = 'Sec-WebSocket-Protocol';
transport.max_early_data = parseInt(ed, 10);
transport.max_early_data = maxEarlyData;
}
}
+12 -18
View File
@@ -3,6 +3,10 @@ import {
produceProxyListOutput,
supportsShadowsocksV2rayPluginMode,
} from '@/core/proxy-utils/producers/utils';
import {
deleteHttpUpgradeEarlyDataMetadata,
normalizeWebSocketEarlyDataPath,
} from '../transport-path';
import $ from '@/core/app';
export default function Stash_Producer() {
@@ -270,25 +274,12 @@ export default function Stash_Producer() {
}
}
if (['ws'].includes(proxy.network)) {
const networkPath = proxy[`${proxy.network}-opts`]?.path;
if (networkPath) {
const reg = /^(.*?)(?:\?ed=(\d+))?$/;
// eslint-disable-next-line no-unused-vars
const [_, path = '', ed = ''] = reg.exec(networkPath);
proxy[`${proxy.network}-opts`].path = path;
if (ed !== '') {
proxy['ws-opts']['early-data-header-name'] =
'Sec-WebSocket-Protocol';
proxy['ws-opts']['max-early-data'] = parseInt(
ed,
10,
);
}
} else {
proxy[`${proxy.network}-opts`] =
proxy[`${proxy.network}-opts`] || {};
proxy[`${proxy.network}-opts`].path = '/';
const networkOptsKey = `${proxy.network}-opts`;
proxy[networkOptsKey] = proxy[networkOptsKey] || {};
if (!proxy[networkOptsKey].path) {
proxy[networkOptsKey].path = '/';
}
normalizeWebSocketEarlyDataPath(proxy[networkOptsKey]);
}
if (proxy['plugin-opts']?.tls) {
@@ -348,6 +339,9 @@ export default function Stash_Producer() {
delete proxy[key];
}
}
deleteHttpUpgradeEarlyDataMetadata(
proxy[`${proxy.network}-opts`],
);
}
if (
['grpc'].includes(proxy.network) &&
+149 -39
View File
@@ -8,6 +8,11 @@ import {
normalizeXhttpPositiveRange,
normalizeXhttpScalarUpperBound,
} from '../xhttp-utils';
import {
extractPathQueryParam,
parseSafeIntegerValue,
setPathQueryParam,
} from '../transport-path';
function toStringHeaderMap(headers, { excludeHost = false } = {}) {
if (!isPlainObject(headers)) {
@@ -28,6 +33,56 @@ function toStringHeaderMap(headers, { excludeHost = false } = {}) {
return Object.keys(parsedHeaders).length > 0 ? parsedHeaders : undefined;
}
function getHttpUpgradeEarlyData(transportOpts, path) {
const httpUpgradeEd = getSafeEarlyDataValue(
transportOpts?.['_v2ray-http-upgrade-ed'],
);
if (httpUpgradeEd !== '') return httpUpgradeEd;
const pathEd = getSafeEarlyDataValue(
extractPathQueryParam(path || '/', 'ed').value,
);
return pathEd !== '' ? pathEd : 2560;
}
function setHttpUpgradeEarlyDataPath(path, transportOpts) {
if (!transportOpts?.['v2ray-http-upgrade-fast-open']) {
return path;
}
return setPathQueryParam(
path || '/',
'ed',
getHttpUpgradeEarlyData(transportOpts, path),
);
}
function setWebSocketEarlyDataPath(path, transportOpts) {
const earlyDataValue = transportOpts?.['max-early-data'];
const earlyData = getSafeEarlyDataValue(earlyDataValue);
if (earlyData === '') {
if (earlyDataValue != null && `${earlyDataValue}` !== '') {
return path == null ? path : extractPathQueryParam(path, 'ed').path;
}
return path;
}
const earlyDataHeaderName = transportOpts?.['early-data-header-name'];
if (
earlyDataHeaderName &&
earlyDataHeaderName !== 'Sec-WebSocket-Protocol'
) {
return path == null ? path : extractPathQueryParam(path, 'ed').path;
}
return setPathQueryParam(path || '/', 'ed', earlyData);
}
function getSafeEarlyDataValue(value) {
if (value == null || `${value}` === '') return '';
return parseSafeIntegerValue(value) == null ? '' : `${value}`;
}
function parseIntegerLikeValue(value) {
return normalizeXhttpIntegerValue(value);
}
@@ -550,16 +605,35 @@ function vless(proxy) {
}
const transportOpts = proxy[`${proxy.network}-opts`] || {};
const isVlessHttpUpgrade =
proxy.network === 'ws' && transportOpts?.['v2ray-http-upgrade'];
let vlessTransportServiceName =
transportOpts?.[`${proxy.network}-service-name`];
let vlessTransportPath = transportOpts?.path;
let vlessTransportHost = getTransportHost(proxy.network, transportOpts);
const vlessWsEarlyData = getSafeEarlyDataValue(
proxy['ws-opts']?.['max-early-data'],
);
if (Array.isArray(vlessTransportPath)) {
vlessTransportPath = vlessTransportPath[0];
}
if (isVlessHttpUpgrade && transportOpts?.['v2ray-http-upgrade-fast-open']) {
vlessTransportPath = setHttpUpgradeEarlyDataPath(
vlessTransportPath,
transportOpts,
);
} else if (
proxy.network === 'ws' &&
proxy['ws-opts']?.['max-early-data'] != null &&
vlessTransportPath
) {
vlessTransportPath = extractPathQueryParam(
vlessTransportPath,
'ed',
).path;
}
if (vlessTransportPath) {
vlessTransport += `&path=${encodeURIComponent(
Array.isArray(vlessTransportPath)
? vlessTransportPath[0]
: vlessTransportPath,
)}`;
vlessTransport += `&path=${encodeURIComponent(vlessTransportPath)}`;
}
if (vlessTransportHost) {
vlessTransport += `&host=${encodeURIComponent(
@@ -590,26 +664,15 @@ function vless(proxy) {
}
if (
proxy.network === 'ws' &&
!proxy['ws-opts']?.['v2ray-http-upgrade'] &&
proxy['ws-opts']?.['max-early-data'] != null
!isVlessHttpUpgrade &&
vlessWsEarlyData !== ''
) {
vlessTransport += `&ed=${encodeURIComponent(
proxy['ws-opts']['max-early-data'],
)}`;
}
if (
proxy.network === 'ws' &&
proxy['ws-opts']?.['v2ray-http-upgrade'] &&
proxy['ws-opts']?.['max-early-data'] != null
) {
vlessTransport += `&ed=${encodeURIComponent(
proxy['ws-opts']['max-early-data'],
)}`;
vlessTransport += `&ed=${encodeURIComponent(vlessWsEarlyData)}`;
}
const earlyDataHeaderName = proxy['ws-opts']?.['early-data-header-name'];
if (
earlyDataHeaderName &&
(proxy['ws-opts']?.['v2ray-http-upgrade'] ||
(isVlessHttpUpgrade ||
proxy['ws-opts']?.['max-early-data'] == null ||
earlyDataHeaderName !== 'Sec-WebSocket-Protocol')
) {
@@ -760,14 +823,30 @@ export default function URI_Producer() {
'gun',
)}`;
}
let ssTransportPath = proxy[`${proxy.network}-opts`]?.path;
let ssTransportHost =
proxy[`${proxy.network}-opts`]?.headers?.Host;
const ssTransportOpts =
proxy[`${proxy.network}-opts`] || {};
const isSsHttpUpgrade =
proxy.network === 'ws' &&
ssTransportOpts?.['v2ray-http-upgrade'];
let ssTransportPath = ssTransportOpts?.path;
let ssTransportHost = ssTransportOpts?.headers?.Host;
if (Array.isArray(ssTransportPath)) {
ssTransportPath = ssTransportPath[0];
}
if (isSsHttpUpgrade) {
ssTransportPath = setHttpUpgradeEarlyDataPath(
ssTransportPath,
ssTransportOpts,
);
} else if (proxy.network === 'ws') {
ssTransportPath = setWebSocketEarlyDataPath(
ssTransportPath,
ssTransportOpts,
);
}
if (ssTransportPath) {
ssTransport += `&path=${encodeURIComponent(
Array.isArray(ssTransportPath)
? ssTransportPath[0]
: ssTransportPath,
ssTransportPath,
)}`;
}
if (ssTransportHost) {
@@ -880,10 +959,13 @@ export default function URI_Producer() {
}
// obfs
if (proxy.network) {
let vmessTransportPath =
proxy[`${proxy.network}-opts`]?.path;
let vmessTransportHost =
proxy[`${proxy.network}-opts`]?.headers?.Host;
const vmessTransportOpts =
proxy[`${proxy.network}-opts`] || {};
const isVmessHttpUpgrade =
proxy.network === 'ws' &&
vmessTransportOpts?.['v2ray-http-upgrade'];
let vmessTransportPath = vmessTransportOpts?.path;
let vmessTransportHost = vmessTransportOpts?.headers?.Host;
if (['grpc'].includes(proxy.network)) {
result.path =
@@ -911,10 +993,22 @@ export default function URI_Producer() {
`_${proxy.network}-path`
];
} else {
if (Array.isArray(vmessTransportPath)) {
vmessTransportPath = vmessTransportPath[0];
}
if (isVmessHttpUpgrade) {
vmessTransportPath = setHttpUpgradeEarlyDataPath(
vmessTransportPath,
vmessTransportOpts,
);
} else if (proxy.network === 'ws') {
vmessTransportPath = setWebSocketEarlyDataPath(
vmessTransportPath,
vmessTransportOpts,
);
}
if (vmessTransportPath) {
result.path = Array.isArray(vmessTransportPath)
? vmessTransportPath[0]
: vmessTransportPath;
result.path = vmessTransportPath;
}
if (vmessTransportHost) {
result.host = Array.isArray(vmessTransportHost)
@@ -961,15 +1055,31 @@ export default function URI_Producer() {
'gun',
)}`;
}
let trojanTransportPath =
proxy[`${proxy.network}-opts`]?.path;
const trojanTransportOpts =
proxy[`${proxy.network}-opts`] || {};
const isTrojanHttpUpgrade =
proxy.network === 'ws' &&
trojanTransportOpts?.['v2ray-http-upgrade'];
let trojanTransportPath = trojanTransportOpts?.path;
let trojanTransportHost =
proxy[`${proxy.network}-opts`]?.headers?.Host;
trojanTransportOpts?.headers?.Host;
if (Array.isArray(trojanTransportPath)) {
trojanTransportPath = trojanTransportPath[0];
}
if (isTrojanHttpUpgrade) {
trojanTransportPath = setHttpUpgradeEarlyDataPath(
trojanTransportPath,
trojanTransportOpts,
);
} else if (proxy.network === 'ws') {
trojanTransportPath = setWebSocketEarlyDataPath(
trojanTransportPath,
trojanTransportOpts,
);
}
if (trojanTransportPath) {
trojanTransport += `&path=${encodeURIComponent(
Array.isArray(trojanTransportPath)
? trojanTransportPath[0]
: trojanTransportPath,
trojanTransportPath,
)}`;
}
if (trojanTransportHost) {
@@ -0,0 +1,164 @@
function decodeQueryComponent(value) {
try {
return decodeURIComponent(`${value}`.replace(/\+/g, '%20'));
} catch (e) {
return value;
}
}
function splitQueryPart(part) {
const separatorIndex = part.indexOf('=');
if (separatorIndex === -1) {
return {
key: decodeQueryComponent(part),
value: '',
};
}
return {
key: decodeQueryComponent(part.slice(0, separatorIndex)),
value: decodeQueryComponent(part.slice(separatorIndex + 1)),
};
}
export function parseSafeIntegerValue(value) {
if (!/^\d+$/.test(`${value}`)) return null;
const parsed = parseInt(`${value}`, 10);
return Number.isSafeInteger(parsed) ? parsed : null;
}
export function extractPathQueryParam(rawPath, paramName) {
const path = rawPath == null ? '' : `${rawPath}`;
const queryIndex = path.indexOf('?');
if (queryIndex === -1) {
return {
path,
value: '',
};
}
const basePath = path.slice(0, queryIndex);
const query = path.slice(queryIndex + 1);
const keptParts = [];
let value = '';
for (const part of query.split('&')) {
if (part === '') continue;
const parsed = splitQueryPart(part);
if (parsed.key === paramName) {
if (value === '' && parsed.value !== '') {
value = parsed.value;
}
continue;
}
keptParts.push(part);
}
return {
path:
keptParts.length > 0
? `${basePath}?${keptParts.join('&')}`
: basePath,
value,
};
}
export function getPathQueryParam(rawPath, paramName) {
const path = rawPath == null ? '' : `${rawPath}`;
const queryIndex = path.indexOf('?');
if (queryIndex === -1) return '';
const query = path.slice(queryIndex + 1);
for (const part of query.split('&')) {
if (part === '') continue;
const parsed = splitQueryPart(part);
if (parsed.key === paramName && parsed.value !== '') {
return parsed.value;
}
}
return '';
}
export function getSafeIntegerPathQueryParam(rawPath, paramName) {
const value = getPathQueryParam(rawPath, paramName);
const parsed = parseSafeIntegerValue(value);
if (parsed == null) {
return {
value: '',
parsed: null,
};
}
return {
value,
parsed,
};
}
function appendPathQueryParam(path, paramName, value) {
const separator = path.includes('?')
? path.endsWith('?') || path.endsWith('&')
? ''
: '&'
: '?';
return `${path}${separator}${encodeURIComponent(
paramName,
)}=${encodeURIComponent(`${value}`)}`;
}
export function setPathQueryParam(rawPath, paramName, value) {
const path = rawPath == null || rawPath === '' ? '/' : `${rawPath}`;
const { path: pathWithoutParam } = extractPathQueryParam(path, paramName);
return appendPathQueryParam(pathWithoutParam, paramName, value);
}
export function normalizeWebSocketEarlyDataPath(wsOpts) {
const networkPath = wsOpts?.path;
if (!wsOpts) return;
const { value: ed, parsed: maxEarlyData } = getSafeIntegerPathQueryParam(
networkPath,
'ed',
);
if (wsOpts['v2ray-http-upgrade']) {
if (ed !== '') {
wsOpts.path = extractPathQueryParam(networkPath, 'ed').path;
wsOpts['v2ray-http-upgrade-fast-open'] = true;
if (
wsOpts['_v2ray-http-upgrade-ed'] == null ||
`${wsOpts['_v2ray-http-upgrade-ed']}` === ''
) {
wsOpts['_v2ray-http-upgrade-ed'] = ed;
}
}
delete wsOpts['early-data-header-name'];
delete wsOpts['max-early-data'];
return;
}
if (ed === '') return;
wsOpts.path = extractPathQueryParam(networkPath, 'ed').path;
if (wsOpts['early-data-header-name'] == null) {
wsOpts['early-data-header-name'] = 'Sec-WebSocket-Protocol';
}
if (wsOpts['max-early-data'] == null) {
wsOpts['max-early-data'] = maxEarlyData;
}
}
export function deleteHttpUpgradeEarlyDataMetadata(wsOpts) {
if (!wsOpts) return;
delete wsOpts['_v2ray-http-upgrade-ed'];
}
+92 -4
View File
@@ -138,8 +138,7 @@ describe('Proxy URI parser coverage', function () {
});
it('parses SIP002 AEAD-2022 plain userinfo without Base64URL decoding', function () {
const password =
'YctPZ6U7xPPcU+gp3u+0tx/tRizJN9K8y+uKlW2qjlI=';
const password = 'YctPZ6U7xPPcU+gp3u+0tx/tRizJN9K8y+uKlW2qjlI=';
const proxy = parseOne(
`ss://2022-blake3-aes-256-gcm:${encodeURIComponent(
password,
@@ -260,6 +259,69 @@ describe('Proxy URI parser coverage', function () {
});
});
it('parses shadowsocks websocket path early data without keeping it in path', function () {
const userInfo = encodeURIComponent(
Base64.encode('aes-128-gcm:secret'),
);
const proxy = parseOne(
`ss://${userInfo}@ss-ws.example.com:443?type=ws&path=${encodeURIComponent(
'/ws?a=1&ed=2048&b=2',
)}&host=cdn.example.com&security=tls#SS%20WS%20Early`,
);
expectSubset(proxy, {
type: 'ss',
name: 'SS WS Early',
server: 'ss-ws.example.com',
port: 443,
cipher: 'aes-128-gcm',
password: 'secret',
tls: true,
network: 'ws',
'ws-opts': {
path: '/ws?a=1&b=2',
headers: {
Host: 'cdn.example.com',
},
'max-early-data': 2048,
'early-data-header-name': 'Sec-WebSocket-Protocol',
},
});
expect(proxy['ws-opts']).to.not.have.property(
'v2ray-http-upgrade',
);
});
it('does not double-decode shadowsocks path query values before extracting early data', function () {
const userInfo = encodeURIComponent(
Base64.encode('aes-128-gcm:secret'),
);
const proxy = parseOne(
`ss://${userInfo}@ss-upgrade.example.com:443?type=httpupgrade&path=${encodeURIComponent(
'/upgrade?redirect=%26ed%3D2048',
)}&host=cdn.example.com&security=tls#SS%20Upgrade%20Escaped`,
);
expectSubset(proxy, {
type: 'ss',
name: 'SS Upgrade Escaped',
network: 'ws',
'ws-opts': {
path: '/upgrade?redirect=%26ed%3D2048',
headers: {
Host: 'cdn.example.com',
},
'v2ray-http-upgrade': true,
},
});
expect(proxy['ws-opts']).to.not.have.property(
'v2ray-http-upgrade-fast-open',
);
expect(proxy['ws-opts']).to.not.have.property(
'_v2ray-http-upgrade-ed',
);
});
it('parses shadowsocks shadow-tls compatibility payloads', function () {
const userInfo = Base64.encode('aes-256-gcm:shadow-pass');
const payload = Base64.encode(
@@ -523,7 +585,7 @@ describe('Proxy URI parser coverage', function () {
it('parses Trojan URIs with websocket transport', function () {
const proxy = parseOne(
'trojan://trojan-pass@trojan-ws.example.com?type=ws&host=ws.example.com&path=%2Fws#Trojan%20WS',
'trojan://trojan-pass@trojan-ws.example.com?type=ws&host=ws.example.com&path=%2Fws%3Fa%3D1%26ed%3D1024%26b%3D2#Trojan%20WS',
);
expectSubset(proxy, {
@@ -534,12 +596,38 @@ describe('Proxy URI parser coverage', function () {
password: 'trojan-pass',
network: 'ws',
'ws-opts': {
path: '/ws',
path: '/ws?a=1&b=2',
headers: {
Host: 'ws.example.com',
},
'max-early-data': 1024,
'early-data-header-name': 'Sec-WebSocket-Protocol',
},
});
});
it('does not double-decode Trojan path query values before extracting early data', function () {
const proxy = parseOne(
`trojan://trojan-pass@trojan-ws.example.com?type=ws&host=ws.example.com&path=${encodeURIComponent(
'/ws?redirect=%26ed%3D2048',
)}#Trojan%20WS%20Escaped`,
);
expectSubset(proxy, {
type: 'trojan',
name: 'Trojan WS Escaped',
network: 'ws',
'ws-opts': {
path: '/ws?redirect=%26ed%3D2048',
headers: {
Host: 'ws.example.com',
},
},
});
expect(proxy['ws-opts']).to.not.have.property('max-early-data');
expect(proxy['ws-opts']).to.not.have.property(
'early-data-header-name',
);
});
it('parses Trojan URIs with pcs as tls fingerprint', function () {
@@ -104,10 +104,12 @@ describe('VMess and VLESS parser coverage', function () {
'client-fingerprint': 'chrome',
network: 'ws',
'ws-opts': {
path: '/fragment?ed=2560',
path: '/fragment',
headers: {
Host: 'fragment.example.com',
},
'max-early-data': 2560,
'early-data-header-name': 'Sec-WebSocket-Protocol',
},
});
});
@@ -273,9 +275,85 @@ describe('VMess and VLESS parser coverage', function () {
Host: 'upgrade.example.com',
},
'v2ray-http-upgrade': true,
'v2ray-http-upgrade-fast-open': true,
},
});
expect(proxy['ws-opts']).to.not.have.property(
'v2ray-http-upgrade-fast-open',
);
});
it('parses V2rayN httpupgrade path early data from multiple query params', function () {
const share = Base64.encode(
JSON.stringify({
ps: 'VMess Upgrade Early',
add: 'vmess-upgrade.example.com',
port: '443',
id: UUID,
aid: '0',
scy: 'auto',
net: 'httpupgrade',
host: 'upgrade.example.com',
path: '/upgrade?a=1&ed=1024&b=2',
}),
);
const proxy = parseOne(`vmess://${share}`);
expectSubset(proxy, {
type: 'vmess',
name: 'VMess Upgrade Early',
server: 'vmess-upgrade.example.com',
port: 443,
network: 'ws',
'ws-opts': {
path: '/upgrade?a=1&b=2',
headers: {
Host: 'upgrade.example.com',
},
'v2ray-http-upgrade': true,
'v2ray-http-upgrade-fast-open': true,
'_v2ray-http-upgrade-ed': '1024',
},
});
expect(proxy['ws-opts']).to.not.have.property('max-early-data');
expect(proxy['ws-opts']).to.not.have.property(
'early-data-header-name',
);
});
it('parses V2rayN httpupgrade top-level early data', function () {
const share = Base64.encode(
JSON.stringify({
ps: 'VMess Upgrade Top Early',
add: 'vmess-upgrade.example.com',
port: '443',
id: UUID,
aid: '0',
scy: 'auto',
net: 'httpupgrade',
host: 'upgrade.example.com',
path: '/upgrade',
ed: '1024',
}),
);
const proxy = parseOne(`vmess://${share}`);
expectSubset(proxy, {
type: 'vmess',
name: 'VMess Upgrade Top Early',
server: 'vmess-upgrade.example.com',
port: 443,
network: 'ws',
'ws-opts': {
path: '/upgrade',
headers: {
Host: 'upgrade.example.com',
},
'v2ray-http-upgrade': true,
'v2ray-http-upgrade-fast-open': true,
'_v2ray-http-upgrade-ed': '1024',
},
});
expect(proxy['ws-opts']).to.not.have.property('max-early-data');
});
it('parses Shadowrocket VMess shares', function () {
@@ -550,9 +628,11 @@ describe('VMess and VLESS parser coverage', function () {
Host: 'upgrade.example.com',
},
'v2ray-http-upgrade': true,
'v2ray-http-upgrade-fast-open': true,
},
});
expect(proxy['ws-opts']).to.not.have.property(
'v2ray-http-upgrade-fast-open',
);
});
it('parses httpupgrade VLESS shares with early data metadata', function () {
@@ -573,10 +653,83 @@ describe('VMess and VLESS parser coverage', function () {
},
'v2ray-http-upgrade': true,
'v2ray-http-upgrade-fast-open': true,
'max-early-data': 1024,
'_v2ray-http-upgrade-ed': '1024',
'early-data-header-name': 'X-Upgrade',
},
});
expect(proxy['ws-opts']).to.not.have.property('max-early-data');
});
it('parses httpupgrade VLESS path early data from multiple query params', function () {
const proxy = parseOne(
`vless://${UUID}@vless-upgrade.example.com:443?type=httpupgrade&host=upgrade.example.com&path=%2Fupgrade%3Fa%3D1%26ed%3D1024%26b%3D2#VLESS%20Upgrade%20Path%20Early`,
);
expectSubset(proxy, {
type: 'vless',
name: 'VLESS Upgrade Path Early',
server: 'vless-upgrade.example.com',
port: 443,
network: 'ws',
'ws-opts': {
path: '/upgrade?a=1&b=2',
headers: {
Host: 'upgrade.example.com',
},
'v2ray-http-upgrade': true,
'v2ray-http-upgrade-fast-open': true,
'_v2ray-http-upgrade-ed': '1024',
},
});
expect(proxy['ws-opts']).to.not.have.property('max-early-data');
expect(proxy['ws-opts']).to.not.have.property(
'early-data-header-name',
);
});
it('prefers VLESS path early data over duplicate top-level early data', function () {
const proxy = parseOne(
`vless://${UUID}@vless-upgrade.example.com:443?type=httpupgrade&host=upgrade.example.com&path=%2Fupgrade%3Fed%3D1024&ed=2048#VLESS%20Upgrade%20Duplicate%20Early`,
);
expectSubset(proxy, {
type: 'vless',
name: 'VLESS Upgrade Duplicate Early',
server: 'vless-upgrade.example.com',
port: 443,
network: 'ws',
'ws-opts': {
path: '/upgrade',
headers: {
Host: 'upgrade.example.com',
},
'v2ray-http-upgrade': true,
'v2ray-http-upgrade-fast-open': true,
'_v2ray-http-upgrade-ed': '1024',
},
});
});
it('parses websocket VLESS path early data from multiple query params', function () {
const proxy = parseOne(
`vless://${UUID}@vless-ws.example.com:443?type=ws&security=tls&host=cdn.example.com&path=%2Fws%3Fa%3D1%26ed%3D2048%26b%3D2#VLESS%20WS%20Path%20Early`,
);
expectSubset(proxy, {
type: 'vless',
name: 'VLESS WS Path Early',
server: 'vless-ws.example.com',
port: 443,
network: 'ws',
'ws-opts': {
path: '/ws?a=1&b=2',
headers: {
Host: 'cdn.example.com',
},
'max-early-data': 2048,
'early-data-header-name': 'Sec-WebSocket-Protocol',
},
});
});
it('parses kcp VLESS shares', function () {
@@ -26,7 +26,7 @@ describe('Proxy structured producers', function () {
sni: 'sni.example.com',
network: 'ws',
'ws-opts': {
path: '/ws?ed=2048',
path: '/ws?a=1&ed=2048&b=2',
headers: {
Host: 'cdn.example.com',
},
@@ -59,7 +59,7 @@ describe('Proxy structured producers', function () {
alterId: 0,
servername: 'sni.example.com',
'ws-opts': {
path: '/ws',
path: '/ws?a=1&b=2',
headers: {
Host: 'cdn.example.com',
},
@@ -195,7 +195,7 @@ describe('Proxy structured producers', function () {
flow: 'xtls-rprx-vision',
network: 'ws',
'ws-opts': {
path: '/ws?ed=2048',
path: '/ws?a=1&ed=2048&b=2',
headers: {
Host: 'cdn.example.com',
},
@@ -218,7 +218,7 @@ describe('Proxy structured producers', function () {
'short-id': '08',
},
'ws-opts': {
path: '/ws',
path: '/ws?a=1&b=2',
'early-data-header-name': 'Sec-WebSocket-Protocol',
'max-early-data': 2048,
},
@@ -230,6 +230,258 @@ describe('Proxy structured producers', function () {
});
});
it('normalizes websocket early data query params for Stash and Shadowrocket', function () {
const proxy = {
type: 'vmess',
name: 'WS Early Query',
server: 'vmess.example.com',
port: 443,
uuid: UUID,
cipher: 'auto',
alterId: 0,
network: 'ws',
'ws-opts': {
path: '/ws?a=1&ed=2048&b=2',
headers: {
Host: 'cdn.example.com',
},
},
};
for (const platform of ['Stash', 'Shadowrocket']) {
const internal = produceInternal(platform, proxy)[0];
expectSubset(internal, {
name: 'WS Early Query',
'ws-opts': {
path: '/ws?a=1&b=2',
'early-data-header-name': 'Sec-WebSocket-Protocol',
'max-early-data': 2048,
},
});
}
});
it('leaves unsafe websocket early data query params untouched', function () {
const proxy = {
type: 'vmess',
name: 'WS Unsafe Early Query',
server: 'vmess.example.com',
port: 443,
uuid: UUID,
cipher: 'auto',
alterId: 0,
network: 'ws',
'ws-opts': {
path: '/ws?a=1&ed=999999999999999999999&b=2',
headers: {
Host: 'cdn.example.com',
},
},
};
const wsOpts = produceInternal('Clash', proxy)[0]['ws-opts'];
expect(wsOpts.path).to.equal('/ws?a=1&ed=999999999999999999999&b=2');
expect(wsOpts).to.not.have.property('early-data-header-name');
expect(wsOpts).to.not.have.property('max-early-data');
});
it('keeps explicit websocket early data fields over stale path query values', function () {
const proxy = {
type: 'vmess',
name: 'WS Explicit Early Fields',
server: 'vmess.example.com',
port: 443,
uuid: UUID,
cipher: 'auto',
alterId: 0,
network: 'ws',
'ws-opts': {
path: '/ws?a=1&ed=2048&b=2',
headers: {
Host: 'cdn.example.com',
},
'max-early-data': 4096,
'early-data-header-name': 'X-Data',
},
};
for (const platform of [
'Clash',
'ClashMeta',
'Stash',
'Shadowrocket',
]) {
const wsOpts = loadProducedYaml(platform, proxy).proxies[0][
'ws-opts'
];
expect(wsOpts.path, platform).to.equal('/ws?a=1&b=2');
expect(wsOpts['max-early-data'], platform).to.equal(4096);
expect(wsOpts['early-data-header-name'], platform).to.equal(
'X-Data',
);
}
});
it('normalizes httpupgrade path early data as httpupgrade metadata', function () {
const proxy = {
type: 'vmess',
name: 'HTTPUpgrade Early Query',
server: 'vmess.example.com',
port: 443,
uuid: UUID,
cipher: 'auto',
alterId: 0,
network: 'ws',
'ws-opts': {
path: '/upgrade?a=1&ed=2048&b=2',
headers: {
Host: 'cdn.example.com',
},
'v2ray-http-upgrade': true,
},
};
for (const platform of [
'Clash',
'ClashMeta',
'Stash',
'Shadowrocket',
]) {
const wsOpts = produceInternal(platform, proxy, {
'include-unsupported-proxy': true,
})[0]['ws-opts'];
expect(wsOpts.path).to.equal('/upgrade?a=1&b=2');
expect(wsOpts['v2ray-http-upgrade']).to.equal(true);
expect(wsOpts['v2ray-http-upgrade-fast-open']).to.equal(true);
expect(wsOpts['_v2ray-http-upgrade-ed']).to.equal('2048');
expect(wsOpts).to.not.have.property('early-data-header-name');
expect(wsOpts).to.not.have.property('max-early-data');
const externalWsOpts = loadProducedYaml(platform, proxy, {
'include-unsupported-proxy': true,
}).proxies[0]['ws-opts'];
expect(externalWsOpts.path).to.equal('/upgrade?a=1&b=2');
expect(externalWsOpts['v2ray-http-upgrade-fast-open']).to.equal(
true,
);
expect(externalWsOpts).to.not.have.property(
'_v2ray-http-upgrade-ed',
);
}
});
it('keeps explicit httpupgrade early data over stale path query values', function () {
const proxy = {
type: 'vmess',
name: 'HTTPUpgrade Explicit Early',
server: 'vmess.example.com',
port: 443,
uuid: UUID,
cipher: 'auto',
alterId: 0,
network: 'ws',
'ws-opts': {
path: '/upgrade?a=1&ed=1024&b=2',
headers: {
Host: 'cdn.example.com',
},
'v2ray-http-upgrade': true,
'v2ray-http-upgrade-fast-open': true,
'_v2ray-http-upgrade-ed': '4096',
},
};
for (const platform of [
'Clash',
'ClashMeta',
'Stash',
'Shadowrocket',
]) {
const wsOpts = produceInternal(platform, proxy, {
'include-unsupported-proxy': true,
})[0]['ws-opts'];
expect(wsOpts.path, platform).to.equal('/upgrade?a=1&b=2');
expect(wsOpts['_v2ray-http-upgrade-ed'], platform).to.equal(
'4096',
);
}
});
it('does not emit websocket early data fields for httpupgrade transports', function () {
const directProxy = {
type: 'vmess',
name: 'HTTPUpgrade Legacy Fields',
server: 'vmess.example.com',
port: 443,
uuid: UUID,
cipher: 'auto',
alterId: 0,
network: 'ws',
'ws-opts': {
path: '/upgrade',
headers: {
Host: 'cdn.example.com',
},
'v2ray-http-upgrade': true,
'v2ray-http-upgrade-fast-open': true,
'max-early-data': 1024,
'early-data-header-name': 'X-Upgrade',
},
};
const directProxyWithoutPath = {
type: 'vmess',
name: 'HTTPUpgrade Legacy Fields Without Path',
server: 'vmess.example.com',
port: 443,
uuid: UUID,
cipher: 'auto',
alterId: 0,
network: 'ws',
'ws-opts': {
headers: {
Host: 'cdn.example.com',
},
'v2ray-http-upgrade': true,
'v2ray-http-upgrade-fast-open': true,
'max-early-data': 2048,
'early-data-header-name': 'X-Upgrade',
},
};
const parsedVlessProxy = ProxyUtils.parse(
`vless://${UUID}@vless-upgrade.example.com:443?type=httpupgrade&host=upgrade.example.com&path=%2Fupgrade&ed=1024&eh=X-Upgrade#VLESS%20Upgrade`,
)[0];
for (const proxy of [
directProxy,
directProxyWithoutPath,
parsedVlessProxy,
]) {
for (const platform of [
'Clash',
'ClashMeta',
'Stash',
'Shadowrocket',
]) {
const externalWsOpts = loadProducedYaml(platform, proxy, {
'include-unsupported-proxy': true,
}).proxies[0]['ws-opts'];
expect(externalWsOpts['v2ray-http-upgrade']).to.equal(true);
expect(externalWsOpts).to.not.have.property(
'early-data-header-name',
);
expect(externalWsOpts).to.not.have.property('max-early-data');
expect(externalWsOpts).to.not.have.property(
'_v2ray-http-upgrade-ed',
);
}
}
});
it('emits Mihomo VLESS xhttp proxies and preserves xhttp options', function () {
const proxy = {
type: 'vless',
@@ -1684,7 +1936,7 @@ describe('Proxy structured producers', function () {
flow: 'xtls-rprx-vision',
network: 'ws',
'ws-opts': {
path: '/ws?ed=2048',
path: '/ws?a=1&ed=2048&b=2',
headers: {
Host: 'cdn.example.com',
},
@@ -1718,7 +1970,7 @@ describe('Proxy structured producers', function () {
},
transport: {
type: 'ws',
path: '/ws',
path: '/ws?a=1&b=2',
headers: {
Host: 'cdn.example.com',
},
@@ -1728,6 +1980,42 @@ describe('Proxy structured producers', function () {
});
});
it('emits sing-box httpupgrade transport without websocket early data fields', function () {
const output = loadProducedJson('sing-box', {
type: 'vless',
name: 'Upgrade',
server: 'vless-upgrade.example.com',
port: 443,
uuid: UUID,
tls: true,
network: 'ws',
'ws-opts': {
path: '/upgrade?a=1&ed=2048&b=2',
headers: {
Host: 'upgrade.example.com',
},
'v2ray-http-upgrade': true,
},
});
expect(output.outbounds).to.have.length(1);
expectSubset(output.outbounds[0], {
tag: 'Upgrade',
type: 'vless',
transport: {
type: 'httpupgrade',
path: '/upgrade?a=1&b=2',
host: 'upgrade.example.com',
},
});
expect(output.outbounds[0].transport).to.not.have.property(
'early_data_header_name',
);
expect(output.outbounds[0].transport).to.not.have.property(
'max_early_data',
);
});
it('normalizes sing-box ech PEM config strings with escaped newlines', function () {
const output = loadProducedJson('sing-box', {
type: 'vless',
+491 -2
View File
@@ -701,6 +701,155 @@ describe('Proxy text producers', function () {
);
});
it('produces URI shadowsocks httpupgrade links with early data metadata', function () {
const output = produceExternal('URI', {
type: 'ss',
name: 'SS Upgrade',
server: 'ss-upgrade.example.com',
port: 443,
cipher: 'aes-128-gcm',
password: 'secret',
tls: true,
network: 'ws',
'ws-opts': {
path: '/upgrade?a=1&b=2',
headers: {
Host: 'upgrade.example.com',
},
'v2ray-http-upgrade': true,
'v2ray-http-upgrade-fast-open': true,
'_v2ray-http-upgrade-ed': '1024',
},
});
expect(output).to.equal(
`ss://${Base64.encode(
'aes-128-gcm:secret',
)}@ss-upgrade.example.com:443?sni=ss-upgrade.example.com&type=httpupgrade&path=%2Fupgrade%3Fa%3D1%26b%3D2%26ed%3D1024&host=upgrade.example.com&security=tls#SS%20Upgrade`,
);
const reparsed = ProxyUtils.parse(output)[0];
expect(reparsed['ws-opts'].path).to.equal('/upgrade?a=1&b=2');
expect(reparsed['ws-opts']['v2ray-http-upgrade-fast-open']).to.equal(
true,
);
expect(reparsed['ws-opts']['_v2ray-http-upgrade-ed']).to.equal('1024');
expect(reparsed['ws-opts']).to.not.have.property('max-early-data');
expect(reparsed['ws-opts']).to.not.have.property(
'early-data-header-name',
);
});
it('produces URI shadowsocks websocket links with early data metadata', function () {
const output = produceExternal('URI', {
type: 'ss',
name: 'SS WS Early',
server: 'ss-ws.example.com',
port: 443,
cipher: 'aes-128-gcm',
password: 'secret',
tls: true,
network: 'ws',
'ws-opts': {
path: '/ws?a=1&b=2',
headers: {
Host: 'cdn.example.com',
},
'max-early-data': 2048,
'early-data-header-name': 'Sec-WebSocket-Protocol',
},
});
expect(output).to.equal(
`ss://${Base64.encode(
'aes-128-gcm:secret',
)}@ss-ws.example.com:443?sni=ss-ws.example.com&type=ws&path=%2Fws%3Fa%3D1%26b%3D2%26ed%3D2048&host=cdn.example.com&security=tls#SS%20WS%20Early`,
);
const reparsed = ProxyUtils.parse(output)[0];
expect(reparsed['ws-opts'].path).to.equal('/ws?a=1&b=2');
expect(reparsed['ws-opts']['max-early-data']).to.equal(2048);
expect(reparsed['ws-opts']['early-data-header-name']).to.equal(
'Sec-WebSocket-Protocol',
);
expect(reparsed['ws-opts']).to.not.have.property(
'v2ray-http-upgrade',
);
});
it('does not serialize URI websocket early data for formats without custom header support', function () {
const proxies = [
{
type: 'ss',
name: 'SS WS Custom Early',
server: 'ss-ws.example.com',
port: 443,
cipher: 'aes-128-gcm',
password: 'secret',
tls: true,
network: 'ws',
'ws-opts': {
path: '/ws?a=1&ed=1024&b=2',
headers: {
Host: 'cdn.example.com',
},
'max-early-data': 2048,
'early-data-header-name': 'X-Data',
},
},
{
type: 'vmess',
name: 'VMess WS Custom Early',
server: 'vmess-ws.example.com',
port: 443,
uuid: UUID,
cipher: 'auto',
alterId: 0,
tls: true,
network: 'ws',
'ws-opts': {
path: '/ws?a=1&ed=1024&b=2',
headers: {
Host: 'cdn.example.com',
},
'max-early-data': 2048,
'early-data-header-name': 'X-Data',
},
},
{
type: 'trojan',
name: 'Trojan WS Custom Early',
server: 'trojan-ws.example.com',
port: 443,
password: 'secret',
tls: true,
network: 'ws',
'ws-opts': {
path: '/ws?a=1&ed=1024&b=2',
headers: {
Host: 'cdn.example.com',
},
'max-early-data': 2048,
'early-data-header-name': 'X-Data',
},
},
];
for (const proxy of proxies) {
const reparsed = ProxyUtils.parse(produceExternal('URI', proxy))[0];
expect(reparsed['ws-opts'].path, proxy.type).to.equal(
'/ws?a=1&b=2',
);
expect(reparsed['ws-opts'], proxy.type).to.not.have.property(
'max-early-data',
);
expect(reparsed['ws-opts'], proxy.type).to.not.have.property(
'early-data-header-name',
);
}
});
it('normalizes boolean v2ray-plugin mux values to integers in URI links', function () {
const muxOnPlugin = encodeURIComponent(
'v2ray-plugin;obfs=websocket;mode=websocket;obfs-host=cdn.example.com;host=cdn.example.com;path=/socket;tls;mux=1',
@@ -905,6 +1054,34 @@ describe('Proxy text producers', function () {
);
});
it('replaces stale URI VLESS websocket path early data with max-early-data', function () {
const output = produceExternal('URI', {
type: 'vless',
name: 'URI WS Conflict',
server: 'vless.example.com',
port: 443,
uuid: UUID,
tls: true,
network: 'ws',
'ws-opts': {
path: '/ws?a=1&ed=999&b=2',
headers: {
Host: 'cdn.example.com',
},
'max-early-data': 2048,
'early-data-header-name': 'Sec-WebSocket-Protocol',
},
});
expect(output).to.equal(
`vless://${UUID}@vless.example.com:443?security=tls&type=ws&path=%2Fws%3Fa%3D1%26b%3D2&host=cdn.example.com&ed=2048#URI%20WS%20Conflict`,
);
const reparsed = ProxyUtils.parse(output)[0];
expect(reparsed['ws-opts'].path).to.equal('/ws?a=1&b=2');
expect(reparsed['ws-opts']['max-early-data']).to.equal(2048);
});
it('produces URI VLESS websocket links with pcs from tls fingerprint', function () {
const output = produceExternal('URI', {
type: 'vless',
@@ -968,13 +1145,258 @@ describe('Proxy text producers', function () {
},
'v2ray-http-upgrade': true,
'v2ray-http-upgrade-fast-open': true,
'max-early-data': 1024,
'_v2ray-http-upgrade-ed': '1024',
'early-data-header-name': 'X-Upgrade',
},
});
expect(output).to.equal(
`vless://${UUID}@vless-upgrade.example.com:443?security=tls&type=httpupgrade&path=%2Fupgrade&host=upgrade.example.com&ed=1024&eh=X-Upgrade#URI%20Upgrade`,
`vless://${UUID}@vless-upgrade.example.com:443?security=tls&type=httpupgrade&path=%2Fupgrade%3Fed%3D1024&host=upgrade.example.com&eh=X-Upgrade#URI%20Upgrade`,
);
});
it('defaults URI VLESS httpupgrade path early data without reusing websocket early data', function () {
const output = produceExternal('URI', {
type: 'vless',
name: 'URI Upgrade Default',
server: 'vless-upgrade.example.com',
port: 443,
uuid: UUID,
tls: true,
network: 'ws',
'ws-opts': {
path: '/upgrade',
headers: {
Host: 'upgrade.example.com',
},
'v2ray-http-upgrade': true,
'v2ray-http-upgrade-fast-open': true,
'max-early-data': 1024,
},
});
expect(output).to.equal(
`vless://${UUID}@vless-upgrade.example.com:443?security=tls&type=httpupgrade&path=%2Fupgrade%3Fed%3D2560&host=upgrade.example.com#URI%20Upgrade%20Default`,
);
});
it('keeps URI VLESS httpupgrade early data from path when metadata is absent', function () {
const output = produceExternal('URI', {
type: 'vless',
name: 'URI Upgrade Existing Path Early',
server: 'vless-upgrade.example.com',
port: 443,
uuid: UUID,
tls: true,
network: 'ws',
'ws-opts': {
path: '/upgrade?a=1&ed=1024&b=2',
headers: {
Host: 'upgrade.example.com',
},
'v2ray-http-upgrade': true,
'v2ray-http-upgrade-fast-open': true,
},
});
expect(output).to.equal(
`vless://${UUID}@vless-upgrade.example.com:443?security=tls&type=httpupgrade&path=%2Fupgrade%3Fa%3D1%26b%3D2%26ed%3D1024&host=upgrade.example.com#URI%20Upgrade%20Existing%20Path%20Early`,
);
});
it('adds URI VLESS httpupgrade early data to paths with existing query params', function () {
const output = produceExternal('URI', {
type: 'vless',
name: 'URI Upgrade Query',
server: 'vless-upgrade.example.com',
port: 443,
uuid: UUID,
tls: true,
network: 'ws',
'ws-opts': {
path: '/upgrade?a=1&b=2',
headers: {
Host: 'upgrade.example.com',
},
'v2ray-http-upgrade': true,
'v2ray-http-upgrade-fast-open': true,
'_v2ray-http-upgrade-ed': '1024',
},
});
expect(output).to.equal(
`vless://${UUID}@vless-upgrade.example.com:443?security=tls&type=httpupgrade&path=%2Fupgrade%3Fa%3D1%26b%3D2%26ed%3D1024&host=upgrade.example.com#URI%20Upgrade%20Query`,
);
});
it('replaces invalid URI VLESS httpupgrade path early data', function () {
const output = produceExternal('URI', {
type: 'vless',
name: 'URI Upgrade Invalid Query',
server: 'vless-upgrade.example.com',
port: 443,
uuid: UUID,
tls: true,
network: 'ws',
'ws-opts': {
path: '/upgrade?ed=abc&x=1',
headers: {
Host: 'upgrade.example.com',
},
'v2ray-http-upgrade': true,
'v2ray-http-upgrade-fast-open': true,
'_v2ray-http-upgrade-ed': '1024',
},
});
expect(output).to.equal(
`vless://${UUID}@vless-upgrade.example.com:443?security=tls&type=httpupgrade&path=%2Fupgrade%3Fx%3D1%26ed%3D1024&host=upgrade.example.com#URI%20Upgrade%20Invalid%20Query`,
);
});
it('does not serialize invalid URI early data values', function () {
const invalidVlessWsOutput = produceExternal('URI', {
type: 'vless',
name: 'URI WS Invalid Early',
server: 'vless.example.com',
port: 443,
uuid: UUID,
tls: true,
network: 'ws',
'ws-opts': {
path: '/ws?a=1&ed=1024&b=2',
headers: {
Host: 'cdn.example.com',
},
'max-early-data': '999999999999999999999',
'early-data-header-name': 'Sec-WebSocket-Protocol',
},
});
const invalidSsWsOutput = produceExternal('URI', {
type: 'ss',
name: 'SS WS Invalid Early',
server: 'ss-ws.example.com',
port: 443,
cipher: 'aes-128-gcm',
password: 'secret',
tls: true,
network: 'ws',
'ws-opts': {
path: '/ws?a=1&ed=1024&b=2',
headers: {
Host: 'cdn.example.com',
},
'max-early-data': 'abc',
'early-data-header-name': 'Sec-WebSocket-Protocol',
},
});
const invalidHttpUpgradeOutput = produceExternal('URI', {
type: 'vless',
name: 'URI Upgrade Invalid Metadata',
server: 'vless-upgrade.example.com',
port: 443,
uuid: UUID,
tls: true,
network: 'ws',
'ws-opts': {
path: '/upgrade?ed=abc',
headers: {
Host: 'upgrade.example.com',
},
'v2ray-http-upgrade': true,
'v2ray-http-upgrade-fast-open': true,
'_v2ray-http-upgrade-ed': 'abc',
},
});
const reparsedVlessWs = ProxyUtils.parse(invalidVlessWsOutput)[0];
const reparsedSsWs = ProxyUtils.parse(invalidSsWsOutput)[0];
expect(reparsedVlessWs['ws-opts'].path).to.equal('/ws?a=1&b=2');
expect(reparsedVlessWs['ws-opts']).to.not.have.property(
'max-early-data',
);
expect(reparsedSsWs['ws-opts'].path).to.equal('/ws?a=1&b=2');
expect(reparsedSsWs['ws-opts']).to.not.have.property(
'max-early-data',
);
expect(invalidHttpUpgradeOutput).to.equal(
`vless://${UUID}@vless-upgrade.example.com:443?security=tls&type=httpupgrade&path=%2Fupgrade%3Fed%3D2560&host=upgrade.example.com#URI%20Upgrade%20Invalid%20Metadata`,
);
});
it('produces URI VMess httpupgrade links with early data metadata', function () {
const output = produceExternal('URI', {
type: 'vmess',
name: 'URI VMess Upgrade',
server: 'vmess-upgrade.example.com',
port: 443,
uuid: UUID,
cipher: 'auto',
alterId: 0,
tls: true,
network: 'ws',
'ws-opts': {
path: '/upgrade?a=1&b=2',
headers: {
Host: 'upgrade.example.com',
},
'v2ray-http-upgrade': true,
'v2ray-http-upgrade-fast-open': true,
'_v2ray-http-upgrade-ed': '1024',
},
});
const payload = JSON.parse(
Base64.decode(output.replace(/^vmess:\/\//, '')),
);
expect(payload.net).to.equal('httpupgrade');
expect(payload.path).to.equal('/upgrade?a=1&b=2&ed=1024');
const reparsed = ProxyUtils.parse(output)[0];
expect(reparsed['ws-opts'].path).to.equal('/upgrade?a=1&b=2');
expect(reparsed['ws-opts']['v2ray-http-upgrade-fast-open']).to.equal(
true,
);
expect(reparsed['ws-opts']['_v2ray-http-upgrade-ed']).to.equal('1024');
expect(reparsed['ws-opts']).to.not.have.property('max-early-data');
expect(reparsed['ws-opts']).to.not.have.property(
'early-data-header-name',
);
});
it('produces URI VMess websocket links with early data metadata', function () {
const output = produceExternal('URI', {
type: 'vmess',
name: 'URI VMess WS Early',
server: 'vmess-ws.example.com',
port: 443,
uuid: UUID,
cipher: 'auto',
alterId: 0,
tls: true,
network: 'ws',
'ws-opts': {
path: '/ws?a=1&b=2',
headers: {
Host: 'cdn.example.com',
},
'max-early-data': 2048,
'early-data-header-name': 'Sec-WebSocket-Protocol',
},
});
const payload = JSON.parse(
Base64.decode(output.replace(/^vmess:\/\//, '')),
);
expect(payload.net).to.equal('ws');
expect(payload.path).to.equal('/ws?a=1&b=2&ed=2048');
const reparsed = ProxyUtils.parse(output)[0];
expect(reparsed['ws-opts'].path).to.equal('/ws?a=1&b=2');
expect(reparsed['ws-opts']['max-early-data']).to.equal(2048);
expect(reparsed['ws-opts']['early-data-header-name']).to.equal(
'Sec-WebSocket-Protocol',
);
});
@@ -2693,6 +3115,73 @@ describe('Proxy text producers', function () {
);
});
it('produces URI Trojan websocket links with early data metadata', function () {
const output = produceExternal('URI', {
type: 'trojan',
name: 'URI Trojan WS Early',
server: 'trojan.example.com',
port: 443,
password: 'secret',
tls: true,
network: 'ws',
'ws-opts': {
path: '/ws?a=1&b=2',
headers: {
Host: 'cdn.example.com',
},
'max-early-data': 2048,
'early-data-header-name': 'Sec-WebSocket-Protocol',
},
});
expect(output).to.equal(
'trojan://secret@trojan.example.com:443?sni=trojan.example.com&type=ws&path=%2Fws%3Fa%3D1%26b%3D2%26ed%3D2048&host=cdn.example.com#URI%20Trojan%20WS%20Early',
);
const reparsed = ProxyUtils.parse(output)[0];
expect(reparsed['ws-opts'].path).to.equal('/ws?a=1&b=2');
expect(reparsed['ws-opts']['max-early-data']).to.equal(2048);
expect(reparsed['ws-opts']['early-data-header-name']).to.equal(
'Sec-WebSocket-Protocol',
);
});
it('produces URI Trojan httpupgrade links with early data metadata', function () {
const output = produceExternal('URI', {
type: 'trojan',
name: 'URI Trojan Upgrade',
server: 'trojan-upgrade.example.com',
port: 443,
password: 'secret',
tls: true,
network: 'ws',
'ws-opts': {
path: '/upgrade?a=1&b=2',
headers: {
Host: 'upgrade.example.com',
},
'v2ray-http-upgrade': true,
'v2ray-http-upgrade-fast-open': true,
'_v2ray-http-upgrade-ed': '1024',
},
});
expect(output).to.equal(
'trojan://secret@trojan-upgrade.example.com:443?sni=trojan-upgrade.example.com&type=httpupgrade&path=%2Fupgrade%3Fa%3D1%26b%3D2%26ed%3D1024&host=upgrade.example.com#URI%20Trojan%20Upgrade',
);
const reparsed = ProxyUtils.parse(output)[0];
expect(reparsed['ws-opts'].path).to.equal('/upgrade?a=1&b=2');
expect(reparsed['ws-opts']['v2ray-http-upgrade-fast-open']).to.equal(
true,
);
expect(reparsed['ws-opts']['_v2ray-http-upgrade-ed']).to.equal('1024');
expect(reparsed['ws-opts']).to.not.have.property('max-early-data');
expect(reparsed['ws-opts']).to.not.have.property(
'early-data-header-name',
);
});
it('produces V2Ray exports as base64 encoded URI lists', function () {
const output = produceExternal('V2Ray', {
type: 'vless',