bug fixed

This commit is contained in:
Peng-YM 2020-09-01 17:56:24 +08:00
parent 8fc5e58276
commit 2d6aa1ef1a
5 changed files with 2875 additions and 2877 deletions

View File

@ -22,8 +22,9 @@
<list default="true" id="8b97a098-48b2-4e64-a9ef-522fe2d30b52" name="Default Changelist" comment="">
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/sub-store.js" beforeDir="false" afterPath="$PROJECT_DIR$/sub-store.js" afterDir="false" />
<change beforePath="$PROJECT_DIR$/../web/src/components/SortOperator.vue" beforeDir="false" afterPath="$PROJECT_DIR$/../web/src/components/SortOperator.vue" afterDir="false" />
<change beforePath="$PROJECT_DIR$/../web/src/components/ProxyList.vue" beforeDir="false" afterPath="$PROJECT_DIR$/../web/src/components/ProxyList.vue" afterDir="false" />
<change beforePath="$PROJECT_DIR$/../web/src/config.js" beforeDir="false" afterPath="$PROJECT_DIR$/../web/src/config.js" afterDir="false" />
<change beforePath="$PROJECT_DIR$/../web/src/views/Subscription.vue" beforeDir="false" afterPath="$PROJECT_DIR$/../web/src/views/Subscription.vue" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
@ -93,6 +94,7 @@
<workItem from="1598900239005" duration="4742000" />
<workItem from="1598931009084" duration="2069000" />
<workItem from="1598946261983" duration="463000" />
<workItem from="1598948545209" duration="3969000" />
</task>
<servers />
</component>

View File

