From e043346d1360f4710d51213757099ddff2ce3f5e Mon Sep 17 00:00:00 2001 From: xream Date: Thu, 5 Nov 2026 19:33:08 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20mihomo=20=E6=94=AF=E6=8C=81=20Snell=20v?= =?UTF-8?q?6?= 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 | 8 +- .../core/proxy-utils/producers/clashmeta.js | 52 +++++-- .../test/proxy-producers/structured.spec.js | 131 +++++++++++++++++- 4 files changed, 171 insertions(+), 22 deletions(-) diff --git a/backend/package.json b/backend/package.json index 684bec32..d1e6b1e9 100644 --- a/backend/package.json +++ b/backend/package.json @@ -1,6 +1,6 @@ { "name": "sub-store", - "version": "2.44.2", + "version": "2.44.3", "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 6b6b9d41..51cd6861 100644 --- a/backend/src/core/proxy-utils/index.js +++ b/backend/src/core/proxy-utils/index.js @@ -870,12 +870,8 @@ function lastParse(proxy) { !proxy.plugin ) { proxy.plugin = 'shadow-tls'; - proxy['plugin-opts'] = { - host: proxy['obfs-opts'].host, - password: proxy['obfs-opts'].password, - version: proxy['obfs-opts'].version, - alpn: proxy['obfs-opts'].alpn, - }; + proxy['plugin-opts'] = { ...proxy['obfs-opts'] }; + delete proxy['plugin-opts'].mode; delete proxy['obfs-opts']; } if (proxy.plugin === 'shadow-tls' && proxy['plugin-opts']) { diff --git a/backend/src/core/proxy-utils/producers/clashmeta.js b/backend/src/core/proxy-utils/producers/clashmeta.js index 68d2db7a..4d10a886 100644 --- a/backend/src/core/proxy-utils/producers/clashmeta.js +++ b/backend/src/core/proxy-utils/producers/clashmeta.js @@ -1,3 +1,4 @@ +import { Buffer } from 'buffer'; import { getWireGuardAddressWithCIDR, isPresent, @@ -78,7 +79,7 @@ export default function ClashMeta_Producer() { return false; } else if ( proxy.type === 'snell' && - !isSupportedMihomoVersion(proxy.version, [1, 2, 3, 4, 5]) + !supportsMihomoSnell(proxy) ) { return false; } else if ( @@ -250,8 +251,6 @@ export default function ClashMeta_Producer() { proxy['pre-shared-key'] = proxy['preshared-key']; proxy.ip = getWireGuardAddressWithCIDR(proxy, 'ipv4'); proxy.ipv6 = getWireGuardAddressWithCIDR(proxy, 'ipv6'); - } else if (proxy.type === 'snell' && proxy.version < 3) { - delete proxy.udp; } else if (proxy.type === 'vless') { if (isPresent(proxy, 'sni')) { proxy.servername = proxy.sni; @@ -300,17 +299,19 @@ export default function ClashMeta_Producer() { } if (proxy.type === 'snell') { + const version = Number(proxy.version ?? 1); + if (proxy.version != null && Number.isInteger(version)) { + proxy.version = version; + } + if (version < 3) delete proxy.udp; + if (version < 6) delete proxy.mode; + const shadowTLSOpts = getMihomoShadowTlsOpts(proxy); if (shadowTLSOpts) { proxy['obfs-opts'] = { + ...shadowTLSOpts, mode: 'shadow-tls', - host: shadowTLSOpts.host, - password: shadowTLSOpts.password, - version: shadowTLSOpts.version, }; - if (shadowTLSOpts.alpn) { - proxy['obfs-opts'].alpn = shadowTLSOpts.alpn; - } delete proxy.plugin; delete proxy['plugin-opts']; } @@ -475,6 +476,39 @@ function isSupportedMihomoVersion(version, supportedVersions) { return Number.isInteger(parsed) && supportedVersions.includes(parsed); } +function supportsMihomoSnell(proxy) { + if (!isSupportedMihomoVersion(proxy.version, [0, 1, 2, 3, 4, 5, 6])) { + return false; + } + + const isV6 = Number(proxy.version) === 6; + const { psk, mode, 'obfs-opts': obfsOpts } = proxy; + const obfsModes = ['', 'shadow-tls', 'restls', 'jls']; + if (!isV6) obfsModes.push('http', 'tls'); + + let reason; + if (typeof psk !== 'string' && !Number.isFinite(psk)) { + reason = 'missing or invalid PSK'; + } else if (obfsOpts != null && !isPlainObject(obfsOpts)) { + reason = 'obfs-opts must be a mapping'; + } else if (!obfsModes.includes(obfsOpts?.mode ?? '')) { + reason = 'unsupported obfs-opts.mode'; + } else if (isV6) { + const pskLength = Buffer.byteLength(String(psk), 'utf8'); + if (pskLength < 12 || pskLength > 255) { + reason = 'PSK must be 12 to 255 UTF-8 bytes'; + } else if ( + !['', 'default', 'unshaped', 'unsafe-raw'].includes(mode ?? '') + ) { + reason = 'unsupported mode'; + } + } + + if (!reason) return true; + $.error(`Mihomo Snell proxy ${proxy.name} has been filtered. ${reason}.`); + return false; +} + function hasMihomoShadowTls(proxy) { return Boolean(getMihomoShadowTlsOpts(proxy)); } diff --git a/backend/src/test/proxy-producers/structured.spec.js b/backend/src/test/proxy-producers/structured.spec.js index 17c13d7e..63845e3f 100644 --- a/backend/src/test/proxy-producers/structured.spec.js +++ b/backend/src/test/proxy-producers/structured.spec.js @@ -327,21 +327,19 @@ describe('Proxy structured producers', function () { } }); - it('keeps Mihomo Snell versions 1 through 5 and Stash versions 1 through 6', function () { + it('keeps Snell versions 1 through 6 for Mihomo and Stash', function () { const proxies = [1, 2, 3, 4, 5, 6, 7].map((version) => ({ type: 'snell', name: `Snell ${version}`, server: 'snell.example.com', port: 44046, - psk: 'secret', + psk: 'snell-test-psk', version, udp: true, })); - for (const [platform, expectedVersions] of [ - ['Mihomo', [1, 2, 3, 4, 5]], - ['Stash', [1, 2, 3, 4, 5, 6]], - ]) { + const expectedVersions = [1, 2, 3, 4, 5, 6]; + for (const platform of ['Mihomo', 'Stash']) { const internal = produceInternal( platform, proxies.map((proxy) => ({ ...proxy })), @@ -376,6 +374,127 @@ describe('Proxy structured producers', function () { } }); + it('round-trips Mihomo Snell v6 modes and obfs options', function () { + const configs = [ + {}, + { + mode: 'default', + 'obfs-opts': { + mode: 'shadow-tls', + host: 'mask.example.com', + password: 'shadow-pass', + version: 3, + alpn: [], + 'name-cert-verify': 'cert.example.com', + 'skip-cert-verify': false, + }, + }, + { + mode: 'unshaped', + 'obfs-opts': { + mode: 'restls', + host: 'mask.example.com', + password: 'restls-pass', + 'version-hint': 'tls13', + }, + }, + { + mode: 'unsafe-raw', + 'obfs-opts': { + mode: 'jls', + host: 'mask.example.com', + username: 'test-user', + password: 'jls-pass', + }, + }, + ].map((config, index) => ({ + type: 'snell', + name: `Snell v6 ${index}`, + server: 'snell.example.com', + port: 44046, + version: 6, + psk: '测试密码', + udp: index % 2 === 0, + reuse: index % 2 !== 0, + ...config, + })); + const proxies = ProxyUtils.parse( + ProxyUtils.yaml.safeDump({ proxies: configs }), + ); + + expect(loadProducedYaml('Mihomo', proxies).proxies).to.deep.equal( + configs, + ); + }); + + it('filters invalid Mihomo Snell v6 settings and removes mode from older versions', function () { + const proxies = [ + { mode: 'unknown' }, + { 'obfs-opts': { mode: 'http' } }, + { 'obfs-opts': { mode: 'tls' } }, + { psk: 'x'.repeat(11) }, + { psk: 'x'.repeat(256) }, + { psk: 'x'.repeat(255) }, + { version: 5 }, + ].map((config, index) => ({ + type: 'snell', + name: `Snell ${index}`, + server: 'snell.example.com', + port: 44046, + version: '6', + mode: 'unshaped', + psk: 'snell-test-psk', + ...config, + })); + const { result } = captureErrors(() => + produceInternal('Mihomo', proxies), + ); + + expect( + result.map(({ name, version, mode }) => [name, version, mode]), + ).to.deep.equal([ + ['Snell 5', 6, 'unshaped'], + ['Snell 6', 5, undefined], + ]); + }); + + it('validates legacy Mihomo Snell options and handles the default version', function () { + const proxies = [ + { name: 'default', 'obfs-opts': { mode: 'http' } }, + { name: 'zero', version: 0, 'obfs-opts': { mode: 'tls' } }, + { name: 'v5', version: ' 5 ', 'obfs-opts': { mode: 'http' } }, + { + name: 'invalid obfs', + version: 5, + 'obfs-opts': { mode: 'unknown' }, + }, + { name: 'invalid obfs options', version: 4, 'obfs-opts': 'http' }, + { name: 'missing PSK', version: 3, psk: undefined }, + ].map((config) => ({ + type: 'snell', + server: 'snell.example.com', + port: 44046, + psk: 'short', + udp: true, + ...config, + })); + + const { result } = captureErrors(() => + loadProducedYaml('Mihomo', proxies), + ); + expect( + result.proxies.map(({ name, version, udp }) => [ + name, + version, + udp, + ]), + ).to.deep.equal([ + ['default', undefined, undefined], + ['zero', 0, undefined], + ['v5', 5, true], + ]); + }); + it('keeps supported Snell in sing-box by default', function () { const proxy = { type: 'snell',