refactor: 优化超时设置并调整缓存处理逻辑
移除冗余的 `requestTimeout` 变量,将 `fetchApiData` 的超时时间调整为 5 秒,并在 `fetchAndServe` 中取消超时限制。同时,增加对 `content-length` 的校验,当响应内容小于 2KB 且与缓存文件大小不一致时,返回 502 错误。
This commit is contained in:
parent
305837ea65
commit
f7dbca480b
@ -1,5 +1,6 @@
|
||||
{
|
||||
"devDependencies": {
|
||||
"javascript-obfuscator": "^4.1.1"
|
||||
}
|
||||
},
|
||||
"packageManager": "pnpm@9.15.1+sha512.1acb565e6193efbebda772702950469150cf12bcc764262e7587e71d19dc98a423dff9536e57ea44c49bdf790ff694e83c27be5faa23d67e0c033b583be4bfcf"
|
||||
}
|
||||
|
24
source.js
24
source.js
@ -6,7 +6,6 @@ const fs = require('fs');
|
||||
const pathModule = require('path');
|
||||
const crypto = require('crypto');
|
||||
|
||||
const requestTimeout = 20000; // 10 seconds
|
||||
const cacheDir = pathModule.join(__dirname, '.cache');
|
||||
const args = process.argv.slice(2);
|
||||
const pathIndex = {};
|
||||
@ -220,9 +219,7 @@ const checkCacheHeaders = async (req, cacheMetaFile) => {
|
||||
const cacheData = JSON.parse(fs.readFileSync(cacheMetaFile, 'utf8'));
|
||||
const ifNoneMatch = req.headers['if-none-match'];
|
||||
const ifModifiedSince = req.headers['if-modified-since'];
|
||||
|
||||
let isNotModified = false;
|
||||
|
||||
if (ifNoneMatch && ifNoneMatch === cacheData.uniqid) {
|
||||
isNotModified = true;
|
||||
} else if (ifModifiedSince) {
|
||||
@ -262,8 +259,10 @@ const fetchApiData = (reqPath, token, sign) => {
|
||||
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.114 Safari/537.36',
|
||||
'token': token
|
||||
},
|
||||
timeout: requestTimeout,
|
||||
rejectUnauthorized: false
|
||||
// 超时设置, 5 秒
|
||||
timeout: 5000,
|
||||
rejectUnauthorized: false,
|
||||
// 超时设置
|
||||
}, (apiRes) => {
|
||||
let data = '';
|
||||
apiRes.on('data', chunk => data += chunk);
|
||||
@ -282,14 +281,25 @@ const fetchApiData = (reqPath, token, sign) => {
|
||||
|
||||
// 从真实 URL 获取数据并写入缓存
|
||||
const fetchAndServe = (data, tempCacheContentFile, cacheContentFile, cacheMetaFile, res) => {
|
||||
https.get(data.realUrl, { timeout: requestTimeout * 10, rejectUnauthorized: false }, (realRes) => {
|
||||
|
||||
// 不限超时
|
||||
https.get(data.realUrl, { timeout: 0, rejectUnauthorized: false }, (realRes) => {
|
||||
const cacheStream = fs.createWriteStream(tempCacheContentFile, { flags: 'w' });
|
||||
|
||||
let isVideo = data.path && typeof data.path === 'string' && data.path.includes('.mp4');
|
||||
|
||||
// 确保 content-length 是有效的
|
||||
const contentLength = realRes.headers['content-length'];
|
||||
if (contentLength) {
|
||||
|
||||
// contentLength 小于 2KB 且与缓存文件大小不一致时,重新获取
|
||||
if (contentLength < 2048 && data.headers['content-length'] !== contentLength) {
|
||||
console.warn('Warning: content-length is different for the response from:', data.realUrl);
|
||||
// 返回错误
|
||||
res.writeHead(502, { 'Content-Type': 'text/plain;charset=UTF-8;' });
|
||||
res.end(`Bad Gateway: ${data.realUrl}`);
|
||||
return;
|
||||
}
|
||||
|
||||
data.headers['content-length'] = contentLength;
|
||||
// 更新 data 到缓存 cacheMetaFile
|
||||
fs.writeFileSync(cacheMetaFile, JSON.stringify(data));
|
||||
|
Loading…
x
Reference in New Issue
Block a user