From d3c6c99b0a3c628d70b1c028bc6d3fd2a2442173 Mon Sep 17 00:00:00 2001 From: xream Date: Sun, 8 Oct 2023 13:21:22 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20proxy=20=E5=A2=9E=E5=8A=A0=20subName(?= =?UTF-8?q?=E8=AE=A2=E9=98=85=E5=90=8D),=20collectionName(=E7=BB=84?= =?UTF-8?q?=E5=90=88=E8=AE=A2=E9=98=85=E5=90=8D);=20=E8=84=9A=E6=9C=AC?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E7=AC=AC=E4=B8=89=E4=B8=AA=E5=8F=82=E6=95=B0?= =?UTF-8?q?=20env(=E5=8C=85=E5=90=AB=E8=AE=A2=E9=98=85/=E7=BB=84=E5=90=88?= =?UTF-8?q?=E8=AE=A2=E9=98=85/=E7=8E=AF=E5=A2=83/=E7=89=88=E6=9C=AC?= =?UTF-8?q?=E7=AD=89=E4=BF=A1=E6=81=AF)?= 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 | 3 ++- backend/src/core/proxy-utils/processors/index.js | 9 +++++---- backend/src/core/proxy-utils/producers/clash.js | 2 ++ .../src/core/proxy-utils/producers/clashmeta.js | 2 ++ .../core/proxy-utils/producers/shadowrocket.js | 2 ++ backend/src/core/proxy-utils/producers/stash.js | 2 ++ backend/src/restful/miscs.js | 16 ++-------------- backend/src/restful/preview.js | 11 +++++++++++ backend/src/restful/sync.js | 16 ++++++++++++++++ backend/src/utils/env.js | 16 ++++++++++++++++ backend/src/vendor/open-api.js | 8 ++++---- 12 files changed, 65 insertions(+), 24 deletions(-) create mode 100644 backend/src/utils/env.js diff --git a/backend/package.json b/backend/package.json index 3f487b8..28b5876 100644 --- a/backend/package.json +++ b/backend/package.json @@ -1,6 +1,6 @@ { "name": "sub-store", - "version": "2.14.62", + "version": "2.14.63", "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 b9426fa..4af9bf2 100644 --- a/backend/src/core/proxy-utils/index.js +++ b/backend/src/core/proxy-utils/index.js @@ -63,7 +63,7 @@ function parse(raw) { return proxies; } -async function process(proxies, operators = [], targetPlatform) { +async function process(proxies, operators = [], targetPlatform, source) { for (const item of operators) { // process script let script; @@ -122,6 +122,7 @@ async function process(proxies, operators = [], targetPlatform) { script, targetPlatform, $arguments, + source, ); } else { processor = PROXY_PROCESSORS[item.type](item.args || {}); diff --git a/backend/src/core/proxy-utils/processors/index.js b/backend/src/core/proxy-utils/processors/index.js index d7b25a3..3e3c2c0 100644 --- a/backend/src/core/proxy-utils/processors/index.js +++ b/backend/src/core/proxy-utils/processors/index.js @@ -7,6 +7,7 @@ import lodash from 'lodash'; import $ from '@/core/app'; import { hex_md5 } from '@/vendor/md5'; import { ProxyUtils } from '@/core/proxy-utils'; +import env from '@/utils/env'; /** The rule "(name CONTAINS "🇨🇳") AND (port IN [80, 443])" can be expressed as follows: @@ -294,7 +295,7 @@ function RegexDeleteOperator(regex) { 1. This function name should be `operator`! 2. Always declare variables before using them! */ -function ScriptOperator(script, targetPlatform, $arguments) { +function ScriptOperator(script, targetPlatform, $arguments, source) { return { name: 'Script Operator', func: async (proxies) => { @@ -305,7 +306,7 @@ function ScriptOperator(script, targetPlatform, $arguments) { script, $arguments, ); - output = operator(proxies, targetPlatform); + output = operator(proxies, targetPlatform, { source, ...env }); })(); return output; }, @@ -562,7 +563,7 @@ function TypeFilter(types) { 1. This function name should be `filter`! 2. Always declare variables before using them! */ -function ScriptFilter(script, targetPlatform, $arguments) { +function ScriptFilter(script, targetPlatform, $arguments, source) { return { name: 'Script Filter', func: async (proxies) => { @@ -573,7 +574,7 @@ function ScriptFilter(script, targetPlatform, $arguments) { script, $arguments, ); - output = filter(proxies, targetPlatform); + output = filter(proxies, targetPlatform, { source, ...env }); })(); return output; }, diff --git a/backend/src/core/proxy-utils/producers/clash.js b/backend/src/core/proxy-utils/producers/clash.js index afebfae..ac5b7b5 100644 --- a/backend/src/core/proxy-utils/producers/clash.js +++ b/backend/src/core/proxy-utils/producers/clash.js @@ -98,6 +98,8 @@ export default function Clash_Producer() { delete proxy.tls; } delete proxy['tls-fingerprint']; + delete proxy.subName; + delete proxy.collectionName; if ( ['grpc'].includes(proxy.network) && proxy[`${proxy.network}-opts`] diff --git a/backend/src/core/proxy-utils/producers/clashmeta.js b/backend/src/core/proxy-utils/producers/clashmeta.js index a55c628..292b361 100644 --- a/backend/src/core/proxy-utils/producers/clashmeta.js +++ b/backend/src/core/proxy-utils/producers/clashmeta.js @@ -117,6 +117,8 @@ export default function ClashMeta_Producer() { } delete proxy['tls-fingerprint']; + delete proxy.subName; + delete proxy.collectionName; if ( ['grpc'].includes(proxy.network) && proxy[`${proxy.network}-opts`] diff --git a/backend/src/core/proxy-utils/producers/shadowrocket.js b/backend/src/core/proxy-utils/producers/shadowrocket.js index 583fba9..c613959 100644 --- a/backend/src/core/proxy-utils/producers/shadowrocket.js +++ b/backend/src/core/proxy-utils/producers/shadowrocket.js @@ -117,6 +117,8 @@ export default function ShadowRocket_Producer() { } delete proxy['tls-fingerprint']; + delete proxy.subName; + delete proxy.collectionName; if ( ['grpc'].includes(proxy.network) && proxy[`${proxy.network}-opts`] diff --git a/backend/src/core/proxy-utils/producers/stash.js b/backend/src/core/proxy-utils/producers/stash.js index ec1a6ca..73a55d7 100644 --- a/backend/src/core/proxy-utils/producers/stash.js +++ b/backend/src/core/proxy-utils/producers/stash.js @@ -128,6 +128,8 @@ export default function Stash_Producer() { delete proxy.tls; } delete proxy['tls-fingerprint']; + delete proxy.subName; + delete proxy.collectionName; if ( ['grpc'].includes(proxy.network) && proxy[`${proxy.network}-opts`] diff --git a/backend/src/restful/miscs.js b/backend/src/restful/miscs.js index b59e84f..b527a48 100644 --- a/backend/src/restful/miscs.js +++ b/backend/src/restful/miscs.js @@ -1,7 +1,6 @@ import $ from '@/core/app'; import { ENV } from '@/vendor/open-api'; import { failed, success } from '@/restful/response'; -import { version as substoreVersion } from '../../package.json'; import { updateArtifactStore, updateGitHubAvatar } from '@/restful/settings'; import resourceCache from '@/utils/resource-cache'; import { @@ -12,6 +11,7 @@ import { import { InternalServerError, RequestInvalidError } from '@/restful/errors'; import Gist from '@/utils/gist'; import migrate from '@/utils/migration'; +import env from '@/utils/env'; export default function register($app) { // utils @@ -49,19 +49,7 @@ export default function register($app) { } function getEnv(req, res) { - const { isNode, isQX, isLoon, isSurge, isStash, isShadowRocket } = ENV(); - let backend = 'Node'; - if (isNode) backend = 'Node'; - if (isQX) backend = 'QX'; - if (isLoon) backend = 'Loon'; - if (isSurge) backend = 'Surge'; - if (isStash) backend = 'Stash'; - if (isShadowRocket) backend = 'ShadowRocket'; - - success(res, { - backend, - version: substoreVersion, - }); + success(res, env); } async function refresh(_, res) { diff --git a/backend/src/restful/preview.js b/backend/src/restful/preview.js index 9e6d75d..d8743dc 100644 --- a/backend/src/restful/preview.js +++ b/backend/src/restful/preview.js @@ -39,6 +39,7 @@ async function compareSub(req, res) { // add id original.forEach((proxy, i) => { proxy.id = i; + proxy.subName = sub.name; }); // apply processors @@ -46,6 +47,7 @@ async function compareSub(req, res) { original, sub.process || [], target, + { [sub.name]: sub }, ); // produce @@ -82,11 +84,18 @@ async function compareCollection(req, res) { } // parse proxies let currentProxies = ProxyUtils.parse(raw); + + currentProxies.forEach((proxy) => { + proxy.subName = sub.name; + proxy.collectionName = collection.name; + }); + // apply processors currentProxies = await ProxyUtils.process( currentProxies, sub.process || [], 'JSON', + { [sub.name]: sub, _collection: collection }, ); results[name] = currentProxies; } catch (err) { @@ -110,12 +119,14 @@ async function compareCollection(req, res) { original.forEach((proxy, i) => { proxy.id = i; + proxy.collectionName = collection.name; }); const processed = await ProxyUtils.process( original, collection.process || [], 'JSON', + { _collection: collection }, ); success(res, { original, processed }); diff --git a/backend/src/restful/sync.js b/backend/src/restful/sync.js index 2cdebac..51a0932 100644 --- a/backend/src/restful/sync.js +++ b/backend/src/restful/sync.js @@ -36,11 +36,15 @@ async function produceArtifact({ type, name, platform }) { } // parse proxies let proxies = ProxyUtils.parse(raw); + proxies.forEach((proxy) => { + proxy.subName = sub.name; + }); // apply processors proxies = await ProxyUtils.process( proxies, sub.process || [], platform, + { [sub.name]: sub }, ); if (proxies.length === 0) { throw new Error(`订阅 ${name} 中不含有效节点`); @@ -86,11 +90,18 @@ async function produceArtifact({ type, name, platform }) { } // parse proxies let currentProxies = ProxyUtils.parse(raw); + + currentProxies.forEach((proxy) => { + proxy.subName = sub.name; + proxy.collectionName = collection.name; + }); + // apply processors currentProxies = await ProxyUtils.process( currentProxies, sub.process || [], platform, + { [sub.name]: sub, _collection: collection }, ); results[name] = currentProxies; processed++; @@ -127,11 +138,16 @@ async function produceArtifact({ type, name, platform }) { subnames.map((name) => results[name] || []), ); + proxies.forEach((proxy) => { + proxy.collectionName = collection.name; + }); + // apply own processors proxies = await ProxyUtils.process( proxies, collection.process || [], platform, + { _collection: collection }, ); if (proxies.length === 0) { throw new Error(`组合订阅 ${name} 中不含有效节点`); diff --git a/backend/src/utils/env.js b/backend/src/utils/env.js new file mode 100644 index 0000000..f99068a --- /dev/null +++ b/backend/src/utils/env.js @@ -0,0 +1,16 @@ +import { version as substoreVersion } from '../../package.json'; +import { ENV } from '@/vendor/open-api'; + +const { isNode, isQX, isLoon, isSurge, isStash, isShadowRocket } = ENV(); +let backend = 'Node'; +if (isNode) backend = 'Node'; +if (isQX) backend = 'QX'; +if (isLoon) backend = 'Loon'; +if (isSurge) backend = 'Surge'; +if (isStash) backend = 'Stash'; +if (isShadowRocket) backend = 'ShadowRocket'; + +export default { + backend, + version: substoreVersion, +}; diff --git a/backend/src/vendor/open-api.js b/backend/src/vendor/open-api.js index 873e0c1..5bb37d7 100644 --- a/backend/src/vendor/open-api.js +++ b/backend/src/vendor/open-api.js @@ -60,7 +60,9 @@ export class OpenAPI { }); this.root = {}; } else { - this.root = JSON.parse(this.node.fs.readFileSync(`${rootPath}`)); + this.root = JSON.parse( + this.node.fs.readFileSync(`${rootPath}`), + ); } // create a json file with the given name if not exists @@ -72,9 +74,7 @@ export class OpenAPI { }); this.cache = {}; } else { - this.cache = JSON.parse( - this.node.fs.readFileSync(`${fpath}`), - ); + this.cache = JSON.parse(this.node.fs.readFileSync(`${fpath}`)); } } }