Skip to content

Commit

Permalink
feat: proxy 增加 subName(订阅名), collectionName(组合订阅名); 脚本增加第三个参数 env(包含订…
Browse files Browse the repository at this point in the history
…阅/组合订阅/环境/版本等信息)
  • Loading branch information
xream committed Oct 8, 2023
1 parent 3fbc280 commit d3c6c99
Show file tree
Hide file tree
Showing 12 changed files with 65 additions and 24 deletions.
2 changes: 1 addition & 1 deletion backend/package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
3 changes: 2 additions & 1 deletion backend/src/core/proxy-utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -122,6 +122,7 @@ async function process(proxies, operators = [], targetPlatform) {
script,
targetPlatform,
$arguments,
source,
);
} else {
processor = PROXY_PROCESSORS[item.type](item.args || {});
Expand Down
9 changes: 5 additions & 4 deletions backend/src/core/proxy-utils/processors/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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) => {
Expand All @@ -305,7 +306,7 @@ function ScriptOperator(script, targetPlatform, $arguments) {
script,
$arguments,
);
output = operator(proxies, targetPlatform);
output = operator(proxies, targetPlatform, { source, ...env });
})();
return output;
},
Expand Down Expand Up @@ -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) => {
Expand All @@ -573,7 +574,7 @@ function ScriptFilter(script, targetPlatform, $arguments) {
script,
$arguments,
);
output = filter(proxies, targetPlatform);
output = filter(proxies, targetPlatform, { source, ...env });
})();
return output;
},
Expand Down
2 changes: 2 additions & 0 deletions backend/src/core/proxy-utils/producers/clash.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`]
Expand Down
2 changes: 2 additions & 0 deletions backend/src/core/proxy-utils/producers/clashmeta.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`]
Expand Down
2 changes: 2 additions & 0 deletions backend/src/core/proxy-utils/producers/shadowrocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`]
Expand Down
2 changes: 2 additions & 0 deletions backend/src/core/proxy-utils/producers/stash.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`]
Expand Down
16 changes: 2 additions & 14 deletions backend/src/restful/miscs.js
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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
Expand Down Expand Up @@ -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) {
Expand Down
11 changes: 11 additions & 0 deletions backend/src/restful/preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,15 @@ async function compareSub(req, res) {
// add id
original.forEach((proxy, i) => {
proxy.id = i;
proxy.subName = sub.name;
});

// apply processors
const processed = await ProxyUtils.process(
original,
sub.process || [],
target,
{ [sub.name]: sub },
);

// produce
Expand Down Expand Up @@ -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) {
Expand All @@ -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 });
Expand Down
16 changes: 16 additions & 0 deletions backend/src/restful/sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -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} 中不含有效节点`);
Expand Down Expand Up @@ -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++;
Expand Down Expand Up @@ -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} 中不含有效节点`);
Expand Down
16 changes: 16 additions & 0 deletions backend/src/utils/env.js
Original file line number Diff line number Diff line change
@@ -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,
};
8 changes: 4 additions & 4 deletions backend/src/vendor/open-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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}`));
}
}
}
Expand Down

0 comments on commit d3c6c99

Please sign in to comment.