mirror of
https://github.com/sub-store-org/Sub-Store.git
synced 2026-12-05 10:48:05 +00:00
feat: 统一 h2 host 兼容逻辑并补齐 Egern fingerprint_sha256 下发
- 解析层把 VMess/VLESS 的 h2 传输 Host 改为统一字段: 将 h2 的 host 从 headers 里搬到 h2-opts.host,支持逗号分隔多 Host,清理空 headers,并保持 path 默认值兼容。 - 同步修正 VMess 解析: V2rayN 中 `net=http` 识别为 h2 处理,VMess/VLESS 生成时支持 host 列表和默认 `path='/'`,避免历史格式不一致。 - 统一 Clash / ClashMeta / Shadowrocket / Stash 的 h2-opts 处理: 兼容 `h2-opts.host` 与 `h2-opts.headers.host|Host`,输出统一为 `host` 数组并移除多余 host header。 - Egern 生产端增强: 支持 h2 作为受限网络分支,添加 http2 transport 输出(含 path/headers/sni/skip_tls_verify), 并将 `tls-fingerprint` 转换为 `fingerprint_sha256`,支持根配置与 transport 透传(http2/tls/wss),空值/空白值会被剔除。 - 测试同步覆盖: 增加/调整 parser、structured producer、text producer 测试,覆盖 h2 host 多值、Host header 保留/提升、以及 fingerprint_sha256 生效与不生效场景。
This commit is contained in:
12 files changed
+765
-52
No files matched your search
@@ -819,24 +819,28 @@ function lastParse(proxy) {
|
||||
if (proxy.network) {
|
||||
let transportHost = proxy[`${proxy.network}-opts`]?.headers?.Host;
|
||||
let transporthost = proxy[`${proxy.network}-opts`]?.headers?.host;
|
||||
if (proxy.network === 'h2') {
|
||||
if (!transporthost && transportHost) {
|
||||
proxy[`${proxy.network}-opts`].headers.host = transportHost;
|
||||
delete proxy[`${proxy.network}-opts`].headers.Host;
|
||||
}
|
||||
} else if (transporthost && !transportHost) {
|
||||
if (proxy.network !== 'h2' && transporthost && !transportHost) {
|
||||
proxy[`${proxy.network}-opts`].headers.Host = transporthost;
|
||||
delete proxy[`${proxy.network}-opts`].headers.host;
|
||||
}
|
||||
}
|
||||
if (proxy.network === 'h2') {
|
||||
const host = proxy['h2-opts']?.headers?.host;
|
||||
const path = proxy['h2-opts']?.path;
|
||||
if (host && !Array.isArray(host)) {
|
||||
proxy['h2-opts'].headers.host = [host];
|
||||
const h2Opts = proxy['h2-opts'];
|
||||
const host =
|
||||
h2Opts?.host ?? h2Opts?.headers?.host ?? h2Opts?.headers?.Host;
|
||||
const path = h2Opts?.path;
|
||||
if (host) {
|
||||
h2Opts.host = Array.isArray(host) ? host : [host];
|
||||
}
|
||||
if (h2Opts?.headers) {
|
||||
delete h2Opts.headers.host;
|
||||
delete h2Opts.headers.Host;
|
||||
if (Object.keys(h2Opts.headers).length === 0) {
|
||||
delete h2Opts.headers;
|
||||
}
|
||||
}
|
||||
if (Array.isArray(path)) {
|
||||
proxy['h2-opts'].path = path[0];
|
||||
h2Opts.path = path[0];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -872,7 +876,10 @@ function lastParse(proxy) {
|
||||
if (proxy.tls && !proxy.sni && proxy.sni !== '') {
|
||||
// 传输层若有设置就使用
|
||||
if (proxy.network) {
|
||||
let transportHost = proxy[`${proxy.network}-opts`]?.headers?.Host;
|
||||
let transportHost =
|
||||
proxy.network === 'h2'
|
||||
? proxy['h2-opts']?.host
|
||||
: proxy[`${proxy.network}-opts`]?.headers?.Host;
|
||||
transportHost = Array.isArray(transportHost)
|
||||
? transportHost[0]
|
||||
: transportHost;
|
||||
|
||||
@@ -102,6 +102,20 @@ function parseEarlyDataSize(value) {
|
||||
return parsed;
|
||||
}
|
||||
|
||||
function splitURIHostList(host) {
|
||||
if (Array.isArray(host)) {
|
||||
return host.flatMap((item) => splitURIHostList(item) || []);
|
||||
}
|
||||
if (typeof host !== 'string') {
|
||||
return host == null ? undefined : [host];
|
||||
}
|
||||
const hosts = host
|
||||
.split(',')
|
||||
.map((item) => item.trim())
|
||||
.filter(Boolean);
|
||||
return hosts.length > 0 ? hosts : undefined;
|
||||
}
|
||||
|
||||
function parseWireGuardURIAddressValue(value) {
|
||||
if (value == null) return null;
|
||||
const raw = `${value}`.trim();
|
||||
@@ -740,11 +754,12 @@ function URI_VMess() {
|
||||
if (params.net === 'ws' || params.obfs === 'websocket') {
|
||||
proxy.network = 'ws';
|
||||
} else if (
|
||||
['http'].includes(params.net) ||
|
||||
['http'].includes(params.obfs) ||
|
||||
['http'].includes(params.type)
|
||||
) {
|
||||
proxy.network = 'http';
|
||||
} else if (params.net === 'http') {
|
||||
proxy.network = 'h2';
|
||||
} else if (['grpc', 'kcp', 'quic'].includes(params.net)) {
|
||||
proxy.network = params.net;
|
||||
} else if (
|
||||
@@ -805,6 +820,10 @@ function URI_VMess() {
|
||||
} else {
|
||||
transportPath = '/';
|
||||
}
|
||||
} else if (proxy.network === 'h2') {
|
||||
if (!transportPath) {
|
||||
transportPath = '/';
|
||||
}
|
||||
}
|
||||
// 传输层应该有配置, 暂时不考虑兼容不给配置的节点
|
||||
if (
|
||||
@@ -832,8 +851,18 @@ function URI_VMess() {
|
||||
} else {
|
||||
const opts = {
|
||||
path: getIfNotBlank(transportPath),
|
||||
headers: { Host: getIfNotBlank(transportHost) },
|
||||
};
|
||||
const normalizedTransportHost =
|
||||
getIfNotBlank(transportHost);
|
||||
if (proxy.network === 'h2') {
|
||||
const h2Hosts =
|
||||
splitURIHostList(normalizedTransportHost);
|
||||
if (h2Hosts) {
|
||||
opts.host = h2Hosts;
|
||||
}
|
||||
} else {
|
||||
opts.headers = { Host: normalizedTransportHost };
|
||||
}
|
||||
if (httpupgrade) {
|
||||
opts['v2ray-http-upgrade'] = true;
|
||||
httpUpgradeEd =
|
||||
@@ -1860,6 +1889,15 @@ function URI_VLESS() {
|
||||
delete opts.headers;
|
||||
}
|
||||
}
|
||||
const h2Host = opts.headers?.Host ?? opts.headers?.host;
|
||||
if (['h2'].includes(proxy.network) && h2Host) {
|
||||
opts.host = splitURIHostList(h2Host);
|
||||
delete opts.headers.Host;
|
||||
delete opts.headers.host;
|
||||
if (Object.keys(opts.headers).length === 0) {
|
||||
delete opts.headers;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (params.serviceName) {
|
||||
opts[`${proxy.network}-service-name`] = params.serviceName;
|
||||
@@ -1880,6 +1918,8 @@ function URI_VLESS() {
|
||||
pathEarlyData = extracted.ed;
|
||||
}
|
||||
opts.path = transportPath;
|
||||
} else if (proxy.network === 'h2') {
|
||||
opts.path = '/';
|
||||
}
|
||||
if (proxy.network === 'http' && params.method) {
|
||||
opts.method = params.method;
|
||||
|
||||
@@ -138,12 +138,27 @@ export default function Clash_Producer() {
|
||||
) {
|
||||
proxy['h2-opts'].path = path[0];
|
||||
}
|
||||
let host = proxy['h2-opts']?.headers?.host;
|
||||
let host =
|
||||
proxy['h2-opts']?.host ??
|
||||
proxy['h2-opts']?.headers?.host ??
|
||||
proxy['h2-opts']?.headers?.Host;
|
||||
if (
|
||||
isPresent(proxy, 'h2-opts.headers.Host') &&
|
||||
!Array.isArray(host)
|
||||
(isPresent(proxy, 'h2-opts.host') ||
|
||||
isPresent(proxy, 'h2-opts.headers.host') ||
|
||||
isPresent(proxy, 'h2-opts.headers.Host'))
|
||||
) {
|
||||
proxy['h2-opts'].headers.host = [host];
|
||||
proxy['h2-opts'].host = Array.isArray(host)
|
||||
? host
|
||||
: [host];
|
||||
}
|
||||
if (proxy['h2-opts']?.headers) {
|
||||
delete proxy['h2-opts'].headers.host;
|
||||
delete proxy['h2-opts'].headers.Host;
|
||||
if (
|
||||
Object.keys(proxy['h2-opts'].headers).length === 0
|
||||
) {
|
||||
delete proxy['h2-opts'].headers;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (['ws'].includes(proxy.network)) {
|
||||
|
||||
@@ -310,12 +310,27 @@ export default function ClashMeta_Producer() {
|
||||
) {
|
||||
proxy['h2-opts'].path = path[0];
|
||||
}
|
||||
let host = proxy['h2-opts']?.headers?.host;
|
||||
let host =
|
||||
proxy['h2-opts']?.host ??
|
||||
proxy['h2-opts']?.headers?.host ??
|
||||
proxy['h2-opts']?.headers?.Host;
|
||||
if (
|
||||
isPresent(proxy, 'h2-opts.headers.Host') &&
|
||||
!Array.isArray(host)
|
||||
(isPresent(proxy, 'h2-opts.host') ||
|
||||
isPresent(proxy, 'h2-opts.headers.host') ||
|
||||
isPresent(proxy, 'h2-opts.headers.Host'))
|
||||
) {
|
||||
proxy['h2-opts'].headers.host = [host];
|
||||
proxy['h2-opts'].host = Array.isArray(host)
|
||||
? host
|
||||
: [host];
|
||||
}
|
||||
if (proxy['h2-opts']?.headers) {
|
||||
delete proxy['h2-opts'].headers.host;
|
||||
delete proxy['h2-opts'].headers.Host;
|
||||
if (
|
||||
Object.keys(proxy['h2-opts'].headers).length === 0
|
||||
) {
|
||||
delete proxy['h2-opts'].headers;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (['ws'].includes(proxy.network)) {
|
||||
|
||||
@@ -61,13 +61,15 @@ export default function Egern_Producer() {
|
||||
'2022-blake3-aes-256-gcm',
|
||||
].includes(proxy.cipher))) ||
|
||||
(proxy.type === 'vmess' &&
|
||||
!['http', 'ws', 'tcp'].includes(proxy.network) &&
|
||||
!['h2', 'http', 'ws', 'tcp'].includes(proxy.network) &&
|
||||
proxy.network) ||
|
||||
(proxy.type === 'trojan' &&
|
||||
!['http', 'ws', 'tcp'].includes(proxy.network) &&
|
||||
proxy.network) ||
|
||||
(proxy.type === 'vless' &&
|
||||
((!['http', 'ws', 'tcp'].includes(proxy.network) &&
|
||||
((!['h2', 'http', 'ws', 'tcp'].includes(
|
||||
proxy.network,
|
||||
) &&
|
||||
proxy.network) ||
|
||||
(typeof proxy.flow !== 'undefined' &&
|
||||
!['xtls-rprx-vision', ''].includes(
|
||||
@@ -132,6 +134,24 @@ export default function Egern_Producer() {
|
||||
}
|
||||
: {}),
|
||||
};
|
||||
} else if (proxy.type === 'https') {
|
||||
proxy = {
|
||||
type: 'https',
|
||||
name: proxy.name,
|
||||
server: proxy.server,
|
||||
port: proxy.port,
|
||||
username: proxy.username,
|
||||
password: proxy.password,
|
||||
...(hasHeaders(proxy)
|
||||
? {
|
||||
headers: proxy.headers,
|
||||
}
|
||||
: {}),
|
||||
tfo: proxy.tfo || proxy['fast-open'],
|
||||
next_hop: proxy.next_hop,
|
||||
sni: proxy.sni,
|
||||
skip_tls_verify: proxy['skip-cert-verify'],
|
||||
};
|
||||
} else if (proxy.type === 'socks5') {
|
||||
proxy = {
|
||||
type: 'socks5',
|
||||
@@ -312,13 +332,8 @@ export default function Egern_Producer() {
|
||||
path: Array.isArray(proxy['h2-opts']?.path)
|
||||
? proxy['h2-opts']?.path[0]
|
||||
: proxy['h2-opts']?.path,
|
||||
headers: {
|
||||
Host: Array.isArray(
|
||||
proxy['h2-opts']?.headers?.Host,
|
||||
)
|
||||
? proxy['h2-opts']?.headers?.Host[0]
|
||||
: proxy['h2-opts']?.headers?.Host,
|
||||
},
|
||||
headers: getH2Headers(proxy['h2-opts']),
|
||||
sni: proxy.sni,
|
||||
skip_tls_verify: proxy['skip-cert-verify'],
|
||||
},
|
||||
};
|
||||
@@ -393,6 +408,18 @@ export default function Egern_Producer() {
|
||||
skip_tls_verify: proxy['skip-cert-verify'],
|
||||
},
|
||||
};
|
||||
} else if (proxy.network === 'h2') {
|
||||
proxy.transport = {
|
||||
http2: {
|
||||
method: proxy['h2-opts']?.method,
|
||||
path: Array.isArray(proxy['h2-opts']?.path)
|
||||
? proxy['h2-opts']?.path[0]
|
||||
: proxy['h2-opts']?.path,
|
||||
headers: getH2Headers(proxy['h2-opts']),
|
||||
sni: proxy.sni,
|
||||
skip_tls_verify: proxy['skip-cert-verify'],
|
||||
},
|
||||
};
|
||||
} else if (proxy.network === 'tcp' || !proxy.network) {
|
||||
let reality;
|
||||
if (
|
||||
@@ -518,6 +545,16 @@ export default function Egern_Producer() {
|
||||
};
|
||||
}
|
||||
}
|
||||
const fingerprintSha256 = getFingerprintSha256(original);
|
||||
if (fingerprintSha256) {
|
||||
if (supportsRootFingerprintSha256(original, proxy)) {
|
||||
proxy.fingerprint_sha256 = fingerprintSha256;
|
||||
}
|
||||
addTransportFingerprintSha256(
|
||||
proxy.transport,
|
||||
fingerprintSha256,
|
||||
);
|
||||
}
|
||||
if (
|
||||
[
|
||||
'socks5',
|
||||
@@ -611,3 +648,72 @@ function hasHeaders(proxy) {
|
||||
Object.keys(proxy.headers).length > 0
|
||||
);
|
||||
}
|
||||
|
||||
function getFirstHeaderValue(headers, ...keys) {
|
||||
for (const key of keys) {
|
||||
const value = getFirstValue(headers?.[key]);
|
||||
if (value) return value;
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
function getFirstH2Host(h2Opts) {
|
||||
return (
|
||||
getFirstValue(h2Opts?.host) ||
|
||||
getFirstHeaderValue(h2Opts?.headers, 'host', 'Host')
|
||||
);
|
||||
}
|
||||
|
||||
function getH2Headers(h2Opts) {
|
||||
const headers = {};
|
||||
if (
|
||||
h2Opts?.headers &&
|
||||
typeof h2Opts.headers === 'object' &&
|
||||
!Array.isArray(h2Opts.headers)
|
||||
) {
|
||||
for (const [key, value] of Object.entries(h2Opts.headers)) {
|
||||
if (/^host$/i.test(key)) continue;
|
||||
const headerValue = getFirstValue(value);
|
||||
if (headerValue != null) {
|
||||
headers[key] = headerValue;
|
||||
}
|
||||
}
|
||||
}
|
||||
const host = getFirstH2Host(h2Opts);
|
||||
if (host) {
|
||||
headers.Host = host;
|
||||
}
|
||||
return Object.keys(headers).length > 0 ? headers : undefined;
|
||||
}
|
||||
|
||||
function getFirstValue(value) {
|
||||
if (Array.isArray(value)) return value[0];
|
||||
if (value != null) return value;
|
||||
return undefined;
|
||||
}
|
||||
|
||||
function getFingerprintSha256(proxy) {
|
||||
const fingerprint = proxy?.['tls-fingerprint'];
|
||||
if (typeof fingerprint !== 'string') return undefined;
|
||||
const trimmedFingerprint = fingerprint.trim();
|
||||
return trimmedFingerprint.length > 0 ? trimmedFingerprint : undefined;
|
||||
}
|
||||
|
||||
function supportsRootFingerprintSha256(original, proxy) {
|
||||
return (
|
||||
['anytls', 'https', 'hysteria2', 'trojan', 'tuic'].includes(
|
||||
original.type,
|
||||
) ||
|
||||
(original.type === 'http' && proxy.type === 'https')
|
||||
);
|
||||
}
|
||||
|
||||
function addTransportFingerprintSha256(transport, fingerprintSha256) {
|
||||
if (!transport) return;
|
||||
|
||||
for (const key of ['http2', 'tls', 'wss']) {
|
||||
if (transport[key]) {
|
||||
transport[key].fingerprint_sha256 = fingerprintSha256;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -203,12 +203,27 @@ export default function Shadowrocket_Producer() {
|
||||
) {
|
||||
proxy['h2-opts'].path = path[0];
|
||||
}
|
||||
let host = proxy['h2-opts']?.headers?.host;
|
||||
let host =
|
||||
proxy['h2-opts']?.host ??
|
||||
proxy['h2-opts']?.headers?.host ??
|
||||
proxy['h2-opts']?.headers?.Host;
|
||||
if (
|
||||
isPresent(proxy, 'h2-opts.headers.Host') &&
|
||||
!Array.isArray(host)
|
||||
(isPresent(proxy, 'h2-opts.host') ||
|
||||
isPresent(proxy, 'h2-opts.headers.host') ||
|
||||
isPresent(proxy, 'h2-opts.headers.Host'))
|
||||
) {
|
||||
proxy['h2-opts'].headers.host = [host];
|
||||
proxy['h2-opts'].host = Array.isArray(host)
|
||||
? host
|
||||
: [host];
|
||||
}
|
||||
if (proxy['h2-opts']?.headers) {
|
||||
delete proxy['h2-opts'].headers.host;
|
||||
delete proxy['h2-opts'].headers.Host;
|
||||
if (
|
||||
Object.keys(proxy['h2-opts'].headers).length === 0
|
||||
) {
|
||||
delete proxy['h2-opts'].headers;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (['ws'].includes(proxy.network)) {
|
||||
|
||||
@@ -265,12 +265,27 @@ export default function Stash_Producer() {
|
||||
) {
|
||||
proxy['h2-opts'].path = path[0];
|
||||
}
|
||||
let host = proxy['h2-opts']?.headers?.host;
|
||||
let host =
|
||||
proxy['h2-opts']?.host ??
|
||||
proxy['h2-opts']?.headers?.host ??
|
||||
proxy['h2-opts']?.headers?.Host;
|
||||
if (
|
||||
isPresent(proxy, 'h2-opts.headers.Host') &&
|
||||
!Array.isArray(host)
|
||||
(isPresent(proxy, 'h2-opts.host') ||
|
||||
isPresent(proxy, 'h2-opts.headers.host') ||
|
||||
isPresent(proxy, 'h2-opts.headers.Host'))
|
||||
) {
|
||||
proxy['h2-opts'].headers.host = [host];
|
||||
proxy['h2-opts'].host = Array.isArray(host)
|
||||
? host
|
||||
: [host];
|
||||
}
|
||||
if (proxy['h2-opts']?.headers) {
|
||||
delete proxy['h2-opts'].headers.host;
|
||||
delete proxy['h2-opts'].headers.Host;
|
||||
if (
|
||||
Object.keys(proxy['h2-opts'].headers).length === 0
|
||||
) {
|
||||
delete proxy['h2-opts'].headers;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (['ws'].includes(proxy.network)) {
|
||||
|
||||
@@ -1017,7 +1017,10 @@ export default function URI_Producer() {
|
||||
proxy.network === 'ws' &&
|
||||
vmessTransportOpts?.['v2ray-http-upgrade'];
|
||||
let vmessTransportPath = vmessTransportOpts?.path;
|
||||
let vmessTransportHost = vmessTransportOpts?.headers?.Host;
|
||||
let vmessTransportHost = getTransportHost(
|
||||
proxy.network,
|
||||
vmessTransportOpts,
|
||||
);
|
||||
|
||||
if (['grpc'].includes(proxy.network)) {
|
||||
result.path =
|
||||
|
||||
Reference in new issue
Block a user