feat: Clash 订阅仅缓存 proxies 数据

This commit is contained in:
xream 2024-12-27 21:55:13 +08:00
parent d9e4d814bb
commit a3ec98caa9
No known key found for this signature in database
GPG Key ID: 1D2C5225471789F9
6 changed files with 49 additions and 4 deletions

View File

@ -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": {

View File

@ -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,

View File

@ -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 {

View File

@ -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;

View File

@ -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;

View File

@ -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;