From d0c5ccf5e27b39fa278a19051b2bd7ffec14af8a Mon Sep 17 00:00:00 2001 From: xream Date: Fri, 1 May 2026 14:55:45 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BC=98=E5=8C=96=20Surge=20=E4=B8=8E?= =?UTF-8?q?=20SurgeMac=20=E7=9A=84=E4=B8=8D=E6=94=AF=E6=8C=81=E4=BB=A3?= =?UTF-8?q?=E7=90=86=E5=A4=84=E7=90=86=E5=92=8C=E9=99=8D=E7=BA=A7=E9=80=BB?= =?UTF-8?q?=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 将 Surge 生成器中的不支持错误统一为 SurgeUnsupportedProxyError,便于上层识别兼容性限制 - SurgeMac 在 useMihomoExternal 场景下仅对不支持错误尝试回退到 Mihomo,否则保留原异常 - 回退失败时给出更明确的手动切换提示,降低误判与排障成本 - 调整部分告警级别为 warn,避免非法 UUID/非 IP 地址误导用户 - 新增测试,覆盖 SurgeMac 不支持节点在 external 模式下不再强制包装为 Mihomo --- backend/src/core/proxy-utils/index.js | 4 +-- .../src/core/proxy-utils/producers/surge.js | 31 +++++++++++++------ .../core/proxy-utils/producers/surgemac.js | 23 ++++++++++---- backend/src/test/proxy-producers/text.spec.js | 21 +++++++++++++ 4 files changed, 61 insertions(+), 18 deletions(-) diff --git a/backend/src/core/proxy-utils/index.js b/backend/src/core/proxy-utils/index.js index 9e729e4a..b5a1e905 100644 --- a/backend/src/core/proxy-utils/index.js +++ b/backend/src/core/proxy-utils/index.js @@ -89,7 +89,7 @@ function parse(raw) { if (['vless', 'vmess'].includes(proxy.type)) { const isProxyUUIDValid = isValidUUID(proxy.uuid); if (!isProxyUUIDValid) { - $.info(`UUID may be invalid: ${proxy.name} ${proxy.uuid}`); + $.warn(`UUID may be invalid: ${proxy.name} ${proxy.uuid}`); } // return isProxyUUIDValid; } else if (['hysteria2'].includes(proxy.type)) { @@ -281,7 +281,7 @@ function produce(proxies, targetPlatform, type, opts = {}) { if (['vless', 'vmess'].includes(proxy.type)) { const isProxyUUIDValid = isValidUUID(proxy.uuid); if (!isProxyUUIDValid) - $.info(`UUID may be invalid: ${proxy.name} ${proxy.uuid}`); + $.warn(`UUID may be invalid: ${proxy.name} ${proxy.uuid}`); // return isProxyUUIDValid; const isVlessType = proxy.type === 'vless'; const realityChecks = isVlessType diff --git a/backend/src/core/proxy-utils/producers/surge.js b/backend/src/core/proxy-utils/producers/surge.js index 906c7ab1..6a05bae3 100644 --- a/backend/src/core/proxy-utils/producers/surge.js +++ b/backend/src/core/proxy-utils/producers/surge.js @@ -4,6 +4,17 @@ import $ from '@/core/app'; const targetPlatform = 'Surge'; +export class SurgeUnsupportedProxyError extends Error { + constructor(message) { + super(message); + this.name = 'SurgeUnsupportedProxyError'; + } +} + +function unsupported(message) { + return new SurgeUnsupportedProxyError(message); +} + const ipVersions = { dual: 'dual', ipv4: 'v4-only', @@ -18,7 +29,7 @@ export default function Surge_Producer() { ['ws'].includes(proxy.network) && proxy['ws-opts']?.['v2ray-http-upgrade'] ) { - throw new Error( + throw unsupported( `Platform ${targetPlatform} does not support network ${proxy.network} with http upgrade`, ); } @@ -62,14 +73,14 @@ export default function Surge_Producer() { (!['tcp'].includes(proxy.network) || (['tcp'].includes(proxy.network) && proxy['reality-opts'])) ) { - throw new Error( + throw unsupported( `Platform ${targetPlatform} does not support proxy type ${proxy.type} with network or reality`, ); } return anytls(proxy); } - throw new Error( + throw unsupported( `Platform ${targetPlatform} does not support proxy type: ${proxy.type}`, ); }; @@ -114,7 +125,7 @@ function shadowsocks(proxy) { '2022-blake3-aes-256-gcm', ].includes(proxy.cipher) ) { - throw new Error(`cipher ${proxy.cipher} is not supported`); + throw unsupported(`cipher ${proxy.cipher} is not supported`); } result.append(`,encrypt-method=${proxy.cipher}`); result.appendIfPresent(`,password="${proxy.password}"`, 'password'); @@ -140,7 +151,7 @@ function shadowsocks(proxy) { 'plugin-opts.path', ); } else if (!['shadow-tls'].includes(proxy.plugin)) { - throw new Error(`plugin ${proxy.plugin} is not supported`); + throw unsupported(`plugin ${proxy.plugin} is not supported`); } } @@ -194,7 +205,7 @@ function shadowsocks(proxy) { } if (version) { if (version < 2) { - throw new Error( + throw unsupported( `shadow-tls version ${version} is not supported`, ); } @@ -593,7 +604,7 @@ function ssh(proxy) { } function http(proxy) { if (proxy.headers && Object.keys(proxy.headers).length > 0) { - throw new Error(`headers is unsupported`); + throw unsupported(`headers is unsupported`); } const result = new Result(proxy); const type = proxy.tls ? 'https' : 'http'; @@ -1166,7 +1177,7 @@ function wireguard_surge(proxy) { function hysteria2(proxy) { if (proxy['obfs-password'] && proxy.obfs != 'salamander') { - throw new Error(`only salamander obfs is supported`); + throw unsupported(`only salamander obfs is supported`); } const result = new Result(proxy); @@ -1300,9 +1311,9 @@ function handleTransport(result, proxy, includeUnsupportedProxy) { ['tcp'].includes(proxy.network) && proxy['reality-opts'] ) { - throw new Error(`reality is unsupported`); + throw unsupported(`reality is unsupported`); } else if (!['tcp'].includes(proxy.network)) { - throw new Error(`network ${proxy.network} is unsupported`); + throw unsupported(`network ${proxy.network} is unsupported`); } } } diff --git a/backend/src/core/proxy-utils/producers/surgemac.js b/backend/src/core/proxy-utils/producers/surgemac.js index 5cb20fc1..e91ae727 100644 --- a/backend/src/core/proxy-utils/producers/surgemac.js +++ b/backend/src/core/proxy-utils/producers/surgemac.js @@ -1,6 +1,6 @@ import { Base64 } from 'js-base64'; import { Result, isPresent } from './utils'; -import Surge_Producer from './surge'; +import Surge_Producer, { SurgeUnsupportedProxyError } from './surge'; import ClashMeta_Producer from './clashmeta'; import { isIPv4, isIPv6 } from '@/utils'; import $ from '@/core/app'; @@ -23,16 +23,27 @@ export default function SurgeMac_Producer() { try { return surge_Producer.produce(proxy, type, opts); } catch (e) { - if (opts.useMihomoExternal) { + if ( + opts.useMihomoExternal && + e instanceof SurgeUnsupportedProxyError + ) { + const output = mihomo(proxy, type, opts); + if (!output) { + throw e; + } $.log( `${proxy.name} is not supported on ${targetPlatform}, try to use Mihomo(SurgeMac - External Proxy Program) instead`, ); - return mihomo(proxy, type, opts); - } else { + return output; + } + + if (e instanceof SurgeUnsupportedProxyError) { throw new Error( - `Surge for macOS 可手动指定链接参数 target=SurgeMac 或在 同步配置 中指定 SurgeMac 来启用 mihomo 支援 Surge 本身不支持的协议`, + `${e.message}. Surge for macOS 可手动指定链接参数 target=SurgeMac 或在 同步配置 中指定 SurgeMac 来启用 mihomo 支援 Surge 本身不支持的协议`, ); } + + throw e; } } } @@ -187,7 +198,7 @@ function mihomo(proxy, type, opts) { if (isIP(proxy.server)) { external_proxy.addresses.push(proxy.server); } else { - $.log( + $.warn( `Platform ${targetPlatform}, proxy type ${proxy.type}: addresses should be an IP address, but got ${proxy.server}`, ); } diff --git a/backend/src/test/proxy-producers/text.spec.js b/backend/src/test/proxy-producers/text.spec.js index 373961c5..bfe01078 100644 --- a/backend/src/test/proxy-producers/text.spec.js +++ b/backend/src/test/proxy-producers/text.spec.js @@ -610,6 +610,27 @@ describe('Proxy text producers', function () { expect(output).to.include('udp-relay=true'); }); + it('does not wrap malformed SurgeMac nodes with Mihomo external mode', function () { + const output = ProxyUtils.produce( + [ + { + type: 'ss', + name: 'Broken Obfs', + server: 'ss.example.com', + port: 8388, + cipher: 'aes-128-gcm', + password: 'secret', + plugin: 'obfs', + }, + ], + 'SurgeMac', + 'external', + { useMihomoExternal: true, localPort: 16666 }, + ); + + expect(output).to.equal(''); + }); + it('forces SurgeMac nodes with _mihomoExternal to use Mihomo external mode', function () { const output = produceExternal( 'SurgeMac',