fix: 优化订阅响应

This commit is contained in:
lyz05
2023-12-06 13:06:40 +08:00
parent ece1b49f4d
commit 3683f36ef6
3 changed files with 38 additions and 19 deletions

View File

@@ -8,6 +8,7 @@ const moment = require("moment");
const axios = require("axios");
const leancloud = require("../utils/leancloud");
// TODO 迁移到leancloud
function getuserinfo(headers) {
if (!headers)
@@ -30,7 +31,7 @@ function getuserinfo(headers) {
}
async function updateDatabase() {
const database = await oss.get("SUB/database.yaml");
const database = await oss.get("SUB/database.yaml",true);
try {
return yaml.load(database);
} catch (e) {
@@ -38,9 +39,12 @@ async function updateDatabase() {
}
}
// 缓存数据库
let database;
updateDatabase().then(ret => database = ret);
/* GET users listing. */
router.get("/", async function (req, res) {
const database = await updateDatabase();
leancloud.add("SubAccess", {
ip: req.ip,
ua: req.headers["user-agent"],
@@ -69,14 +73,19 @@ router.get("/", async function (req, res) {
res.status(404).send("Not Found 找不到这种订阅类型");
}
} else {
// 更新数据库
database = await updateDatabase();
// 缓存所有的协程
let promises = [];
const path = req.protocol + "://" + req.headers.host + req.originalUrl;
const tgproxys = database.telegram;
const ctypes = Object.keys(database.suburl);
let ret = {};
for (const key of ctypes) {
const headers = await oss.head("SUB/" + key);
ret[key] = getuserinfo(headers);
// ret[key] = getuserinfotxt(getuserinfo(headers))
for (const key of ctypes)
promises.push(oss.head("SUB/" + key,true));
const headers = await Promise.all(promises)
for (const i in ctypes) {
ret[ctypes[i]] = getuserinfo(headers[i]);
}
res.render("airportsub", {ret, path, tgproxys, expire: userinfo.expire});
}
@@ -92,7 +101,7 @@ router.get("/", async function (req, res) {
});
router.get("/cache", async function (req, res) {
const database = await updateDatabase();
database = await updateDatabase();
let messages = [];
// 缓存所有的协程
let promises = [];

View File

@@ -150,7 +150,6 @@ describe("App", () => {
.get("/sub/download")
.end((err,res) => {
res.should.have.status(200);
res.text.should.have.string("clash");
res.text.should.have.string("v2ray");
res.text.should.have.string("shadowsocks");
done();

View File

@@ -1,7 +1,6 @@
const OSS = require("ali-oss");
const path = require("path");
const normalendpoint = "oss-cn-hongkong.aliyuncs.com";
const fastendpoint = "oss-accelerate.aliyuncs.com";
const fastregion = "oss-accelerate";
// 引入环境变量
require("dotenv")
@@ -14,9 +13,16 @@ let client = new OSS({
bucket: process.env.OSS_BUCKET,
});
async function get(objname) {
let fastclient = new OSS({
region: fastregion,
accessKeyId: process.env.ALI_ACCESS_KEY,
accessKeySecret: process.env.ALI_ACCESS_KEY_SECRET,
bucket: process.env.OSS_BUCKET,
});
async function get(objname,isFast) {
try {
const result = await client.get(objname);
const result = isFast ? await fastclient.get(objname) : await client.get(objname);
return result.content.toString();
} catch (e) {
console.log(e);
@@ -41,9 +47,9 @@ async function put(objname, content, headers) {
}
}
async function head(objname) {
async function head(objname,isfast) {
try {
const result = await client.head(objname);
const result = isfast ? await fastclient.head(objname) : await client.head(objname);
return result.res.headers;
} catch (e) {
console.log(e);
@@ -52,11 +58,8 @@ async function head(objname) {
async function signurl(objname, isFast) {
try {
let result = await client.signatureUrl(objname);
if (isFast) {
result = result.replace("http://", "//")
.replace(normalendpoint, fastendpoint);
}
let result = isFast ? await fastclient.signatureUrl(objname):await client.signatureUrl(objname);
result = result.replace("http://", "//")
return result;
} catch (e) {
console.log(e);
@@ -84,6 +87,14 @@ module.exports = {
putACL
};
async function main() {
get("SUB/database.yaml",true);
put("SUB/test.txt", "中文");
head("SUB/database.yaml",true);
const url = await signurl("SUB/database.yaml", true);
console.log(url)
}
if (!module.parent) {
get("SUB/database.yaml");
put("SUB/test.txt", "中文");