From 04f44c2ae24d8af6e059eac1b53007c5a9bb76b1 Mon Sep 17 00:00:00 2001 From: xream Date: Sat, 26 Sep 2026 12:03:36 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E8=B0=83=E6=95=B4=20sing-box=20SSH=20?= =?UTF-8?q?=E5=AF=86=E9=92=A5=E9=80=BB=E8=BE=91?= 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 | 12 +++-- .../test/proxy-producers/structured.spec.js | 54 +++++++++++++++++++ backend/src/test/proxy-producers/text.spec.js | 24 +++++---- 4 files changed, 79 insertions(+), 13 deletions(-) diff --git a/backend/package.json b/backend/package.json index 06df9b81..5b101d9f 100644 --- a/backend/package.json +++ b/backend/package.json @@ -1,6 +1,6 @@ { "name": "sub-store", - "version": "2.41.0", + "version": "2.41.2", "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 afa1e5df..601eac21 100644 --- a/backend/src/core/proxy-utils/producers/sing-box.js +++ b/backend/src/core/proxy-utils/producers/sing-box.js @@ -431,9 +431,15 @@ const sshParser = (proxy = {}) => { if (proxy.password) parsedProxy.password = proxy.password; // https://wiki.metacubex.one/config/proxies/ssh // https://sing-box.sagernet.org/zh/configuration/outbound/ssh - if (proxy['privateKey']) parsedProxy.private_key_path = proxy['privateKey']; - if (proxy['private-key']) - parsedProxy.private_key_path = proxy['private-key']; + const privateKey = proxy['private-key'] || proxy.privateKey; + if (privateKey) { + // 不严谨 但是跟 mihomo 判断逻辑一致 + parsedProxy[ + privateKey.includes('PRIVATE KEY') + ? 'private_key' + : 'private_key_path' + ] = privateKey; + } if (proxy['private-key-passphrase']) parsedProxy.private_key_passphrase = proxy['private-key-passphrase']; if (proxy['server-fingerprint']) { diff --git a/backend/src/test/proxy-producers/structured.spec.js b/backend/src/test/proxy-producers/structured.spec.js index 1a5e42af..9bc0fccc 100644 --- a/backend/src/test/proxy-producers/structured.spec.js +++ b/backend/src/test/proxy-producers/structured.spec.js @@ -4029,6 +4029,60 @@ describe('Proxy structured producers', function () { expect(errors).to.have.length(0); }); + it('distinguishes sing-box SSH private key content from paths', function () { + const privateKey = + '-----BEGIN OPENSSH PRIVATE KEY-----\ntest-key-data\n-----END OPENSSH PRIVATE KEY-----'; + const encryptedKey = + '-----BEGIN ENCRYPTED PRIVATE KEY-----\ntest-key-data\n-----END ENCRYPTED PRIVATE KEY-----'; + for (const [options, expected] of [ + [{ 'private-key': privateKey }, { private_key: privateKey }], + [{ 'private-key': encryptedKey }, { private_key: encryptedKey }], + [{ 'private-key': './id_rsa' }, { private_key_path: './id_rsa' }], + [ + { 'private-key': '/keys/id_ed25519' }, + { private_key_path: '/keys/id_ed25519' }, + ], + [{ privateKey }, { private_key: privateKey }], + [{ privateKey: './id_rsa' }, { private_key_path: './id_rsa' }], + [ + { 'private-key': privateKey, privateKey: './id_rsa' }, + { private_key: privateKey }, + ], + [ + { 'private-key': './id_rsa', privateKey }, + { private_key_path: './id_rsa' }, + ], + [ + { 'private-key': '', privateKey: './id_rsa' }, + { private_key_path: './id_rsa' }, + ], + [{ 'private-key': '' }, {}], + [{}, {}], + ]) { + const output = loadProducedJson('sing-box', { + type: 'ssh', + name: 'SSH', + server: 'ssh.example.com', + port: 22, + username: 'user', + 'private-key-passphrase': 'test-passphrase', + ...options, + }); + + expect(output.outbounds).to.deep.equal([ + { + type: 'ssh', + tag: 'SSH', + server: 'ssh.example.com', + server_port: 22, + user: 'user', + private_key_passphrase: 'test-passphrase', + ...expected, + }, + ]); + } + }); + it('preserves supported HTTP root headers for sing-box and JSON outputs', function () { const buildProxy = (name) => ({ type: 'http', diff --git a/backend/src/test/proxy-producers/text.spec.js b/backend/src/test/proxy-producers/text.spec.js index 7efea45b..53084acc 100644 --- a/backend/src/test/proxy-producers/text.spec.js +++ b/backend/src/test/proxy-producers/text.spec.js @@ -864,7 +864,9 @@ describe('Proxy text producers', function () { }, ]); - expect(output.match(/tls-profile=chrome147(?=,|$)/gm)).to.have.length(9); + expect(output.match(/tls-profile=chrome147(?=,|$)/gm)).to.have.length( + 9, + ); expect(output.match(/alpn="http\/1\.1,h2,h3"/g)).to.have.length(9); }); @@ -906,7 +908,9 @@ describe('Proxy text producers', function () { expect(output).to.include('tls-profile=ios18'); expect(output).to.include('tls-profile=default'); expect(output).to.match(/tls-profile=chrome(?=,|$)/m); - expect(output.match(/tls-profile=chrome147(?=,|$)/gm)).to.have.length(2); + expect(output.match(/tls-profile=chrome147(?=,|$)/gm)).to.have.length( + 2, + ); expect(output).to.include('tls-profile=ios26'); expect(output.match(/tls-profile=/g)).to.have.length(6); }); @@ -1079,8 +1083,10 @@ describe('Proxy text producers', function () { ? [`,udp-relay=${udp}`] : []; - expect(output.match(/,udp-relay=[^,\n]+/g) || [], output) - .to.deep.equal(expected); + expect( + output.match(/,udp-relay=[^,\n]+/g) || [], + output, + ).to.deep.equal(expected); } } } @@ -2474,7 +2480,7 @@ describe('Proxy text producers', function () { expect(output).to.equal( `ss://${Base64.encode( 'aes-128-gcm:secret', - )}@ss.example.com:443/?plugin=${plugin}#SS%20V2ray%20Flags`, + )}@ss.example.com:443/?plugin=${plugin}&udp=0#SS%20V2ray%20Flags`, ); }); @@ -2534,7 +2540,7 @@ describe('Proxy text producers', function () { expect(output).to.equal( `ss://${Base64.encode( 'aes-128-gcm:secret', - )}@ss-upgrade.example.com:443?sni=ss-upgrade.example.com&type=httpupgrade&path=%2Fupgrade%3Fa%3D1%26b%3D2%26ed%3D1024&host=upgrade.example.com&security=tls#SS%20Upgrade`, + )}@ss-upgrade.example.com:443?udp=0&sni=ss-upgrade.example.com&type=httpupgrade&path=%2Fupgrade%3Fa%3D1%26b%3D2%26ed%3D1024&host=upgrade.example.com&security=tls#SS%20Upgrade`, ); const reparsed = ProxyUtils.parse(output)[0]; @@ -2572,7 +2578,7 @@ describe('Proxy text producers', function () { expect(output).to.equal( `ss://${Base64.encode( 'aes-128-gcm:secret', - )}@ss-ws.example.com:443?sni=ss-ws.example.com&type=ws&path=%2Fws%3Fa%3D1%26b%3D2%26ed%3D2048&host=cdn.example.com&security=tls#SS%20WS%20Early`, + )}@ss-ws.example.com:443?udp=0&sni=ss-ws.example.com&type=ws&path=%2Fws%3Fa%3D1%26b%3D2%26ed%3D2048&host=cdn.example.com&security=tls#SS%20WS%20Early`, ); const reparsed = ProxyUtils.parse(output)[0]; @@ -2700,12 +2706,12 @@ describe('Proxy text producers', function () { expect(muxOnOutput).to.equal( `ss://${Base64.encode( 'aes-128-gcm:secret', - )}@ss.example.com:443/?plugin=${muxOnPlugin}#SS%20Boolean%20Mux%20On`, + )}@ss.example.com:443/?plugin=${muxOnPlugin}&udp=0#SS%20Boolean%20Mux%20On`, ); expect(muxOffOutput).to.equal( `ss://${Base64.encode( 'aes-128-gcm:secret', - )}@ss.example.com:443/?plugin=${muxOffPlugin}#SS%20Boolean%20Mux%20Off`, + )}@ss.example.com:443/?plugin=${muxOffPlugin}&udp=0#SS%20Boolean%20Mux%20Off`, ); });