Compare commits

...

3 Commits

Author SHA1 Message Date
xream
f3de132d70 feat: 脚本链接的末尾加上 #noCache 关闭缓存 2023-11-14 21:14:47 +08:00
xream
3e30a35bc4 feat: 脚本操作支持节点快捷脚本. 语法与 Shadowrocket 脚本类似
```
$server.name = '前缀-' + $server.name
$server.ecn = true
$server['test-url'] = 'http://1.0.0.1/generate_204'
```
2023-11-14 17:07:01 +08:00
xream
3e5f3eafdd feat: 脚本操作 ProxyUtils 增加了 isIPv4, isIPv6, isIP 方法 2023-11-14 00:57:52 +08:00
3 changed files with 57 additions and 6 deletions

View File

@@ -1,6 +1,6 @@
{
"name": "sub-store",
"version": "2.14.87",
"version": "2.14.90",
"description": "Advanced Subscription Manager for QX, Loon, Surge, Stash and ShadowRocket.",
"main": "src/main.js",
"scripts": {

View File

@@ -71,7 +71,12 @@ async function process(proxies, operators = [], targetPlatform, source) {
if (item.type.indexOf('Script') !== -1) {
const { mode, content } = item.args;
if (mode === 'link') {
const url = content;
let noCache;
let url = content;
if (url.endsWith('#noCache')) {
url = url.replace(/#noCache$/, '');
noCache = true;
}
// extract link arguments
const rawArgs = url.split('#');
if (rawArgs.length > 1) {
@@ -93,7 +98,9 @@ async function process(proxies, operators = [], targetPlatform, source) {
// if this is a remote script, download it
try {
script = await download(url.split('#')[0]);
script = await download(
`${url.split('#')[0]}${noCache ? '#noCache' : ''}`,
);
// $.info(`Script loaded: >>>\n ${script}`);
} catch (err) {
$.error(
@@ -183,6 +190,9 @@ export const ProxyUtils = {
parse,
process,
produce,
isIPv4,
isIPv6,
isIP,
};
function tryParse(parser, line) {

View File

@@ -311,6 +311,23 @@ function ScriptOperator(script, targetPlatform, $arguments, source) {
})();
return output;
},
nodeFunc: async (proxies) => {
let output = proxies;
await (async function () {
const operator = createDynamicFunction(
'operator',
`async function operator(proxies = []) {
return proxies.map(($server = {}) => {
${script}
return $server
})
}`,
$arguments,
);
output = operator(proxies, targetPlatform, { source, ...env });
})();
return output;
},
};
}
@@ -620,9 +637,33 @@ async function ApplyOperator(operator, objs) {
const output_ = await operator.func(output);
if (output_) output = output_;
} catch (err) {
// print log and skip this operator
$.error(`Cannot apply operator ${operator.name}! Reason: ${err}`);
throw new Error(`脚本操作失败 ${err.message ?? err}`);
$.error(
`Cannot apply operator ${operator.name}(function operator)! Reason: ${err}`,
);
let funcErr = '';
let funcErrMsg = `${err.message ?? err}`;
if (funcErrMsg.includes('$server is not defined')) {
funcErr = '';
} else {
funcErr = `执行 function operator 失败 ${funcErrMsg}; `;
}
try {
const output_ = await operator.nodeFunc(output);
if (output_) output = output_;
} catch (err) {
$.error(
`Cannot apply operator ${operator.name}(node script)! Reason: ${err}`,
);
let nodeErr = '';
let nodeErrMsg = `${err.message ?? err}`;
if (funcErr && nodeErrMsg === funcErrMsg) {
nodeErr = '';
funcErr = `执行失败 ${funcErrMsg}`;
} else {
nodeErr = `执行节点快捷脚本 失败 ${nodeErr}`;
}
throw new Error(`脚本操作 ${funcErr}${nodeErr}`);
}
}
return output;
}