From 2a173b88dcbd7d232864f05087998855da59eec1 Mon Sep 17 00:00:00 2001 From: xream Date: Mon, 16 Nov 2026 15:49:33 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20Surge/Surfboard=20=E5=A4=84=E7=90=86?= =?UTF-8?q?=E4=B8=8D=E5=90=88=E6=B3=95=E7=9A=84=E8=8A=82=E7=82=B9=E5=90=8D?= 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 | 18 ++++++++- .../core/proxy-utils/producers/surfboard.js | 1 - .../src/core/proxy-utils/producers/surge.js | 1 - backend/src/test/proxy-producers/text.spec.js | 38 +++++++++++++++++-- 5 files changed, 53 insertions(+), 7 deletions(-) diff --git a/backend/package.json b/backend/package.json index 43e4a91c..ccbf96ca 100644 --- a/backend/package.json +++ b/backend/package.json @@ -1,6 +1,6 @@ { "name": "sub-store", - "version": "2.44.15", + "version": "2.44.16", "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 56ed2349..84fa6a86 100644 --- a/backend/src/core/proxy-utils/index.js +++ b/backend/src/core/proxy-utils/index.js @@ -422,6 +422,13 @@ function normalizeResponse(response = {}) { return normalized; } +function sanitizeSurgeProxyName(name) { + return name + .replace(/[=,]/g, '') + .replace(/(^|\s)(?:[#;]|\/\/)+/g, '$1') + .trim(); +} + function produce(proxies, targetPlatform, type, opts = {}) { const producer = PROXY_PRODUCERS[targetPlatform]; if (!producer) { @@ -429,6 +436,9 @@ function produce(proxies, targetPlatform, type, opts = {}) { } const normalizedTarget = String(targetPlatform).toLowerCase(); + const isSurgeStyleTarget = ['surge', 'surgemac', 'surfboard'].includes( + normalizedTarget, + ); // filter unsupported proxies proxies = proxies.filter((proxy) => { @@ -542,6 +552,9 @@ function produce(proxies, targetPlatform, type, opts = {}) { proxies = proxies.map((proxy) => { proxy._resolved = proxy.resolved; + if (isSurgeStyleTarget && typeof proxy.name === 'string') { + proxy.name = sanitizeSurgeProxyName(proxy.name); + } if (!isNotBlank(proxy.name)) { proxy.name = `${proxy.type} ${proxy.server}:${proxy.port}`; } @@ -604,7 +617,10 @@ function produce(proxies, targetPlatform, type, opts = {}) { list.push( SurgeMac_Producer().produce( { - name: opts._merged.name, + name: + sanitizeSurgeProxyName( + String(opts._merged.name ?? ''), + ) || 'mihomo merged', type: 'external', udp: true, exec: opts._merged.exec, diff --git a/backend/src/core/proxy-utils/producers/surfboard.js b/backend/src/core/proxy-utils/producers/surfboard.js index 8bb9d9aa..e32b123e 100644 --- a/backend/src/core/proxy-utils/producers/surfboard.js +++ b/backend/src/core/proxy-utils/producers/surfboard.js @@ -34,7 +34,6 @@ export default function Surfboard_Producer() { `Platform ${targetPlatform} does not support network ${proxy.network} with http upgrade`, ); } - proxy.name = proxy.name.replace(/=|,/g, ''); switch (proxy.type) { case 'ss': return shadowsocks(proxy); diff --git a/backend/src/core/proxy-utils/producers/surge.js b/backend/src/core/proxy-utils/producers/surge.js index 26abc283..f3848b51 100644 --- a/backend/src/core/proxy-utils/producers/surge.js +++ b/backend/src/core/proxy-utils/producers/surge.js @@ -174,7 +174,6 @@ export default function Surge_Producer() { `Platform ${targetPlatform} does not support network ${proxy.network} with http upgrade`, ); } - proxy.name = proxy.name.replace(/=|,/g, ''); if (proxy.ports) { proxy.ports = String(proxy.ports); } diff --git a/backend/src/test/proxy-producers/text.spec.js b/backend/src/test/proxy-producers/text.spec.js index 522b6d48..d763737f 100644 --- a/backend/src/test/proxy-producers/text.spec.js +++ b/backend/src/test/proxy-producers/text.spec.js @@ -1123,6 +1123,35 @@ describe('Proxy text producers', function () { ); }); + it('removes comment markers from Surge and Surfboard proxy names', function () { + const names = [ + ['HK=, #1 /=,/Backup ;VIP', 'HK 1 Backup VIP'], + [' // #JP', 'JP'], + ['JP#2//A;B / C', 'JP#2//A;B / C'], + ['#//;', 'http example.com:443'], + ]; + const targets = [ + 'Surge', + 'surge', + 'SurgeMac', + 'Surfboard', + 'surfboard', + ]; + for (const target of targets) { + for (const [name, expected] of names) { + const output = produceExternal(target, { + type: 'http', + name, + server: 'example.com', + port: 443, + }); + expect(output, target).to.equal( + `${expected}=http,example.com,443`, + ); + } + } + }); + it('emits udp-relay only for applicable Surge policies', function () { // https://manual.nssurge.com/policies/udp.html const proxies = [ @@ -2377,7 +2406,7 @@ describe('Proxy text producers', function () { it('produces SurgeMac external lines', function () { const output = produceExternal('SurgeMac', { type: 'external', - name: 'External', + name: 'External #1', exec: '/usr/bin/ssh', 'local-port': 1080, args: ['-D', 'localhost:1080'], @@ -2389,7 +2418,7 @@ describe('Proxy text producers', function () { }); expect(output).to.equal( - 'External=external,exec="/usr/bin/ssh",local-port=1080,args="-D",args="localhost:1080",addresses=1.1.1.1,addresses=2001:db8::1,udp-relay=true,tfo=true,test-url=https://test.example.com,block-quic=on', + 'External 1=external,exec="/usr/bin/ssh",local-port=1080,args="-D",args="localhost:1080",addresses=1.1.1.1,addresses=2001:db8::1,udp-relay=true,tfo=true,test-url=https://test.example.com,block-quic=on', ); }); @@ -2851,7 +2880,10 @@ describe('Proxy text producers', function () { _config: override, })); - const output = ProxyUtils.produce(proxies, 'SurgeMac', 'external', {}); + const output = ProxyUtils.produce(proxies, 'SurgeMac', 'external', { + mergeName: 'Merged //Proxy', + }); + expect(output).to.include('\nMerged Proxy=external,'); const args = [...output.matchAll(/args="([^"]+)"/g)].map((m) => m[1]); const config = JSON.parse(Base64.decode(args[args.length - 1]));