From a3ec98caa9a3c7a4ad17b0f3f1093cec3e83905d Mon Sep 17 00:00:00 2001 From: xream Date: Fri, 27 Dec 2024 21:55:13 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20Clash=20=E8=AE=A2=E9=98=85=E4=BB=85?= =?UTF-8?q?=E7=BC=93=E5=AD=98=20proxies=20=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/package.json | 2 +- backend/src/products/resource-parser.loon.js | 11 ++++++++- backend/src/restful/file.js | 5 ++++ backend/src/restful/preview.js | 8 +++++++ backend/src/restful/sync.js | 3 +++ backend/src/utils/download.js | 24 ++++++++++++++++++-- 6 files changed, 49 insertions(+), 4 deletions(-) diff --git a/backend/package.json b/backend/package.json index c6b7c62..1450b1c 100644 --- a/backend/package.json +++ b/backend/package.json @@ -1,6 +1,6 @@ { "name": "sub-store", - "version": "2.15.0", + "version": "2.15.1", "description": "Advanced Subscription Manager for QX, Loon, Surge, Stash and ShadowRocket.", "main": "src/main.js", "scripts": { diff --git a/backend/src/products/resource-parser.loon.js b/backend/src/products/resource-parser.loon.js index c02c357..d373f1e 100644 --- a/backend/src/products/resource-parser.loon.js +++ b/backend/src/products/resource-parser.loon.js @@ -47,7 +47,16 @@ let resourceUrl = typeof $resourceUrl !== 'undefined' ? $resourceUrl : ''; if ((!result || /^\s*$/.test(result)) && resourceUrl) { console.log(`解析器: 尝试从 ${resourceUrl} 获取订阅`); try { - let raw = await download(resourceUrl, arg?.ua, arg?.timeout); + let raw = await download( + resourceUrl, + arg?.ua, + arg?.timeout, + undefined, + undefined, + undefined, + undefined, + true, + ); let proxies = ProxyUtils.parse(raw); result = ProxyUtils.produce(proxies, 'Loon', undefined, { 'include-unsupported-proxy': arg?.includeUnsupportedProxy, diff --git a/backend/src/restful/file.js b/backend/src/restful/file.js index b315078..87d7abc 100644 --- a/backend/src/restful/file.js +++ b/backend/src/restful/file.js @@ -62,6 +62,7 @@ async function getFile(req, res) { mergeSources, ignoreFailedRemoteFile, proxy, + noCache, } = req.query; let $options = {}; if (req.query.$options) { @@ -113,6 +114,9 @@ async function getFile(req, res) { ignoreFailedRemoteFile = decodeURIComponent(ignoreFailedRemoteFile); $.info(`指定忽略失败的远程文件: ${ignoreFailedRemoteFile}`); } + if (noCache) { + $.info(`指定不使用缓存: ${noCache}`); + } const allFiles = $.read(FILES_KEY); const file = findByName(allFiles, name); @@ -128,6 +132,7 @@ async function getFile(req, res) { ignoreFailedRemoteFile, $options, proxy, + noCache, }); try { diff --git a/backend/src/restful/preview.js b/backend/src/restful/preview.js index 7fae34d..20c0a63 100644 --- a/backend/src/restful/preview.js +++ b/backend/src/restful/preview.js @@ -114,6 +114,10 @@ async function compareSub(req, res) { sub.ua, undefined, sub.proxy, + undefined, + undefined, + undefined, + true, ); } catch (err) { errors[url] = err; @@ -219,6 +223,10 @@ async function compareCollection(req, res) { sub.ua, undefined, sub.proxy, + undefined, + undefined, + undefined, + true, ); } catch (err) { errors[url] = err; diff --git a/backend/src/restful/sync.js b/backend/src/restful/sync.js index d1d193b..1f2f433 100644 --- a/backend/src/restful/sync.js +++ b/backend/src/restful/sync.js @@ -74,6 +74,7 @@ async function produceArtifact({ undefined, awaitCustomCache, noCache, + true, ); } catch (err) { errors[url] = err; @@ -122,6 +123,7 @@ async function produceArtifact({ undefined, awaitCustomCache, noCache, + true, ); } catch (err) { errors[url] = err; @@ -243,6 +245,7 @@ async function produceArtifact({ undefined, undefined, noCache, + true, ); } catch (err) { errors[url] = err; diff --git a/backend/src/utils/download.js b/backend/src/utils/download.js index 68293b6..a6dcb70 100644 --- a/backend/src/utils/download.js +++ b/backend/src/utils/download.js @@ -11,6 +11,10 @@ import { validCheck, } from '@/utils/flow'; import $ from '@/core/app'; +import PROXY_PREPROCESSORS from '@/core/proxy-utils/preprocessors'; +const clashPreprocessor = PROXY_PREPROCESSORS.find( + (processor) => processor.name === 'Clash Pre-processor', +); const tasks = new Map(); @@ -22,6 +26,7 @@ export default async function download( skipCustomCache, awaitCustomCache, noCache, + preprocess, ) { let $arguments = {}; let url = rawUrl.replace(/#noFlow$/, ''); @@ -87,6 +92,9 @@ export default async function download( timeout, proxy, true, + undefined, + undefined, + preprocess, ); } catch (e) { $.error( @@ -107,6 +115,9 @@ export default async function download( timeout, proxy, true, + undefined, + undefined, + preprocess, ).catch((e) => { $.error( `乐观缓存: URL ${url} 异步更新缓存发生错误 ${ @@ -169,10 +180,10 @@ export default async function download( : { insecure: true } : undefined; $.info( - `Downloading...\nUser-Agent: ${userAgent}\nTimeout: ${requestTimeout}\nProxy: ${proxy}\nInsecure: ${!!insecure}\nURL: ${url}`, + `Downloading...\nUser-Agent: ${userAgent}\nTimeout: ${requestTimeout}\nProxy: ${proxy}\nInsecure: ${!!insecure}\nPreprocess: ${preprocess}\nURL: ${url}`, ); try { - const { body, headers, statusCode } = await http.get({ + let { body, headers, statusCode } = await http.get({ url, ...(proxy ? { proxy } : {}), ...(isLoon && proxy ? { node: proxy } : {}), @@ -193,6 +204,15 @@ export default async function download( } if (body.replace(/\s/g, '').length === 0) throw new Error(new Error('远程资源内容为空')); + if (preprocess) { + try { + if (clashPreprocessor.test(body)) { + body = clashPreprocessor.parse(body); + } + } catch (e) { + $.error(`Clash Pre-processor error: ${e}`); + } + } let shouldCache = true; if (cacheThreshold) { const size = body.length / 1024;