This commit is contained in:
蒋小陌 2024-11-07 13:14:41 +08:00
parent cf106d1228
commit 1204584a77
2 changed files with 30 additions and 12 deletions

File diff suppressed because one or more lines are too long

View File

@ -25,6 +25,8 @@ const viewsInfo = {
cacheReadError: 0,
// API 调用错误次数
fetchApiError: 0,
// API 调用错误次数
fetchApiWarning: 0,
};
// 默认端口号和 API 地址
@ -168,6 +170,16 @@ const server = http.createServer(async (req, res) => {
// 增加 API 调用次数
viewsInfo.apiCall++;
const apiData = await fetchApiData(reqPath, token, sign);
// 302 或 301 重定向
if (apiData.code === 302 || apiData.code === 301) {
res.writeHead(302, { Location: apiData.data.url });
res.end();
return;
}
if (apiData.code === 200 && apiData.data && apiData.data.url) {
const { url: realUrl, cloudtype, expiration, path, headers, uniqid } = apiData.data;
const data = { realUrl, cloudtype, expiration: expiration * 1000, path, headers, uniqid };
@ -189,25 +201,31 @@ const server = http.createServer(async (req, res) => {
} else {
// 记录响应错误
viewsInfo.fetchApiError++;
viewsInfo.fetchApiWarning++;
// 如果有缓存有忽略过期,直接调用
if (pathIndex[uniqidhex] && fs.existsSync(cacheMetaFile) && fs.existsSync(cacheContentFile)) {
serveFromCache(JSON.parse(fs.readFileSync(cacheMetaFile, 'utf8')), cacheContentFile, cacheMetaFile, res);
} else {
// utf8 解码
res.writeHead(502, { 'Content-Type': 'text/plain;charset=UTF-8;' });
res.end(apiData.message || 'Bad Gateway');
}
// utf8 解码
res.writeHead(502, { 'Content-Type': 'text/plain;charset=UTF-8;' });
res.end(apiData.message || 'Bad Gateway');
}
} catch (error) {
// 记录响应错误
viewsInfo.fetchApiError++;
// 如果有缓存有忽略过期,直接调用
if (pathIndex[uniqidhex] && fs.existsSync(cacheMetaFile) && fs.existsSync(cacheContentFile)) {
serveFromCache(JSON.parse(fs.readFileSync(cacheMetaFile, 'utf8')), cacheContentFile, cacheMetaFile, res);
} else {
// utf8 解码
res.writeHead(502, { 'Content-Type': 'text/plain;charset=UTF-8;' });
res.end('Bad Gateway: Failed to decode JSON' + error);
}
// 起用备用 API
// apiEndpoint = 'https://x-mo.cn:9001/get/';
// utf8 解码
res.writeHead(502, { 'Content-Type': 'text/plain;charset=UTF-8;' });
res.end('Bad Gateway: Failed to decode JSON' + error);
}
}
});