fix: 修复 Clash Pre-processor

This commit is contained in:
xream 2025-01-08 19:49:34 +08:00
parent 85a3e2ee54
commit 75012503f8
3 changed files with 31 additions and 26 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "sub-store", "name": "sub-store",
"version": "2.16.3", "version": "2.16.4",
"description": "Advanced Subscription Manager for QX, Loon, Surge, Stash and ShadowRocket.", "description": "Advanced Subscription Manager for QX, Loon, Surge, Stash and ShadowRocket.",
"main": "src/main.js", "main": "src/main.js",
"scripts": { "scripts": {

View File

@ -48,7 +48,7 @@ function Clash() {
const content = safeLoad(raw); const content = safeLoad(raw);
return content.proxies && Array.isArray(content.proxies); return content.proxies && Array.isArray(content.proxies);
}; };
const parse = function (raw) { const parse = function (raw, includeProxies) {
// Clash YAML format // Clash YAML format
// 防止 VLESS节点 reality-opts 选项中的 short-id 被解析成 Infinity // 防止 VLESS节点 reality-opts 选项中的 short-id 被解析成 Infinity
@ -56,35 +56,40 @@ function Clash() {
const afterReplace = raw.replace( const afterReplace = raw.replace(
/short-id:([ ]*[^,\n}]*)/g, /short-id:([ ]*[^,\n}]*)/g,
(matched, value) => { (matched, value) => {
const afterTrim = value.trim(); const afterTrim = value.trim();
// 为空 // 为空
if (!afterTrim || afterTrim === '') { if (!afterTrim || afterTrim === '') {
return 'short-id: ""' return 'short-id: ""';
} }
// 是否被引号包裹 // 是否被引号包裹
if (/^(['"]).*\1$/.test(afterTrim)) { if (/^(['"]).*\1$/.test(afterTrim)) {
return `short-id: ${afterTrim}`; return `short-id: ${afterTrim}`;
} else { } else {
return `short-id: "${afterTrim}"` return `short-id: "${afterTrim}"`;
} }
} },
); );
const { const {
proxies, proxies,
'global-client-fingerprint': globalClientFingerprint, 'global-client-fingerprint': globalClientFingerprint,
} = safeLoad(afterReplace); } = safeLoad(afterReplace);
return proxies return (
.map((p) => { (includeProxies ? 'proxies:\n' : '') +
// https://github.com/MetaCubeX/mihomo/blob/Alpha/docs/config.yaml#L73C1-L73C26 proxies
if (globalClientFingerprint && !p['client-fingerprint']) { .map((p) => {
p['client-fingerprint'] = globalClientFingerprint; // https://github.com/MetaCubeX/mihomo/blob/Alpha/docs/config.yaml#L73C1-L73C26
} if (globalClientFingerprint && !p['client-fingerprint']) {
return JSON.stringify(p); p['client-fingerprint'] = globalClientFingerprint;
}) }
.join('\n'); return `${includeProxies ? ' - ' : ''}${JSON.stringify(
p,
)}\n`;
})
.join('')
);
}; };
return { name, test, parse }; return { name, test, parse };
} }

View File

@ -207,7 +207,7 @@ export default async function download(
if (preprocess) { if (preprocess) {
try { try {
if (clashPreprocessor.test(body)) { if (clashPreprocessor.test(body)) {
body = clashPreprocessor.parse(body); body = clashPreprocessor.parse(body, true);
} }
} catch (e) { } catch (e) {
$.error(`Clash Pre-processor error: ${e}`); $.error(`Clash Pre-processor error: ${e}`);