@ -9,8 +9,9 @@
* 3. 订阅拆分组合
*/
const $ = API("sub-store", true);
const $code = new Base64Code();
$.http = HTTP("", {
const $app = express();
$.http = HTTP({
headers: {
"User-Agent": "Quantumult",
},
@ -18,6 +19,26 @@ $.http = HTTP("", {
// Constants
const SUBS_KEY = "subs";
const COLLECTIONS_KEY = "collections";
const AVAILABLE_FILTERS = {
"Keyword Filter": KeywordFilter,
"Useless Filter": UselessFilter,
"Region Filter": RegionFilter,
"Regex Filter": RegexFilter,
"Type Filter": TypeFilter,
"Script Filter": ScriptFilter,
};
const AVAILABLE_OPERATORS = {
"Set Property Operator": SetPropertyOperator,
"Flag Operator": FlagOperator,
"Sort Operator": SortOperator,
"Keyword Sort Operator": KeywordSortOperator,
"Keyword Rename Operator": KeywordRenameOperator,
"Keyword Delete Operator": KeywordDeleteOperator,
"Regex Rename Operator": RegexRenameOperator,
"Regex Delete Operator": RegexDeleteOperator,
"Script Operator": ScriptOperator,
};
// SOME INITIALIZATIONS
if (!$.read(SUBS_KEY)) $.write({}, SUBS_KEY);
@ -25,20 +46,19 @@ if (!$.read(COLLECTIONS_KEY)) $.write({}, COLLECTIONS_KEY);
// BACKEND API
$.log("Initializing Express...");
const $app = express();
// download
$app.get("/download/collection/:name", downloadCollection);
$app.get("/download/:name", downloadSub);
// refresh
$app.post("/api/refresh", refreshResource);
// subscriptions
$app.route("/api/sub/:name").get(getSub).patch(updateSub).delete(deleteSub);
$app.route("/api/sub").get(getAllSubs).post(newSub).delete(deleteAllSubs);
// refresh
$app.post("/api/refresh", refreshResource);
// collections
$app
.route("/api/collection/:name")
@ -56,41 +76,13 @@ $app.all("/", async (req, res) => {
res.send("Hello from Sub-Store! Made with ❤️ by Peng-YM.");
});
$app.start();
$.log("Express initialized");
// SOME CONSTANTS
var DEFAULT_SUPPORTED_PLATFORMS = {
QX: true,
Loon: true,
Surge: true,
Raw: true,
};
var AVAILABLE_FILTERS = {
"Keyword Filter": KeywordFilter,
"Useless Filter": UselessFilter,
"Region Filter": RegionFilter,
"Regex Filter": RegexFilter,
"Type Filter": TypeFilter,
"Script Filter": ScriptFilter,
};
var AVAILABLE_OPERATORS = {
"Set Property Operator": SetPropertyOperator,
"Flag Operator": FlagOperator,
"Sort Operator": SortOperator,
"Keyword Sort Operator": KeywordSortOperator,
"Keyword Rename Operator": KeywordRenameOperator,
"Keyword Delete Operator": KeywordDeleteOperator,
"Regex Rename Operator": RegexRenameOperator,
"Regex Delete Operator": RegexDeleteOperator,
"Script Operator": ScriptOperator,
};
$app.start();
/**************************** API -- Subscriptions ***************************************/
// refresh resource
async function refreshResource(req, res) {
const Base64 = new Base64Code();
const {url} = req.body;
const raw = await $.http
.get(url)
@ -101,7 +93,7 @@ async function refreshResource(req, res) {
message: `Cannot refresh remote resource: ${url}\n Reason: ${err}`,
});
});
$.write(raw, `#${$code.safeEncode(url)}`);
$.write(raw, `#${Base64.safeEncode(url)}`);
res.json({
status: "success",
});
@ -133,7 +125,7 @@ async function downloadSub(req, res) {
async function parseSub(sub, platform) {
let raw;
const key = $code.safeEncode(sub.url);
const key = new Base64Code().safeEncode(sub.url);
if (platform === "Raw") {
const cache = $.read(`#${key}`);
@ -221,7 +213,7 @@ async function parseSub(sub, platform) {
if (item.type.indexOf("Filter") !== -1) {
const filter = AVAILABLE_FILTERS[item.type];
if (filter) {
$filter.addFilters(filter(item.args));
$filter.setFilter(filter(item.args));
proxies = $filter.process(proxies);
$.log(
`Applying filter "${item.type}" with arguments:\n >>> ${
@ -232,7 +224,7 @@ async function parseSub(sub, platform) {
} else if (item.type.indexOf("Operator") !== -1) {
const operator = AVAILABLE_OPERATORS[item.type];
if (operator) {
$operator.addOperators(operator(item.args));
$operator.setOperator(operator(item.args));
proxies = $operator.process(proxies);
$.log(
`Applying operator "${item.type}" with arguments: \n >>> ${
@ -336,6 +328,7 @@ async function newSub(req, res) {
async function updateSub(req, res) {
const {name} = req.params;
$.log(`Updating subscription: ${name}`);
let sub = req.body;
const allSubs = $.read(SUBS_KEY);
if (allSubs[name]) {
@ -666,54 +659,52 @@ function ProxyParser(targetPlatform) {
}
function ProxyFilter() {
const filters = [];
let filter;
function addFilters(...args) {
args.forEach((a) => filters.push(a));
function setFilter(arg) {
filter = arg;
}
// select proxies
function process(proxies) {
let selected = FULL(proxies.length, true);
for (const filter of filters) {
try {
selected = AND(selected, filter.func(proxies));
} catch (err) {
console.log(`Cannot apply filter ${filter.name}\n Reason: ${err}`);
}
}
return proxies.filter((_, i) => selected[i]);
}
return {
process,
addFilters,
setFilter,
};
}
function ProxyOperator() {
const operators = [];
let operator;
function addOperators(...args) {
args.forEach((a) => operators.push(a));
function setOperator(arg) {
operator = arg;
}
// run all operators
function process(proxies) {
let output = objClone(proxies);
for (const op of operators) {
try {
const output_ = op.func(output);
const output_ = operator.func(output);
if (output_) output = output_;
} catch (err) {
// print log and skip this operator
console.log(`ERROR: cannot apply operator ${op.name}! Reason: ${err}`);
}
}
return output;
}
return { addOperators, process };
return {setOperator, process};
}
/**************************** URI Format ***************************************/
@ -1719,10 +1710,10 @@ function FlagOperator(add = true) {
name: "Flag Operator",
func: (proxies) => {
return proxies.map((proxy) => {
if (!add)
if (!add) {
// no flag
proxy.name = removeFlag(proxy.name);
else {
} else {
// get flag
const newFlag = getFlag(proxy.name);
// remove old flag
@ -2184,7 +2175,7 @@ function removeFlag(str) {
}
// objClone an object
function objobjClone(obj) {
function objClone(obj) {
return JSON.parse(JSON.stringify(obj));
}
@ -2271,20 +2262,22 @@ function ENV() {
return {isQX, isLoon, isSurge, isNode, isJSBox, isRequest};
}
function HTTP(baseURL, defaultOptions = {}) {
function HTTP(defaultOptions = {}) {
const {isQX, isLoon, isSurge} = ENV();
const methods = ["GET", "POST", "PUT", "DELETE", "HEAD", "OPTIONS", "PATCH"];
function send(method, options) {
options = typeof options === "string" ? { url: options } : options;
options.url = baseURL ? baseURL + options.url : options.url;
options = options.hasOwnProperty("url") ? options : {url: options};
options.url = defaultOptions.baseURL ? defaultOptions.baseURL + options.url : options.url;
options = {...defaultOptions, ...options};
const timeout = options.timeout;
const events = {
...{
onRequest: () => {},
onRequest: () => {
},
onResponse: (resp) => resp,
onTimeout: () => {},
onTimeout: () => {
},
},
...options.events,
};
@ -2581,6 +2574,8 @@ 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);
}
@ -2614,9 +2609,6 @@ function express(port = 3000) {
body,
};
const res = Response();
if (typeof handler.callback === "undefined") {
$.notify("FUCK");
}
handler.callback(req, res, next).catch((err) => {
res.status(500).json({
status: "failed",
@ -2658,6 +2650,7 @@ function express(port = 3000) {
// start service
app.start = () => {
console.log(`STARTING TO ROUTE: ${JSON.stringify($request)}`);
dispatch($request);
};

View File

@ -36,7 +36,7 @@ import {axios} from "@/utils";
export default {
name: "ProxyList",
props: ['url'],
props: ['url', 'sub'],
data: function (){
return {
proxies: []
@ -44,7 +44,7 @@ export default {
},
methods: {
refresh() {
axios.post(`/refresh`, {url: this.url}).then(() => {
axios.post(`/refresh`, {url: this.sub}).then(() => {
this.fetch();
})
},

View File

@ -1,2 +1,3 @@
const DEBUG = process.env.NODE_ENV === "development";
// const DEBUG = process.env.NODE_ENV === "development";
const DEBUG = false;
export const BACKEND_BASE = DEBUG ? `http://192.168.1.134:3000` : `https://sub.store`;

View File

@ -148,7 +148,7 @@
</v-toolbar>
</v-card-title>
<v-card-text class="pl-0 pr-0">
<proxy-list :url="url" ref="proxyList" :key="url"></proxy-list>
<proxy-list :url="url" :sub="sub" ref="proxyList" :key="url"></proxy-list>
</v-card-text>
</v-card>
</v-dialog>
@ -166,6 +166,7 @@ export default {
opened: false,
showProxyList: false,
url: "",
sub: [],
editMenu: [
{
title: "复制",
@ -227,7 +228,8 @@ export default {
},
preview(item, type = 'sub') {
if (type === 'sub') {
this.url = `${BACKEND_BASE}/download/${item.name}`
this.url = `${BACKEND_BASE}/download/${item.name}`;
this.sub = [item.url];
} else {
this.url = `${BACKEND_BASE}/download/collection/${item.name}`
}