diff --git a/backend/package.json b/backend/package.json index 70681ce6..6bde18c7 100644 --- a/backend/package.json +++ b/backend/package.json @@ -1,6 +1,6 @@ { "name": "sub-store", - "version": "2.44.19", + "version": "2.44.20", "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/ech-utils.js b/backend/src/core/proxy-utils/ech-utils.js index b1f3ba54..e1fbd48b 100644 --- a/backend/src/core/proxy-utils/ech-utils.js +++ b/backend/src/core/proxy-utils/ech-utils.js @@ -5,6 +5,26 @@ export const ECH_FORCE_QUERY_FIELD = '_force-query'; export const ECH_SOCKOPT_FIELD = '_sockopt'; export const DEFAULT_XRAY_ECH_DNS = 'https://dns.alidns.com/dns-query'; +export function normalizeInlineEchConfigList(value) { + if (typeof value !== 'string') return undefined; + + const match = /^([A-Za-z0-9+/]+|[A-Za-z0-9_-]+)(={0,2})$/.exec( + value.trim(), + ); + if (!match) return undefined; + + const [, config, padding] = match; + const paddingLength = (4 - (config.length % 4)) % 4; + if (paddingLength === 3 || (padding && padding.length !== paddingLength)) { + return undefined; + } + + // Mihomo expects standard padded Base64. + return ( + config.replace(/-/g, '+').replace(/_/g, '/') + '='.repeat(paddingLength) + ); +} + export function parseXrayEchConfigList(echConfigList) { if (!isNotBlank(echConfigList)) { return undefined; @@ -44,7 +64,7 @@ export function isSupportedXrayEchForceQuery(forceQuery) { return ['none', 'half', 'full'].includes(forceQuery); } -function isMihomoEchEnabled(value) { +export function isMihomoEchEnabled(value) { // Match mihomo's current `ech-opts.enable` handling: // - adapter/parser.go decodes proxies with WeaklyTypedInput=true. // - common/structure/structure.go decodeBool accepts bool directly and diff --git a/backend/src/core/proxy-utils/parsers/peggy/surge.js b/backend/src/core/proxy-utils/parsers/peggy/surge.js index 6381adb3..70154d6a 100644 --- a/backend/src/core/proxy-utils/parsers/peggy/surge.js +++ b/backend/src/core/proxy-utils/parsers/peggy/surge.js @@ -1,4 +1,5 @@ import peggy from 'peggy'; +import { normalizeInlineEchConfigList } from '../../ech-utils'; import { setSurgeXhttpOption } from '../../surge-xhttp'; const grammars = String.raw` @@ -24,6 +25,7 @@ const grammars = String.raw` const xhttpOpts = { mode: "auto" }; let xhttpEnabled = false; let clientFingerprint; + let echConfig; const $ = {}; function handleWebsocket() { @@ -385,6 +387,15 @@ start = (masque/anytls/shadowsocks/vmess/vless/trojan/h2_connect/https/http/snel if (clientFingerprint === "apple-26") proxy["client-fingerprint"] = "ios"; if (clientFingerprint === "chromium-155") proxy["client-fingerprint"] = "chrome"; } + if ( + echConfig !== undefined && + (proxy.tls || ["trojan", "tuic", "hysteria2"].includes(proxy.type)) + ) { + if (proxy["reality-opts"]) error("ECH cannot be combined with REALITY"); + // Preserve malformed ECH for export validation so another parser cannot discard it. + const config = options.normalizeInlineEchConfigList(echConfig) ?? echConfig; + proxy["ech-opts"] = { enable: true, config }; + } return proxy; } @@ -399,7 +410,7 @@ shadowsocks = tag equals "ss" address (method/passwordk/obfs/obfs_host/obfs_uri/ } handleShadowTLS(); } -vmess = tag equals "vmess" address (vmess_uuid/vmess_aead/ws/ws_http_upgrade/ws_path/ws_headers/vmess_method/ip_version/underlying_proxy/tos/allow_other_interface/interface/test_url/test_udp/test_timeout/hybrid/no_error_alert/tls/sni/cert_verify_name/alpn/tls_fingerprint/client_fingerprint/tls_verification/client_cert/fast_open/tfo/udp_relay/shadow_tls_version/shadow_tls_sni/shadow_tls_password/block_quic/others)* { +vmess = tag equals "vmess" address (vmess_uuid/vmess_aead/ws/ws_http_upgrade/ws_path/ws_headers/vmess_method/ip_version/underlying_proxy/tos/allow_other_interface/interface/test_url/test_udp/test_timeout/hybrid/no_error_alert/tls/sni/cert_verify_name/alpn/tls_fingerprint/client_fingerprint/tls_verification/client_cert/ech_config/fast_open/tfo/udp_relay/shadow_tls_version/shadow_tls_sni/shadow_tls_password/block_quic/others)* { proxy.type = "vmess"; proxy.cipher = proxy.cipher || "auto"; // Surfboard 与 Surge 默认不一致, 不管 Surfboard https://getsurfboard.com/docs/profile-format/proxy/external-proxy/vmess @@ -411,7 +422,7 @@ vmess = tag equals "vmess" address (vmess_uuid/vmess_aead/ws/ws_http_upgrade/ws_ handleWebsocket(); handleShadowTLS(); } -vless = tag equals "vless" address (vless_username/vless_option/reality_option/ws/ws_http_upgrade/ws_path/ws_headers/grpc/grpc_service_name/grpc_mode/grpc_authority/xhttp/xhttp_headers/xhttp_option/ip_version/underlying_proxy/tos/allow_other_interface/interface/test_url/test_udp/test_timeout/hybrid/no_error_alert/tls/sni/cert_verify_name/alpn/tls_fingerprint/client_fingerprint/tls_verification/client_cert/fast_open/tfo/shadow_tls_version/shadow_tls_sni/shadow_tls_password/block_quic/others)* { +vless = tag equals "vless" address (vless_username/vless_option/reality_option/ws/ws_http_upgrade/ws_path/ws_headers/grpc/grpc_service_name/grpc_mode/grpc_authority/xhttp/xhttp_headers/xhttp_option/ip_version/underlying_proxy/tos/allow_other_interface/interface/test_url/test_udp/test_timeout/hybrid/no_error_alert/tls/sni/cert_verify_name/alpn/tls_fingerprint/client_fingerprint/tls_verification/client_cert/ech_config/fast_open/tfo/shadow_tls_version/shadow_tls_sni/shadow_tls_password/block_quic/others)* { proxy.type = "vless"; proxy["packet-encoding"] = "xudp"; if (proxy["reality-opts"] && proxy["reality-opts"]["public-key"]) proxy.tls = true; @@ -423,17 +434,17 @@ vless = tag equals "vless" address (vless_username/vless_option/reality_option/w } handleShadowTLS(); } -trojan = tag equals "trojan" address (passwordk/ws/ws_http_upgrade/ws_path/ws_headers/tls/sni/cert_verify_name/alpn/tls_fingerprint/client_fingerprint/tls_verification/client_cert/ip_version/underlying_proxy/tos/allow_other_interface/interface/test_url/test_udp/test_timeout/hybrid/no_error_alert/fast_open/tfo/udp_relay/shadow_tls_version/shadow_tls_sni/shadow_tls_password/block_quic/others)* { +trojan = tag equals "trojan" address (passwordk/ws/ws_http_upgrade/ws_path/ws_headers/tls/sni/cert_verify_name/alpn/tls_fingerprint/client_fingerprint/tls_verification/client_cert/ech_config/ip_version/underlying_proxy/tos/allow_other_interface/interface/test_url/test_udp/test_timeout/hybrid/no_error_alert/fast_open/tfo/udp_relay/shadow_tls_version/shadow_tls_sni/shadow_tls_password/block_quic/others)* { proxy.type = "trojan"; handleWebsocket(); handleShadowTLS(); } -https = tag equals "https" address (username password)? (usernamek passwordk)? (headers/sni/cert_verify_name/alpn/tls_fingerprint/client_fingerprint/tls_verification/client_cert/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/cert_verify_name/alpn/tls_fingerprint/client_fingerprint/tls_verification/client_cert/ech_config/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(); } -h2_connect = tag equals "h2-connect" address (username password)? (usernamek passwordk)? (headers/max_streams/sni/cert_verify_name/alpn/tls_fingerprint/client_fingerprint/tls_verification/client_cert/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/max_streams/sni/cert_verify_name/alpn/tls_fingerprint/client_fingerprint/tls_verification/client_cert/ech_config/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(); @@ -456,11 +467,11 @@ snell = tag equals ("snell"i/"opensnell"i) address (snell_version/snell_mode/sne } handleShadowTLS(); } -tuic = tag equals "tuic" address (alpn/token/ip_version/underlying_proxy/tos/allow_other_interface/interface/test_url/test_udp/test_timeout/hybrid/no_error_alert/tls_fingerprint/tls_verification/client_cert/sni/cert_verify_name/fast_open/tfo/ecn/shadow_tls_version/shadow_tls_sni/shadow_tls_password/block_quic/port_hopping_interval/others)* { +tuic = tag equals "tuic" address (alpn/token/ip_version/underlying_proxy/tos/allow_other_interface/interface/test_url/test_udp/test_timeout/hybrid/no_error_alert/tls_fingerprint/tls_verification/client_cert/ech_config/sni/cert_verify_name/fast_open/tfo/ecn/shadow_tls_version/shadow_tls_sni/shadow_tls_password/block_quic/port_hopping_interval/others)* { proxy.type = "tuic"; handleShadowTLS(); } -tuic_v5 = tag equals "tuic-v5" address (alpn/passwordk/uuidk/ip_version/underlying_proxy/tos/allow_other_interface/interface/test_url/test_udp/test_timeout/hybrid/no_error_alert/tls_fingerprint/tls_verification/client_cert/sni/cert_verify_name/fast_open/tfo/ecn/shadow_tls_version/shadow_tls_sni/shadow_tls_password/block_quic/port_hopping_interval/others)* { +tuic_v5 = tag equals "tuic-v5" address (alpn/passwordk/uuidk/ip_version/underlying_proxy/tos/allow_other_interface/interface/test_url/test_udp/test_timeout/hybrid/no_error_alert/tls_fingerprint/tls_verification/client_cert/ech_config/sni/cert_verify_name/fast_open/tfo/ecn/shadow_tls_version/shadow_tls_sni/shadow_tls_password/block_quic/port_hopping_interval/others)* { proxy.type = "tuic"; proxy.version = 5; handleShadowTLS(); @@ -469,7 +480,7 @@ wireguard = tag equals "wireguard" (section_name/no_error_alert/ip_version/under proxy.type = "wireguard-surge"; handleShadowTLS(); } -hysteria2 = tag equals "hysteria2" address (no_error_alert/ip_version/underlying_proxy/tos/allow_other_interface/interface/test_url/test_udp/test_timeout/hybrid/sni/cert_verify_name/alpn/tls_verification/client_cert/passwordk/tls_fingerprint/download_bandwidth/ecn/shadow_tls_version/shadow_tls_sni/shadow_tls_password/block_quic/port_hopping_interval/salamander_password/gecko_password/others)* { +hysteria2 = tag equals "hysteria2" address (no_error_alert/ip_version/underlying_proxy/tos/allow_other_interface/interface/test_url/test_udp/test_timeout/hybrid/sni/cert_verify_name/alpn/tls_verification/client_cert/ech_config/passwordk/tls_fingerprint/download_bandwidth/ecn/shadow_tls_version/shadow_tls_sni/shadow_tls_password/block_quic/port_hopping_interval/salamander_password/gecko_password/others)* { proxy.type = "hysteria2"; handleShadowTLS(); } @@ -477,20 +488,20 @@ socks5 = tag equals "socks5" address (username password)? (usernamek passwordk)? proxy.type = "socks5"; handleShadowTLS(); } -socks5_tls = tag equals "socks5-tls" address (username password)? (usernamek passwordk)? (udp_relay/no_error_alert/ip_version/underlying_proxy/tos/allow_other_interface/interface/test_url/test_udp/test_timeout/hybrid/sni/cert_verify_name/alpn/tls_fingerprint/client_fingerprint/tls_verification/client_cert/fast_open/tfo/shadow_tls_version/shadow_tls_sni/shadow_tls_password/block_quic/others)* { +socks5_tls = tag equals "socks5-tls" address (username password)? (usernamek passwordk)? (udp_relay/no_error_alert/ip_version/underlying_proxy/tos/allow_other_interface/interface/test_url/test_udp/test_timeout/hybrid/sni/cert_verify_name/alpn/tls_fingerprint/client_fingerprint/tls_verification/client_cert/ech_config/fast_open/tfo/shadow_tls_version/shadow_tls_sni/shadow_tls_password/block_quic/others)* { proxy.type = "socks5"; proxy.tls = true; handleShadowTLS(); } -anytls = tag equals "anytls" address (passwordk/reuse/ip_version/underlying_proxy/tos/allow_other_interface/interface/test_url/test_udp/test_timeout/hybrid/no_error_alert/tls_fingerprint/client_fingerprint/tls_verification/client_cert/sni/cert_verify_name/alpn/fast_open/tfo/block_quic/others)* { +anytls = tag equals "anytls" address (passwordk/reuse/ip_version/underlying_proxy/tos/allow_other_interface/interface/test_url/test_udp/test_timeout/hybrid/no_error_alert/tls_fingerprint/client_fingerprint/tls_verification/client_cert/ech_config/sni/cert_verify_name/alpn/fast_open/tfo/block_quic/others)* { proxy.type = "anytls"; proxy.tls = true; } -trust_tunnel = tag equals "trust-tunnel" address (usernamek/passwordk/client_random_auth_key/headers/max_streams/reuse/ip_version/underlying_proxy/tos/allow_other_interface/interface/test_url/test_udp/test_timeout/hybrid/no_error_alert/tls_fingerprint/client_fingerprint/tls_verification/client_cert/sni/cert_verify_name/alpn/h3/fast_open/tfo/block_quic/others)* { +trust_tunnel = tag equals "trust-tunnel" address (usernamek/passwordk/client_random_auth_key/headers/max_streams/reuse/ip_version/underlying_proxy/tos/allow_other_interface/interface/test_url/test_udp/test_timeout/hybrid/no_error_alert/tls_fingerprint/client_fingerprint/tls_verification/client_cert/ech_config/sni/cert_verify_name/alpn/h3/fast_open/tfo/block_quic/others)* { proxy.type = "trusttunnel"; proxy.tls = true; } -masque = tag equals "masque" address (usernamek/passwordk/port_hopping_interval/ip_version/underlying_proxy/tos/allow_other_interface/interface/test_url/test_udp/test_timeout/hybrid/no_error_alert/tls_fingerprint/tls_verification/client_cert/sni/cert_verify_name/alpn/fast_open/tfo/udp_relay/ecn/block_quic/others)* { +masque = tag equals "masque" address (usernamek/passwordk/port_hopping_interval/ip_version/underlying_proxy/tos/allow_other_interface/interface/test_url/test_udp/test_timeout/hybrid/no_error_alert/tls_fingerprint/tls_verification/client_cert/ech_config/sni/cert_verify_name/alpn/fast_open/tfo/udp_relay/ecn/block_quic/others)* { proxy.type = "masque-surge"; proxy.tls = true; } @@ -572,6 +583,7 @@ tls_verification = comma "skip-cert-verify" equals flag:bool { proxy["skip-cert- tls_fingerprint = comma "server-cert-fingerprint-sha256" equals tls_fingerprint:$[^,]+ { proxy["tls-fingerprint"] = tls_fingerprint.trim(); } client_fingerprint = comma "client-fingerprint" equals value:quoted_value { clientFingerprint = value.trim().toLowerCase(); } client_cert = comma "client-cert" equals match:[^,]+ { proxy["keystore-client-cert"] = stripQuotes(match.join("")); } +ech_config = comma "ech-config" equals value:quoted_value { echConfig = value; } snell_psk = comma "psk" equals value:quoted_value { proxy.psk = value; } snell_version = comma "version" equals match:$[0-9]+ { proxy.version = parseInt(match.trim()); } @@ -690,7 +702,11 @@ export default function getParser() { if (!parser) { const generated = peggy.generate(grammars); parser = { - parse: (input) => generated.parse(input, { setSurgeXhttpOption }), + parse: (input) => + generated.parse(input, { + normalizeInlineEchConfigList, + setSurgeXhttpOption, + }), }; } return parser; diff --git a/backend/src/core/proxy-utils/producers/surge.js b/backend/src/core/proxy-utils/producers/surge.js index 270c4468..3b92ff33 100644 --- a/backend/src/core/proxy-utils/producers/surge.js +++ b/backend/src/core/proxy-utils/producers/surge.js @@ -2,6 +2,7 @@ import _ from 'lodash'; import { Result, isPresent } from './utils'; import { isNotBlank, getIfNotBlank, isPlainObject } from '@/utils'; import $ from '@/core/app'; +import { isMihomoEchEnabled, normalizeInlineEchConfigList } from '../ech-utils'; import { SURGE_XHTTP_FIELDS } from '../surge-xhttp'; import { formatSurgeVmessEncryptMethod } from '../vmess-security'; @@ -167,6 +168,24 @@ function appendTlsProxyParams(result, proxy, enabled = true) { result.append(`,client-cert=${quoteSurgeValue(clientCert)}`); } appendClientFingerprint(result, proxy); + + const echOpts = isPlainObject(proxy['ech-opts']) + ? proxy['ech-opts'] + : undefined; + const echEnabled = echOpts + ? isMihomoEchEnabled(echOpts.enable) + : isNotBlank(proxy._echConfigList); + if (echEnabled && !proxy['reality-opts']) { + const config = normalizeInlineEchConfigList( + echOpts ? echOpts.config : proxy._echConfigList, + ); + if (!config) { + throw unsupported( + 'Surge ECH requires an inline ECHConfigList in base64 or base64url', + ); + } + result.append(`,ech-config=${config}`); + } } function appendSshPrivateKey(result, proxy) { diff --git a/backend/src/test/proxy-producers/text.spec.js b/backend/src/test/proxy-producers/text.spec.js index 73ae0ee0..554a9cf3 100644 --- a/backend/src/test/proxy-producers/text.spec.js +++ b/backend/src/test/proxy-producers/text.spec.js @@ -1643,6 +1643,110 @@ describe('Proxy text producers', function () { } }); + it('round-trips Surge ech-config for TLS and QUIC proxies', function () { + const config = 'AQID+///AA=='; + const echOpts = { enable: true, config }; + const cases = [ + ['https', '', config], + ['socks5-tls', '', '"AQID-___AA"'], + ['h2-connect', '', `'${config}'`], + ['trust-tunnel', ',h3=true', config], + ['trojan', ',password=secret', config], + ['anytls', ',password=secret', config], + ['vmess', `,username=${UUID},tls=true`, config], + ['vless', `,username=${UUID},tls=true`, config], + ['tuic', ',token=secret', config], + ['tuic-v5', `,uuid=${UUID},password=secret`, config], + ['hysteria2', ',password=secret', config], + ['masque', '', config], + ]; + + for (const [type, params, value] of cases) { + const [proxy] = ProxyUtils.parse( + `ECH=${type},example.com,443,ech-config=${value},sni=sni.example.com${params}`, + ); + expectSubset(proxy, { + 'ech-opts': echOpts, + sni: 'sni.example.com', + }); + + for (const target of ['Surge', 'SurgeMac']) { + const output = produceExternal(target, proxy); + expect(output).to.include(`,ech-config=${config}`); + expect(ProxyUtils.parse(output)[0]['ech-opts']).to.deep.equal( + echOpts, + ); + } + } + }); + + it('emits Surge ech-config from a VLESS share link with a static list', function () { + const [proxy] = ProxyUtils.parse( + `vless://${UUID}@example.com:443?security=tls&ech=AQID-___AA`, + ); + + expect(produceExternal('Surge', proxy)).to.include( + ',ech-config=AQID+///AA==', + ); + }); + + it('honors TLS, ECH enablement, and REALITY when producing Surge proxies', function () { + const config = 'AQID+///AA=='; + const echOpts = { enable: true, config }; + const [tls] = ProxyUtils.parse( + `Test=vless,example.com,443,username=${UUID},tls=true`, + ); + const realityKey = 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA'; + const reality = { 'public-key': realityKey }; + + expect( + produceExternal('Surge', { + ...tls, + 'ech-opts': { enable: false, config }, + _echConfigList: config, + }), + ).to.not.include('ech-config='); + expect( + produceExternal('Surge', { + ...tls, + tls: false, + 'ech-opts': echOpts, + }), + ).to.not.include('ech-config='); + + const realityOutput = produceExternal('Surge', { + ...tls, + 'reality-opts': reality, + 'ech-opts': echOpts, + }); + expect(realityOutput).to.include(`,reality-public-key=${realityKey}`); + expect(realityOutput).to.not.include('ech-config='); + expect(ProxyUtils.parse(`${realityOutput},ech-config=${config}`)).to.be + .empty; + }); + + it('filters ECH-enabled nodes without a static list from Surge output', function () { + const [proxy] = ProxyUtils.parse('ECH=https,example.com,443'); + const [invalid] = ProxyUtils.parse( + 'ECH=https,example.com,443,ech-config=bad!', + ); + const dns = { enable: true, 'query-server-name': 'ech.example.com' }; + const unsafe = { enable: true, config: 'AQID,skip-cert-verify=true' }; + + for (const candidate of [ + invalid, + { ...proxy, 'ech-opts': dns }, + { ...proxy, 'ech-opts': unsafe }, + ]) { + const { result, errors } = captureErrors(() => + produceInternal('Surge', candidate), + ); + + expect(result).to.be.empty; + expect(errors[0]).to.include('requires an inline ECHConfigList'); + } + }); + it('round-trips Surge TrustTunnel h3 as network', function () { const [proxy] = ProxyUtils.parse( 'Surge TrustTunnel H3 = trust-tunnel,trust.example.com,443,h3=true',