111
This commit is contained in:
parent
cf106d1228
commit
1204584a77
40
source.js
40
source.js
@ -25,6 +25,8 @@ const viewsInfo = {
|
|||||||
cacheReadError: 0,
|
cacheReadError: 0,
|
||||||
// API 调用错误次数
|
// API 调用错误次数
|
||||||
fetchApiError: 0,
|
fetchApiError: 0,
|
||||||
|
// API 调用错误次数
|
||||||
|
fetchApiWarning: 0,
|
||||||
};
|
};
|
||||||
|
|
||||||
// 默认端口号和 API 地址
|
// 默认端口号和 API 地址
|
||||||
@ -168,6 +170,16 @@ const server = http.createServer(async (req, res) => {
|
|||||||
// 增加 API 调用次数
|
// 增加 API 调用次数
|
||||||
viewsInfo.apiCall++;
|
viewsInfo.apiCall++;
|
||||||
const apiData = await fetchApiData(reqPath, token, sign);
|
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) {
|
if (apiData.code === 200 && apiData.data && apiData.data.url) {
|
||||||
const { url: realUrl, cloudtype, expiration, path, headers, uniqid } = apiData.data;
|
const { url: realUrl, cloudtype, expiration, path, headers, uniqid } = apiData.data;
|
||||||
const data = { realUrl, cloudtype, expiration: expiration * 1000, path, headers, uniqid };
|
const data = { realUrl, cloudtype, expiration: expiration * 1000, path, headers, uniqid };
|
||||||
@ -189,25 +201,31 @@ const server = http.createServer(async (req, res) => {
|
|||||||
} else {
|
} 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 解码
|
// utf8 解码
|
||||||
res.writeHead(502, { 'Content-Type': 'text/plain;charset=UTF-8;' });
|
|
||||||
res.end(apiData.message || 'Bad Gateway');
|
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
// 记录响应错误
|
// 记录响应错误
|
||||||
viewsInfo.fetchApiError++;
|
viewsInfo.fetchApiError++;
|
||||||
|
|
||||||
// 如果有缓存有忽略过期,直接调用
|
// 起用备用 API
|
||||||
if (pathIndex[uniqidhex] && fs.existsSync(cacheMetaFile) && fs.existsSync(cacheContentFile)) {
|
// apiEndpoint = 'https://x-mo.cn:9001/get/';
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
// utf8 解码
|
||||||
|
res.writeHead(502, { 'Content-Type': 'text/plain;charset=UTF-8;' });
|
||||||
|
res.end('Bad Gateway: Failed to decode JSON' + error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user