mirror of
https://github.com/sub-store-org/Sub-Store.git
synced 2026-12-03 09:45:54 +00:00
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:
14 files changed
+1625
-159
No files matched your search
@@ -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)) {
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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) &&
|
||||
|
||||
@@ -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'];
|
||||
}
|
||||
Reference in new issue
Block a user