From 542fcc44a12884525ac2962437a4998bab3e9fc7 Mon Sep 17 00:00:00 2001 From: xream Date: Tue, 3 Jun 2025 00:10:45 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E8=AE=A2=E9=98=85=E5=92=8C=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E7=9A=84=E8=BF=9C=E7=A8=8B=E9=93=BE=E6=8E=A5=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E4=BD=BF=E7=94=A8=E6=8D=A2=E8=A1=8C=E6=B7=B7=E5=86=99?= =?UTF-8?q?=E4=B8=89=E7=A7=8D=E6=A0=BC=E5=BC=8F=201.=20=E5=AE=8C=E6=95=B4?= =?UTF-8?q?=E8=BF=9C=E7=A8=8B=E9=93=BE=E6=8E=A5=202.=20=E7=B1=BB=E4=BC=BC?= =?UTF-8?q?=20/api/file/name=20=E7=9A=84=E5=86=85=E9=83=A8=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E8=B0=83=E7=94=A8=E8=B7=AF=E5=BE=84=203.=20=E6=9C=AC?= =?UTF-8?q?=E5=9C=B0=E6=96=87=E4=BB=B6=E7=9A=84=E7=BB=9D=E5=AF=B9=E8=B7=AF?= =?UTF-8?q?=E5=BE=84?= 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 | 2 +- backend/src/restful/download.js | 4 +- backend/src/restful/subscriptions.js | 2 +- backend/src/utils/download.js | 65 ++++++++++++++++++++------- backend/src/utils/flow.js | 2 +- 6 files changed, 55 insertions(+), 22 deletions(-) diff --git a/backend/package.json b/backend/package.json index 4750c3f..4d00074 100644 --- a/backend/package.json +++ b/backend/package.json @@ -1,6 +1,6 @@ { "name": "sub-store", - "version": "2.19.59", + "version": "2.19.60", "description": "Advanced Subscription Manager for QX, Loon, Surge, Stash and Shadowrocket.", "main": "src/main.js", "scripts": { diff --git a/backend/src/core/proxy-utils/index.js b/backend/src/core/proxy-utils/index.js index 446f3d9..bcd8549 100644 --- a/backend/src/core/proxy-utils/index.js +++ b/backend/src/core/proxy-utils/index.js @@ -177,7 +177,7 @@ async function processFn( } else if (url?.startsWith('/')) { try { const fs = eval(`require("fs")`); - script = fs.readFileSync(url, 'utf8'); + script = fs.readFileSync(url.split('#')[0], 'utf8'); // $.info(`Script loaded: >>>\n ${script}`); } catch (err) { $.error( diff --git a/backend/src/restful/download.js b/backend/src/restful/download.js index bea243f..80afacb 100644 --- a/backend/src/restful/download.js +++ b/backend/src/restful/download.js @@ -259,7 +259,7 @@ async function downloadSubscription(req, res) { } } } - if (!$arguments.noFlow) { + if (!$arguments.noFlow && /^https?/.test(url)) { // forward flow headers flowInfo = await getFlowHeaders( $arguments?.insecure ? `${url}#insecure` : url, @@ -506,7 +506,7 @@ async function downloadCollection(req, res) { } } } - if (!$arguments.noFlow) { + if (!$arguments.noFlow && /^https?:/.test(url)) { subUserInfoOfSub = await getFlowHeaders( $arguments?.insecure ? `${url}#insecure` : url, $arguments.flowUserAgent, diff --git a/backend/src/restful/subscriptions.js b/backend/src/restful/subscriptions.js index 0e6d363..411021a 100644 --- a/backend/src/restful/subscriptions.js +++ b/backend/src/restful/subscriptions.js @@ -140,7 +140,7 @@ async function getFlowInfo(req, res) { } } } - if ($arguments.noFlow) { + if ($arguments.noFlow || !/^https?/.test(url)) { failed( res, new RequestInvalidError( diff --git a/backend/src/utils/download.js b/backend/src/utils/download.js index d886edf..e36e12d 100644 --- a/backend/src/utils/download.js +++ b/backend/src/utils/download.js @@ -1,4 +1,4 @@ -import { SETTINGS_KEY } from '@/constants'; +import { SETTINGS_KEY, FILES_KEY, MODULES_KEY } from '@/constants'; import { HTTP, ENV } from '@/vendor/open-api'; import { hex_md5 } from '@/vendor/md5'; import { getPolicyDescriptor } from '@/utils'; @@ -11,6 +11,8 @@ import { validCheck, } from '@/utils/flow'; import $ from '@/core/app'; +import { findByName } from '@/utils/database'; +import { produceArtifact } from '@/restful/sync'; import PROXY_PREPROCESSORS from '@/core/proxy-utils/preprocessors'; const clashPreprocessor = PROXY_PREPROCESSORS.find( (processor) => processor.name === 'Clash Pre-processor', @@ -130,22 +132,53 @@ export default async function download( } } - // const downloadUrlMatch = url.match(/^\/api\/(file|module)\/(.+)/); - // if (downloadUrlMatch) { - // let type = downloadUrlMatch?.[1]; - // let name = downloadUrlMatch?.[2]; - // if (name == null) { - // throw new Error(`本地 ${type} URL 无效: ${url}`); - // } - // name = decodeURIComponent(name); - // const key = type === 'module' ? MODULES_KEY : FILES_KEY; - // const item = findByName($.read(key), name); - // if (!item) { - // throw new Error(`找不到本地 ${type}: ${name}`); - // } + const downloadUrlMatch = url + .split('#')[0] + .match(/^\/api\/(file|module)\/(.+)/); + if (downloadUrlMatch) { + let type = ''; + try { + type = downloadUrlMatch?.[1]; + let name = downloadUrlMatch?.[2]; + if (name == null) { + throw new Error(`本地 ${type} URL 无效: ${url}`); + } + name = decodeURIComponent(name); + const key = type === 'module' ? MODULES_KEY : FILES_KEY; + const item = findByName($.read(key), name); + if (!item) { + throw new Error(`找不到 ${type}: ${name}`); + } - // return item.content; - // } + if (type === 'module') { + return item.content; + } else { + return await produceArtifact({ + type: 'file', + name, + }); + } + } catch (err) { + $.error( + `Error when loading ${type}: ${ + url.split('#')[0] + }.\n Reason: ${err}`, + ); + throw new Error(`无法加载 ${type}: ${url}`); + } + } else if (url?.startsWith('/')) { + try { + const fs = eval(`require("fs")`); + return fs.readFileSync(url.split('#')[0], 'utf8'); + } catch (err) { + $.error( + `Error when reading local file: ${ + url.split('#')[0] + }.\n Reason: ${err}`, + ); + throw new Error(`无法从该路径读取文本内容: ${url}`); + } + } if (!isNode && tasks.has(id)) { return tasks.get(id); diff --git a/backend/src/utils/flow.js b/backend/src/utils/flow.js index b45e48e..e97805d 100644 --- a/backend/src/utils/flow.js +++ b/backend/src/utils/flow.js @@ -49,7 +49,7 @@ export async function getFlowHeaders( } } } - if ($arguments?.noFlow) { + if ($arguments?.noFlow || !/^https?/.test(url)) { return; } const { isStash, isLoon, isShadowRocket, isQX } = ENV();