diff --git a/backend/package.json b/backend/package.json index 2e7bd255..408fbb47 100644 --- a/backend/package.json +++ b/backend/package.json @@ -1,6 +1,6 @@ { "name": "sub-store", - "version": "2.23.21", + "version": "2.23.22", "description": "Advanced Subscription Manager for QX, Loon, Surge, Stash and Shadowrocket.", "main": "src/main.js", "packageManager": "pnpm@11.0.9", diff --git a/backend/src/core/proxy-utils/index.js b/backend/src/core/proxy-utils/index.js index 52a94e9d..6b03be54 100644 --- a/backend/src/core/proxy-utils/index.js +++ b/backend/src/core/proxy-utils/index.js @@ -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; diff --git a/backend/src/core/proxy-utils/parsers/index.js b/backend/src/core/proxy-utils/parsers/index.js index cbc06807..e2a42249 100644 --- a/backend/src/core/proxy-utils/parsers/index.js +++ b/backend/src/core/proxy-utils/parsers/index.js @@ -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; diff --git a/backend/src/core/proxy-utils/producers/clash.js b/backend/src/core/proxy-utils/producers/clash.js index 8af2fb35..09ad3ed7 100644 --- a/backend/src/core/proxy-utils/producers/clash.js +++ b/backend/src/core/proxy-utils/producers/clash.js @@ -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)) { diff --git a/backend/src/core/proxy-utils/producers/clashmeta.js b/backend/src/core/proxy-utils/producers/clashmeta.js index 7e0b46df..d335de9b 100644 --- a/backend/src/core/proxy-utils/producers/clashmeta.js +++ b/backend/src/core/proxy-utils/producers/clashmeta.js @@ -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)) { diff --git a/backend/src/core/proxy-utils/producers/egern.js b/backend/src/core/proxy-utils/producers/egern.js index 487f404b..0888a49f 100644 --- a/backend/src/core/proxy-utils/producers/egern.js +++ b/backend/src/core/proxy-utils/producers/egern.js @@ -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; + } + } +} diff --git a/backend/src/core/proxy-utils/producers/shadowrocket.js b/backend/src/core/proxy-utils/producers/shadowrocket.js index 14d87e43..9606e9f0 100644 --- a/backend/src/core/proxy-utils/producers/shadowrocket.js +++ b/backend/src/core/proxy-utils/producers/shadowrocket.js @@ -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)) { diff --git a/backend/src/core/proxy-utils/producers/stash.js b/backend/src/core/proxy-utils/producers/stash.js index 086d6ba3..b3795647 100644 --- a/backend/src/core/proxy-utils/producers/stash.js +++ b/backend/src/core/proxy-utils/producers/stash.js @@ -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)) { diff --git a/backend/src/core/proxy-utils/producers/uri.js b/backend/src/core/proxy-utils/producers/uri.js index 603425bd..6c2ccc6f 100644 --- a/backend/src/core/proxy-utils/producers/uri.js +++ b/backend/src/core/proxy-utils/producers/uri.js @@ -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 = diff --git a/backend/src/test/proxy-parsers/v2ray-and-platforms.spec.js b/backend/src/test/proxy-parsers/v2ray-and-platforms.spec.js index e01d4f4e..86ae0eba 100644 --- a/backend/src/test/proxy-parsers/v2ray-and-platforms.spec.js +++ b/backend/src/test/proxy-parsers/v2ray-and-platforms.spec.js @@ -151,12 +151,12 @@ describe('VMess and VLESS parser coverage', function () { }); }); - it('parses V2rayN http shares and normalizes http paths', function () { + it('parses V2rayN http transport shares as h2', function () { const share = Base64.encode( JSON.stringify({ - ps: 'VMess HTTP', + ps: 'VMess H2', add: 'vmess-http.example.com', - port: '80', + port: '443', id: UUID, aid: '1', scy: 'unknown-cipher', @@ -169,8 +169,41 @@ describe('VMess and VLESS parser coverage', function () { expectSubset(proxy, { type: 'vmess', - name: 'VMess HTTP', + name: 'VMess H2', server: 'vmess-http.example.com', + port: 443, + uuid: UUID, + alterId: 1, + cipher: 'auto', + network: 'h2', + 'h2-opts': { + host: ['h1.example.com', 'h2.example.com'], + path: '/a,/b', + }, + }); + }); + + it('parses V2rayN tcp fake-http shares as http header transport', function () { + const share = Base64.encode( + JSON.stringify({ + ps: 'VMess HTTP Header', + add: 'vmess-http-header.example.com', + port: '80', + id: UUID, + aid: '1', + scy: 'auto', + net: 'tcp', + type: 'http', + host: 'h1.example.com,h2.example.com', + path: '/a,/b', + }), + ); + const proxy = parseOne(`vmess://${share}`); + + expectSubset(proxy, { + type: 'vmess', + name: 'VMess HTTP Header', + server: 'vmess-http-header.example.com', port: 80, uuid: UUID, alterId: 1, @@ -796,7 +829,7 @@ describe('VMess and VLESS parser coverage', function () { it('parses h2 VLESS shares from share-link http transport', function () { const proxy = parseOne( - `vless://${UUID}@vless-h2.example.com:443?type=http&host=h2.example.com&path=%2Fh2&h2=1&packetEncoding=none#VLESS%20H2`, + `vless://${UUID}@vless-h2.example.com:443?type=http&host=h2.example.com,h2-alt.example.com&path=%2Fh2&h2=1&packetEncoding=none#VLESS%20H2`, ); expectSubset(proxy, { @@ -808,15 +841,38 @@ describe('VMess and VLESS parser coverage', function () { network: 'h2', _h2: true, 'h2-opts': { - headers: { - host: ['h2.example.com'], - }, + host: ['h2.example.com', 'h2-alt.example.com'], path: '/h2', }, }); expect(proxy).to.not.have.property('xudp'); }); + it('keeps non-Host h2 headers while lifting Host into h2-opts host', function () { + const headers = encodeURIComponent( + JSON.stringify({ + Host: 'cdn.example.com', + 'User-Agent': 'curl/7.77.0', + }), + ); + const proxy = parseOne( + `vless://${UUID}@vless-h2.example.com:443?type=http&obfsParam=${headers}&path=%2Fh2#VLESS%20H2%20Headers`, + ); + + expectSubset(proxy, { + type: 'vless', + name: 'VLESS H2 Headers', + network: 'h2', + 'h2-opts': { + headers: { + 'User-Agent': 'curl/7.77.0', + }, + host: ['cdn.example.com'], + path: '/h2', + }, + }); + }); + it('parses xhttp VLESS shares with mihomo transport extras', function () { const extra = JSON.stringify({ noGRPCHeader: true, diff --git a/backend/src/test/proxy-producers/structured.spec.js b/backend/src/test/proxy-producers/structured.spec.js index 19537d55..e7de85ec 100644 --- a/backend/src/test/proxy-producers/structured.spec.js +++ b/backend/src/test/proxy-producers/structured.spec.js @@ -1781,6 +1781,424 @@ describe('Proxy structured producers', function () { }); }); + it('emits Egern fingerprint_sha256 for supported TLS proxy types and transports', function () { + const fingerprint = 'SHA256:FINGERPRINT'; + const proxies = [ + { + type: 'http', + name: 'Egern HTTPS Fingerprint', + server: 'https.example.com', + port: 443, + tls: true, + sni: 'sni.example.com', + 'tls-fingerprint': fingerprint, + }, + { + type: 'https', + name: 'Egern Direct HTTPS Fingerprint', + server: 'direct-https.example.com', + port: 443, + sni: 'direct-sni.example.com', + 'tls-fingerprint': fingerprint, + }, + { + type: 'trojan', + name: 'Egern Trojan Fingerprint', + server: 'trojan.example.com', + port: 443, + password: 'secret', + 'tls-fingerprint': fingerprint, + }, + { + type: 'anytls', + name: 'Egern AnyTLS Fingerprint', + server: 'anytls.example.com', + port: 443, + password: 'secret', + 'tls-fingerprint': fingerprint, + }, + { + type: 'hysteria2', + name: 'Egern Hysteria2 Fingerprint', + server: 'hy2.example.com', + port: 443, + password: 'secret', + 'tls-fingerprint': fingerprint, + }, + { + type: 'tuic', + name: 'Egern TUIC Fingerprint', + server: 'tuic.example.com', + port: 443, + uuid: UUID, + password: 'secret', + 'tls-fingerprint': fingerprint, + }, + { + type: 'vmess', + name: 'Egern VMess H2 Fingerprint', + server: 'vmess-h2.example.com', + port: 443, + uuid: UUID, + cipher: 'auto', + tls: true, + network: 'h2', + sni: 'vmess-h2.example.com', + 'h2-opts': { + path: '/h2', + host: ['h2.example.com'], + headers: { + Host: 'fallback-h2.example.com', + b: 'a,b', + }, + }, + 'tls-fingerprint': fingerprint, + }, + { + type: 'vmess', + name: 'Egern VMess TLS Fingerprint', + server: 'vmess-tls.example.com', + port: 443, + uuid: UUID, + cipher: 'auto', + tls: true, + network: 'tcp', + sni: 'vmess-tls.example.com', + 'tls-fingerprint': fingerprint, + }, + { + type: 'vmess', + name: 'Egern VMess WSS Fingerprint', + server: 'vmess-wss.example.com', + port: 443, + uuid: UUID, + cipher: 'auto', + tls: true, + network: 'ws', + sni: 'vmess-wss.example.com', + 'ws-opts': { + path: '/ws', + headers: { + Host: 'cdn.example.com', + }, + }, + 'tls-fingerprint': fingerprint, + }, + { + type: 'vless', + name: 'Egern VLESS H2 Fingerprint', + server: 'vless-h2.example.com', + port: 443, + uuid: UUID, + tls: true, + network: 'h2', + sni: 'vless-h2.example.com', + 'h2-opts': { + path: '/h2', + host: ['h2.example.com'], + headers: { + Host: 'fallback-h2.example.com', + b: 'a,b', + }, + }, + 'tls-fingerprint': fingerprint, + }, + { + type: 'vless', + name: 'Egern VLESS TLS Fingerprint', + server: 'vless-tls.example.com', + port: 443, + uuid: UUID, + tls: true, + network: 'tcp', + sni: 'vless-tls.example.com', + 'tls-fingerprint': fingerprint, + }, + { + type: 'vless', + name: 'Egern VLESS WSS Fingerprint', + server: 'vless-wss.example.com', + port: 443, + uuid: UUID, + tls: true, + network: 'ws', + sni: 'vless-wss.example.com', + 'ws-opts': { + path: '/ws', + headers: { + Host: 'cdn.example.com', + }, + }, + 'tls-fingerprint': fingerprint, + }, + ]; + const getProxy = (items, type, name) => + items.find((item) => item[type]?.name === name)?.[type]; + + const internal = produceInternal('Egern', proxies); + const external = loadProducedYaml('Egern', proxies); + + for (const output of [internal, external.proxies]) { + for (const [type, name] of [ + ['https', 'Egern HTTPS Fingerprint'], + ['https', 'Egern Direct HTTPS Fingerprint'], + ['trojan', 'Egern Trojan Fingerprint'], + ['anytls', 'Egern AnyTLS Fingerprint'], + ['hysteria2', 'Egern Hysteria2 Fingerprint'], + ['tuic', 'Egern TUIC Fingerprint'], + ]) { + const producedProxy = getProxy(output, type, name); + expectSubset(producedProxy, { + fingerprint_sha256: fingerprint, + }); + expect(producedProxy).to.not.have.property('tls-fingerprint'); + } + + for (const [type, name, transport] of [ + ['vmess', 'Egern VMess H2 Fingerprint', 'http2'], + ['vmess', 'Egern VMess TLS Fingerprint', 'tls'], + ['vmess', 'Egern VMess WSS Fingerprint', 'wss'], + ['vless', 'Egern VLESS H2 Fingerprint', 'http2'], + ['vless', 'Egern VLESS TLS Fingerprint', 'tls'], + ['vless', 'Egern VLESS WSS Fingerprint', 'wss'], + ]) { + const producedProxy = getProxy(output, type, name); + expectSubset(producedProxy, { + transport: { + [transport]: { + fingerprint_sha256: fingerprint, + }, + }, + }); + expect(producedProxy).to.not.have.property('tls-fingerprint'); + } + + expectSubset( + getProxy(output, 'vmess', 'Egern VMess H2 Fingerprint'), + { + transport: { + http2: { + headers: { + Host: 'h2.example.com', + b: 'a,b', + }, + sni: 'vmess-h2.example.com', + }, + }, + }, + ); + expectSubset( + getProxy(output, 'vless', 'Egern VLESS H2 Fingerprint'), + { + transport: { + http2: { + headers: { + Host: 'h2.example.com', + b: 'a,b', + }, + sni: 'vless-h2.example.com', + }, + }, + }, + ); + } + }); + + it('omits Egern fingerprint_sha256 for blank fingerprints and non-TLS transports', function () { + const proxies = [ + { + type: 'http', + name: 'Egern HTTP Plain Fingerprint', + server: 'http.example.com', + port: 8080, + 'tls-fingerprint': 'SHA256:FINGERPRINT', + }, + { + type: 'https', + name: 'Egern HTTPS Blank Fingerprint', + server: 'https.example.com', + port: 443, + 'tls-fingerprint': ' ', + }, + { + type: 'vmess', + name: 'Egern VMess HTTP1 Fingerprint', + server: 'vmess-http.example.com', + port: 80, + uuid: UUID, + cipher: 'auto', + network: 'http', + 'tls-fingerprint': 'SHA256:FINGERPRINT', + }, + { + type: 'vmess', + name: 'Egern VMess WS Fingerprint', + server: 'vmess-ws.example.com', + port: 80, + uuid: UUID, + cipher: 'auto', + network: 'ws', + 'ws-opts': { + path: '/ws', + }, + 'tls-fingerprint': 'SHA256:FINGERPRINT', + }, + { + type: 'vmess', + name: 'Egern VMess TCP Fingerprint', + server: 'vmess-tcp.example.com', + port: 80, + uuid: UUID, + cipher: 'auto', + network: 'tcp', + 'tls-fingerprint': 'SHA256:FINGERPRINT', + }, + { + type: 'vless', + name: 'Egern VLESS HTTP1 Fingerprint', + server: 'vless-http.example.com', + port: 80, + uuid: UUID, + network: 'http', + 'tls-fingerprint': 'SHA256:FINGERPRINT', + }, + { + type: 'vless', + name: 'Egern VLESS WS Fingerprint', + server: 'vless-ws.example.com', + port: 80, + uuid: UUID, + network: 'ws', + 'ws-opts': { + path: '/ws', + }, + 'tls-fingerprint': 'SHA256:FINGERPRINT', + }, + { + type: 'vless', + name: 'Egern VLESS TCP Fingerprint', + server: 'vless-tcp.example.com', + port: 80, + uuid: UUID, + network: 'tcp', + 'tls-fingerprint': 'SHA256:FINGERPRINT', + }, + ]; + const findByName = (items, name) => + Object.values( + items.find((item) => + Object.values(item).some((proxy) => proxy?.name === name), + ), + )[0]; + const expectNoFingerprint = (proxy) => { + expect(proxy).to.not.have.property('fingerprint_sha256'); + expect(proxy).to.not.have.property('tls-fingerprint'); + if (proxy.transport) { + for (const transport of Object.values(proxy.transport)) { + expect(transport).to.not.have.property( + 'fingerprint_sha256', + ); + } + } + }; + + const internal = produceInternal('Egern', proxies); + const external = loadProducedYaml('Egern', proxies); + + for (const output of [internal, external.proxies]) { + for (const proxy of proxies) { + expectNoFingerprint(findByName(output, proxy.name)); + } + } + }); + + it('trims Egern fingerprint_sha256 values', function () { + const external = loadProducedYaml('Egern', { + type: 'https', + name: 'Egern HTTPS Trimmed Fingerprint', + server: 'https.example.com', + port: 443, + 'tls-fingerprint': ' SHA256:FINGERPRINT ', + }); + + expect(external.proxies[0].https.fingerprint_sha256).to.equal( + 'SHA256:FINGERPRINT', + ); + }); + + it('keeps Mihomo h2 headers while lifting Host into h2-opts host', function () { + const input = `proxies: + - name: vmess-h2 + type: vmess + server: server + port: 443 + uuid: ${UUID} + alterId: 32 + cipher: auto + network: h2 + tls: true + fingerprint: xxxx + h2-opts: + host: + - http.example.com + - http-alt.example.com + headers: + Host: v2ray.com + b: a + path: +`; + const [proxy] = ProxyUtils.parse(input); + const external = loadProducedYaml('Mihomo', [proxy]); + const h2Opts = external.proxies[0]['h2-opts']; + + expect(h2Opts).to.deep.equal({ + host: ['http.example.com', 'http-alt.example.com'], + headers: { + b: 'a', + }, + path: '/', + }); + expect(external.proxies[0].servername).to.equal('http.example.com'); + expect(h2Opts.headers).to.not.have.property('Host'); + expect(h2Opts.headers).to.not.have.property('host'); + }); + + it('keeps h2 Host arrays when lifting legacy headers into h2-opts host', function () { + const proxy = { + type: 'vmess', + name: 'VMess H2 Legacy Host Array', + server: 'server', + port: 443, + uuid: UUID, + alterId: 0, + cipher: 'auto', + tls: true, + network: 'h2', + 'h2-opts': { + path: '/', + headers: { + Host: ['cdn.example.com', 'alt.example.com'], + 'User-Agent': 'curl/7.77.0', + }, + }, + }; + + for (const platform of ['Clash', 'Mihomo', 'Stash', 'Shadowrocket']) { + const external = loadProducedYaml(platform, proxy); + const h2Opts = external.proxies[0]['h2-opts']; + + expect(h2Opts).to.deep.equal({ + host: ['cdn.example.com', 'alt.example.com'], + headers: { + 'User-Agent': 'curl/7.77.0', + }, + path: '/', + }); + expect(h2Opts.headers).to.not.have.property('Host'); + expect(h2Opts.headers).to.not.have.property('host'); + } + }); + it('keeps Mihomo HTTP headers and filters unsupported h2-connect/trusttunnel header variants', function () { const { result, errors } = captureErrors(() => produceInternal('Mihomo', [ diff --git a/backend/src/test/proxy-producers/text.spec.js b/backend/src/test/proxy-producers/text.spec.js index 24d133f3..27146f98 100644 --- a/backend/src/test/proxy-producers/text.spec.js +++ b/backend/src/test/proxy-producers/text.spec.js @@ -1952,6 +1952,31 @@ describe('Proxy text producers', function () { ); }); + it('produces URI VMess h2 links from mihomo h2-opts host', function () { + const output = produceExternal('URI', { + type: 'vmess', + name: 'URI VMess H2', + server: 'vmess-h2.example.com', + port: 443, + uuid: UUID, + cipher: 'auto', + alterId: 0, + tls: true, + network: 'h2', + 'h2-opts': { + path: '/h2', + host: ['h2.example.com'], + }, + }); + const payload = JSON.parse( + Base64.decode(output.replace(/^vmess:\/\//, '')), + ); + + expect(payload.net).to.equal('h2'); + expect(payload.path).to.equal('/h2'); + expect(payload.host).to.equal('h2.example.com'); + }); + it('produces URI VLESS h2 links using share-link http transport type', function () { const output = produceExternal('URI', { type: 'vless', @@ -1965,9 +1990,7 @@ describe('Proxy text producers', function () { _h2: true, 'h2-opts': { path: '/h2', - headers: { - host: ['h2.example.com'], - }, + host: ['h2.example.com'], }, });