From 75012503f8bdddcc04f76a05a933d9cc56561721 Mon Sep 17 00:00:00 2001 From: xream Date: Wed, 8 Jan 2025 19:49:34 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=20Clash=20Pre-process?= =?UTF-8?q?or?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/package.json | 2 +- .../core/proxy-utils/preprocessors/index.js | 53 ++++++++++--------- backend/src/utils/download.js | 2 +- 3 files changed, 31 insertions(+), 26 deletions(-) diff --git a/backend/package.json b/backend/package.json index 934d2ec..e470960 100644 --- a/backend/package.json +++ b/backend/package.json @@ -1,6 +1,6 @@ { "name": "sub-store", - "version": "2.16.3", + "version": "2.16.4", "description": "Advanced Subscription Manager for QX, Loon, Surge, Stash and ShadowRocket.", "main": "src/main.js", "scripts": { diff --git a/backend/src/core/proxy-utils/preprocessors/index.js b/backend/src/core/proxy-utils/preprocessors/index.js index d2fb84c..6067b1e 100644 --- a/backend/src/core/proxy-utils/preprocessors/index.js +++ b/backend/src/core/proxy-utils/preprocessors/index.js @@ -48,7 +48,7 @@ function Clash() { const content = safeLoad(raw); return content.proxies && Array.isArray(content.proxies); }; - const parse = function (raw) { + const parse = function (raw, includeProxies) { // Clash YAML format // 防止 VLESS节点 reality-opts 选项中的 short-id 被解析成 Infinity @@ -56,35 +56,40 @@ function Clash() { const afterReplace = raw.replace( /short-id:([ ]*[^,\n}]*)/g, (matched, value) => { - const afterTrim = value.trim(); - - // 为空 - if (!afterTrim || afterTrim === '') { - return 'short-id: ""' - } - - // 是否被引号包裹 - if (/^(['"]).*\1$/.test(afterTrim)) { - return `short-id: ${afterTrim}`; - } else { - return `short-id: "${afterTrim}"` - } - } + const afterTrim = value.trim(); + + // 为空 + if (!afterTrim || afterTrim === '') { + return 'short-id: ""'; + } + + // 是否被引号包裹 + if (/^(['"]).*\1$/.test(afterTrim)) { + return `short-id: ${afterTrim}`; + } else { + return `short-id: "${afterTrim}"`; + } + }, ); const { proxies, 'global-client-fingerprint': globalClientFingerprint, } = safeLoad(afterReplace); - return proxies - .map((p) => { - // https://github.com/MetaCubeX/mihomo/blob/Alpha/docs/config.yaml#L73C1-L73C26 - if (globalClientFingerprint && !p['client-fingerprint']) { - p['client-fingerprint'] = globalClientFingerprint; - } - return JSON.stringify(p); - }) - .join('\n'); + return ( + (includeProxies ? 'proxies:\n' : '') + + proxies + .map((p) => { + // https://github.com/MetaCubeX/mihomo/blob/Alpha/docs/config.yaml#L73C1-L73C26 + if (globalClientFingerprint && !p['client-fingerprint']) { + p['client-fingerprint'] = globalClientFingerprint; + } + return `${includeProxies ? ' - ' : ''}${JSON.stringify( + p, + )}\n`; + }) + .join('') + ); }; return { name, test, parse }; } diff --git a/backend/src/utils/download.js b/backend/src/utils/download.js index a6dcb70..dadae3f 100644 --- a/backend/src/utils/download.js +++ b/backend/src/utils/download.js @@ -207,7 +207,7 @@ export default async function download( if (preprocess) { try { if (clashPreprocessor.test(body)) { - body = clashPreprocessor.parse(body); + body = clashPreprocessor.parse(body, true); } } catch (e) { $.error(`Clash Pre-processor error: ${e}`);