This commit is contained in:
蒋小陌 2024-08-30 10:29:02 +08:00
parent d5798ce19b
commit 4f4d93a8d6

View File

@ -11,6 +11,7 @@ const cache = {};
const PORT = process.env.PORT || 9001;
const server = http.createServer((req, res) => {
if (req.url === '/favicon.ico') {
res.writeHead(204);
res.end();
@ -21,6 +22,13 @@ const server = http.createServer((req, res) => {
const path = parsedUrl.pathname;
const sign = parsedUrl.query.sign || '';
if (sign == '' || path == '/') {
res.writeHead(400, { 'Content-Type': 'text/plain' });
res.end('Bad Request: Missing sign or path');
return;
}
// Check if the data is in cache and not expired
const cacheEntry = cache[path];
if (cacheEntry && cacheEntry.expiration > Date.now()) {
@ -58,7 +66,6 @@ const server = http.createServer((req, res) => {
const apiData = JSON.parse(data);
if (apiData.code === 200 && apiData.data && apiData.data.url) {
const { url: realUrl, cloudtype, expiration } = apiData.data;
// Cache the response if expiration is greater than 0
if (expiration > 0) {
cache[path] = {
@ -67,7 +74,6 @@ const server = http.createServer((req, res) => {
expiration: Date.now() + expiration * 1000
};
}
fetchAndServe(realUrl, cloudtype, res);
} else {
res.writeHead(502, { 'Content-Type': 'text/plain' });