This commit is contained in:
蒋小陌 2024-11-03 00:12:15 +08:00
parent 3a38632505
commit 7b1971da4e
2 changed files with 15 additions and 18 deletions

File diff suppressed because one or more lines are too long

View File

@ -6,7 +6,7 @@ const fs = require('fs');
const pathModule = require('path'); const pathModule = require('path');
const crypto = require('crypto'); const crypto = require('crypto');
const requestTimeout = 10000; // 10 seconds const requestTimeout = 20000; // 10 seconds
const cacheDir = pathModule.join(__dirname, '.cache'); const cacheDir = pathModule.join(__dirname, '.cache');
const args = process.argv.slice(2); const args = process.argv.slice(2);
const pathIndex = {}; const pathIndex = {};
@ -62,11 +62,11 @@ const server = http.createServer(async (req, res) => {
req.url = req.url.replace(/\/{2,}/g, '/'); req.url = req.url.replace(/\/{2,}/g, '/');
const parsedUrl = url.parse(req.url, true); const parsedUrl = url.parse(req.url, true);
// 获取第一个路径 // 获取第一个路径
let reqPath = parsedUrl.pathname.split('/')[1]; let reqPath = parsedUrl.pathname.split('/')[1];
// 获取第二路径为 token
let token = parsedUrl.pathname.split('/')[2]; // 取第一个路径以外的路径
let token = parsedUrl.pathname.split('/').slice(2).join('/');
// 处理根路径请求 // 处理根路径请求
if (reqPath === 'favicon.ico') { if (reqPath === 'favicon.ico') {
@ -97,15 +97,15 @@ const server = http.createServer(async (req, res) => {
reqPath = 'go'; reqPath = 'go';
} }
// 检查第一个路径只能是 attachment,avatar,endpoint,go,bbs,www // 检查第一个路径只能是 avatar,endpoint,go,bbs,www
if (!['attachment', 'avatar', 'go', 'bbs', 'www', 'url'].includes(reqPath)) { if (!['avatar', 'go', 'bbs', 'www', 'url', 'thumb'].includes(reqPath)) {
res.writeHead(404, { 'Content-Type': 'text/plain' }); res.writeHead(404, { 'Content-Type': 'text/plain;charset=UTF-8' });
res.end('Not Found'); res.end('Not Found');
return; return;
} }
if (!token || reqPath === '') { if (!token || reqPath === '') {
res.writeHead(400, { 'Content-Type': 'text/plain' }); res.writeHead(400, { 'Content-Type': 'text/plain;charset=UTF-8' });
res.end('Bad Request: Missing Token or path (' + reqPath + ')'); res.end('Bad Request: Missing Token or path (' + reqPath + ')');
return; return;
} }
@ -141,7 +141,6 @@ const server = http.createServer(async (req, res) => {
// 增加 API 调用次数 // 增加 API 调用次数
viewsInfo.apiCall++; viewsInfo.apiCall++;
const apiData = await fetchApiData(reqPath, token); const apiData = await fetchApiData(reqPath, token);
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;
@ -163,11 +162,12 @@ const server = http.createServer(async (req, res) => {
fetchAndServe(data, tempCacheContentFile, cacheContentFile, cacheMetaFile, res); fetchAndServe(data, tempCacheContentFile, cacheContentFile, cacheMetaFile, res);
} }
} else { } else {
res.writeHead(502, { 'Content-Type': 'text/plain' }); // utf8 解码
res.writeHead(502, { 'Content-Type': 'text/plain;charset=UTF-8;' });
res.end(apiData.message || 'Bad Gateway'); res.end(apiData.message || 'Bad Gateway');
} }
} catch (error) { } catch (error) {
res.writeHead(502, { 'Content-Type': 'text/plain' }); res.writeHead(502, { 'Content-Type': 'text/plain;charset=UTF-8' });
res.end('Bad Gateway: Failed to decode JSON ' + error); res.end('Bad Gateway: Failed to decode JSON ' + error);
} }
} }
@ -224,7 +224,7 @@ const fetchApiData = (reqPath, token) => {
try { try {
resolve(JSON.parse(data)); resolve(JSON.parse(data));
} catch (error) { } catch (error) {
reject(error); reject(error + data);
} }
}); });
}); });
@ -307,10 +307,7 @@ const serveFromCache = (cacheData, cacheContentFile, cacheMetaFile, res) => {
} }
} }
readStream.on('open', () => { readStream.on('open', () => {
const defaultHeaders = { const defaultHeaders = {
'Cloud-Type': cacheData.cloudtype, 'Cloud-Type': cacheData.cloudtype,
'Cloud-Expiration': cacheData.expiration, 'Cloud-Expiration': cacheData.expiration,
@ -336,7 +333,7 @@ const serveFromCache = (cacheData, cacheContentFile, cacheMetaFile, res) => {
// 处理响应错误 // 处理响应错误
const handleResponseError = (res, tempCacheContentFile, realUrl) => { const handleResponseError = (res, tempCacheContentFile, realUrl) => {
if (!res.headersSent) { if (!res.headersSent) {
res.writeHead(502, { 'Content-Type': 'text/plain' }); res.writeHead(502, { 'Content-Type': 'text/plain;charset=UTF-8' });
res.end(`Bad Gateway: ${realUrl}`); res.end(`Bad Gateway: ${realUrl}`);
} }
if (fs.existsSync(tempCacheContentFile)) { if (fs.existsSync(tempCacheContentFile)) {
@ -347,7 +344,7 @@ const handleResponseError = (res, tempCacheContentFile, realUrl) => {
// 处理缓存读取错误 // 处理缓存读取错误
const handleCacheReadError = (res) => { const handleCacheReadError = (res) => {
if (!res.headersSent) { if (!res.headersSent) {
res.writeHead(500, { 'Content-Type': 'text/plain' }); res.writeHead(500, { 'Content-Type': 'text/plain;charset=UTF-8' });
res.end('Internal Server Error: Unable to read cache content file'); res.end('Internal Server Error: Unable to read cache content file');
} }
}; };