Modularized Sub-Store

This commit is contained in:
Peng-YM 2022-05-23 18:33:16 +08:00
parent 3caf743b09
commit 9216f5c256
22 changed files with 11029 additions and 8460 deletions

4
.gitignore vendored
View File

@ -1,7 +1,7 @@
.DS_Store
# json config
backend/sub-store.json
backend/root.json
backend/src/sub-store.json
backend/src/root.json
# Logs
logs

14
backend/banner Normal file
View File

@ -0,0 +1,14 @@
/**
* ███████╗██╗ ██╗██████╗ ███████╗████████╗ ██████╗ ██████╗ ███████╗
* ██╔════╝██║ ██║██╔══██╗ ██╔════╝╚══██╔══╝██╔═══██╗██╔══██╗██╔════╝
* ███████╗██║ ██║██████╔╝█████╗███████╗ ██║ ██║ ██║██████╔╝█████╗
* ╚════██║██║ ██║██╔══██╗╚════╝╚════██║ ██║ ██║ ██║██╔══██╗██╔══╝
* ███████║╚██████╔╝██████╔╝ ███████║ ██║ ╚██████╔╝██║ ██║███████╗
* ╚══════╝ ╚═════╝ ╚═════╝ ╚══════╝ ╚═╝ ╚═════╝ ╚═╝ ╚═╝╚══════╝
* Advanced Subscription Manager for QX, Loon, Surge and Clash.
* @version: 1.5
* @author: Peng-YM
* @github: https://github.com/Peng-YM/Sub-Store
* @documentation: https://www.notion.so/Sub-Store-6259586994d34c11a4ced5c406264b46
*/

7298
backend/package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -1,21 +1,25 @@
{
"name": "sub-store-backend",
"version": "0.0.1",
"description": "Advanced Subscription Manager for QX, Loon, and Surge.",
"main": "sub-store.js",
"name": "sub-store",
"version": "1.5",
"description": "Advanced Subscription Manager for QX, Loon, Surge, Stash and ShadowRocket.",
"main": "src/main.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"serve": "node sub-store.js",
"build": " curl -X POST -s --data-urlencode 'input@sub-store.js' https://javascript-minifier.com/raw > sub-store.min.js && printf \"// UPDATED AT: $(date) \\n\" | cat - sub-store.min.js > temp && mv temp sub-store.min.js"
"serve": "node src/main.js",
"build": "browserify -p tinyify src/main.js > bundle && cat banner bundle > sub-store.min.js && rm bundle"
},
"author": "",
"author": "Peng-YM",
"license": "GPL",
"dependencies": {
"body-parser": "^1.19.0",
"express": "^4.17.1",
"request": "^2.88.2"
"js-base64": "^3.7.2",
"request": "^2.88.2",
"static-js-yaml": "^1.0.0"
},
"devDependencies": {
"axios": "^0.20.0"
"axios": "^0.20.0",
"browserify": "^17.0.0",
"tinyify": "^3.0.0"
}
}

4
backend/src/core/app.js Normal file
View File

