From 30d4f9572ecf0b16a33ab95f8f7ab22209fbfb62 Mon Sep 17 00:00:00 2001 From: xream Date: Sun, 31 May 2026 21:49:23 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20sing-box=20=E6=94=AF=E6=8C=81=20hysteri?= =?UTF-8?q?a2=20=E7=9A=84=20obfs=20gecko=20-=20=E4=B8=BA=20gecko=20?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=20obfs-min-packet-size=20/=20obfs-max-packet?= =?UTF-8?q?-size=20=E5=8F=82=E6=95=B0=E8=A7=A3=E6=9E=90=E4=B8=8E=E6=A0=A1?= =?UTF-8?q?=E9=AA=8C=20-=20=E5=A4=8D=E7=94=A8=E5=AE=89=E5=85=A8=E6=95=B4?= =?UTF-8?q?=E6=95=B0=E8=A7=A3=E6=9E=90=EF=BC=8C=E8=BF=87=E6=BB=A4=E6=97=A0?= =?UTF-8?q?=E6=95=88=E5=80=BC=E5=B9=B6=E4=B8=8A=E6=8A=A5=E9=94=99=E8=AF=AF?= =?UTF-8?q?=20-=20=E5=BD=93=20max=20=E8=B6=85=E8=BF=87=202048=20=E6=97=B6?= =?UTF-8?q?=E8=87=AA=E5=8A=A8=20clamp=20=E5=B9=B6=E8=BE=93=E5=87=BA?= =?UTF-8?q?=E8=AD=A6=E5=91=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/package.json | 2 +- .../core/proxy-utils/producers/sing-box.js | 47 ++++- .../test/proxy-producers/structured.spec.js | 177 ++++++++++++++++++ 3 files changed, 224 insertions(+), 2 deletions(-) diff --git a/backend/package.json b/backend/package.json index 005461ae..6e505048 100644 --- a/backend/package.json +++ b/backend/package.json @@ -1,6 +1,6 @@ { "name": "sub-store", - "version": "2.23.30", + "version": "2.23.31", "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/producers/sing-box.js b/backend/src/core/proxy-utils/producers/sing-box.js index 19dc83a9..2c52116f 100644 --- a/backend/src/core/proxy-utils/producers/sing-box.js +++ b/backend/src/core/proxy-utils/producers/sing-box.js @@ -5,6 +5,7 @@ import { getWireGuardAddressWithCIDR, normalizePluginMuxValue } from './utils'; import { extractPathQueryParam, getSafeIntegerPathQueryParam, + parseSafeIntegerValue, } from '../transport-path'; const ipVersions = { @@ -927,6 +928,7 @@ const hysteriaParser = (proxy = {}) => { domainResolverParser(proxy, parsedProxy); return parsedProxy; }; + const hysteria2Parser = (proxy = {}) => { const parsedProxy = { tag: proxy.name, @@ -950,7 +952,50 @@ const hysteria2Parser = (proxy = {}) => { }); if (proxy.up) parsedProxy.up_mbps = parseInt(`${proxy.up}`, 10); if (proxy.down) parsedProxy.down_mbps = parseInt(`${proxy.down}`, 10); - if (proxy.obfs === 'salamander') parsedProxy.obfs.type = 'salamander'; + if (['salamander', 'gecko'].includes(proxy.obfs)) + parsedProxy.obfs.type = proxy.obfs; + if (proxy.obfs === 'gecko') { + const minRaw = proxy['obfs-min-packet-size']; + const maxRaw = proxy['obfs-max-packet-size']; + const hasMin = + minRaw !== undefined && minRaw !== null && `${minRaw}` !== ''; + const hasMax = + maxRaw !== undefined && maxRaw !== null && `${maxRaw}` !== ''; + if (hasMin || hasMax) { + const minPacketSize = hasMin + ? parseSafeIntegerValue(`${minRaw}`.trim()) + : undefined; + const rawMaxPacketSize = hasMax + ? parseSafeIntegerValue(`${maxRaw}`.trim()) + : undefined; + const maxPacketSize = + rawMaxPacketSize != null + ? Math.min(rawMaxPacketSize, 2048) + : rawMaxPacketSize; + const effectiveMinPacketSize = minPacketSize ?? 512; + const effectiveMaxPacketSize = maxPacketSize ?? 1200; + + if (hasMax && rawMaxPacketSize != null && rawMaxPacketSize > 2048) { + $.warn( + `Gecko obfs max packet size for proxy ${proxy.name} exceeds 2048, clamped to 2048: ${maxRaw}`, + ); + } + + if ( + (hasMin && (minPacketSize == null || minPacketSize <= 0)) || + (hasMax && + (rawMaxPacketSize == null || rawMaxPacketSize <= 0)) || + effectiveMaxPacketSize < effectiveMinPacketSize + ) { + $.error( + `Invalid obfs packet size for proxy ${proxy.name}: min=${minRaw} max=${maxRaw}`, + ); + } else { + if (hasMin) parsedProxy.obfs.min_packet_size = minPacketSize; + if (hasMax) parsedProxy.obfs.max_packet_size = maxPacketSize; + } + } + } if (proxy['obfs-password']) parsedProxy.obfs.password = proxy['obfs-password']; if (!parsedProxy.obfs.type) delete parsedProxy.obfs; diff --git a/backend/src/test/proxy-producers/structured.spec.js b/backend/src/test/proxy-producers/structured.spec.js index 21bbe707..00476eb3 100644 --- a/backend/src/test/proxy-producers/structured.spec.js +++ b/backend/src/test/proxy-producers/structured.spec.js @@ -2855,6 +2855,183 @@ describe('Proxy structured producers', function () { expect(tailscale).to.not.have.property('detour'); }); + it('emits boolean ssh_server for sing-box Tailscale endpoints', function () { + const output = loadProducedJson('sing-box', { + type: 'tailscale', + name: 'Tailscale SSH Boolean', + 'ssh-server': true, + }); + + const tailscale = output.endpoints.find( + (endpoint) => endpoint.tag === 'Tailscale SSH Boolean', + ); + + expect(tailscale.ssh_server).to.equal(true); + }); + + it('emits object ssh_server for sing-box Tailscale endpoints', function () { + const output = loadProducedJson('sing-box', { + type: 'tailscale', + name: 'Tailscale SSH Object', + 'ssh-server': { + enabled: false, + 'disable-pty': true, + 'disable-sftp': true, + 'disable-forwarding': true, + }, + }); + + const tailscale = output.endpoints.find( + (endpoint) => endpoint.tag === 'Tailscale SSH Object', + ); + + expect(tailscale.ssh_server).to.deep.equal({ + enabled: false, + disable_pty: true, + disable_sftp: true, + disable_forwarding: true, + }); + }); + + it('does not emit gecko packet sizes for sing-box when using default values', function () { + const output = loadProducedJson('sing-box', { + type: 'hysteria2', + name: 'Gecko Default Sizes', + server: 'hy2.example.com', + port: 443, + password: 'secret', + obfs: 'gecko', + 'obfs-password': 'mask', + }); + + const hysteria2 = output.outbounds.find( + (outbound) => outbound.tag === 'Gecko Default Sizes', + ); + + expectSubset(hysteria2, { + type: 'hysteria2', + obfs: { + type: 'gecko', + password: 'mask', + }, + }); + expect(hysteria2.obfs).to.not.have.property('min_packet_size'); + expect(hysteria2.obfs).to.not.have.property('max_packet_size'); + }); + + it('emits a single gecko packet size for sing-box and relies on the other default', function () { + const minOnlyOutput = loadProducedJson('sing-box', { + type: 'hysteria2', + name: 'Gecko Min Only', + server: 'hy2.example.com', + port: 443, + password: 'secret', + obfs: 'gecko', + 'obfs-password': 'mask', + 'obfs-min-packet-size': 600, + }); + const maxOnlyOutput = loadProducedJson('sing-box', { + type: 'hysteria2', + name: 'Gecko Max Only', + server: 'hy2.example.com', + port: 443, + password: 'secret', + obfs: 'gecko', + 'obfs-password': 'mask', + 'obfs-max-packet-size': 1300, + }); + + const minOnly = minOnlyOutput.outbounds.find( + (outbound) => outbound.tag === 'Gecko Min Only', + ); + const maxOnly = maxOnlyOutput.outbounds.find( + (outbound) => outbound.tag === 'Gecko Max Only', + ); + + expectSubset(minOnly, { + obfs: { + type: 'gecko', + password: 'mask', + min_packet_size: 600, + }, + }); + expect(minOnly.obfs).to.not.have.property('max_packet_size'); + + expectSubset(maxOnly, { + obfs: { + type: 'gecko', + password: 'mask', + max_packet_size: 1300, + }, + }); + expect(maxOnly.obfs).to.not.have.property('min_packet_size'); + }); + + it('does not emit invalid gecko packet sizes for sing-box', function () { + const { result, errors } = captureErrors(() => + produceInternal('sing-box', [ + { + type: 'hysteria2', + name: 'Gecko Invalid Decimal', + server: 'hy2.example.com', + port: 443, + password: 'secret', + obfs: 'gecko', + 'obfs-password': 'mask', + 'obfs-min-packet-size': '600.5', + }, + { + type: 'hysteria2', + name: 'Gecko Invalid Range', + server: 'hy2.example.com', + port: 443, + password: 'secret', + obfs: 'gecko', + 'obfs-password': 'mask', + 'obfs-min-packet-size': 1300, + }, + ]), + ); + + expect(errors).to.have.length(2); + expect(errors[0]).to.include('Gecko Invalid Decimal'); + expect(errors[1]).to.include('Gecko Invalid Range'); + for (const hysteria2 of result) { + expect(hysteria2.obfs).to.deep.equal({ + type: 'gecko', + password: 'mask', + }); + } + }); + + it('clamps oversized gecko max packet size for sing-box and logs a warning', function () { + const { result, warnings } = captureWarns(() => + produceInternal('sing-box', { + type: 'hysteria2', + name: 'Gecko Oversized Max', + server: 'hy2.example.com', + port: 443, + password: 'secret', + obfs: 'gecko', + 'obfs-password': 'mask', + 'obfs-min-packet-size': 1024, + 'obfs-max-packet-size': 4096, + }), + ); + + expect(warnings).to.have.length(1); + expect(warnings[0]).to.include('Gecko Oversized Max'); + expect(warnings[0]).to.include('clamped to 2048'); + expectSubset(result[0], { + obfs: { + type: 'gecko', + password: 'mask', + min_packet_size: 1024, + max_packet_size: 2048, + }, + }); + }); + it('emits WireGuard interface CIDR suffixes for Egern exports', function () { const proxies = [ {