From 53bb4866e7fc5cb078a6f7e1dd95fd79cdf40d59 Mon Sep 17 00:00:00 2001 From: xream Date: Tue, 12 Mar 2024 00:55:30 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E8=AE=A2=E9=98=85?= =?UTF-8?q?=E6=B5=81=E9=87=8F=E4=BC=A0=E9=80=92?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/src/restful/download.js | 81 +++++++++++++++++++++++++++++---- 1 file changed, 72 insertions(+), 9 deletions(-) diff --git a/backend/src/restful/download.js b/backend/src/restful/download.js index daa0289..7ba2d25 100644 --- a/backend/src/restful/download.js +++ b/backend/src/restful/download.js @@ -77,12 +77,44 @@ async function downloadSubscription(req, res) { }, }); - if (sub.source !== 'local' || url) { + if ( + sub.source !== 'local' || + ['localFirst', 'remoteFirst'].includes(sub.mergeSources) || + url + ) { try { - // forward flow headers - const flowInfo = await getFlowHeaders(url || sub.url); - if (flowInfo) { - res.set('subscription-userinfo', flowInfo); + url = `${url || sub.url}` + .split(/[\r\n]+/) + .map((i) => i.trim()) + .filter((i) => i.length)?.[0]; + + let $arguments = {}; + const rawArgs = url.split('#'); + url = url.split('#')[0]; + if (rawArgs.length > 1) { + try { + // 支持 `#${encodeURIComponent(JSON.stringify({arg1: "1"}))}` + $arguments = JSON.parse( + decodeURIComponent(rawArgs[1]), + ); + } catch (e) { + for (const pair of rawArgs[1].split('&')) { + const key = pair.split('=')[0]; + const value = pair.split('=')[1]; + // 部分兼容之前的逻辑 const value = pair.split('=')[1] || true; + $arguments[key] = + value == null || value === '' + ? true + : decodeURIComponent(value); + } + } + } + if (!$arguments.noFlow) { + // forward flow headers + const flowInfo = await getFlowHeaders(url); + if (flowInfo) { + res.set('subscription-userinfo', flowInfo); + } } } catch (err) { $.error( @@ -176,11 +208,42 @@ async function downloadCollection(req, res) { const subnames = collection.subscriptions; if (subnames.length > 0) { const sub = findByName(allSubs, subnames[0]); - if (sub.source !== 'local') { + if ( + sub.source !== 'local' || + ['localFirst', 'remoteFirst'].includes(sub.mergeSources) + ) { try { - const flowInfo = await getFlowHeaders(sub.url); - if (flowInfo) { - res.set('subscription-userinfo', flowInfo); + let url = `${sub.url}` + .split(/[\r\n]+/) + .map((i) => i.trim()) + .filter((i) => i.length)?.[0]; + + let $arguments = {}; + const rawArgs = url.split('#'); + url = url.split('#')[0]; + if (rawArgs.length > 1) { + try { + // 支持 `#${encodeURIComponent(JSON.stringify({arg1: "1"}))}` + $arguments = JSON.parse( + decodeURIComponent(rawArgs[1]), + ); + } catch (e) { + for (const pair of rawArgs[1].split('&')) { + const key = pair.split('=')[0]; + const value = pair.split('=')[1]; + // 部分兼容之前的逻辑 const value = pair.split('=')[1] || true; + $arguments[key] = + value == null || value === '' + ? true + : decodeURIComponent(value); + } + } + } + if (!$arguments.noFlow) { + const flowInfo = await getFlowHeaders(url); + if (flowInfo) { + res.set('subscription-userinfo', flowInfo); + } } } catch (err) { $.error(