From de5fea225dc0154f63809b8bae130db3ca8dbe87 Mon Sep 17 00:00:00 2001 From: xream Date: Sat, 11 Apr 2026 23:35:35 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20Hysteria2=20=E7=AB=AF=E5=8F=A3=E8=B7=B3?= =?UTF-8?q?=E8=B7=83=E6=94=AF=E6=8C=81=E8=8C=83=E5=9B=B4=E6=A0=BC=E5=BC=8F?= =?UTF-8?q?,=20=E5=85=88=E5=8F=AA=E5=81=9A=E6=8B=86=E5=88=86=E5=85=BC?= =?UTF-8?q?=E5=AE=B9?= 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 | 32 ++++++++++ backend/src/core/proxy-utils/parsers/index.js | 4 +- .../src/test/proxy-parsers/pipeline.spec.js | 61 ++++++++++++++++++- backend/src/test/proxy-parsers/uri.spec.js | 32 ++++++++++ 5 files changed, 127 insertions(+), 4 deletions(-) diff --git a/backend/package.json b/backend/package.json index cda40119..85d64f64 100644 --- a/backend/package.json +++ b/backend/package.json @@ -1,6 +1,6 @@ { "name": "sub-store", - "version": "2.21.87", + "version": "2.21.88", "description": "Advanced Subscription Manager for QX, Loon, Surge, Stash and Shadowrocket.", "main": "src/main.js", "scripts": { diff --git a/backend/src/core/proxy-utils/index.js b/backend/src/core/proxy-utils/index.js index 575f0715..5be0f1a7 100644 --- a/backend/src/core/proxy-utils/index.js +++ b/backend/src/core/proxy-utils/index.js @@ -400,6 +400,38 @@ function lastParse(proxy) { if (typeof proxy.password === 'number') { proxy.password = numberToString(proxy.password); } + if (proxy['hop-interval'] != null) { + const hopInterval = `${proxy['hop-interval']}`.trim(); + const hopIntervalRangeMatch = hopInterval.match(/^(\d+)\s*-\s*(\d+)$/); + + if (hopIntervalRangeMatch) { + const hopIntervalMin = parseInt(hopIntervalRangeMatch[1], 10); + const hopIntervalMax = parseInt(hopIntervalRangeMatch[2], 10); + + if (hopIntervalMin > 0 && hopIntervalMin <= hopIntervalMax) { + // 暂时只在统一收口阶段拆分 mihomo 的 hop-interval 区间写法, + // 不对其他客户端做进一步转换,等 mihomo / sing-box 新版覆盖率上来后再统一处理。 + proxy['hop-interval'] = hopIntervalMin; + proxy['hop-interval-max'] = hopIntervalMax; + } else { + delete proxy['hop-interval']; + delete proxy['hop-interval-max']; + } + } else if (/^\d+$/.test(hopInterval)) { + const parsedHopInterval = parseInt(hopInterval, 10); + + if (parsedHopInterval > 0) { + proxy['hop-interval'] = parsedHopInterval; + delete proxy['hop-interval-max']; + } else { + delete proxy['hop-interval']; + delete proxy['hop-interval-max']; + } + } else { + delete proxy['hop-interval']; + delete proxy['hop-interval-max']; + } + } if ( ['ss'].includes(proxy.type) && proxy.cipher === 'none' && diff --git a/backend/src/core/proxy-utils/parsers/index.js b/backend/src/core/proxy-utils/parsers/index.js index d8ad3f06..d7d1ce9e 100644 --- a/backend/src/core/proxy-utils/parsers/index.js +++ b/backend/src/core/proxy-utils/parsers/index.js @@ -1215,8 +1215,8 @@ function URI_Hysteria2() { proxy['tls-fingerprint'] = params.pinSHA256; let hop_interval = params['hop-interval'] || params['hop_interval']; - if (/^\d+$/.test(hop_interval)) { - proxy['hop-interval'] = parseInt(`${hop_interval}`, 10); + if (hop_interval != null) { + proxy['hop-interval'] = hop_interval; } let keepalive = params['keepalive']; diff --git a/backend/src/test/proxy-parsers/pipeline.spec.js b/backend/src/test/proxy-parsers/pipeline.spec.js index f4290f53..2039ac3d 100644 --- a/backend/src/test/proxy-parsers/pipeline.spec.js +++ b/backend/src/test/proxy-parsers/pipeline.spec.js @@ -142,6 +142,66 @@ FINAL,DIRECT }); }); + it('splits mihomo hop-interval ranges for Clash-style object inputs', function () { + const proxy = parseOne( + JSON.stringify({ + name: 'hy2-range-inline', + type: 'hysteria2', + server: 'hy2.example.com', + port: 443, + password: 'secret', + 'hop-interval': '15-30', + }), + ); + + expectSubset(proxy, { + name: 'hy2-range-inline', + type: 'hysteria2', + 'hop-interval': 15, + 'hop-interval-max': 30, + }); + }); + + it('drops invalid mihomo hop-interval values for Clash-style object inputs', function () { + const invalidHopIntervals = [ + { title: 'zero string', value: '0' }, + { title: 'zero number', value: 0 }, + { title: 'negative string', value: '-5' }, + { title: 'negative number', value: -5 }, + { title: 'decimal string', value: '15.5' }, + { title: 'decimal number', value: 15.5 }, + { title: 'comma list', value: '15,30' }, + { title: 'plain text', value: 'abc' }, + { title: 'empty string', value: '' }, + { title: 'blank string', value: ' ' }, + { title: 'zero-start range', value: '0-15' }, + { title: 'zero-end range', value: '15-0' }, + { title: 'reverse range', value: '30-15' }, + { title: 'multi-range', value: '15-30-45' }, + { title: 'double hyphen range', value: '15--30' }, + { title: 'boolean', value: true }, + { title: 'array', value: [15, 30] }, + { title: 'object', value: { min: 15, max: 30 } }, + ]; + + for (const { title, value } of invalidHopIntervals) { + const proxy = parseOne( + JSON.stringify({ + name: `hy2-invalid-${title}`, + type: 'hysteria2', + server: 'hy2.example.com', + port: 443, + password: 'secret', + 'hop-interval': value, + 'hop-interval-max': 999, + }), + ); + + expect(proxy).to.not.have.property('hop-interval'); + expect(proxy).to.not.have.property('hop-interval-max'); + } + }); + it('accepts every Clash-supported proxy type as inline objects', function () { const supportedTypes = [ 'tailscale', @@ -185,4 +245,3 @@ FINAL,DIRECT } }); }); - diff --git a/backend/src/test/proxy-parsers/uri.spec.js b/backend/src/test/proxy-parsers/uri.spec.js index cb0b10ac..30eb73b2 100644 --- a/backend/src/test/proxy-parsers/uri.spec.js +++ b/backend/src/test/proxy-parsers/uri.spec.js @@ -282,6 +282,38 @@ describe('Proxy URI parser coverage', function () { expect(proxy.port).to.be.within(8443, 8445); }); + it('splits Hysteria2 URI hop-interval ranges during normalization', function () { + const proxy = parseOne( + 'hy2://hy2-secret@hy2.example.com:443?hop-interval=15-30#Hy2%20Hop%20Range', + ); + + expectSubset(proxy, { + type: 'hysteria2', + name: 'Hy2 Hop Range', + server: 'hy2.example.com', + port: 443, + password: 'hy2-secret', + 'hop-interval': 15, + 'hop-interval-max': 30, + }); + }); + + it('drops invalid Hysteria2 URI hop-interval values during normalization', function () { + const proxy = parseOne( + 'hy2://hy2-secret@hy2.example.com:443?hop-interval=30-15#Hy2%20Invalid%20Hop', + ); + + expectSubset(proxy, { + type: 'hysteria2', + name: 'Hy2 Invalid Hop', + server: 'hy2.example.com', + port: 443, + password: 'hy2-secret', + }); + expect(proxy).to.not.have.property('hop-interval'); + expect(proxy).to.not.have.property('hop-interval-max'); + }); + it('parses Hysteria2 URIs with mport overrides', function () { const proxy = parseOne( 'hy2://hy2-secret@hy2.example.com:443?mport=9000,9002-9004#Hy2%20Mport',