@ -0,0 +1,4 @@
const { API } = require('../utils/open-api');
const $ = API('sub-store');
module.exports = $;

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,293 @@
const $ = require("./app");
const RULE_TYPES_MAPPING = [
[ /^(DOMAIN|host|HOST)$/, 'DOMAIN' ],
[ /^(DOMAIN-KEYWORD|host-keyword|HOST-KEYWORD)$/, 'DOMAIN-KEYWORD' ],
[ /^(DOMAIN-SUFFIX|host-suffix|HOST-SUFFIX)$/, 'DOMAIN-SUFFIX' ],
[ /^USER-AGENT$/i, 'USER-AGENT' ],
[ /^PROCESS-NAME$/, 'PROCESS-NAME' ],
[ /^(DEST-PORT|DST-PORT)$/, 'DST-PORT' ],
[ /^SRC-IP(-CIDR)?$/, 'SRC-IP' ],
[ /^(IN|SRC)-PORT$/, 'IN-PORT' ],
[ /^PROTOCOL$/, 'PROTOCOL' ],
[ /^IP-CIDR$/i, 'IP-CIDR' ],
[ /^(IP-CIDR6|ip6-cidr|IP6-CIDR)$/ ]
];
const RULE_PREPROCESSORS = (function() {
function HTML() {
const name = 'HTML';
const test = (raw) => /^<!DOCTYPE html>/.test(raw);
// simply discard HTML
const parse = (_) => '';
return { name, test, parse };
}
function ClashProvider() {
const name = 'Clash Provider';
const test = (raw) => raw.indexOf('payload:') === 0;
const parse = (raw) => {
return raw.replace('payload:', '').replace(/^\s*-\s*/gm, '');
};
return { name, test, parse };
}
return [ HTML(), ClashProvider() ];
})();
const RULE_PARSERS = (function() {
function AllRuleParser() {
const name = 'Universal Rule Parser';
const test = () => true;
const parse = (raw) => {
const lines = raw.split('\n');
const result = [];
for (let line of lines) {
line = line.trim();
// skip empty line
if (line.length === 0) continue;
// skip comments
if (/\s*#/.test(line)) continue;
try {
const params = line.split(',').map((w) => w.trim());
let rawType = params[0];
let matched = false;
for (const item of RULE_TYPES_MAPPING) {
const regex = item[0];
if (regex.test(rawType)) {
matched = true;
const rule = {
type: item[1],
content: params[1]
};
if (rule.type === 'IP-CIDR' || rule.type === 'IP-CIDR6') {
rule.options = params.slice(2);
}
result.push(rule);
}
}
if (!matched) throw new Error('Invalid rule type: ' + rawType);
} catch (e) {
console.error(`Failed to parse line: ${line}\n Reason: ${e}`);
}
}
return result;
};
return { name, test, parse };
}
return [ AllRuleParser() ];
})();
const RULE_PROCESSORS = (function() {
function RegexFilter({ regex = [], keep = true }) {
return {
name: 'Regex Filter',
func: (rules) => {
return rules.map((rule) => {
const selected = regex.some((r) => {
r = new RegExp(r);
return r.test(rule);
});
return keep ? selected : !selected;
});
}
};
}
function TypeFilter(types) {
return {
name: 'Type Filter',
func: (rules) => {
return rules.map((rule) => types.some((t) => rule.type === t));
}
};
}
function RemoveDuplicateFilter() {
return {
name: 'Remove Duplicate Filter',
func: (rules) => {
const seen = new Set();
const result = [];
rules.forEach((rule) => {
const options = rule.options || [];
options.sort();
const key = `${rule.type},${rule.content},${JSON.stringify(options)}`;
if (!seen.has(key)) {
result.push(rule);
seen.add(key);
}
});
return result;
}
};
}
// regex: [{expr: "string format regex", now: "now"}]
function RegexReplaceOperator(regex) {
return {
name: 'Regex Rename Operator',
func: (rules) => {
return rules.map((rule) => {
for (const { expr, now } of regex) {
rule.content = rule.content.replace(new RegExp(expr, 'g'), now).trim();
}
return rule;
});
}
};
}
return {
'Regex Filter': RegexFilter,
'Remove Duplicate Filter': RemoveDuplicateFilter,
'Type Filter': TypeFilter,
'Regex Replace Operator': RegexReplaceOperator
};
})();
const RULE_PRODUCERS = (function() {
function QXFilter() {
const type = 'SINGLE';
const func = (rule) => {
// skip unsupported rules
const UNSUPPORTED = [ 'URL-REGEX', 'DEST-PORT', 'SRC-IP', 'IN-PORT', 'PROTOCOL' ];
if (UNSUPPORTED.indexOf(rule.type) !== -1) return null;
const TRANSFORM = {
'DOMAIN-KEYWORD': 'HOST-KEYWORD',
'DOMAIN-SUFFIX': 'HOST-SUFFIX',
DOMAIN: 'HOST',
'IP-CIDR6': 'IP6-CIDR'
};
// QX does not support the no-resolve option
return `${TRANSFORM[rule.type] || rule.type},${rule.content},SUB-STORE`;
};
return { type, func };
}
function SurgeRuleSet() {
const type = 'SINGLE';
const func = (rule) => {
let output = `${rule.type},${rule.content}`;
if (rule.type === 'IP-CIDR' || rule.type === 'IP-CIDR6') {
output += rule.options ? `,${rule.options[0]}` : '';
}
return output;
};
return { type, func };
}
function LoonRules() {
const type = 'SINGLE';
const func = (rule) => {
// skip unsupported rules
const UNSUPPORTED = [ 'DEST-PORT', 'SRC-IP', 'IN-PORT', 'PROTOCOL' ];
if (UNSUPPORTED.indexOf(rule.type) !== -1) return null;
return SurgeRuleSet().func(rule);
};
return { type, func };
}
function ClashRuleProvider() {
const type = 'ALL';
const func = (rules) => {
const TRANSFORM = {
'DEST-PORT': 'DST-PORT',
'SRC-IP': 'SRC-IP-CIDR',
'IN-PORT': 'SRC-PORT'
};
const conf = {
payload: rules.map((rule) => {
let output = `${TRANSFORM[rule.type] || rule.type},${rule.content}`;
if (rule.type === 'IP-CIDR' || rule.type === 'IP-CIDR6') {
output += rule.options ? `,${rule.options[0]}` : '';
}
return output;
})
};
return YAML.stringify(conf);
};
return { type, func };
}
return {
QX: QXFilter(),
Surge: SurgeRuleSet(),
Loon: LoonRules(),
Clash: ClashRuleProvider()
};
})();
const RuleUtils = (function() {
function preprocess(raw) {
for (const processor of RULE_PREPROCESSORS) {
try {
if (processor.test(raw)) {
$.info(`Pre-processor [${processor.name}] activated`);
return processor.parse(raw);
}
} catch (e) {
$.error(`Parser [${processor.name}] failed\n Reason: ${e}`);
}
}
return raw;
}
function parse(raw) {
raw = preprocess(raw);
for (const parser of RULE_PARSERS) {
let matched;
try {
matched = parser.test(raw);
} catch (err) {
matched = false;
}
if (matched) {
$.info(`Rule parser [${parser.name}] is activated!`);
return parser.parse(raw);
}
}
}
async function process(rules, operators) {
for (const item of operators) {
if (!RULE_PROCESSORS[item.type]) {
console.error(`Unknown operator: ${item.type}!`);
continue;
}
const processor = RULE_PROCESSORS[item.type](item.args);
$.info(`Applying "${item.type}" with arguments: \n >>> ${JSON.stringify(item.args) || 'None'}`);
rules = ApplyProcessor(processor, rules);
}
return rules;
}
function produce(rules, targetPlatform) {
const producer = RULE_PRODUCERS[targetPlatform];
if (!producer) {
throw new Error(`Target platform: ${targetPlatform} is not supported!`);
}
if (typeof producer.type === 'undefined' || producer.type === 'SINGLE') {
return rules
.map((rule) => {
try {
return producer.func(rule);
} catch (err) {
console.log(`ERROR: cannot produce rule: ${JSON.stringify(rule)}\nReason: ${err}`);
return '';
}
})
.filter((line) => line.length > 0)
.join('\n');
} else if (producer.type === 'ALL') {
return producer.func(rules);
}
}
return { parse, process, produce };
})();
module.exports = {
RuleUtils
};

View File

@ -0,0 +1,353 @@
const $ = require('../core/app');
const download = require('../utils/download');
const Gist = require('../utils/gist');
const { ProxyUtils } = require('../core/proxy-utils');
const { RuleUtils } = require('../core/rule-utils');
const { SUBS_KEY, ARTIFACTS_KEY, ARTIFACT_REPOSITORY_KEY, COLLECTIONS_KEY, RULES_KEY, SETTINGS_KEY } = require('./constants');
function register($app) {
// Initialization
if (!$.read(ARTIFACTS_KEY)) $.write({}, ARTIFACTS_KEY);
// RESTful APIs
$app.route('/api/artifacts').get(getAllArtifacts).post(createArtifact);
$app.route('/api/artifact/:name').get(getArtifact).patch(updateArtifact).delete(deleteArtifact);
// sync all artifacts
$app.get('/api/cron/sync-artifacts', cronSyncArtifacts);
}
async function getArtifact(req, res) {
const name = req.params.name;
const action = req.query.action;
const allArtifacts = $.read(ARTIFACTS_KEY);
const artifact = allArtifacts[name];
if (artifact) {
if (action) {
let item;
switch (artifact.type) {
case 'subscription':
item = $.read(SUBS_KEY)[artifact.source];
break;
case 'collection':
item = $.read(COLLECTIONS_KEY)[artifact.source];
break;
case 'rule':
item = $.read(RULES_KEY)[artifact.source];
break;
}
const output = await produceArtifact({
type: artifact.type,
item,
platform: artifact.platform
});
if (action === 'preview') {
res.send(output);
} else if (action === 'sync') {
$.info(`正在上传配置:${artifact.name}\n>>>`);
console.log(JSON.stringify(artifact, null, 2));
try {
const resp = await syncArtifact({
[artifact.name]: { content: output }
});
artifact.updated = new Date().getTime();
const body = JSON.parse(resp.body);
artifact.url = body.files[artifact.name].raw_url.replace(/\/raw\/[^\/]*\/(.*)/, '/raw/$1');
$.write(allArtifacts, ARTIFACTS_KEY);
res.json({
status: 'success'
});
} catch (err) {
res.status(500).json({
status: 'failed',
message: err
});
}
}
} else {
res.json({
status: 'success',
data: artifact
});
}
} else {
res.status(404).json({
status: 'failed',
message: '未找到对应的配置!'
});
}
}
function createArtifact(req, res) {
const artifact = req.body;
$.info(`正在创建远程配置:${artifact.name}`);
const allArtifacts = $.read(ARTIFACTS_KEY);
if (allArtifacts[artifact.name]) {
res.status(500).json({
status: 'failed',
message: `远程配置${artifact.name}已存在!`
});
} else {
if (/^[\w-_.]*$/.test(artifact.name)) {
allArtifacts[artifact.name] = artifact;
$.write(allArtifacts, ARTIFACTS_KEY);
res.status(201).json({
status: 'success',
data: artifact
});
} else {
res.status(500).json({
status: 'failed',
message: `远程配置名称 ${artifact.name} 中含有非法字符!名称中只能包含英文字母、数字、下划线、横杠。`
});
}
}
}
function updateArtifact(req, res) {
const allArtifacts = $.read(ARTIFACTS_KEY);
const oldName = req.params.name;
const artifact = allArtifacts[oldName];
if (artifact) {
$.info(`正在更新远程配置:${artifact.name}`);
const newArtifact = req.body;
if (typeof newArtifact.name !== 'undefined' && !/^[\w-_.]*$/.test(newArtifact.name)) {
res.status(500).json({
status: 'failed',
message: `远程配置名称 ${newArtifact.name} 中含有非法字符!名称中只能包含英文字母、数字、下划线、横杠。`
});
} else {
const merged = {
...artifact,
...newArtifact
};
allArtifacts[merged.name] = merged;
if (merged.name !== oldName) delete allArtifacts[oldName];
$.write(allArtifacts, ARTIFACTS_KEY);
res.json({
status: 'success',
data: merged
});
}
} else {
res.status(404).json({
status: 'failed',
message: '未找到对应的远程配置!'
});
}
}
async function cronSyncArtifacts(_, res) {
$.info('开始同步所有远程配置...');
const allArtifacts = $.read(ARTIFACTS_KEY);
const files = {};
try {
await Promise.all(
Object.values(allArtifacts).map(async (artifact) => {
if (artifact.sync) {
$.info(`正在同步云配置:${artifact.name}...`);
let item;
switch (artifact.type) {
case 'subscription':
item = $.read(SUBS_KEY)[artifact.source];
break;
case 'collection':
item = $.read(COLLECTIONS_KEY)[artifact.source];
break;
case 'rule':
item = $.read(RULES_KEY)[artifact.source];
break;
}
const output = await produceArtifact({
type: artifact.type,
item,
platform: artifact.platform
});
files[artifact.name] = {
content: output
};
}
})
);
const resp = await syncArtifact(files);
const body = JSON.parse(resp.body);
for (const artifact of Object.values(allArtifacts)) {
artifact.updated = new Date().getTime();
// extract real url from gist
artifact.url = body.files[artifact.name].raw_url.replace(/\/raw\/[^\/]*\/(.*)/, '/raw/$1');
}
$.write(allArtifacts, ARTIFACTS_KEY);
$.info('全部订阅同步成功!');
res.status(200).end();
} catch (err) {
res.status(500).json({
error: err
});
$.info(`同步订阅失败,原因:${err}`);
}
}
async function deleteArtifact(req, res) {
const name = req.params.name;
$.info(`正在删除远程配置:${name}`);
const allArtifacts = $.read(ARTIFACTS_KEY);
try {
const artifact = allArtifacts[name];
if (!artifact) throw new Error(`远程配置:${name}不存在!`);
if (artifact.updated) {
// delete gist
await syncArtifact({
filename: name,
content: ''
});
}
// delete local cache
delete allArtifacts[name];
$.write(allArtifacts, ARTIFACTS_KEY);
res.json({
status: 'success'
});
} catch (err) {
// delete local cache
delete allArtifacts[name];
$.write(allArtifacts, ARTIFACTS_KEY);
res.status(500).json({
status: 'failed',
message: `无法删除远程配置:${name}, 原因:${err}`
});
}
}
function getAllArtifacts(req, res) {
const allArtifacts = $.read(ARTIFACTS_KEY);
res.json({
status: 'success',
data: allArtifacts
});
}
async function syncArtifact(files) {
const { gistToken } = $.read(SETTINGS_KEY);
if (!gistToken) {
return Promise.reject('未设置Gist Token');
}
const manager = new Gist({
token: gistToken,
key: ARTIFACT_REPOSITORY_KEY
});
return manager.upload(files);
}
async function produceArtifact(
{ type, item, platform, noProcessor } = {
platform: 'JSON',
noProcessor: false
}
) {
if (type === 'subscription') {
const sub = item;
const raw = await download(sub.url, sub.ua);
// parse proxies
let proxies = ProxyUtils.parse(raw);
if (!noProcessor) {
// apply processors
proxies = await ProxyUtils.process(proxies, sub.process || [], platform);
}
// check duplicate
const exist = {};
for (const proxy of proxies) {
if (exist[proxy.name]) {
$.notify('🌍 『 𝑺𝒖𝒃-𝑺𝒕𝒐𝒓𝒆 』', '⚠️ 订阅包含重复节点!', '请仔细检测配置!', {
'media-url':
'https://cdn3.iconfinder.com/data/icons/seo-outline-1/512/25_code_program_programming_develop_bug_search_developer-512.png'
});
break;
}
exist[proxy.name] = true;
}
// produce
return ProxyUtils.produce(proxies, platform);
} else if (type === 'collection') {
const allSubs = $.read(SUBS_KEY);
const collection = item;
const subs = collection['subscriptions'];
let proxies = [];
let processed = 0;
await Promise.all(
subs.map(async (name) => {
const sub = allSubs[name];
try {
$.info(`正在处理子订阅:${sub.name}...`);
const raw = await download(sub.url, sub.ua);
// parse proxies
let currentProxies = ProxyUtils.parse(raw);
if (!noProcessor) {
// apply processors
currentProxies = await ProxyUtils.process(currentProxies, sub.process || [], platform);
}
// merge
proxies = proxies.concat(currentProxies);
processed++;
$.info(`✅ 子订阅:${sub.name}加载成功,进度--${100 * (processed / subs.length).toFixed(1)}% `);
} catch (err) {
processed++;
$.error(
`❌ 处理组合订阅中的子订阅: ${sub.name}时出现错误:${err},该订阅已被跳过!进度--${100 *
(processed / subs.length).toFixed(1)}%`
);
}
})
);
if (!noProcessor) {
// apply own processors
proxies = await ProxyUtils.process(proxies, collection.process || [], platform);
}
if (proxies.length === 0) {
throw new Error(`组合订阅中不含有效节点!`);
}
// check duplicate
const exist = {};
for (const proxy of proxies) {
if (exist[proxy.name]) {
$.notify('🌍 『 𝑺𝒖𝒃-𝑺𝒕𝒐𝒓𝒆 』', '⚠️ 订阅包含重复节点!', '请仔细检测配置!', {
'media-url':
'https://cdn3.iconfinder.com/data/icons/seo-outline-1/512/25_code_program_programming_develop_bug_search_developer-512.png'
});
break;
}
exist[proxy.name] = true;
}
return ProxyUtils.produce(proxies, platform);
} else if (type === 'rule') {
const rule = item;
let rules = [];
for (let i = 0; i < rule.urls.length; i++) {
const url = rule.urls[i];
$.info(`正在处理URL${url},进度--${100 * ((i + 1) / rule.urls.length).toFixed(1)}% `);
try {
const { body } = await download(url);
const currentRules = RuleUtils.parse(body);
rules = rules.concat(currentRules);
} catch (err) {
$.error(`处理分流订阅中的URL: ${url}时出现错误:${err}! 该订阅已被跳过。`);
}
}
// remove duplicates
rules = await RuleUtils.process(rules, [ { type: 'Remove Duplicate Filter' } ]);
// produce output
return RuleUtils.produce(rules, platform);
}
}
module.exports = { register, produceArtifact };

View File

@ -0,0 +1,155 @@
const $ = require('../core/app');
const { SUBS_KEY, COLLECTIONS_KEY } = require('./constants');
const { getPlatformFromHeaders, getFlowHeaders } = require('./subscriptions');
const { produceArtifact } = require('./artifacts');
function register($app) {
if (!$.read(COLLECTIONS_KEY)) $.write({}, COLLECTIONS_KEY);
$app.get("/download/collection/:name", downloadCollection);
$app.route('/api/collection/:name').get(getCollection).patch(updateCollection).delete(deleteCollection);
$app.route('/api/collections').get(getAllCollections).post(createCollection);
}
// collection API
async function downloadCollection(req, res) {
const { name } = req.params;
const { raw } = req.query || 'false';
const platform = req.query.target || getPlatformFromHeaders(req.headers) || 'JSON';
const allCollections = $.read(COLLECTIONS_KEY);
const collection = allCollections[name];
$.info(`正在下载组合订阅:${name}`);
// forward flow header from the first subscription in this collection
const allSubs = $.read(SUBS_KEY);
const subs = collection['subscriptions'];
if (subs.length > 0) {
const sub = allSubs[subs[0]];
const flowInfo = await getFlowHeaders(sub.url);
if (flowInfo) {
res.set('subscription-userinfo', flowInfo);
}
}
if (collection) {
try {
const output = await produceArtifact({
type: 'collection',
item: collection,
platform,
noProcessor: raw
});
if (platform === 'JSON') {
res.set('Content-Type', 'application/json;charset=utf-8').send(output);
} else {
res.send(output);
}
} catch (err) {
$.notify(`🌍 『 𝑺𝒖𝒃-𝑺𝒕𝒐𝒓𝒆 』 下载组合订阅失败`, `❌ 下载组合订阅错误:${name}`, `🤔 原因:${err}`);
res.status(500).json({
status: 'failed',
message: err
});
}
} else {
$.notify(`🌍 『 𝑺𝒖𝒃-𝑺𝒕𝒐𝒓𝒆 』 下载组合订阅失败`, `❌ 未找到组合订阅:${name}`);
res.status(404).json({
status: 'failed'
});
}
}
function createCollection(req, res) {
const collection = req.body;
$.info(`正在创建组合订阅:${collection.name}`);
const allCol = $.read(COLLECTIONS_KEY);
if (allCol[collection.name]) {
res.status(500).json({
status: 'failed',
message: `订阅集${collection.name}已存在!`
});
}
// validate name
if (/^[\w-_]*$/.test(collection.name)) {
allCol[collection.name] = collection;
$.write(allCol, COLLECTIONS_KEY);
res.status(201).json({
status: 'success',
data: collection
});
} else {
res.status(500).json({
status: 'failed',
message: `订阅集名称 ${collection.name} 中含有非法字符!名称中只能包含英文字母、数字、下划线、横杠。`
});
}
}
function getCollection(req, res) {
const { name } = req.params;
const collection = $.read(COLLECTIONS_KEY)[name];
if (collection) {
res.json({
status: 'success',
data: collection
});
} else {
res.status(404).json({
status: 'failed',
message: `未找到订阅集:${name}!`
});
}
}
function updateCollection(req, res) {
const { name } = req.params;
let collection = req.body;
const allCol = $.read(COLLECTIONS_KEY);
if (allCol[name]) {
const newCol = {
...allCol[name],
...collection
};
$.info(`正在更新组合订阅:${name}...`);
// allow users to update collection name
delete allCol[name];
allCol[collection.name || name] = newCol;
$.write(allCol, COLLECTIONS_KEY);
res.json({
status: 'success',
data: newCol
});
} else {
res.status(500).json({
status: 'failed',
message: `订阅集${name}不存在,无法更新!`
});
}
}
function deleteCollection(req, res) {
const { name } = req.params;
$.info(`正在删除组合订阅:${name}`);
let allCol = $.read(COLLECTIONS_KEY);
delete allCol[name];
$.write(allCol, COLLECTIONS_KEY);
res.json({
status: 'success'
});
}
function getAllCollections(req, res) {
const allCols = $.read(COLLECTIONS_KEY);
res.json({
status: 'success',
data: allCols
});
}
module.exports = {
register
};

View File

@ -0,0 +1,11 @@
module.exports = {
SETTINGS_KEY: 'settings',
SUBS_KEY: 'subs',
COLLECTIONS_KEY: 'collections',
RULES_KEY: 'rules',
BUILT_IN_KEY: 'builtin',
ARTIFACTS_KEY: 'artifacts',
GIST_BACKUP_KEY: 'Auto Generated Sub-Store Backup',
GIST_BACKUP_FILE_NAME: 'Sub-Store',
ARTIFACT_REPOSITORY_KEY: 'Sub-Store Artifacts Repository'
};

115
backend/src/facade/index.js Normal file
View File

@ -0,0 +1,115 @@
const $ = require('../core/app');
const { ENV } = require('../utils/open-api');
const { IP_API } = require('../utils/geo');
const Gist = require('../utils/gist');
const { SETTINGS_KEY, GIST_BACKUP_KEY, GIST_BACKUP_FILE_NAME } = require('./constants');
function serve() {
const express = require('../utils/express');
const $app = express();
// register routes
const collections = require('./collections');
collections.register($app);
const subscriptions = require('./subscriptions');
subscriptions.register($app);
const settings = require('./settings');
settings.register($app);
const artifacts = require('./artifacts');
artifacts.register($app);
// utils
$app.get('/api/utils/IP_API/:server', IP_API); // IP-API reverse proxy
$app.get('/api/utils/env', getEnv); // get runtime environment
$app.get('/api/utils/backup', gistBackup); // gist backup actions
// Redirect sub.store to vercel webpage
$app.get('/', async (req, res) => {
// 302 redirect
res.set('location', 'https://sub-store.vercel.app/').status(302).end();
});
// handle preflight request for QX
if (ENV().isQX) {
$app.options('/', async (req, res) => {
res.status(200).end();
});
}
$app.all('/', (_, res) => {
res.send('Hello from sub-store, made with ❤️ by Peng-YM');
});
$app.start();
}
function getEnv(req, res) {
const { isNode, isQX, isLoon, isSurge } = ENV();
let backend = 'Node';
if (isNode) backend = 'Node';
if (isQX) backend = 'QX';
if (isLoon) backend = 'Loon';
if (isSurge) backend = 'Surge';
res.json({
backend
});
}
async function gistBackup(req, res) {
const { action } = req.query;
// read token
const { gistToken } = $.read(SETTINGS_KEY);
if (!gistToken) {
res.status(500).json({
status: 'failed',
message: '未找到Gist备份Token!'
});
} else {
const gist = new Gist({
token: gistToken,
key: GIST_BACKUP_KEY
});
try {
let content;
switch (action) {
case 'upload':
// update syncTime.
const settings = $.read(SETTINGS_KEY);
settings.syncTime = new Date().getTime();
$.write(settings, SETTINGS_KEY);
content = $.read('#sub-store');
if ($.env.isNode) content = JSON.stringify($.cache, null, ` `);
$.info(`上传备份中...`);
await gist.upload({ [GIST_BACKUP_FILE_NAME]: { content } });
break;
case 'download':
$.info(`还原备份中...`);
content = await gist.download(GIST_BACKUP_FILE_NAME);
// restore settings
$.write(content, '#sub-store');
if ($.env.isNode) {
content = JSON.parse(content);
Object.keys(content).forEach((key) => {
$.write(content[key], key);
});
}
break;
}
res.json({
status: 'success'
});
} catch (err) {
const msg = `${action === 'upload' ? '上传' : '下载'}备份失败!${err}`;
$.error(msg);
res.status(500).json({
status: 'failed',
message: msg
});
}
}
}
module.exports = serve;

View File

@ -0,0 +1,32 @@
const $ = require('../core/app');
const { SETTINGS_KEY } = require('./constants');
function register($app) {
if (!$.read(SETTINGS_KEY)) $.write({}, SETTINGS_KEY);
$app.route('/api/settings').get(getSettings).patch(updateSettings);
}
function getSettings(req, res) {
const settings = $.read(SETTINGS_KEY);
res.json(settings);
}
function updateSettings(req, res) {
const data = req.body;
const settings = $.read(SETTINGS_KEY);
$.write(
{
...settings,
...data
},
SETTINGS_KEY
);
res.json({
status: 'success'
});
}
module.exports = {
register
};

View File

@ -0,0 +1,205 @@
const $ = require("../core/app");
const { produceArtifact } = require('./artifacts');
const { SUBS_KEY, COLLECTIONS_KEY } = require('./constants');
function register($app) {
if (!$.read(SUBS_KEY)) $.write({}, SUBS_KEY);
$app.get('/download/:name', downloadSubscription);
$app.route('/api/sub/:name').get(getSubscription).patch(updateSubscription).delete(deleteSubscription);
$app.route('/api/subs').get(getAllSubscriptions).post(createSubscription);
}
// subscriptions API
async function downloadSubscription(req, res) {
const { name } = req.params;
const { raw } = req.query || 'false';
const platform = req.query.target || getPlatformFromHeaders(req.headers) || 'JSON';
$.info(`正在下载订阅:${name}`);
const allSubs = $.read(SUBS_KEY);
const sub = allSubs[name];
if (sub) {
try {
const output = await produceArtifact({
type: 'subscription',
item: sub,
platform,
noProcessor: raw
});
// forward flow headers
const flowInfo = await getFlowHeaders(sub.url);
if (flowInfo) {
res.set('subscription-userinfo', flowInfo);
}
if (platform === 'JSON') {
res.set('Content-Type', 'application/json;charset=utf-8').send(output);
} else {
res.send(output);
}
} catch (err) {
$.notify(`🌍 『 𝑺𝒖𝒃-𝑺𝒕𝒐𝒓𝒆 』 下载订阅失败`, `❌ 无法下载订阅:${name}`, `🤔 原因:${JSON.stringify(err)}`);
$.error(JSON.stringify(err));
res.status(500).json({
status: 'failed',
message: err
});
}
} else {
$.notify(`🌍 『 𝑺𝒖𝒃-𝑺𝒕𝒐𝒓𝒆 』 下载订阅失败`, `❌ 未找到订阅:${name}`);
res.status(404).json({
status: 'failed'
});
}
}
function createSubscription(req, res) {
const sub = req.body;
const allSubs = $.read(SUBS_KEY);
$.info(`正在创建订阅: ${sub.name}`);
if (allSubs[sub.name]) {
res.status(500).json({
status: 'failed',
message: `订阅${sub.name}已存在!`
});
}
// validate name
if (/^[\w-_]*$/.test(sub.name)) {
allSubs[sub.name] = sub;
$.write(allSubs, SUBS_KEY);
res.status(201).json({
status: 'success',
data: sub
});
} else {
res.status(500).json({
status: 'failed',
message: `订阅名称 ${sub.name} 中含有非法字符!名称中只能包含英文字母、数字、下划线、横杠。`
});
}
}
function getSubscription(req, res) {
const { name } = req.params;
const sub = $.read(SUBS_KEY)[name];
if (sub) {
res.json({
status: 'success',
data: sub
});
} else {
res.status(404).json({
status: 'failed',
message: `未找到订阅:${name}!`
});
}
}
function updateSubscription(req, res) {
const { name } = req.params;
let sub = req.body;
const allSubs = $.read(SUBS_KEY);
if (allSubs[name]) {
const newSub = {
...allSubs[name],
...sub
};
$.info(`正在更新订阅: ${name}`);
// allow users to update the subscription name
if (name !== sub.name) {
// we need to find out all collections refer to this name
const allCols = $.read(COLLECTIONS_KEY);
for (const k of Object.keys(allCols)) {
const idx = allCols[k].subscriptions.indexOf(name);
if (idx !== -1) {
allCols[k].subscriptions[idx] = sub.name;
}
}
// update subscriptions
delete allSubs[name];
allSubs[sub.name] = newSub;
} else {
allSubs[name] = newSub;
}
$.write(allSubs, SUBS_KEY);
res.json({
status: 'success',
data: newSub
});
} else {
res.status(500).json({
status: 'failed',
message: `订阅${name}不存在,无法更新!`
});
}
}
function deleteSubscription(req, res) {
const { name } = req.params;
$.info(`删除订阅:${name}...`);
// delete from subscriptions
let allSubs = $.read(SUBS_KEY);
delete allSubs[name];
$.write(allSubs, SUBS_KEY);
// delete from collections
let allCols = $.read(COLLECTIONS_KEY);
for (const k of Object.keys(allCols)) {
allCols[k].subscriptions = allCols[k].subscriptions.filter((s) => s !== name);
}
$.write(allCols, COLLECTIONS_KEY);
res.json({
status: 'success'
});
}
function getAllSubscriptions(req, res) {
const allSubs = $.read(SUBS_KEY);
res.json({
status: 'success',
data: allSubs
});
}
async function getFlowHeaders(url) {
const { headers } = await $.http.get({
url,
headers: {
'User-Agent': 'Quantumult/1.0.13 (iPhone10,3; iOS 14.0)'
}
});
const subkey = Object.keys(headers).filter((k) => /SUBSCRIPTION-USERINFO/i.test(k))[0];
return headers[subkey];
}
function getPlatformFromHeaders(headers) {
const keys = Object.keys(headers);
let UA = '';
for (let k of keys) {
if (/USER-AGENT/i.test(k)) {
UA = headers[k];
break;
}
}
if (UA.indexOf('Quantumult%20X') !== -1) {
return 'QX';
} else if (UA.indexOf('Surge') !== -1) {
return 'Surge';
} else if (UA.indexOf('Decar') !== -1 || UA.indexOf('Loon') !== -1) {
return 'Loon';
} else if (UA.indexOf('Stash') !== -1 || UA.indexOf('Shadowrocket') !== -1) {
return 'Clash';
} else {
return null;
}
}
module.exports = {
register,
getPlatformFromHeaders,
getFlowHeaders
};

23
backend/src/main.js Normal file
View File

@ -0,0 +1,23 @@
/**
*
*
*
*
*
*
* Advanced Subscription Manager for QX, Loon, Surge and Clash.
* @author: Peng-YM
* @github: https://github.com/Peng-YM/Sub-Store
* @documentation: https://www.notion.so/Sub-Store-6259586994d34c11a4ced5c406264b46
*/
console.log(
`
𝑺𝒖𝒃-𝑺𝒕𝒐𝒓𝒆 © 𝑷𝒆𝒏𝒈-𝒀𝑴
`
);
const serve = require('./facade');
serve();

View File

@ -0,0 +1,29 @@
const { HTTP } = require('./open-api');
const cache = new Map();
async function download(url, userAgent = 'Quantumult%20X') {
const id = userAgent + url;
if (cache.has(id)) {
return cache.get(id);
}
const $http = HTTP({
headers: {
'User-Agent': userAgent
}
});
const result = new Promise((resolve, reject) => {
$http.get(url).then((resp) => {
const body = resp.body;
if (body.replace(/\s/g, '').length === 0) reject(new Error('订阅内容为空!'));
else resolve(body);
});
});
cache[id] = result;
return result;
}
module.exports = download;

View File

@ -0,0 +1,275 @@
const $ = require('../core/app');
const { ENV } = require('./open-api');
function express({ port } = { port: 3000 }) {
const { isNode } = ENV();
const DEFAULT_HEADERS = {
'Content-Type': 'text/plain;charset=UTF-8',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'POST,GET,OPTIONS,PATCH,PUT,DELETE',
'Access-Control-Allow-Headers': 'Origin, X-Requested-With, Content-Type, Accept'
};
// node support
if (isNode) {
const express_ = eval(`require("express")`);
const bodyParser = eval(`require("body-parser")`);
const app = express_();
app.use(bodyParser.json({ verify: rawBodySaver }));
app.use(bodyParser.urlencoded({ verify: rawBodySaver, extended: true }));
app.use(bodyParser.raw({ verify: rawBodySaver, type: '*/*' }));
app.use((_, res, next) => {
res.set(DEFAULT_HEADERS);
next();
});
// adapter
app.start = () => {
app.listen(port, () => {
$.log(`Express started on port: ${port}`);
});
};
return app;
}
// route handlers
const handlers = [];
// http methods
const METHODS_NAMES = [ 'GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS', "HEAD'", 'ALL' ];
// dispatch url to route
const dispatch = (request, start = 0) => {
let { method, url, headers, body } = request;
if (/json/i.test(headers['Content-Type'])) {
body = JSON.parse(body);
}
method = method.toUpperCase();
const { path, query } = extractURL(url);
// pattern match
let handler = null;
let i;
let longestMatchedPattern = 0;
for (i = start; i < handlers.length; i++) {
if (handlers[i].method === 'ALL' || method === handlers[i].method) {
const { pattern } = handlers[i];
if (patternMatched(pattern, path)) {
if (pattern.split('/').length > longestMatchedPattern) {
handler = handlers[i];
longestMatchedPattern = pattern.split('/').length;
}
}
}
}
if (handler) {
// dispatch to next handler
const next = () => {
dispatch(method, url, i);
};
const req = {
method,
url,
path,
query,
params: extractPathParams(handler.pattern, path),
headers,
body
};
const res = Response();
const cb = handler.callback;
const errFunc = (err) => {
res.status(500).json({
status: 'failed',
message: `Internal Server Error: ${err}`
});
};
if (cb.constructor.name === 'AsyncFunction') {
cb(req, res, next).catch(errFunc);
} else {
try {
cb(req, res, next);
} catch (err) {
errFunc(err);
}
}
} else {
// no route, return 404
const res = Response();
res.status(404).json({
status: 'failed',
message: 'ERROR: 404 not found'
});
}
};
const app = {};
// attach http methods
METHODS_NAMES.forEach((method) => {
app[method.toLowerCase()] = (pattern, callback) => {
// add handler
handlers.push({ method, pattern, callback });
};
});
// chainable route
app.route = (pattern) => {
const chainApp = {};
METHODS_NAMES.forEach((method) => {
chainApp[method.toLowerCase()] = (callback) => {
// add handler
handlers.push({ method, pattern, callback });
return chainApp;
};
});
return chainApp;
};
// start service
app.start = () => {
dispatch($request);
};
return app;
/************************************************
Utility Functions
*************************************************/
function rawBodySaver(req, res, buf, encoding) {
if (buf && buf.length) {
req.rawBody = buf.toString(encoding || 'utf8');
}
}
function Response() {
let statusCode = 200;
const { isQX, isLoon, isSurge } = ENV();
const headers = DEFAULT_HEADERS;
const STATUS_CODE_MAP = {
200: 'HTTP/1.1 200 OK',
201: 'HTTP/1.1 201 Created',
302: 'HTTP/1.1 302 Found',
307: 'HTTP/1.1 307 Temporary Redirect',
308: 'HTTP/1.1 308 Permanent Redirect',
404: 'HTTP/1.1 404 Not Found',
500: 'HTTP/1.1 500 Internal Server Error'
};
return new class {
status(code) {
statusCode = code;
return this;
}
send(body = '') {
const response = {
status: isQX ? STATUS_CODE_MAP[statusCode] : statusCode,
body,
headers
};
if (isQX) {
$done(response);
} else if (isLoon || isSurge) {
$done({
response
});
}
}
end() {
this.send();
}
html(data) {
this.set('Content-Type', 'text/html;charset=UTF-8');
this.send(data);
}
json(data) {
this.set('Content-Type', 'application/json;charset=UTF-8');
this.send(JSON.stringify(data));
}
set(key, val) {
headers[key] = val;
return this;
}
}();
}
function patternMatched(pattern, path) {
if (pattern instanceof RegExp && pattern.test(path)) {
return true;
} else {
// root pattern, match all
if (pattern === '/') return true;
// normal string pattern
if (pattern.indexOf(':') === -1) {
const spath = path.split('/');
const spattern = pattern.split('/');
for (let i = 0; i < spattern.length; i++) {
if (spath[i] !== spattern[i]) {
return false;
}
}
return true;
} else if (extractPathParams(pattern, path)) {
// string pattern with path parameters
return true;
}
}
return false;
}
function extractURL(url) {
// extract path
const match = url.match(/https?:\/\/[^\/]+(\/[^?]*)/) || [];
const path = match[1] || '/';
// extract query string
const split = url.indexOf('?');
const query = {};
if (split !== -1) {
let hashes = url.slice(url.indexOf('?') + 1).split('&');
for (let i = 0; i < hashes.length; i++) {
hash = hashes[i].split('=');
query[hash[0]] = hash[1];
}
}
return {
path,
query
};
}
function extractPathParams(pattern, path) {
if (pattern.indexOf(':') === -1) {
return null;
} else {
const params = {};
for (let i = 0, j = 0; i < pattern.length; i++, j++) {
if (pattern[i] === ':') {
let key = [];
let val = [];
while (pattern[++i] !== '/' && i < pattern.length) {
key.push(pattern[i]);
}
while (path[j] !== '/' && j < path.length) {
val.push(path[j++]);
}
params[key.join('')] = val.join('');
} else {
if (pattern[i] !== path[j]) {
return null;
}
}
}
return params;
}
}
}
module.exports = express;

209
backend/src/utils/geo.js Normal file
View File

@ -0,0 +1,209 @@
const { HTTP } = require('./open-api');
// get proxy flag according to its name
function getFlag(name) {
// flags from @KOP-XIAO: https://github.com/KOP-XIAO/QuantumultX/blob/master/Scripts/resource-parser.js
const flags = {
'🇦🇿': [ '阿塞拜疆' ],
'🇦🇹': [ '奥地利', '奧地利', 'Austria', '维也纳' ],
'🇦🇺': [ 'AU', 'Australia', 'Sydney', '澳大利亚', '澳洲', '墨尔本', '悉尼', '土澳', '京澳', '廣澳', '滬澳', '沪澳', '广澳' ],
'🇧🇪': [ 'BE', '比利時', '比利时' ],
'🇧🇬': [ '保加利亚', '保加利亞', 'Bulgaria' ],
'🇧🇭': [ 'BH', '巴林' ],
'🇧🇩': [ 'BD', '孟加拉' ],
'🇵🇰': [ '巴基斯坦' ],
'🇰🇭': [ '柬埔寨' ],
'🇺🇦': [ '烏克蘭', '乌克兰' ],
'🇭🇷': [ '克罗地亚', 'HR', '克羅地亞' ],
'🇨🇦': [ 'Canada', 'CANADA', 'CAN', 'Waterloo', '加拿大', '蒙特利尔', '温哥华', '楓葉', '枫叶', '滑铁卢', '多伦多', 'CA' ],
'🇨🇭': [ '瑞士', '苏黎世', 'Switzerland', 'Zurich' ],
'🇳🇬': [ '尼日利亚', 'NG', '尼日利亞' ],
'🇨🇿': [ 'Czechia', '捷克' ],
'🇸🇰': [ '斯洛伐克', 'SK' ],
'🇷🇸': [ 'RS', '塞尔维亚' ],
'🇲🇩': [ '摩爾多瓦', 'MD', '摩尔多瓦' ],
'🇩🇪': [ 'DE', 'German', 'GERMAN', '德国', '德國', '法兰克福', '京德', '滬德', '廣德', '沪德', '广德', 'Frankfurt' ],
'🇩🇰': [ 'DK', 'DNK', '丹麦', '丹麥' ],
'🇪🇸': [ 'ES', '西班牙', 'Spain' ],
'🇪🇺': [ 'EU', '欧盟', '欧罗巴' ],
'🇫🇮': [ 'Finland', '芬兰', '芬蘭', '赫尔辛基' ],
'🇫🇷': [ 'FR', 'France', '法国', '法國', '巴黎' ],
'🇬🇧': [ 'UK', 'GB', 'England', 'United Kingdom', '英国', '伦敦', '英', 'London' ],
'🇲🇴': [ 'MO', 'Macao', '澳门', '澳門', 'CTM' ],
'🇰🇿': [ '哈萨克斯坦', '哈萨克' ],
'🇭🇺': [ '匈牙利', 'Hungary' ],
'🇭🇰': [
'HK',
'Hongkong',
'Hong Kong',
'HongKong',
'HONG KONG',
'香港',
'深港',
'沪港',
'呼港',
'HKT',
'HKBN',
'HGC',
'WTT',
'CMI',
'穗港',
'京港',
'港'
],
'🇮🇩': [ 'Indonesia', '印尼', '印度尼西亚', '雅加达' ],
'🇮🇪': [ 'Ireland', 'IRELAND', '爱尔兰', '愛爾蘭', '都柏林' ],
'🇮🇱': [ 'Israel', '以色列' ],
'🇮🇳': [ 'India', 'IND', 'INDIA', '印度', '孟买', 'MFumbai' ],
'🇮🇸': [ 'IS', 'ISL', '冰岛', '冰島' ],
'🇰🇵': [ 'KP', '朝鲜' ],
'🇰🇷': [ 'KR', 'Korea', 'KOR', '韩国', '首尔', '韩', '韓', '春川', 'Chuncheon', 'Seoul' ],
'🇱🇺': [ '卢森堡' ],
'🇱🇻': [ 'Latvia', 'Latvija', '拉脱维亚' ],
'🇲🇽': [ 'MEX', 'MX', '墨西哥' ],
'🇲🇾': [ 'MY', 'Malaysia', 'MALAYSIA', '马来西亚', '大馬', '馬來西亞', '吉隆坡' ],
'🇳🇱': [ 'NL', 'Netherlands', '荷兰', '荷蘭', '尼德蘭', '阿姆斯特丹' ],
'🇳🇵': [ '尼泊尔' ],
'🇵🇭': [ 'PH', 'Philippines', '菲律宾', '菲律賓' ],
'🇵🇷': [ 'PR', '波多黎各' ],
'🇷🇴': [ 'RO', '罗马尼亚' ],
'🇷🇺': [
'RU',
'Russia',
'俄罗斯',
'俄国',
'俄羅斯',
'伯力',
'莫斯科',
'圣彼得堡',
'西伯利亚',
'新西伯利亚',
'京俄',
'杭俄',
'廣俄',
'滬俄',
'广俄',
'沪俄',
'Moscow'
],
'🇸🇦': [ '沙特' ],
'🇸🇪': [ 'SE', 'Sweden', '瑞典' ],
'🇲🇹': [ '马耳他' ],
'🇲🇦': [ 'MA', '摩洛哥' ],
'🇸🇬': [ 'SG', 'Singapore', 'SINGAPORE', '新加坡', '狮城', '沪新', '京新', '泉新', '穗新', '深新', '杭新', '广新', '廣新', '滬新' ],
'🇹🇭': [ 'TH', 'Thailand', '泰国', '泰國', '曼谷' ],
'🇹🇷': [ 'TR', 'Turkey', '土耳其', '伊斯坦布尔' ],
'🇹🇼': [ 'TW', 'Taiwan', 'TAIWAN', '台湾', '台北', '台中', '新北', '彰化', 'CHT', '台', 'HINET', 'Taipei' ],
'🇺🇸': [
'US',
'USA',
'America',
'United States',
'美国',
'美',
'京美',
'波特兰',
'达拉斯',
'俄勒冈',
'凤凰城',
'费利蒙',
'硅谷',
'矽谷',
'拉斯维加斯',
'洛杉矶',
'圣何塞',
'圣克拉拉',
'西雅图',
'芝加哥',
'沪美',
'哥伦布',
'纽约',
'Los Angeles',
'San Jose',
'Sillicon Valley',
'Michigan'
],
'🇻🇳': [ 'VN', '越南', '胡志明市' ],
'🇻🇪': [ 'VE', '委内瑞拉' ],
'🇮🇹': [ 'Italy', 'IT', 'Nachash', '意大利', '米兰', '義大利' ],
'🇿🇦': [ 'South Africa', '南非' ],
'🇦🇪': [ 'United Arab Emirates', '阿联酋', '迪拜', 'AE' ],
'🇧🇷': [ 'BR', 'Brazil', '巴西', '圣保罗' ],
'🇯🇵': [
'JP',
'Japan',
'JAPAN',
'日本',
'东京',
'大阪',
'埼玉',
'沪日',
'穗日',
'川日',
'中日',
'泉日',
'杭日',
'深日',
'辽日',
'广日',
'大坂',
'Osaka',
'Tokyo'
],
'🇦🇷': [ 'AR', '阿根廷' ],
'🇳🇴': [ 'Norway', '挪威', 'NO' ],
'🇨🇳': [ 'CN', 'China', '回国', '中国', '中國', '江苏', '北京', '上海', '广州', '深圳', '杭州', '徐州', '青岛', '宁波', '镇江', 'back' ],
'🇵🇱': [ 'PL', 'POL', '波兰', '波蘭' ],
'🇨🇱': [ '智利' ],
'🇳🇿': [ '新西蘭', '新西兰' ],
'🇬🇷': [ '希腊', '希臘' ],
'🇪🇬': [ '埃及' ],
'🇨🇾': [ 'CY', '塞浦路斯' ],
'🇨🇷': [ 'CR', '哥斯达黎加' ],
'🇸🇮': [ 'SI', '斯洛文尼亚' ],
'🇱🇹': [ 'LT', '立陶宛' ],
'🇵🇦': [ 'PA', '巴拿马' ],
'🇹🇳': [ 'TN', '突尼斯' ],
'🇮🇲': [ '马恩岛', '馬恩島' ],
'🇧🇾': [ 'BY', '白俄', '白俄罗斯' ],
'🇵🇹': [ '葡萄牙' ],
'🇰🇪': [ 'KE', '肯尼亚' ],
'🇰🇬': [ 'KG', '吉尔吉斯坦' ],
'🇯🇴': [ 'JO', '约旦' ],
'🇺🇾': [ 'UY', '乌拉圭' ],
'🇲🇳': [ '蒙古' ],
'🇮🇷': [ 'IR', '伊朗' ],
'🇵🇪': [ '秘鲁', '祕魯' ],
'🇨🇴': [ '哥伦比亚' ],
'🇪🇪': [ '爱沙尼亚' ],
'🇪🇨': [ 'EC', '厄瓜多尔' ],
'🇲🇰': [ '马其顿', '馬其頓' ],
'🇧🇦': [ '波黑共和国', '波黑' ],
'🇬🇪': [ '格魯吉亞', '格鲁吉亚' ],
'🇦🇱': [ '阿爾巴尼亞', '阿尔巴尼亚' ],
'🏳️‍🌈': [ '流量', '时间', '应急', '过期', 'Bandwidth', 'expire' ]
};
for (let k of Object.keys(flags)) {
if (flags[k].some((item) => name.indexOf(item) !== -1)) {
return k;
}
}
// no flag found
const oldFlag = (name.match(/[\uD83C][\uDDE6-\uDDFF][\uD83C][\uDDE6-\uDDFF]/) || [])[0];
return oldFlag || '🏴‍☠️';
}
// util API
async function IP_API(req, res) {
const server = decodeURIComponent(req.params.server);
const $http = HTTP();
const result = await $http
.get(`http://ip-api.com/json/${server}?lang=zh-CN`)
.then((resp) => JSON.parse(resp.body));
res.json(result);
}
module.exports = {
getFlag,
IP_API
};

75
backend/src/utils/gist.js Normal file
View File

@ -0,0 +1,75 @@
const { HTTP } = require('./open-api');
/**
* Gist backup
*/
function Gist({ token, key }) {
const http = HTTP({
baseURL: 'https://api.github.com',
headers: {
Authorization: `token ${token}`,
'User-Agent':
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.141 Safari/537.36'
},
events: {
onResponse: (resp) => {
if (/^[45]/.test(String(resp.statusCode))) {
return Promise.reject(`ERROR: ${JSON.parse(resp.body).message}`);
} else {
return resp;
}
}
}
});
async function locate() {
return http.get('/gists').then((response) => {
const gists = JSON.parse(response.body);
for (let g of gists) {
if (g.description === key) {
return g.id;
}
}
return -1;
});
}
this.upload = async function(files) {
const id = await locate();
if (id === -1) {
// create a new gist for backup
return http.post({
url: '/gists',
body: JSON.stringify({
description: key,
public: false,
files
})
});
} else {
// update an existing gist
return http.patch({
url: `/gists/${id}`,
body: JSON.stringify({ files })
});
}
};
this.download = async function(filename) {
const id = await locate();
if (id === -1) {
return Promise.reject('未找到Gist备份');
} else {
try {
const { files } = await http.get(`/gists/${id}`).then((resp) => JSON.parse(resp.body));
const url = files[filename].raw_url;
return await http.get(url).then((resp) => resp.body);
} catch (err) {
return Promise.reject(err);
}
}
};
}
module.exports = Gist;

View File

@ -0,0 +1,22 @@
function AND(...args) {
return args.reduce((a, b) => a.map((c, i) => b[i] && c));
}
function OR(...args) {
return args.reduce((a, b) => a.map((c, i) => b[i] || c));
}
function NOT(array) {
return array.map((c) => !c);
}
function FULL(length, bool) {
return [...Array(length).keys()].map(() => bool);
}
module.exports = {
AND,
OR,
NOT,
FULL
}

View File

@ -0,0 +1,271 @@
function ENV() {
const isQX = typeof $task !== 'undefined';
const isLoon = typeof $loon !== 'undefined';
const isSurge = typeof $httpClient !== 'undefined' && !isLoon;
const isNode = eval(`typeof process !== "undefined"`);
return { isQX, isLoon, isSurge, isNode };
}
function HTTP(defaultOptions = { baseURL: '' }) {
const { isQX, isLoon, isSurge, isNode } = ENV();
const methods = [ 'GET', 'POST', 'PUT', 'DELETE', 'HEAD', 'OPTIONS', 'PATCH' ];
const URL_REGEX = /https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)/;
function send(method, options) {
options = typeof options === 'string' ? { url: options } : options;
const baseURL = defaultOptions.baseURL;
if (baseURL && !URL_REGEX.test(options.url || '')) {
options.url = baseURL ? baseURL + options.url : options.url;
}
options = { ...defaultOptions, ...options };
const timeout = options.timeout;
const events = {
...{
onRequest: () => {},
onResponse: (resp) => resp,
onTimeout: () => {}
},
...options.events
};
events.onRequest(method, options);
let worker;
if (isQX) {
worker = $task.fetch({
method,
url: options.url,
headers: options.headers,
body: options.body
});
} else if (isLoon || isSurge || isNode) {
worker = new Promise((resolve, reject) => {
const request = isNode ? eval("require('request')") : $httpClient;
request[method.toLowerCase()](options, (err, response, body) => {
if (err) reject(err);
else
resolve({
statusCode: response.status || response.statusCode,
headers: response.headers,
body
});
});
});
}
let timeoutid;
const timer = timeout
? new Promise((_, reject) => {
timeoutid = setTimeout(() => {
events.onTimeout();
return reject(`${method} URL: ${options.url} exceeds the timeout ${timeout} ms`);
}, timeout);
})
: null;
return (timer
? Promise.race([ timer, worker ]).then((res) => {
clearTimeout(timeoutid);
return res;
})
: worker).then((resp) => events.onResponse(resp));
}
const http = {};
methods.forEach((method) => (http[method.toLowerCase()] = (options) => send(method, options)));
return http;
}
function API(name = 'untitled', debug = false) {
const { isQX, isLoon, isSurge, isNode } = ENV();
return new class {
constructor(name, debug) {
this.name = name;
this.debug = debug;
this.http = HTTP();
this.env = ENV();
this.node = (() => {
if (isNode) {
const fs = eval("require('fs')");
return {
fs
};
} else {
return null;
}
})();
this.initCache();
const delay = (t, v) =>
new Promise(function(resolve) {
setTimeout(resolve.bind(null, v), t);
});
Promise.prototype.delay = function(t) {
return this.then(function(v) {
return delay(t, v);
});
};
}
// persistence
// initialize cache
initCache() {
if (isQX) this.cache = JSON.parse($prefs.valueForKey(this.name) || '{}');
if (isLoon || isSurge) this.cache = JSON.parse($persistentStore.read(this.name) || '{}');
if (isNode) {
// create a json for root cache
let fpath = 'root.json';
if (!this.node.fs.existsSync(fpath)) {
this.node.fs.writeFileSync(fpath, JSON.stringify({}), { flag: 'wx' }, (err) => console.log(err));
}
this.root = {};
// create a json file with the given name if not exists
fpath = `${this.name}.json`;
if (!this.node.fs.existsSync(fpath)) {
this.node.fs.writeFileSync(fpath, JSON.stringify({}), { flag: 'wx' }, (err) => console.log(err));
this.cache = {};
} else {
this.cache = JSON.parse(this.node.fs.readFileSync(`${this.name}.json`));
}
}
}
// store cache
persistCache() {
const data = JSON.stringify(this.cache, null, 2);
if (isQX) $prefs.setValueForKey(data, this.name);
if (isLoon || isSurge) $persistentStore.write(data, this.name);
if (isNode) {
this.node.fs.writeFileSync(`${this.name}.json`, data, { flag: 'w' }, (err) => console.log(err));
this.node.fs.writeFileSync('root.json', JSON.stringify(this.root, null, 2), { flag: 'w' }, (err) =>
console.log(err)
);
}
}
write(data, key) {
this.log(`SET ${key}`);
if (key.indexOf('#') !== -1) {
key = key.substr(1);
if (isSurge || isLoon) {
return $persistentStore.write(data, key);
}
if (isQX) {
return $prefs.setValueForKey(data, key);
}
if (isNode) {
this.root[key] = data;
}
} else {
this.cache[key] = data;
}
this.persistCache();
}
read(key) {
this.log(`READ ${key}`);
if (key.indexOf('#') !== -1) {
key = key.substr(1);
if (isSurge || isLoon) {
return $persistentStore.read(key);
}
if (isQX) {
return $prefs.valueForKey(key);
}
if (isNode) {
return this.root[key];
}
} else {
return this.cache[key];
}
}
delete(key) {
this.log(`DELETE ${key}`);
if (key.indexOf('#') !== -1) {
key = key.substr(1);
if (isSurge || isLoon) {
return $persistentStore.write(null, key);
}
if (isQX) {
return $prefs.removeValueForKey(key);
}
if (isNode) {
delete this.root[key];
}
} else {
delete this.cache[key];
}
this.persistCache();
}
// notification
notify(title, subtitle = '', content = '', options = {}) {
const openURL = options['open-url'];
const mediaURL = options['media-url'];
if (isQX) $notify(title, subtitle, content, options);
if (isSurge) {
$notification.post(title, subtitle, content + `${mediaURL ? '\n多媒体:' + mediaURL : ''}`, {
url: openURL
});
}
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) {
const content_ =
content + (openURL ? `\n点击跳转: ${openURL}` : '') + (mediaURL ? `\n多媒体: ${mediaURL}` : '');
console.log(`${title}\n${subtitle}\n${content_}\n\n`);
}
}
// other helper functions
log(msg) {
if (this.debug) console.log(`[${this.name}] LOG: ${msg}`);
}
info(msg) {
console.log(`[${this.name}] INFO: ${msg}`);
}
error(msg) {
console.log(`[${this.name}] ERROR: ${msg}`);
}
wait(millisec) {
return new Promise((resolve) => setTimeout(resolve, millisec));
}
done(value = {}) {
if (isQX || isLoon || isSurge) {
$done(value);
} else if (isNode) {
if (typeof $context !== 'undefined') {
$context.headers = value.headers;
$context.statusCode = value.statusCode;
$context.body = value.body;
}
}
}
}(name, debug);
}
module.exports = {
HTTP,
ENV,
API
};

File diff suppressed because it is too large Load Diff

File diff suppressed because one or more lines are too long