From 3ed39271b13bc1c45218f233acb0f727248f2839 Mon Sep 17 00:00:00 2001
From: Peng-YM <1048217874pengym@gmail.com>
Date: Fri, 4 Sep 2020 15:05:25 +0800
Subject: [PATCH] Bug Fixed for reording multiple processors
---
backend/.idea/workspace.xml | 77 +++++++++++-----------
backend/sub-store.js | 61 +++++++++++-------
web/src/components/ScriptOperator.vue | 2 +-
web/src/views/SubEditor.vue | 93 +++++++++++++++++++--------
4 files changed, 144 insertions(+), 89 deletions(-)
diff --git a/backend/.idea/workspace.xml b/backend/.idea/workspace.xml
index 0e2eb0b..b26a8fc 100644
--- a/backend/.idea/workspace.xml
+++ b/backend/.idea/workspace.xml
@@ -22,9 +22,8 @@
-
-
+
@@ -52,6 +51,7 @@
+
@@ -104,6 +104,7 @@
+
@@ -123,55 +124,55 @@
-
+
-
+
-
-
+
+
-
+
-
-
+
+
-
+
-
-
+
+
-
+
-
-
+
+
@@ -182,8 +183,8 @@
-
-
+
+
@@ -194,8 +195,8 @@
-
-
+
+
@@ -206,8 +207,8 @@
-
-
+
+
@@ -218,8 +219,8 @@
-
-
+
+
@@ -230,8 +231,8 @@
-
-
+
+
@@ -242,8 +243,8 @@
-
-
+
+
@@ -254,8 +255,8 @@
-
-
+
+
@@ -266,8 +267,8 @@
-
-
+
+
@@ -278,8 +279,8 @@
-
-
+
+
@@ -290,8 +291,8 @@
-
-
+
+
@@ -302,8 +303,8 @@
-
-
+
+
@@ -314,7 +315,7 @@
-
+
diff --git a/backend/sub-store.js b/backend/sub-store.js
index a8279c4..b32e743 100644
--- a/backend/sub-store.js
+++ b/backend/sub-store.js
@@ -8,7 +8,7 @@
* 2. 节点过滤,重命名,排序等。
* 3. 订阅拆分,组合。
*/
-const $ = API("sub-store", true);
+const $ = API("sub-store");
const $app = express();
$.http = HTTP({
@@ -45,7 +45,7 @@ if (!$.read(SUBS_KEY)) $.write({}, SUBS_KEY);
if (!$.read(COLLECTIONS_KEY)) $.write({}, COLLECTIONS_KEY);
// BACKEND API
-$.log("Initializing Express...");
+$.info("Initializing Express...");
// download
$app.get("/download/collection/:name", downloadCollection);
@@ -76,7 +76,7 @@ $app.all("/", async (req, res) => {
res.send("Hello from Sub-Store! Made with ❤️ by Peng-YM.");
});
-$.log("Express initialized");
+$.info("Express initialized");
$app.start();
/**************************** API -- Subscriptions ***************************************/
@@ -151,13 +151,13 @@ async function parseSub(sub, platform) {
$.write(raw, `#${sub.url}`);
}
- $.log(
+ $.info(
"======================================================================="
);
- $.log(
+ $.info(
`Processing subscription: ${sub.name}, target platform ==> ${platform}.`
);
- $.log(`Initializing parsers...`);
+ $.info(`Initializing parsers...`);
const $parser = ProxyParser(platform);
// Parsers
$parser.addParsers([
@@ -186,7 +186,7 @@ async function parseSub(sub, platform) {
Surge_Http,
]);
- $.log(`Parsers initialized.`);
+ $.info(`Parsers initialized.`);
let proxies = $parser.parse(raw);
// filters
@@ -195,12 +195,13 @@ async function parseSub(sub, platform) {
const $operator = ProxyOperator();
for (const item of sub.process || []) {
+ let script;
// process script
if (item.type.indexOf("Script") !== -1) {
const {mode, content} = item.args;
if (mode === "link") {
// if this is remote script, download it
- item.args = await $.http
+ script = await $.http
.get(content)
.then((resp) => resp.body)
.catch((err) => {
@@ -209,30 +210,49 @@ async function parseSub(sub, platform) {
);
});
} else {
- item.args = content;
+ script = content;
}
}
if (item.type.indexOf("Filter") !== -1) {
const filter = AVAILABLE_FILTERS[item.type];
if (filter) {
- $filter.setFilter(filter(item.args));
- proxies = $filter.process(proxies);
- $.log(
+ $.info(
`Applying filter "${item.type}" with arguments:\n >>> ${
JSON.stringify(item.args) || "None"
}`
);
+ if (item.type.indexOf("Script") !== -1) {
+ $filter.setFilter(filter(script));
+ } else {
+ $filter.setFilter(filter(item.args));
+ }
+ try {
+
+ proxies = $filter.process(proxies);
+ } catch (err) {
+ $.error(
+ `Failed to apply filter "${item.type}"!\n REASON: ${err}`
+ );
+ }
}
} else if (item.type.indexOf("Operator") !== -1) {
const operator = AVAILABLE_OPERATORS[item.type];
if (operator) {
- $operator.setOperator(operator(item.args));
- proxies = $operator.process(proxies);
- $.log(
+ $.info(
`Applying operator "${item.type}" with arguments: \n >>> ${
- 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 {
+ proxies = $operator.process(proxies);
+ } catch (err) {
+ `Failed to apply operator "${item.type}"!\n REASON: ${err}`
+ }
}
}
}
@@ -330,7 +350,7 @@ async function newSub(req, res) {
async function updateSub(req, res) {
const {name} = req.params;
- $.log(`Updating subscription: ${name}`);
+ $.info(`Updating subscription: ${name}`);
let sub = req.body;
const allSubs = $.read(SUBS_KEY);
if (allSubs[name]) {
@@ -528,7 +548,7 @@ function ProxyParser(targetPlatform) {
function addParsers(args) {
args.forEach((a) => parsers.push(a()));
- $.log(`${args.length} parser added.`);
+ $.info(`${args.length} parser added.`);
}
function addProducers(args) {
@@ -2557,7 +2577,7 @@ function express(port = 3000) {
// adapter
app.start = () => {
app.listen(port, () => {
- $.log(`Express started on port: ${port}`);
+ $.info(`Express started on port: ${port}`);
});
};
return app;
@@ -2581,8 +2601,6 @@ function express(port = 3000) {
// dispatch url to route
const dispatch = (request, start = 0) => {
let {method, url, headers, body} = request;
-
- console.log("DISPATCHING: " + method + ": " + url);
if (/json/i.test(headers["Content-Type"])) {
body = JSON.parse(body);
}
@@ -2657,7 +2675,6 @@ function express(port = 3000) {
// start service
app.start = () => {
- console.log(`STARTING TO ROUTE: ${JSON.stringify($request)}`);
dispatch($request);
};
diff --git a/web/src/components/ScriptOperator.vue b/web/src/components/ScriptOperator.vue
index d397b37..8aa1b8a 100644
--- a/web/src/components/ScriptOperator.vue
+++ b/web/src/components/ScriptOperator.vue
@@ -2,7 +2,7 @@
code
- 脚本过滤器
+ 脚本操作
keyboard_arrow_up
diff --git a/web/src/views/SubEditor.vue b/web/src/views/SubEditor.vue
index a89b7f8..105404e 100644
--- a/web/src/views/SubEditor.vue
+++ b/web/src/views/SubEditor.vue
@@ -139,9 +139,9 @@
- {
+ return this.process.map(p => {
return {
component: AVAILABLE_PROCESSORS[p.type].component,
- args: p.args
+ args: p.args,
+ id: p.id
}
});
}
@@ -285,21 +288,21 @@ export default {
methods: {
save() {
if (this.options.name && this.options.url) {
- const sub = buildSubscription(this.options);
+ const sub = buildSubscription(this.options, this.process);
if (this.$route.params.name !== "UNTITLED") {
this.$store.dispatch("UPDATE_SUBSCRIPTION", {
name: this.$route.params.name,
sub
}).then(() => {
- showInfo(`成功保存订阅:${this.options.name}!`)
+ showInfo(`成功保存订阅:${this.options.name}!`);
}).catch(() => {
- showError(`发生错误,无法保存订阅!`)
+ showError(`发生错误,无法保存订阅!`);
});
} else {
this.$store.dispatch("NEW_SUBSCRIPTION", sub).then(() => {
- showInfo(`成功创建订阅:${this.options.name}!`)
+ showInfo(`成功创建订阅:${this.options.name}!`);
}).catch(() => {
- showError(`发生错误,无法创建订阅!`)
+ showError(`发生错误,无法创建订阅!`);
});
}
}
@@ -310,30 +313,55 @@ export default {
},
dataChanged(content) {
- const process = this.options.process[content.idx];
- process.args = content.args;
- this.options.process.splice(content.idx, 1, process);
+ let index = 0;
+ for (; index < this.process[index].length; index++) {
+ if (this.process.id === content.idx) {
+ break;
+ }
+ }
+ this.process[index].args = content.args;
},
addProcess(type) {
- this.options.process.push({type});
+ this.process.push({type, id: uuidv4()});
this.dialog = false;
},
- deleteProcess(key) {
- this.options.process.splice(key, 1);
+ deleteProcess(id) {
+ let index = 0;
+ for (; index < this.process[index].length; index++) {
+ if (this.process.id === id) {
+ break;
+ }
+ }
+ this.process.splice(index, 1);
},
- moveUp(index) {
+ moveUp(id) {
+ let index = 0;
+ for (; index < this.process.length; index++) {
+ if (this.process[index].id === id) {
+ break;
+ }
+ }
if (index === 0) return;
// otherwise swap with previous one
- this.options.process.splice(index - 1, 2, this.options.process[index], this.options.process[index - 1]);
+ const prev = this.process[index - 1];
+ const cur = this.process[index];
+ this.process.splice(index - 1, 2, cur, prev);
},
- moveDown(index) {
- if (index === this.options.process.length) return;
+ moveDown(id) {
+ let index = 0;
+ for (; index < this.process.length; index++) {
+ if (this.process[index].id === id) {
+ break;
+ }
+ }
// otherwise swap with latter one
- this.options.process.splice(index, 2, this.options.process[index + 1], this.options.process[index]);
+ const cur = this.process[index];
+ const next = this.process[index + 1]
+ this.process.splice(index, 2, next, cur);
}
}
}
@@ -343,8 +371,9 @@ function loadSubscription(options, sub) {
...options,
name: sub.name,
url: sub.url,
- process: []
}
+ let process = []
+
// flag
for (const p of (sub.process || [])) {
switch (p.type) {
@@ -355,13 +384,14 @@ function loadSubscription(options, sub) {
options[p.args.key] = p.args.value ? "FORCE_OPEN" : "FORCE_CLOSE";
break
default:
- options.process.push(p);
+ p.id = uuidv4();
+ process.push(p);
}
}
- return options;
+ return {options, process};
}
-function buildSubscription(options) {
+function buildSubscription(options, process) {
const sub = {
name: options.name,
url: options.url,
@@ -382,11 +412,18 @@ function buildSubscription(options) {
});
}
}
- for (const p of options.process) {
+ for (const p of process) {
sub.process.push(p);
}
return sub;
}
+
+function uuidv4() {
+ return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
+ var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
+ return v.toString(16);
+ });
+}