diff --git a/backend/package.json b/backend/package.json index 0c9bffa..0721ed3 100644 --- a/backend/package.json +++ b/backend/package.json @@ -1,6 +1,6 @@ { "name": "sub-store", - "version": "2.14.104", + "version": "2.14.105", "description": "Advanced Subscription Manager for QX, Loon, Surge, Stash and ShadowRocket.", "main": "src/main.js", "scripts": { diff --git a/backend/src/restful/download.js b/backend/src/restful/download.js index d729786..6a9e4c8 100644 --- a/backend/src/restful/download.js +++ b/backend/src/restful/download.js @@ -20,18 +20,22 @@ async function downloadSubscription(req, res) { req.query.target || getPlatformFromHeaders(req.headers) || 'JSON'; $.info(`正在下载订阅:${name}`); - let { url, ua, content } = req.query; + let { url, ua, content, mergeSources } = req.query; if (url) { url = decodeURIComponent(url); - $.info(`指定 url: ${url}`); + $.info(`指定远程订阅 URL: ${url}`); } if (ua) { ua = decodeURIComponent(ua); - $.info(`指定 ua: ${ua}`); + $.info(`指定远程订阅 User-Agent: ${ua}`); } if (content) { content = decodeURIComponent(content); - $.info(`指定 content: ${content}`); + $.info(`指定本地订阅: ${content}`); + } + if (mergeSources) { + mergeSources = decodeURIComponent(mergeSources); + $.info(`指定合并来源: ${mergeSources}`); } const allSubs = $.read(SUBS_KEY); @@ -45,6 +49,7 @@ async function downloadSubscription(req, res) { url, ua, content, + mergeSources, }); if (sub.source !== 'local' || url) { diff --git a/backend/src/restful/preview.js b/backend/src/restful/preview.js index 97d1b50..68896ec 100644 --- a/backend/src/restful/preview.js +++ b/backend/src/restful/preview.js @@ -16,7 +16,10 @@ async function compareSub(req, res) { const sub = req.body; const target = req.query.target || 'JSON'; let content; - if (sub.source === 'local') { + if ( + sub.source === 'local' && + !['localFirst', 'remoteFirst'].includes(sub.mergeSources) + ) { content = sub.content; } else { try { @@ -38,6 +41,11 @@ async function compareSub(req, res) { ); return; } + if (sub.mergeSources === 'localFirst') { + content.unshift(sub.content); + } else if (sub.mergeSources === 'remoteFirst') { + content.push(sub.content); + } } // parse proxies const original = (Array.isArray(content) ? content : [content]) @@ -85,7 +93,12 @@ async function compareCollection(req, res) { const sub = findByName(allSubs, name); try { let raw; - if (sub.source === 'local') { + if ( + sub.source === 'local' && + !['localFirst', 'remoteFirst'].includes( + sub.mergeSources, + ) + ) { raw = sub.content; } else { raw = await Promise.all( @@ -95,6 +108,11 @@ async function compareCollection(req, res) { .filter((i) => i.length) .map((url) => download(url, sub.ua)), ); + if (sub.mergeSources === 'localFirst') { + raw.unshift(sub.content); + } else if (sub.mergeSources === 'remoteFirst') { + raw.push(sub.content); + } } // parse proxies let currentProxies = (Array.isArray(raw) ? raw : [raw]) diff --git a/backend/src/restful/sync.js b/backend/src/restful/sync.js index d860746..c80fe39 100644 --- a/backend/src/restful/sync.js +++ b/backend/src/restful/sync.js @@ -22,14 +22,24 @@ export default function register($app) { $app.get('/api/sync/artifact/:name', syncArtifact); } -async function produceArtifact({ type, name, platform, url, ua, content }) { +async function produceArtifact({ + type, + name, + platform, + url, + ua, + content, + mergeSources, +}) { platform = platform || 'JSON'; if (type === 'subscription') { const allSubs = $.read(SUBS_KEY); const sub = findByName(allSubs, name); let raw; - if (url) { + if (content && !['localFirst', 'remoteFirst'].includes(mergeSources)) { + raw = content; + } else if (url) { raw = await Promise.all( url .split(/[\r\n]+/) @@ -37,9 +47,15 @@ async function produceArtifact({ type, name, platform, url, ua, content }) { .filter((i) => i.length) .map((url) => download(url, ua)), ); - } else if (content) { - raw = content; - } else if (sub.source === 'local') { + if (mergeSources === 'localFirst') { + raw.unshift(content); + } else if (mergeSources === 'remoteFirst') { + raw.push(content); + } + } else if ( + sub.source === 'local' && + !['localFirst', 'remoteFirst'].includes(sub.mergeSources) + ) { raw = sub.content; } else { raw = await Promise.all( @@ -49,6 +65,11 @@ async function produceArtifact({ type, name, platform, url, ua, content }) { .filter((i) => i.length) .map((url) => download(url, sub.ua)), ); + if (sub.mergeSources === 'localFirst') { + raw.unshift(sub.content); + } else if (sub.mergeSources === 'remoteFirst') { + raw.push(sub.content); + } } // parse proxies let proxies = (Array.isArray(raw) ? raw : [raw]) @@ -102,7 +123,12 @@ async function produceArtifact({ type, name, platform, url, ua, content }) { try { $.info(`正在处理子订阅:${sub.name}...`); let raw; - if (sub.source === 'local') { + if ( + sub.source === 'local' && + !['localFirst', 'remoteFirst'].includes( + sub.mergeSources, + ) + ) { raw = sub.content; } else { raw = await await Promise.all( @@ -112,6 +138,11 @@ async function produceArtifact({ type, name, platform, url, ua, content }) { .filter((i) => i.length) .map((url) => download(url, sub.ua)), ); + if (sub.mergeSources === 'localFirst') { + raw.unshift(sub.content); + } else if (sub.mergeSources === 'remoteFirst') { + raw.push(sub.content); + } } // parse proxies let currentProxies = (Array.isArray(raw) ? raw : [raw])