From 12968bf66e995e9a9cbce40da6202f842abded22 Mon Sep 17 00:00:00 2001 From: xream Date: Sun, 22 Nov 2026 19:27:11 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20Quantumult=20X=20Hysteria=202=20?= =?UTF-8?q?=E6=94=AF=E6=8C=81=20Gecko?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/package.json | 2 +- backend/src/core/proxy-utils/index.js | 2 +- .../src/core/proxy-utils/parsers/peggy/qx.js | 20 ++++-- backend/src/core/proxy-utils/producers/qx.js | 24 +++++-- backend/src/test/proxy-parsers/uri.spec.js | 12 ++-- backend/src/test/proxy-producers/text.spec.js | 71 +++++++++++++++++++ 6 files changed, 113 insertions(+), 18 deletions(-) diff --git a/backend/package.json b/backend/package.json index 4200d4d0..b0d4a544 100644 --- a/backend/package.json +++ b/backend/package.json @@ -1,6 +1,6 @@ { "name": "sub-store", - "version": "2.44.24", + "version": "2.44.25", "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 144ed462..c67bd370 100644 --- a/backend/src/core/proxy-utils/index.js +++ b/backend/src/core/proxy-utils/index.js @@ -1093,7 +1093,7 @@ function lastParse(proxy) { if ( ['hysteria2'].includes(proxy.type) && proxy.obfs && - !['salamander'].includes(proxy.obfs) && + !['salamander', 'gecko'].includes(proxy.obfs) && !proxy['obfs-password'] ) { proxy['obfs-password'] = proxy.obfs; diff --git a/backend/src/core/proxy-utils/parsers/peggy/qx.js b/backend/src/core/proxy-utils/parsers/peggy/qx.js index 11224b61..c2b8da03 100644 --- a/backend/src/core/proxy-utils/parsers/peggy/qx.js +++ b/backend/src/core/proxy-utils/parsers/peggy/qx.js @@ -151,7 +151,7 @@ mieru = "mieru" equals address } hysteria2 = "hysteria2" equals address - (password/tls_host/tls_pubkey_sha256/tls_alpn/tls_no_session_ticket/tls_no_session_reuse/tls_fingerprint/tls_verification/udp_relay/download_bandwidth/salamander_password/port_hopping/port_hopping_interval/server_check_url/tag/others)* { + (password/tls_host/tls_pubkey_sha256/tls_alpn/tls_no_session_ticket/tls_no_session_reuse/tls_fingerprint/tls_verification/udp_relay/download_bandwidth/hysteria2_obfs_password/port_hopping/port_hopping_interval/server_check_url/tag/others)* { proxy.type = "hysteria2"; proxy.tls = true; } @@ -223,9 +223,16 @@ mieru_udp_over_tcp = comma "udp-over-tcp" equals raw:$[^,]* { } download_bandwidth = comma "download-bandwidth" equals value:$[^,]+ { proxy.down = value.trim(); } -salamander_password = comma "salamander-password" equals value:$((!next_parameter .)+) { - proxy.obfs = "salamander"; - proxy["obfs-password"] = value.trim(); +hysteria2_obfs_password = comma type:("salamander"/"gecko") "-password" equals value:$((!next_parameter .)*) { + if (proxy.obfs && proxy.obfs !== type) { + throw new Error("QX Hysteria 2 cannot combine salamander-password and gecko-password"); + } + const password = value.trim(); + if (options.byteLength(password, "utf8") < 4) { + throw new Error("QX Hysteria 2 " + type + "-password must be at least 4 bytes"); + } + proxy.obfs = type; + proxy["obfs-password"] = password; } port_hopping = comma "port-hopping" equals ports:$(hopping_port (comma hopping_port)*) { proxy.ports = ports.replace(/\s/g, ""); @@ -318,7 +325,10 @@ export default function getParser() { const generated = peggy.generate(grammars); parser = { parse(input, options) { - const proxy = generated.parse(input, options); + const proxy = generated.parse(input, { + ...options, + byteLength: Buffer.byteLength, + }); const alpn = decodeQxAlpn(proxy['tls-alpn']); if (alpn) proxy.alpn = alpn; return proxy; diff --git a/backend/src/core/proxy-utils/producers/qx.js b/backend/src/core/proxy-utils/producers/qx.js index 75997359..ad3cc228 100644 --- a/backend/src/core/proxy-utils/producers/qx.js +++ b/backend/src/core/proxy-utils/producers/qx.js @@ -618,8 +618,11 @@ function anytls(proxy) { } function hysteria2(proxy) { - if (proxy.obfs && proxy.obfs !== 'salamander') { - throw new Error('QX Hysteria 2 only supports Salamander obfuscation'); + const obfs = proxy.obfs || 'salamander'; + if (!['salamander', 'gecko'].includes(obfs)) { + throw new Error( + 'QX Hysteria 2 only supports Salamander and Gecko obfuscation', + ); } const result = new Result(proxy); @@ -632,10 +635,19 @@ function hysteria2(proxy) { if (Number.isInteger(interval) && interval >= 5 && interval <= 3600) { result.append(`,port-hopping-interval=${interval}`); } - result.appendIfPresent( - `,salamander-password=${proxy['obfs-password']}`, - 'obfs-password', - ); + if (isPresent(proxy, 'obfs-password')) { + const obfsPassword = String(proxy['obfs-password']); + if (Buffer.byteLength(obfsPassword, 'utf8') < 4) { + throw new Error( + `QX Hysteria 2 ${obfs}-password must be at least 4 bytes`, + ); + } + result.append(`,${obfs}-password=${obfsPassword}`); + } else if (obfs === 'gecko') { + throw new Error( + 'QX Hysteria 2 Gecko obfuscation requires gecko-password', + ); + } result.appendIfPresent(`,tls-host=${proxy.sni}`, 'sni'); result.appendIfPresent( `,tls-pubkey-sha256=${proxy['tls-pubkey-sha256']}`, diff --git a/backend/src/test/proxy-parsers/uri.spec.js b/backend/src/test/proxy-parsers/uri.spec.js index 640ad12b..0770b61d 100644 --- a/backend/src/test/proxy-parsers/uri.spec.js +++ b/backend/src/test/proxy-parsers/uri.spec.js @@ -617,12 +617,14 @@ describe('Proxy URI parser coverage', function () { }); }); - it('rejects Hysteria2 salamander obfs without obfs-password', function () { - const proxies = parseAll( - 'hy2://hy2-secret@hy2.example.com:443?obfs=salamander#Hy2%20Missing%20Password', - ); + it('rejects Hysteria2 obfs modes without obfs-password', function () { + for (const obfs of ['salamander', 'gecko']) { + const proxies = parseAll( + `hy2://hy2-secret@hy2.example.com:443?obfs=${obfs}#Hy2%20Missing%20Password`, + ); - expect(proxies).to.deep.equal([]); + expect(proxies).to.be.empty; + } }); it('parses TUIC URIs with colon-containing passwords and booleans', function () { diff --git a/backend/src/test/proxy-producers/text.spec.js b/backend/src/test/proxy-producers/text.spec.js index 17d4d168..c7205ad4 100644 --- a/backend/src/test/proxy-producers/text.spec.js +++ b/backend/src/test/proxy-producers/text.spec.js @@ -579,6 +579,77 @@ describe('Proxy text producers', function () { ).not.to.include(',port-hopping'); }); + it('round-trips Quantumult X Hysteria 2 Gecko obfuscation', function () { + const raw = + 'hysteria2=example.com:443,password=pwd,gecko-password=obfskey,tls-host=apple.com,tls-verification=true,udp-relay=true,tag=hy2-05'; + const [proxy] = ProxyUtils.parse(raw); + + expectSubset(proxy, { obfs: 'gecko', 'obfs-password': 'obfskey' }); + expect(produceExternal('QX', proxy)).to.equal(raw); + }); + + it('rejects short Quantumult X Hysteria 2 obfuscation passwords on import', function () { + for (const obfs of ['gecko', 'salamander']) { + const prefix = `hysteria2=example.com:443,password=pwd,${obfs}-password=`; + const input = ['x', '中', '', '中x'] + .map((key) => `${prefix}${key},tag=hy2`) + .join('\n'); + const { result } = captureErrors(() => ProxyUtils.parse(input)); + + expect(result).to.have.length(1); + expectSubset(result[0], { obfs, 'obfs-password': '中x' }); + } + }); + + it('requires Quantumult X Hysteria 2 obfuscation passwords to be at least 4 bytes', function () { + for (const obfs of ['gecko', 'salamander']) { + const proxies = ['x', '中', '中x'].map((obfsPassword) => ({ + type: 'hysteria2', + name: 'hy2', + server: 'example.com', + port: 443, + password: 'pwd', + obfs, + 'obfs-password': obfsPassword, + })); + const { result, errors } = captureErrors(() => + produceExternal('QX', proxies), + ); + + expect(result).to.equal( + `hysteria2=example.com:443,password=pwd,${obfs}-password=中x,tag=hy2`, + ); + expect(errors).to.have.length(2); + } + }); + + it('rejects Quantumult X Hysteria 2 Gecko export without a key', function () { + const proxy = { + type: 'hysteria2', + name: 'hy2', + server: 'example.com', + port: 443, + password: 'pwd', + obfs: 'gecko', + }; + const { result, errors } = captureErrors(() => + ProxyUtils.produce([proxy], 'QX', 'external'), + ); + + expect(result).to.equal(''); + expect(errors[0]).to.include('requires gecko-password'); + }); + + it('rejects combined Gecko and Salamander in Quantumult X Hysteria 2', function () { + const { result } = captureErrors(() => + ProxyUtils.parse( + 'hysteria2=example.com:443,password=pwd,salamander-password=obfskey,gecko-password=obfskey,udp-relay=true,tag=hy2', + ), + ); + + expect(result).to.be.empty; + }); + it('limits Quantumult X Hysteria 2 port hopping intervals to 5–3600 seconds', function () { const prefix = 'hysteria2=example.com:443,password=pwd,port-hopping=2000-3000';