提供更方便的脚本操作接口

This commit is contained in:
Peng-YM 2020-09-15 11:13:56 +08:00
parent a0d8e79d3c
commit 910f2c1b67
2 changed files with 38 additions and 75 deletions

View File

@ -202,11 +202,6 @@ async function parseSub(sub, platform) {
$.info(`Parsers initialized.`); $.info(`Parsers initialized.`);
let proxies = $parser.parse(raw); let proxies = $parser.parse(raw);
// filters
const $filter = ProxyFilter();
// operators
const $operator = ProxyOperator();
for (const item of sub.process || []) { for (const item of sub.process || []) {
let script; let script;
// process script // process script
@ -234,13 +229,14 @@ async function parseSub(sub, platform) {
JSON.stringify(item.args) || "None" JSON.stringify(item.args) || "None"
}` }`
); );
if (item.type.indexOf("Script") !== -1) {
$filter.setFilter(filter(script));
} else {
$filter.setFilter(filter(item.args));
}
try { try {
proxies = $filter.process(proxies); if (item.type.indexOf("Script") !== -1) {
proxies = processFilter(filter(script), proxies);
} else {
proxies = processFilter(filter(item.args), proxies);
}
} catch (err) { } catch (err) {
$.error(`Failed to apply filter "${item.type}"!\n REASON: ${err}`); $.error(`Failed to apply filter "${item.type}"!\n REASON: ${err}`);
} }
@ -253,13 +249,12 @@ async function parseSub(sub, platform) {
JSON.stringify(item.args) || "None" JSON.stringify(item.args) || "None"
}` }`
); );
if (item.type.indexOf("Script") !== -1) {
$operator.setOperator(operator(script));
} else {
$operator.setOperator(operator(item.args));
}
try { try {
proxies = $operator.process(proxies); if (item.type.indexOf("Script") !== -1) {
proxies = processOperator(operator(script), proxies);
} else {
proxies = processOperator(operator(item.args), proxies);
}
} catch (err) { } catch (err) {
`Failed to apply operator "${item.type}"!\n REASON: ${err}`; `Failed to apply operator "${item.type}"!\n REASON: ${err}`;
} }
@ -729,40 +724,18 @@ function ProxyParser(targetPlatform) {
}; };
} }
function ProxyFilter() { function processFilter(filter, proxies) {
let filter;
function setFilter(arg) {
filter = arg;
}
// select proxies // select proxies
function process(proxies) {
let selected = FULL(proxies.length, true); let selected = FULL(proxies.length, true);
try { try {
selected = AND(selected, filter.func(proxies)); selected = AND(selected, filter.func(proxies));
} catch (err) { } catch (err) {
console.log(`Cannot apply filter ${filter.name}\n Reason: ${err}`); console.log(`Cannot apply filter ${filter.name}\n Reason: ${err}`);
} }
return proxies.filter((_, i) => selected[i]); return proxies.filter((_, i) => selected[i]);
} }
return { function processOperator(operator, proxies) {
process,
setFilter,
};
}
function ProxyOperator() {
let operator;
function setOperator(arg) {
operator = arg;
}
// run all operators
function process(proxies) {
let output = objClone(proxies); let output = objClone(proxies);
try { try {
const output_ = operator.func(output); const output_ = operator.func(output);
@ -775,9 +748,6 @@ function ProxyOperator() {
return output; return output;
} }
return { setOperator, process };
}
/**************************** URI Format ***************************************/ /**************************** URI Format ***************************************/
// Parse SS URI format (only supports new SIP002, legacy format is depreciated). // Parse SS URI format (only supports new SIP002, legacy format is depreciated).
// reference: https://shadowsocks.org/en/spec/SIP002-URI-Scheme.html // reference: https://shadowsocks.org/en/spec/SIP002-URI-Scheme.html
@ -1799,7 +1769,6 @@ function URI_Producer() {
); );
break; break;
default: default:
console.log(`FUCK`);
throw new Error(`Unsupported plugin option: ${proxy.plugin}`); throw new Error(`Unsupported plugin option: ${proxy.plugin}`);
} }
} }
@ -2018,13 +1987,16 @@ function ScriptOperator(script) {
(function () { (function () {
// interface to get internal operators // interface to get internal operators
const $get = (name, args) => { const $get = (name, args) => {
const operator = AVAILABLE_OPERATORS[name]; const item = AVAILABLE_OPERATORS[name] || AVAILABLE_FILTERS[name];
if (operator) { return item(args);
return operator(args).func;
} else {
throw new Error(`No operator named ${name} is found!`);
}
}; };
const $process = (item, proxies) => {
if (item.name.indexOf("Filter") !== -1) {
return processOperator(item, proxies);
} else if (item.name.indexOf("Operator") !== -1) {
return processFilter(item, proxies);
}
}
eval(script); eval(script);
output = operator(proxies); output = operator(proxies);
})(); })();
@ -2132,15 +2104,6 @@ function ScriptFilter(script) {
func: (proxies) => { func: (proxies) => {
let output = FULL(proxies.length, true); let output = FULL(proxies.length, true);
!(function () { !(function () {
// interface to get internal filters
const $get = (name, args) => {
const filter = AVAILABLE_FILTERS[name];
if (filter) {
return filter(args).func;
} else {
throw new Error(`No filter named ${name} is found!`);
}
};
eval(script); eval(script);
output = filter(proxies); output = filter(proxies);
})(); })();

File diff suppressed because one or more lines are too long