diff --git a/backend/src/core/proxy-utils/index.js b/backend/src/core/proxy-utils/index.js index 35216224..580f1dc8 100644 --- a/backend/src/core/proxy-utils/index.js +++ b/backend/src/core/proxy-utils/index.js @@ -265,15 +265,33 @@ function produce(proxies, targetPlatform, type, opts = {}) { // filter unsupported proxies proxies = proxies.filter((proxy) => { + const includeUnsupportedProxy = opts['include-unsupported-proxy']; + // 检查代理是否支持目标平台 - if (proxy.supported && proxy.supported[targetPlatform] === false) { + if ( + !includeUnsupportedProxy && + proxy.supported && + proxy.supported[targetPlatform] === false + ) { return false; } if ( + !includeUnsupportedProxy && + hasRootProxyHeaders(proxy) && + isRootHeaderSensitiveProxy(proxy) && + !supportsRootProxyHeaders(proxy, targetPlatform) + ) { + $.error( + `Target platform ${targetPlatform} does not support headers for ${getRootHeaderProxyLabel(proxy)} proxy ${proxy.name || `${proxy.server}:${proxy.port}`}. Proxy has been filtered.`, + ); + return false; + } + + if ( + !includeUnsupportedProxy && isShadowsocksOverTls(proxy) && - !supportedShadowsocksOverTlsTargets.has(normalizedTarget) && - !opts['include-unsupported-proxy'] + !supportedShadowsocksOverTlsTargets.has(normalizedTarget) ) { return false; } @@ -439,6 +457,64 @@ ${list}`; } } +function hasRootProxyHeaders(proxy) { + return ( + proxy?.headers && + typeof proxy.headers === 'object' && + Object.keys(proxy.headers).length > 0 + ); +} + +function isRootHeaderSensitiveProxy(proxy) { + return ['http', 'h2-connect', 'trusttunnel'].includes(proxy?.type); +} + +function supportsRootProxyHeaders(proxy, targetPlatform) { + const normalizedTarget = `${targetPlatform}`.toLowerCase(); + + if (normalizedTarget.startsWith('surge')) { + return ['http', 'h2-connect', 'trusttunnel'].includes(proxy.type); + } + + if (normalizedTarget === 'egern') { + return proxy.type === 'http'; + } + + if ( + ['clashmeta', 'clash.meta', 'meta', 'mihomo'].includes( + normalizedTarget, + ) + ) { + return proxy.type === 'http'; + } + + if (['singbox', 'sing-box'].includes(normalizedTarget)) { + return proxy.type === 'http'; + } + + if (normalizedTarget === 'json') { + return ['http', 'h2-connect', 'trusttunnel'].includes(proxy.type); + } + + return false; +} + +function getRootHeaderProxyLabel(proxy) { + if (proxy.type === 'http') { + return proxy.tls ? 'HTTPS' : 'HTTP'; + } + + if (proxy.type === 'h2-connect') { + return 'HTTP/2 CONNECT'; + } + + if (proxy.type === 'trusttunnel') { + return 'TrustTunnel'; + } + + return proxy.type; +} + export const ProxyUtils = { parse, process: processFn, @@ -609,6 +685,7 @@ function lastParse(proxy) { 'juicity', 'anytls', 'trusttunnel', + 'h2-connect', 'naive', ].includes(proxy.type) ) { diff --git a/backend/src/core/proxy-utils/parsers/index.js b/backend/src/core/proxy-utils/parsers/index.js index ba262f30..63768c63 100644 --- a/backend/src/core/proxy-utils/parsers/index.js +++ b/backend/src/core/proxy-utils/parsers/index.js @@ -2387,6 +2387,7 @@ function Clash_All() { 'openvpn', 'tailscale', 'trusttunnel', + 'h2-connect', 'naive', 'anytls', 'mieru', @@ -2747,6 +2748,14 @@ function Surge_TrustTunnel() { const parse = (line) => getSurgeParser().parse(line); return { name, test, parse }; } +function Surge_H2Connect() { + const name = 'Surge HTTP/2 CONNECT Parser'; + const test = (line) => { + return /^.*=\s*h2-connect/.test(line.split(',')[0]); + }; + const parse = (line) => getSurgeParser().parse(line); + return { name, test, parse }; +} function Surge_SSH() { const name = 'Surge SSH Parser'; const test = (line) => { @@ -2951,6 +2960,7 @@ export default [ Surge_Direct(), Surge_AnyTLS(), Surge_TrustTunnel(), + Surge_H2Connect(), Surge_SSH(), Surge_SS(), Surge_VMess(), diff --git a/backend/src/core/proxy-utils/parsers/peggy/surge.js b/backend/src/core/proxy-utils/parsers/peggy/surge.js index ec8c22fb..d3897661 100644 --- a/backend/src/core/proxy-utils/parsers/peggy/surge.js +++ b/backend/src/core/proxy-utils/parsers/peggy/surge.js @@ -35,9 +35,28 @@ const grammars = String.raw` proxy['shadow-tls-version'] = 2; } } + function parseHeaders(headers) { + const result = {}; + headers.split(";").forEach((pair) => { + const index = pair.indexOf(":"); + if (index === -1) return; + + const key = pair.slice(0, index).trim(); + const value = pair + .slice(index + 1) + .trim() + .replace(/^"(.*?)"$/, "$1") + .replace(/^'(.*?)'$/, "$1"); + + if (key) { + result[key] = value; + } + }); + return result; + } } -start = (anytls/shadowsocks/vmess/trojan/https/http/snell/socks5/socks5_tls/tuic/tuic_v5/wireguard/hysteria2/ssh/trust_tunnel/direct) { +start = (anytls/shadowsocks/vmess/trojan/h2_connect/https/http/snell/socks5/socks5_tls/tuic/tuic_v5/wireguard/hysteria2/ssh/trust_tunnel/direct) { return proxy; } @@ -69,12 +88,17 @@ trojan = tag equals "trojan" address (passwordk/ws/ws_path/ws_headers/tls/sni/tl handleWebsocket(); handleShadowTLS(); } -https = tag equals "https" address (username password)? (usernamek passwordk)? (sni/tls_fingerprint/tls_verification/ip_version/underlying_proxy/tos/allow_other_interface/interface/test_url/test_udp/test_timeout/hybrid/no_error_alert/fast_open/tfo/shadow_tls_version/shadow_tls_sni/shadow_tls_password/block_quic/others)* { +https = tag equals "https" address (username password)? (usernamek passwordk)? (headers/sni/tls_fingerprint/tls_verification/ip_version/underlying_proxy/tos/allow_other_interface/interface/test_url/test_udp/test_timeout/hybrid/no_error_alert/fast_open/tfo/shadow_tls_version/shadow_tls_sni/shadow_tls_password/block_quic/others)* { proxy.type = "http"; proxy.tls = true; handleShadowTLS(); } -http = tag equals "http" address (username password)? (usernamek passwordk)? (ip_version/underlying_proxy/tos/allow_other_interface/interface/test_url/test_udp/test_timeout/hybrid/no_error_alert/fast_open/tfo/shadow_tls_version/shadow_tls_sni/shadow_tls_password/block_quic/others)* { +h2_connect = tag equals "h2-connect" address (username password)? (usernamek passwordk)? (headers/sni/tls_fingerprint/tls_verification/ip_version/underlying_proxy/tos/allow_other_interface/interface/test_url/test_udp/test_timeout/hybrid/no_error_alert/fast_open/tfo/shadow_tls_version/shadow_tls_sni/shadow_tls_password/block_quic/others)* { + proxy.type = "h2-connect"; + proxy.tls = true; + handleShadowTLS(); +} +http = tag equals "http" address (username password)? (usernamek passwordk)? (headers/ip_version/underlying_proxy/tos/allow_other_interface/interface/test_url/test_udp/test_timeout/hybrid/no_error_alert/fast_open/tfo/shadow_tls_version/shadow_tls_sni/shadow_tls_password/block_quic/others)* { proxy.type = "http"; handleShadowTLS(); } @@ -122,7 +146,7 @@ anytls = tag equals "anytls" address (passwordk/reuse/ip_version/underlying_prox proxy.type = "anytls"; proxy.tls = true; } -trust_tunnel = tag equals "trust-tunnel" address (usernamek/passwordk/reuse/ip_version/underlying_proxy/tos/allow_other_interface/interface/test_url/test_udp/test_timeout/hybrid/no_error_alert/tls_fingerprint/tls_verification/sni/fast_open/tfo/block_quic/others)* { +trust_tunnel = tag equals "trust-tunnel" address (usernamek/passwordk/headers/reuse/ip_version/underlying_proxy/tos/allow_other_interface/interface/test_url/test_udp/test_timeout/hybrid/no_error_alert/tls_fingerprint/tls_verification/sni/fast_open/tfo/block_quic/others)* { proxy.type = "trusttunnel"; proxy.tls = true; } @@ -226,6 +250,7 @@ ws_headers = comma "ws-headers" equals headers:$[^,]+ { obfs["ws-headers"] = result; } ws_path = comma "ws-path" equals path:uri { obfs.path = path.trim().replace(/^"(.*?)"$/, '$1').replace(/^'(.*?)'$/, '$1'); } +headers = comma "headers" equals headers:$[^,]+ { proxy.headers = parseHeaders(headers); } obfs = comma "obfs" equals type:("http"/"tls") { obfs.type = type; } obfs_host = comma "obfs-host" equals match:[^,]+ { obfs.host = match.join("").replace(/^"(.*)"$/, '$1'); }; diff --git a/backend/src/core/proxy-utils/producers/clashmeta.js b/backend/src/core/proxy-utils/producers/clashmeta.js index 3078384d..787bd0fd 100644 --- a/backend/src/core/proxy-utils/producers/clashmeta.js +++ b/backend/src/core/proxy-utils/producers/clashmeta.js @@ -52,6 +52,19 @@ export default function ClashMeta_Producer() { const list = proxies .filter((proxy) => { if (opts['include-unsupported-proxy']) return true; + + if (proxy.type === 'h2-connect') { + $.error( + `Mihomo does not support HTTP/2 CONNECT proxy type. Proxy ${proxy.name} has been filtered.`, + ); + return false; + } + if (hasRootHeaders(proxy) && proxy.type === 'trusttunnel') { + $.error( + `Mihomo does not support headers for TrustTunnel proxy ${proxy.name}. Proxy has been filtered.`, + ); + return false; + } if (!supportsShadowsocksV2rayPluginMode(proxy, ['websocket'])) { return false; } else if (proxy.type === 'snell' && proxy.version >= 4) { @@ -366,3 +379,11 @@ export default function ClashMeta_Producer() { }; return { type, produce }; } + +function hasRootHeaders(proxy) { + return ( + proxy?.headers && + typeof proxy.headers === 'object' && + Object.keys(proxy.headers).length > 0 + ); +} diff --git a/backend/src/core/proxy-utils/producers/egern.js b/backend/src/core/proxy-utils/producers/egern.js index 22c8bf68..b6e248c7 100644 --- a/backend/src/core/proxy-utils/producers/egern.js +++ b/backend/src/core/proxy-utils/producers/egern.js @@ -117,6 +117,11 @@ export default function Egern_Producer() { 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, ...(proxy.tls @@ -590,3 +595,11 @@ export default function Egern_Producer() { }; return { type, produce }; } + +function hasHeaders(proxy) { + return ( + proxy?.headers && + typeof proxy.headers === 'object' && + Object.keys(proxy.headers).length > 0 + ); +} diff --git a/backend/src/core/proxy-utils/producers/surge.js b/backend/src/core/proxy-utils/producers/surge.js index c6f2c282..2127fc0e 100644 --- a/backend/src/core/proxy-utils/producers/surge.js +++ b/backend/src/core/proxy-utils/producers/surge.js @@ -46,6 +46,8 @@ export default function Surge_Producer() { return vmess(proxy, opts['include-unsupported-proxy']); case 'http': return http(proxy); + case 'h2-connect': + return h2Connect(proxy); case 'direct': return direct(proxy); case 'socks5': @@ -384,6 +386,7 @@ function trusttunnel(proxy) { result.append(`${proxy.name}=trust-tunnel,${proxy.server},${proxy.port}`); result.appendIfPresent(`,username="${proxy.username}"`, 'username'); result.appendIfPresent(`,password="${proxy.password}"`, 'password'); + appendHeaders(result, proxy); const ip_version = ipVersions[proxy['ip-version']] || proxy['ip-version']; result.appendIfPresent(`,ip-version=${ip_version}`, 'ip-version'); @@ -445,6 +448,75 @@ function trusttunnel(proxy) { return result.toString(); } +function h2Connect(proxy) { + const result = new Result(proxy); + result.append(`${proxy.name}=h2-connect,${proxy.server},${proxy.port}`); + result.appendIfPresent(`,username="${proxy.username}"`, 'username'); + result.appendIfPresent(`,password="${proxy.password}"`, 'password'); + appendHeaders(result, proxy); + + const ip_version = ipVersions[proxy['ip-version']] || proxy['ip-version']; + result.appendIfPresent(`,ip-version=${ip_version}`, 'ip-version'); + + result.appendIfPresent( + `,no-error-alert=${proxy['no-error-alert']}`, + 'no-error-alert', + ); + + result.appendIfPresent( + `,server-cert-fingerprint-sha256=${proxy['tls-fingerprint']}`, + 'tls-fingerprint', + ); + result.appendIfPresent(`,sni="${proxy.sni}"`, 'sni'); + result.appendIfPresent( + `,skip-cert-verify=${proxy['skip-cert-verify']}`, + 'skip-cert-verify', + ); + + if (proxy.tfo) { + $.info(`Option tfo is not supported by Surge, thus omitted`); + } + + result.appendIfPresent(`,udp-relay=${proxy.udp}`, 'udp'); + result.appendIfPresent(`,test-url=${proxy['test-url']}`, 'test-url'); + result.appendIfPresent( + `,test-timeout=${proxy['test-timeout']}`, + 'test-timeout', + ); + result.appendIfPresent(`,test-udp=${proxy['test-udp']}`, 'test-udp'); + result.appendIfPresent(`,hybrid=${proxy['hybrid']}`, 'hybrid'); + result.appendIfPresent(`,tos=${proxy['tos']}`, 'tos'); + result.appendIfPresent( + `,allow-other-interface=${proxy['allow-other-interface']}`, + 'allow-other-interface', + ); + result.appendIfPresent( + `,interface=${proxy['interface-name']}`, + 'interface-name', + ); + result.appendIfPresent(`,interface=${proxy['interface']}`, 'interface'); + + if (isPresent(proxy, 'shadow-tls-password')) { + result.append(`,shadow-tls-password=${proxy['shadow-tls-password']}`); + + result.appendIfPresent( + `,shadow-tls-version=${proxy['shadow-tls-version']}`, + 'shadow-tls-version', + ); + result.appendIfPresent( + `,shadow-tls-sni=${proxy['shadow-tls-sni']}`, + 'shadow-tls-sni', + ); + } + + result.appendIfPresent(`,block-quic=${proxy['block-quic']}`, 'block-quic'); + result.appendIfPresent( + `,underlying-proxy=${proxy['underlying-proxy']}`, + 'underlying-proxy', + ); + + return result.toString(); +} function vmess(proxy, includeUnsupportedProxy) { const result = new Result(proxy); @@ -603,14 +675,12 @@ function ssh(proxy) { return result.toString(); } function http(proxy) { - if (proxy.headers && Object.keys(proxy.headers).length > 0) { - throw unsupported(`headers is unsupported`); - } const result = new Result(proxy); const type = proxy.tls ? 'https' : 'http'; result.append(`${proxy.name}=${type},${proxy.server},${proxy.port}`); result.appendIfPresent(`,username="${proxy.username}"`, 'username'); result.appendIfPresent(`,password="${proxy.password}"`, 'password'); + appendHeaders(result, proxy); const ip_version = ipVersions[proxy['ip-version']] || proxy['ip-version']; result.appendIfPresent(`,ip-version=${ip_version}`, 'ip-version'); @@ -814,6 +884,24 @@ function socks5(proxy) { return result.toString(); } +function appendHeaders(result, proxy) { + const value = formatHeaders(proxy.headers); + if (isNotBlank(value)) { + result.append(`,headers=${value}`); + } +} + +function formatHeaders(headers) { + if (!headers || typeof headers !== 'object') { + return ''; + } + + return Object.entries(headers) + .filter(([key, value]) => isNotBlank(key) && value != null) + .map(([key, value]) => `${key}:${value}`) + .join(';'); +} + function snell(proxy) { const result = new Result(proxy); result.append(`${proxy.name}=${proxy.type},${proxy.server},${proxy.port}`); 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 129950df..6e649c53 100644 --- a/backend/src/test/proxy-parsers/v2ray-and-platforms.spec.js +++ b/backend/src/test/proxy-parsers/v2ray-and-platforms.spec.js @@ -2859,7 +2859,7 @@ describe('Platform raw-format parser coverage', function () { }, { title: 'parses trust-tunnel lines', - input: 'Surge TrustTunnel = trust-tunnel,surge-trust.example.com,443,username=user,password=secret,sni=sni.example.com,skip-cert-verify=true,reuse=true', + input: 'Surge TrustTunnel = trust-tunnel,surge-trust.example.com,443,username=user,password=secret,headers=X-Client:Surge;X-Token:abc,sni=sni.example.com,skip-cert-verify=true,reuse=true', expected: { type: 'trusttunnel', name: 'Surge TrustTunnel', @@ -2868,11 +2868,31 @@ describe('Platform raw-format parser coverage', function () { username: 'user', password: 'secret', tls: true, + headers: { + 'X-Client': 'Surge', + 'X-Token': 'abc', + }, sni: 'sni.example.com', 'skip-cert-verify': true, reuse: true, }, }, + { + title: 'parses h2-connect lines with dynamic headers', + input: 'Surge H2 = h2-connect,h2.example.com,443,headers=X-Padding:,sni=sni.example.com,skip-cert-verify=true', + expected: { + type: 'h2-connect', + name: 'Surge H2', + server: 'h2.example.com', + port: 443, + tls: true, + headers: { + 'X-Padding': '', + }, + sni: 'sni.example.com', + 'skip-cert-verify': true, + }, + }, { title: 'parses ssh lines', input: 'Surge SSH = ssh,surge-ssh.example.com,22,user,pass,server-fingerprint=sshfp', @@ -2951,7 +2971,7 @@ describe('Platform raw-format parser coverage', function () { }, { title: 'parses https auth lines', - input: 'Surge HTTPS = https,surge-http.example.com,8443,user,pass,sni=sni.example.com,skip-cert-verify=true', + input: 'Surge HTTPS = https,surge-http.example.com,8443,user,pass,headers=X-Token:abc,sni=sni.example.com,skip-cert-verify=true', expected: { type: 'http', name: 'Surge HTTPS', @@ -2960,6 +2980,9 @@ describe('Platform raw-format parser coverage', function () { username: 'user', password: 'pass', tls: true, + headers: { + 'X-Token': 'abc', + }, sni: 'sni.example.com', 'skip-cert-verify': true, }, diff --git a/backend/src/test/proxy-producers/structured.spec.js b/backend/src/test/proxy-producers/structured.spec.js index 9beffa22..7f5b99b8 100644 --- a/backend/src/test/proxy-producers/structured.spec.js +++ b/backend/src/test/proxy-producers/structured.spec.js @@ -24,6 +24,18 @@ function captureWarns(fn) { } } +function captureErrors(fn) { + const originalError = $.error; + const errors = []; + $.error = (message) => errors.push(message); + try { + const result = fn(); + return { result, errors }; + } finally { + $.error = originalError; + } +} + describe('Proxy structured producers', function () { it('filters unsupported Clash proxies by default and normalizes vmess ws early data', function () { const proxies = [ @@ -819,6 +831,31 @@ describe('Proxy structured producers', function () { expect(internal).to.have.length(0); }); + it('does not let include-unsupported-proxy bypass malformed VLESS Reality validation', function () { + const proxy = { + type: 'vless', + name: 'Reality Empty Key', + server: 'vless.example.com', + port: 443, + uuid: UUID, + tls: true, + network: 'tcp', + 'reality-opts': { 'public-key': '' }, + }; + + const { result, errors } = captureErrors(() => + produceInternal('Mihomo', proxy, { + 'include-unsupported-proxy': true, + }), + ); + + expect(result).to.have.length(0); + expect(errors).to.have.length(1); + expect(errors[0]).to.include( + 'Skipping VLESS Reality proxy Reality Empty Key: empty reality-opts.public-key', + ); + }); + it('normalizes Stash TUIC defaults and external yaml wrapper', function () { const proxy = { type: 'tuic', @@ -1240,6 +1277,217 @@ describe('Proxy structured producers', function () { }); }); + it('emits Egern HTTP and HTTPS root headers', function () { + const proxies = [ + { + type: 'http', + name: 'Egern HTTP Headers', + server: 'http.example.com', + port: 8080, + username: 'user', + password: 'pass', + headers: { + 'X-Client': 'Egern', + 'X-Token': 'abc', + }, + }, + { + type: 'http', + name: 'Egern HTTPS Headers', + server: 'https.example.com', + port: 443, + tls: true, + sni: 'sni.example.com', + headers: { + 'X-Padding': '', + }, + }, + ]; + + const internal = produceInternal('Egern', proxies); + const external = loadProducedYaml('Egern', proxies); + + expectSubset(internal[0], { + http: { + name: 'Egern HTTP Headers', + headers: { + 'X-Client': 'Egern', + 'X-Token': 'abc', + }, + }, + }); + expectSubset(internal[1], { + https: { + name: 'Egern HTTPS Headers', + headers: { + 'X-Padding': '', + }, + }, + }); + expectSubset(external.proxies[0], { + http: { + headers: { + 'X-Client': 'Egern', + 'X-Token': 'abc', + }, + }, + }); + expectSubset(external.proxies[1], { + https: { + headers: { + 'X-Padding': '', + }, + }, + }); + }); + + it('keeps Mihomo HTTP headers and filters unsupported h2-connect/trusttunnel header variants', function () { + const { result, errors } = captureErrors(() => + produceInternal('Mihomo', [ + { + type: 'http', + name: 'Mihomo HTTPS Headers', + server: 'https.example.com', + port: 443, + tls: true, + headers: { + 'X-Token': 'abc', + }, + }, + { + type: 'h2-connect', + name: 'Mihomo H2 Headers', + server: 'h2.example.com', + port: 443, + headers: { + 'X-Padding': '', + }, + }, + { + type: 'trusttunnel', + name: 'Mihomo Trust Headers', + server: 'trust.example.com', + port: 443, + headers: { + 'X-Client': 'Surge', + }, + }, + ]), + ); + + expect(result).to.have.length(1); + expectSubset(result[0], { + type: 'http', + name: 'Mihomo HTTPS Headers', + headers: { + 'X-Token': 'abc', + }, + }); + expect(errors).to.have.length(2); + expect(errors[0]).to.include( + 'Target platform Mihomo does not support headers for HTTP/2 CONNECT proxy Mihomo H2 Headers', + ); + expect(errors[1]).to.include( + 'Target platform Mihomo does not support headers for TrustTunnel proxy Mihomo Trust Headers', + ); + }); + + it('keeps Mihomo h2-connect/trusttunnel header variants when include-unsupported-proxy is enabled', function () { + const { result, errors } = captureErrors(() => + produceInternal( + 'Mihomo', + [ + { + type: 'h2-connect', + name: 'Mihomo H2 Headers', + server: 'h2.example.com', + port: 443, + headers: { + 'X-Padding': '', + }, + }, + { + type: 'trusttunnel', + name: 'Mihomo Trust Headers', + server: 'trust.example.com', + port: 443, + headers: { + 'X-Client': 'Surge', + }, + }, + ], + { 'include-unsupported-proxy': true }, + ), + ); + + expect(result).to.have.length(2); + expectSubset(result[0], { + type: 'h2-connect', + name: 'Mihomo H2 Headers', + headers: { + 'X-Padding': '', + }, + }); + expectSubset(result[1], { + type: 'trusttunnel', + name: 'Mihomo Trust Headers', + headers: { + 'X-Client': 'Surge', + }, + }); + expect(errors).to.have.length(0); + }); + + it('preserves supported HTTP root headers for sing-box and JSON outputs', function () { + const buildProxy = (name) => ({ + type: 'http', + name, + server: 'http.example.com', + port: 8080, + username: 'user', + password: 'pass', + headers: { + 'X-Token': 'abc', + }, + }); + + const singBoxInternal = produceInternal( + 'sing-box', + buildProxy('sing-box HTTP Headers'), + ); + const singBoxExternal = loadProducedJson( + 'sing-box', + buildProxy('sing-box HTTP Headers'), + ); + const jsonExternal = loadProducedJson( + 'JSON', + buildProxy('JSON HTTP Headers'), + ); + + expect(singBoxInternal).to.have.length(1); + expectSubset(singBoxInternal[0], { + type: 'http', + tag: 'sing-box HTTP Headers', + headers: { + 'X-Token': 'abc', + }, + }); + expectSubset(singBoxExternal.outbounds[0], { + type: 'http', + tag: 'sing-box HTTP Headers', + headers: { + 'X-Token': 'abc', + }, + }); + expectSubset(jsonExternal[0], { + type: 'http', + name: 'JSON HTTP Headers', + headers: { + 'X-Token': 'abc', + }, + }); + }); + it('maps shadowsocks obfs plugin fields into Egern nested structures', function () { const proxy = { type: 'ss', diff --git a/backend/src/test/proxy-producers/text.spec.js b/backend/src/test/proxy-producers/text.spec.js index ba615674..0213ead8 100644 --- a/backend/src/test/proxy-producers/text.spec.js +++ b/backend/src/test/proxy-producers/text.spec.js @@ -19,6 +19,18 @@ function captureWarns(fn) { } } +function captureErrors(fn) { + const originalError = $.error; + const errors = []; + $.error = (message) => errors.push(message); + try { + const result = fn(); + return { result, errors }; + } finally { + $.error = originalError; + } +} + describe('Proxy text producers', function () { it('produces Quantumult X shadowsocks over-tls lines from canonical tls nodes', function () { const output = produceExternal('QX', { @@ -441,6 +453,122 @@ describe('Proxy text producers', function () { ); }); + it('produces Surge root headers for HTTP, HTTPS, HTTP/2 CONNECT, and TrustTunnel', function () { + const output = ProxyUtils.produce( + [ + { + type: 'http', + name: 'Surge HTTP Headers', + server: 'http.example.com', + port: 8080, + username: 'user', + password: 'pass', + headers: { + 'X-Client': 'Surge', + 'X-Token': 'abc', + }, + }, + { + type: 'http', + name: 'Surge HTTPS Headers', + server: 'https.example.com', + port: 443, + tls: true, + sni: 'sni.example.com', + headers: { + 'X-Padding': '', + }, + }, + { + type: 'h2-connect', + name: 'Surge H2 Headers', + server: 'h2.example.com', + port: 443, + tls: true, + sni: 'sni.example.com', + headers: { + 'X-Padding': '', + }, + }, + { + type: 'trusttunnel', + name: 'Surge Trust Headers', + server: 'trust.example.com', + port: 443, + username: 'user', + password: 'pass', + headers: { + 'X-Client': 'Surge', + }, + sni: 'sni.example.com', + }, + ], + 'Surge', + 'external', + ); + + expect(output.split('\n')).to.deep.equal([ + 'Surge HTTP Headers=http,http.example.com,8080,username="user",password="pass",headers=X-Client:Surge;X-Token:abc', + 'Surge HTTPS Headers=https,https.example.com,443,headers=X-Padding:,sni="sni.example.com"', + 'Surge H2 Headers=h2-connect,h2.example.com,443,headers=X-Padding:,sni="sni.example.com"', + 'Surge Trust Headers=trust-tunnel,trust.example.com,443,username="user",password="pass",headers=X-Client:Surge,sni="sni.example.com"', + ]); + }); + + it('filters root proxy headers for unsupported text targets with an error log', function () { + const { result, errors } = captureErrors(() => + ProxyUtils.produce( + [ + { + type: 'http', + name: 'QX HTTPS Headers', + server: 'https.example.com', + port: 443, + tls: true, + headers: { + 'X-Token': 'abc', + }, + }, + ], + 'QX', + 'external', + ), + ); + + expect(result).to.equal(''); + expect(errors).to.have.length(1); + expect(errors[0]).to.include( + 'Target platform QX does not support headers for HTTPS proxy QX HTTPS Headers', + ); + }); + + it('keeps root proxy headers for unsupported text targets when include-unsupported-proxy is enabled', function () { + const { result, errors } = captureErrors(() => + ProxyUtils.produce( + [ + { + type: 'http', + name: 'QX HTTPS Headers', + server: 'https.example.com', + port: 443, + tls: true, + headers: { + 'X-Token': 'abc', + }, + }, + ], + 'QX', + 'external', + { 'include-unsupported-proxy': true }, + ), + ); + + expect(result).to.equal( + 'http=https.example.com:443,over-tls=true,tag=QX HTTPS Headers', + ); + expect(errors).to.have.length(0); + }); + it('produces Loon WireGuard lines without leaking CIDR metadata fields', function () { const output = produceExternal('Loon', { type: 'wireguard', diff --git a/docs/brainstorms/2026-05-15-h2-connect-proxy-headers-requirements.md b/docs/brainstorms/2026-05-15-h2-connect-proxy-headers-requirements.md new file mode 100644 index 00000000..45dd4669 --- /dev/null +++ b/docs/brainstorms/2026-05-15-h2-connect-proxy-headers-requirements.md @@ -0,0 +1,67 @@ +--- +date: 2026-05-15 +topic: h2-connect-proxy-headers +--- + +# HTTP/2 CONNECT Proxy Headers + +## Summary + +Sub-Store will preserve Surge HTTP/2 CONNECT proxies as canonical `h2-connect` nodes and round-trip custom proxy request headers for the Surge proxy types that now support them. Egern HTTP/HTTPS output will also carry root `headers` fields, while unsupported targets must not silently drop headers. + +--- + +## Problem Frame + +Surge now supports custom request headers on HTTP, HTTPS, HTTP/2 CONNECT, and TrustTunnel proxy definitions, including dynamic placeholders such as `` and ``. Without parser and producer support, these nodes either fail to round-trip or lose headers during conversion. Egern documentation also lists `headers` for HTTP and HTTPS proxies, but current output does not emit it. + +--- + +## Requirements + +**Protocol Coverage** +- R1. Surge input must parse `h2-connect` proxy lines into type `h2-connect`. +- R2. Surge output must emit type `h2-connect` for canonical `h2-connect` nodes. +- R3. Frontend protocol filtering must expose HTTP/2 CONNECT and use value `h2-connect`. + +**Header Handling** +- R4. Surge input and output must preserve root `headers` for HTTP, HTTPS, HTTP/2 CONNECT, and TrustTunnel proxies. +- R5. Header placeholder text such as `` and `` must round-trip as literal configuration text. +- R6. Egern HTTP and HTTPS output must include root `headers` when present. +- R7. Outputs that cannot represent root headers for HTTP/HTTPS, HTTP/2 CONNECT, or TrustTunnel must log and filter or error instead of silently dropping those headers. + +**Compatibility** +- R8. Existing proxy types and transport-layer headers, such as WebSocket or VMess/VLESS h2 transport headers, must keep current behavior. +- R9. Mihomo support must be based on local source verification, not assumption. + +--- + +## Acceptance Examples + +- AE1. **Covers R1, R4, R5.** Given `Proxy = h2-connect, example.com, 443, headers=X-Padding:`, when Surge input is parsed, the node type is `h2-connect` and the header value remains unchanged. +- AE2. **Covers R2, R4.** Given a Surge HTTP, HTTPS, HTTP/2 CONNECT, or TrustTunnel node with root headers, when Surge output is generated, the line contains `headers=...`. +- AE3. **Covers R6.** Given an Egern HTTP or HTTPS node with root headers, when Egern output is generated, the YAML object includes `headers`. +- AE4. **Covers R7.** Given a TrustTunnel node with root headers, when outputting to a target without TrustTunnel header support, Sub-Store logs the unsupported header condition and omits or errors the node. + +--- + +## Success Criteria + +- Users can parse and re-export Surge HTTP/2 CONNECT and supported header-bearing Surge proxies without losing header configuration. +- Downstream implementation has explicit platform boundaries for header support and does not need to infer unsupported-target behavior. + +--- + +## Scope Boundaries + +- No frontend node editor for authoring headers is added in this pass. +- No runtime generation of `` values is implemented by Sub-Store; placeholders remain Surge configuration text. +- No claim that Mihomo supports HTTP/2 CONNECT as a standalone outbound type unless verified in `adapter/parser.go` and `constant/adapters.go`. + +--- + +## Dependencies / Assumptions + +- Egern HTTP/HTTPS `headers` support is based on the current Egern proxy configuration documentation. +- Local Mihomo source shows HTTP proxy `headers` support for the regular HTTP outbound, but no standalone `h2-connect` adapter type and no TrustTunnel `headers` option. +- sing-box HTTP outbound supports root `headers`, and JSON output preserves canonical proxy objects without platform-specific header loss.