From 34474c193bdea74f23837d83279d3dbadf2e4373 Mon Sep 17 00:00:00 2001 From: Peng-YM <1048217874pengym@gmail.com> Date: Wed, 9 Sep 2020 19:51:04 +0800 Subject: [PATCH] Fixed Clash config memory issue --- backend/sub-store.js | 55 ++++++++++++++++++++++++++++---- config/Surge.sgmodule | 2 +- web/src/components/ProxyList.vue | 4 ++- 3 files changed, 52 insertions(+), 9 deletions(-) diff --git a/backend/sub-store.js b/backend/sub-store.js index 8d1a78b..6a3bf55 100644 --- a/backend/sub-store.js +++ b/backend/sub-store.js @@ -146,8 +146,16 @@ async function parseSub(sub, platform) { .catch((err) => { throw new Error(err); }); + // trim Clash config to save memory + const start = raw.indexOf("proxies:"); + if (start !== -1) { + const end = raw.lastIndexOf("}") + 1; + raw = raw.substring(start, end); + } $.write(raw, `#${key}`); } else { + // 我也不知道这里为什么要等10ms,不加Surge报错。 + await $.wait(10); raw = cache; } } else { @@ -158,6 +166,12 @@ async function parseSub(sub, platform) { .catch((err) => { throw new Error(err); }); + // trim Clash config to save memory + const start = raw.indexOf("proxies:"); + if (start !== -1) { + const end = raw.lastIndexOf("}") + 1; + raw = raw.substring(start, end); + } $.write(raw, `#${sub.url}`); } @@ -648,6 +662,7 @@ function ProxyParser(targetPlatform) { // HTML format, maybe a wrong URL! throw new Error("Invalid format HTML!"); } else if (raw.indexOf("proxies") !== -1) { + console.log(`Preprocessing Clash config...`); // Clash YAML format // codes are modified from @KOP-XIAO // https://github.com/KOP-XIAO/QuantumultX @@ -664,6 +679,7 @@ function ProxyParser(targetPlatform) { .replace(/,/g, "\n ") } raw = raw.replace(/ -\n.*name/g, " - name").replace(/\$|\`/g, "").split("proxy-providers:")[0].split("proxy-groups:")[0].replace(/\"(name|type|server|port|cipher|password|)\"/g, "$1") + console.log(raw); const proxies = YAML.eval(raw).proxies; output = proxies.map((p) => JSON.stringify(p)); } else if (raw.indexOf("ssd://") === 0) { @@ -1774,6 +1790,7 @@ function URI_Producer() { result += encodeURIComponent(`v2ray-plugin;obfs=${opts.mode}${opts.host ? ";obfs-host" + opts.host : ""}${opts.tls ? ";tls" : ""}`); break default: + console.log(`FUCK`); throw new Error(`Unsupported plugin option: ${proxy.plugin}`); } } @@ -2461,7 +2478,7 @@ function HTTP(defaultOptions = {}) { } function API(name = "untitled", debug = false) { - const {isQX, isLoon, isSurge, isNode, isJSBox} = ENV(); + const {isQX, isLoon, isSurge, isNode, isJSBox, isScriptable} = ENV(); return new (class { constructor(name, debug) { this.name = name; @@ -2495,8 +2512,7 @@ function API(name = "untitled", debug = false) { }; } - // persistance - + // persistent // initialize cache initCache() { if (isQX) this.cache = JSON.parse($prefs.valueForKey(this.name) || "{}"); @@ -2559,7 +2575,7 @@ function API(name = "untitled", debug = false) { this.log(`SET ${key}`); if (key.indexOf("#") !== -1) { key = key.substr(1); - if (isSurge || isLoon) { + if (isSurge & isLoon) { $persistentStore.write(data, key); } if (isQX) { @@ -2578,7 +2594,7 @@ function API(name = "untitled", debug = false) { this.log(`READ ${key}`); if (key.indexOf("#") !== -1) { key = key.substr(1); - if (isSurge || isLoon) { + if (isSurge & isLoon) { return $persistentStore.read(key); } if (isQX) { @@ -2623,8 +2639,17 @@ function API(name = "untitled", debug = false) { if (isQX) $notify(title, subtitle, content, options); if (isSurge) $notification.post(title, subtitle, content_); - if (isLoon) $notification.post(title, subtitle, content, openURL); - if (isNode) { + if (isLoon) { + let opts = {}; + if (openURL) opts["openUrl"] = openURL; + if (mediaURL) opts["mediaUrl"] = mediaURL; + if (JSON.stringify(opts) === '{}') { + $notification.post(title, subtitle, content); + } else { + $notification.post(title, subtitle, content, opts); + } + } + if (isNode || isScriptable) { if (isJSBox) { const push = require("push"); push.schedule({ @@ -2649,6 +2674,22 @@ function API(name = "untitled", debug = false) { error(msg) { console.log("ERROR: " + msg); } + + wait(millisec) { + return new Promise((resolve) => setTimeout(resolve, millisec)); + } + + done(value = {}) { + if (isQX || isLoon || isSurge) { + $done(value); + } else if (isNode && !isJSBox) { + if (typeof $context !== "undefined") { + $context.headers = value.headers; + $context.statusCode = value.statusCode; + $context.body = value.body; + } + } + } })(name, debug); } diff --git a/config/Surge.sgmodule b/config/Surge.sgmodule index c938d1b..669d7f9 100644 --- a/config/Surge.sgmodule +++ b/config/Surge.sgmodule @@ -4,4 +4,4 @@ hostname=%APPEND% sub.store [Script] -Sub-Store = type=http-request,pattern=^https?:\/\/sub\.store,script-path=https://raw.githubusercontent.com/Peng-YM/Sub-Store/master/backend/sub-store.js,requires-body=true,timeout=120 +Sub-Store = type=http-request,pattern=^https?:\/\/sub\.store,script-path=https://raw.githubusercontent.com/Peng-YM/Sub-Store/master/backend/sub-store.js,requires-body=true,timeout=120,max-size=131072 diff --git a/web/src/components/ProxyList.vue b/web/src/components/ProxyList.vue index 474b498..db5d8ca 100644 --- a/web/src/components/ProxyList.vue +++ b/web/src/components/ProxyList.vue @@ -69,7 +69,9 @@ {{ info.name }} - +