From 049f4682004d21520efdaa511597e3fcacdc7d54 Mon Sep 17 00:00:00 2001 From: xream Date: Tue, 19 May 2026 23:05:09 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=20Loon=20Hysteria=202=20=E6=94=AF?= =?UTF-8?q?=E6=8C=81=20server-ports,=20hop-interval?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/package.json | 2 +- .../core/proxy-utils/parsers/peggy/loon.js | 4 +- .../src/core/proxy-utils/producers/loon.js | 11 ++ .../core/proxy-utils/producers/surfboard.js | 17 ++- .../src/core/proxy-utils/producers/surge.js | 30 +++-- .../proxy-parsers/v2ray-and-platforms.spec.js | 24 ++++ backend/src/test/proxy-producers/text.spec.js | 117 ++++++++++++++++++ 7 files changed, 185 insertions(+), 20 deletions(-) diff --git a/backend/package.json b/backend/package.json index 5264ec46..09d4e8eb 100644 --- a/backend/package.json +++ b/backend/package.json @@ -1,6 +1,6 @@ { "name": "sub-store", - "version": "2.23.14", + "version": "2.23.15", "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/parsers/peggy/loon.js b/backend/src/core/proxy-utils/parsers/peggy/loon.js index 58f5fd16..fee43e4a 100644 --- a/backend/src/core/proxy-utils/parsers/peggy/loon.js +++ b/backend/src/core/proxy-utils/parsers/peggy/loon.js @@ -72,7 +72,7 @@ anytls = tag equals "anytls"i address password (transport/transport_host/transpo proxy.type = "anytls"; handleTransport(); } -hysteria2 = tag equals "hysteria2"i address password (tls_name/sni/tls_verification/tls_cert_sha256/tls_pubkey_sha256/udp_relay/fast_open/download_bandwidth/salamander_password/ecn/ip_mode/block_quic/others)* { +hysteria2 = tag equals "hysteria2"i address password (tls_name/sni/tls_verification/tls_cert_sha256/tls_pubkey_sha256/udp_relay/fast_open/download_bandwidth/server_ports/hop_interval/salamander_password/ecn/ip_mode/block_quic/others)* { proxy.type = "hysteria2"; } https = tag equals "https"i address (username password)? (tls_name/sni/tls_verification/tls_cert_sha256/tls_pubkey_sha256/fast_open/udp_relay/ip_mode/block_quic/others)* { @@ -195,6 +195,8 @@ ip_mode = comma "ip-mode" equals match:[^,]+ { proxy["ip-version"] = match.join( ecn = comma "ecn" equals flag:bool { proxy.ecn = flag; } download_bandwidth = comma "download-bandwidth" equals match:[^,]+ { proxy.down = match.join(""); } +server_ports = comma "server-ports" equals '"' match:$[^"]+ '"' { proxy.ports = match.trim().replace(/\s*-\s*/g, "-").replace(/\s*,\s*/g, ","); } +hop_interval = comma "hop-interval" equals match:$[0-9]+ { proxy["hop-interval"] = parseInt(match, 10); } salamander_password = comma "salamander-password" equals match:[^,]+ { proxy['obfs-password'] = match.join(""); proxy.obfs = 'salamander'; } block_quic = comma "block-quic" equals flag:bool { if(flag) proxy["block-quic"] = "on"; else proxy["block-quic"] = "off"; } diff --git a/backend/src/core/proxy-utils/producers/loon.js b/backend/src/core/proxy-utils/producers/loon.js index 66c4ac4b..d9f9fa90 100644 --- a/backend/src/core/proxy-utils/producers/loon.js +++ b/backend/src/core/proxy-utils/producers/loon.js @@ -749,6 +749,17 @@ function hysteria2(proxy) { result.appendIfPresent(`,"${proxy.password}"`, 'password'); + if (isPresent(proxy, 'ports') && `${proxy.ports}`.trim().length > 0) { + result.append(`,server-ports="${proxy.ports}"`); + } + + if ( + isPresent(proxy, 'hop-interval') && + `${proxy['hop-interval']}`.trim().length > 0 + ) { + result.append(`,hop-interval=${proxy['hop-interval']}`); + } + // sni result.appendIfPresent(`,tls-name=${proxy.sni}`, 'sni'); result.appendIfPresent( diff --git a/backend/src/core/proxy-utils/producers/surfboard.js b/backend/src/core/proxy-utils/producers/surfboard.js index b70ba165..831b12e8 100644 --- a/backend/src/core/proxy-utils/producers/surfboard.js +++ b/backend/src/core/proxy-utils/producers/surfboard.js @@ -4,6 +4,10 @@ import { isNotBlank } from '@/utils'; const targetPlatform = 'Surfboard'; +function hasNonBlankValue(value) { + return value != null && `${value}`.trim().length > 0; +} + export default function Surfboard_Producer() { const produce = (proxy) => { if ( @@ -62,14 +66,15 @@ function hysteria2(proxy) { result.appendIfPresent(`,password="${proxy.password}"`, 'password'); - if (isPresent(proxy, 'ports')) { - result.append(`,port-hopping="${proxy.ports.replace(/,/g, ';')}"`); + if (hasNonBlankValue(proxy.ports)) { + result.append( + `,port-hopping="${String(proxy.ports).replace(/,/g, ';')}"`, + ); } - result.appendIfPresent( - `,port-hopping-interval=${proxy['hop-interval']}`, - 'hop-interval', - ); + if (hasNonBlankValue(proxy['hop-interval'])) { + result.append(`,port-hopping-interval=${proxy['hop-interval']}`); + } // tls verification result.appendIfPresent(`,sni="${proxy.sni}"`, 'sni'); diff --git a/backend/src/core/proxy-utils/producers/surge.js b/backend/src/core/proxy-utils/producers/surge.js index 9e054061..3c966dd6 100644 --- a/backend/src/core/proxy-utils/producers/surge.js +++ b/backend/src/core/proxy-utils/producers/surge.js @@ -44,6 +44,10 @@ function quoteSurgeValue(value) { .replace(/"/g, '\\"')}"`; } +function hasNonBlankValue(value) { + return value != null && `${value}`.trim().length > 0; +} + function appendClientCert(result, proxy) { const clientCert = isPresent(proxy, 'keystore-client-cert') ? proxy['keystore-client-cert'] @@ -1076,14 +1080,15 @@ function tuic(proxy) { 'alpn', ); - if (isPresent(proxy, 'ports')) { - result.append(`,port-hopping="${proxy.ports.replace(/,/g, ';')}"`); + if (hasNonBlankValue(proxy.ports)) { + result.append( + `,port-hopping="${String(proxy.ports).replace(/,/g, ';')}"`, + ); } - result.appendIfPresent( - `,port-hopping-interval=${proxy['hop-interval']}`, - 'hop-interval', - ); + if (hasNonBlankValue(proxy['hop-interval'])) { + result.append(`,port-hopping-interval=${proxy['hop-interval']}`); + } const ip_version = ipVersions[proxy['ip-version']] || proxy['ip-version']; result.appendIfPresent(`,ip-version=${ip_version}`, 'ip-version'); @@ -1348,14 +1353,15 @@ function hysteria2(proxy) { result.appendIfPresent(`,password="${proxy.password}"`, 'password'); - if (isPresent(proxy, 'ports')) { - result.append(`,port-hopping="${proxy.ports.replace(/,/g, ';')}"`); + if (hasNonBlankValue(proxy.ports)) { + result.append( + `,port-hopping="${String(proxy.ports).replace(/,/g, ';')}"`, + ); } - result.appendIfPresent( - `,port-hopping-interval=${proxy['hop-interval']}`, - 'hop-interval', - ); + if (hasNonBlankValue(proxy['hop-interval'])) { + result.append(`,port-hopping-interval=${proxy['hop-interval']}`); + } if (proxy['obfs-password'] && proxy.obfs == 'salamander') { result.append(`,salamander-password="${proxy['obfs-password']}"`); 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 c58c8ffa..e01d4f4e 100644 --- a/backend/src/test/proxy-parsers/v2ray-and-platforms.spec.js +++ b/backend/src/test/proxy-parsers/v2ray-and-platforms.spec.js @@ -2559,6 +2559,7 @@ describe('Platform raw-format parser coverage', function () { }, }, ]); + }); describe('Loon raw inputs', function () { @@ -2709,6 +2710,21 @@ describe('Platform raw-format parser coverage', function () { ecn: true, }, }, + { + title: 'parses hysteria2 port hopping lines', + input: 'Loon Hysteria2 Port Hopping=hysteria2,loon-hy2.example.com,443,"secret",server-ports="1000,2000-3000,5000",hop-interval=30,tls-name=peer.example.com,skip-cert-verify=true', + expected: { + type: 'hysteria2', + name: 'Loon Hysteria2 Port Hopping', + server: 'loon-hy2.example.com', + port: 443, + ports: '1000,2000-3000,5000', + 'hop-interval': 30, + password: 'secret', + sni: 'peer.example.com', + 'skip-cert-verify': true, + }, + }, { title: 'parses https auth lines', input: 'Loon HTTPS=https,loon-http.example.com,8443,user,"pass",tls-name=sni.example.com,skip-cert-verify=true', @@ -2829,6 +2845,14 @@ describe('Platform raw-format parser coverage', function () { }, }, ]); + + it('rejects hysteria2 hop interval ranges', function () { + expect( + parseAll( + 'Loon Hysteria2 Hop Range=hysteria2,loon-hy2.example.com,443,"secret",hop-interval=15-30', + ), + ).to.have.length(0); + }); }); describe('Surge raw inputs', function () { diff --git a/backend/src/test/proxy-producers/text.spec.js b/backend/src/test/proxy-producers/text.spec.js index 17975e5d..24d133f3 100644 --- a/backend/src/test/proxy-producers/text.spec.js +++ b/backend/src/test/proxy-producers/text.spec.js @@ -433,6 +433,53 @@ describe('Proxy text producers', function () { ); }); + it('produces Loon Hysteria2 lines with port hopping', function () { + const output = produceExternal('Loon', { + type: 'hysteria2', + name: 'Loon Hysteria2', + server: 'hy2.example.com', + port: 443, + password: 'secret', + ports: '1000,2000-3000,5000', + 'hop-interval': 30, + sni: 'peer.example.com', + 'skip-cert-verify': true, + }); + + expect(output).to.equal( + 'Loon Hysteria2=Hysteria2,hy2.example.com,443,"secret",server-ports="1000,2000-3000,5000",hop-interval=30,tls-name=peer.example.com,skip-cert-verify=true', + ); + }); + + it('does not emit empty Loon Hysteria2 port hopping fields', function () { + const output = produceExternal('Loon', { + type: 'hysteria2', + name: 'Loon Hysteria2 Plain', + server: 'hy2.example.com', + port: 443, + password: 'secret', + ports: '', + 'hop-interval': ' ', + sni: 'peer.example.com', + 'skip-cert-verify': true, + }); + + expect(output).to.equal( + 'Loon Hysteria2 Plain=Hysteria2,hy2.example.com,443,"secret",tls-name=peer.example.com,skip-cert-verify=true', + ); + }); + + it('exports URI Hysteria2 port hopping nodes to Loon fields', function () { + const [proxy] = ProxyUtils.parse( + 'hy2://secret@hy2.example.com:443?mport=1000,2000-3000,5000&hop-interval=30#URI%20Hysteria2', + ); + const output = produceExternal('Loon', proxy); + + expect(output).to.equal( + 'URI Hysteria2=Hysteria2,hy2.example.com,443,"secret",server-ports="1000,2000-3000,5000",hop-interval=30,tls-name=hy2.example.com,skip-cert-verify=false,fast-open=false', + ); + }); + it('produces Surge TUIC v5 lines with port hopping', function () { const output = produceExternal('Surge', { type: 'tuic', @@ -453,6 +500,58 @@ describe('Proxy text producers', function () { ); }); + it('does not emit empty Surge port hopping fields', function () { + const output = produceExternal('Surge', [ + { + type: 'tuic', + name: 'Surge TUIC Plain', + server: 'tuic.example.com', + port: 443, + uuid: UUID, + password: 'secret', + ports: '', + 'hop-interval': ' ', + sni: 'sni.example.com', + 'skip-cert-verify': true, + alpn: ['h3'], + }, + { + type: 'hysteria2', + name: 'Surge Hysteria2 Plain', + server: 'hy2.example.com', + port: 443, + password: 'secret', + ports: '', + 'hop-interval': ' ', + sni: 'peer.example.com', + 'skip-cert-verify': true, + }, + ]); + + expect(output).to.equal( + `Surge TUIC Plain=tuic-v5,tuic.example.com,443,uuid=${UUID},password="secret",alpn=h3,sni="sni.example.com",skip-cert-verify=true\nSurge Hysteria2 Plain=hysteria2,hy2.example.com,443,password="secret",sni="peer.example.com",skip-cert-verify=true`, + ); + }); + + it('produces Surge Hysteria2 lines with port hopping', function () { + const output = produceExternal('Surge', { + type: 'hysteria2', + name: 'Surge Hysteria2', + server: 'hy2.example.com', + port: 443, + password: 'secret', + ports: '8443,8445-8447', + 'hop-interval': 30, + sni: 'peer.example.com', + 'skip-cert-verify': true, + down: '100 Mbps', + }); + + expect(output).to.equal( + 'Surge Hysteria2=hysteria2,hy2.example.com,443,password="secret",port-hopping="8443;8445-8447",port-hopping-interval=30,sni="peer.example.com",skip-cert-verify=true,download-bandwidth=100', + ); + }); + it('quotes Surge SSH private-key and TLS client-cert keystore values', function () { const output = ProxyUtils.produce( [ @@ -818,6 +917,24 @@ describe('Proxy text producers', function () { ); }); + it('does not emit empty Surfboard Hysteria2 port hopping fields', function () { + const output = produceExternal('Surfboard', { + type: 'hysteria2', + name: 'Surfboard Hysteria2 Plain', + server: 'hy2.example.com', + port: 443, + password: 'secret', + ports: '', + 'hop-interval': ' ', + sni: 'peer.example.com', + 'skip-cert-verify': true, + }); + + expect(output).to.equal( + 'Surfboard Hysteria2 Plain=hysteria2,hy2.example.com,443,password="secret",sni="peer.example.com",skip-cert-verify=true', + ); + }); + it('omits Surfboard Hysteria2 lines when obfs is present', function () { const output = ProxyUtils.produce( [