alist-proxy/index.js

311 lines
10 KiB
JavaScript
Raw Normal View History

2024-08-30 09:36:55 +08:00
const http = require('http');
const https = require('https');
const url = require('url');
const querystring = require('querystring');
2024-09-27 15:32:37 +08:00
const fs = require('fs');
2024-10-12 17:11:47 +08:00
const pathModule = require('path');
2024-09-28 13:56:09 +08:00
const crypto = require('crypto');
2024-08-30 09:36:55 +08:00
const requestTimeout = 10000; // 10 seconds
2024-10-12 17:11:47 +08:00
const cacheDir = pathModule.join(__dirname, '.cache');
2024-08-31 17:51:00 +08:00
const args = process.argv.slice(2);
2024-10-12 17:11:47 +08:00
const pathIndex = {};
2024-08-31 17:51:00 +08:00
2024-10-14 17:13:29 +08:00
// 增加访问计数器
const viewsInfo = {
// 请求次数
request: 0,
// 缓存命中次数
cacheHit: 0,
// API调用次数
apiCall: 0,
2024-10-15 11:14:11 +08:00
// 缓存调用次数
cacheCall: 0,
2024-10-14 17:13:29 +08:00
};
// 默认端口号和 API 地址
2024-08-31 17:51:00 +08:00
let port = 9001;
let apiEndpoint = 'https://oss.x-php.com/alist/link';
// 解析命令行参数
args.forEach(arg => {
2024-10-13 16:34:07 +08:00
// 去掉--
if (arg.startsWith('--')) {
arg = arg.substring(2);
}
2024-08-31 17:51:00 +08:00
const [key, value] = arg.split('=');
if (key === 'port') {
port = parseInt(value, 10);
} else if (key === 'api') {
apiEndpoint = value;
}
});
2024-08-30 09:36:55 +08:00
2024-09-27 15:32:37 +08:00
// 确保缓存目录存在
if (!fs.existsSync(cacheDir)) {
fs.mkdirSync(cacheDir);
}
2024-10-13 15:09:25 +08:00
// 定时清理过期缓存数据
setInterval(() => {
2024-10-12 18:11:22 +08:00
const currentTime = Date.now();
for (const key in pathIndex) {
2024-10-13 15:09:25 +08:00
if (currentTime - pathIndex[key].timestamp > 24 * 60 * 60 * 1000) {
2024-10-12 18:11:22 +08:00
delete pathIndex[key];
}
}
2024-10-14 17:13:29 +08:00
}, 60 * 60 * 1000); // 每隔 1 小时执行一次
2024-10-12 18:11:22 +08:00
2024-10-13 15:09:25 +08:00
// 处理请求并返回数据
2024-08-31 17:51:00 +08:00
const server = http.createServer(async (req, res) => {
2024-10-13 15:23:45 +08:00
req.url = req.url.replace(/\/{2,}/g, '/');
2024-10-14 17:45:05 +08:00
const parsedUrl = url.parse(req.url, true);
const reqPath = parsedUrl.pathname;
const sign = parsedUrl.query.sign || '';
// 处理根路径请求
2024-10-13 15:23:45 +08:00
2024-10-14 17:45:05 +08:00
if (reqPath === '/favicon.ico') {
2024-08-30 09:36:55 +08:00
res.writeHead(204);
res.end();
return;
}
2024-10-13 15:09:25 +08:00
// 返回 endpoint, 缓存目录, 缓存数量, 用于监听服务是否正常运行
2024-10-14 17:45:05 +08:00
if (reqPath === '/endpoint') {
2024-10-13 15:09:25 +08:00
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({
code: 200,
data: {
api: apiEndpoint,
port: port,
cacheDir: cacheDir,
pathIndexCount: Object.keys(pathIndex).length,
2024-10-14 17:13:29 +08:00
viewsInfo: viewsInfo
2024-10-13 15:09:25 +08:00
}
}));
return;
}
2024-09-27 15:32:37 +08:00
if (!sign || reqPath === '/') {
2024-08-30 10:29:02 +08:00
res.writeHead(400, { 'Content-Type': 'text/plain' });
2024-10-14 17:45:05 +08:00
res.end('Bad Request: Missing sign or path (' + reqPath + ')');
2024-08-30 10:29:02 +08:00
return;
}
2024-10-14 17:13:29 +08:00
// 增加请求次数
viewsInfo.request++;
2024-10-12 17:11:47 +08:00
const uniqidhex = crypto.createHash('md5').update(reqPath + sign).digest('hex');
let cacheMetaFile = '';
let cacheContentFile = '';
let tempCacheContentFile = '';
if (pathIndex[uniqidhex]) {
2024-10-12 18:11:22 +08:00
cacheMetaFile = pathModule.join(cacheDir, `${pathIndex[uniqidhex].uniqid}.meta`);
cacheContentFile = pathModule.join(cacheDir, `${pathIndex[uniqidhex].uniqid}.content`);
2024-10-12 17:11:47 +08:00
}
if (pathIndex[uniqidhex] && isCacheValid(cacheMetaFile, cacheContentFile)) {
2024-10-14 17:13:29 +08:00
// 增加缓存命中次数
viewsInfo.cacheHit++;
2024-09-27 15:32:37 +08:00
serveFromCache(cacheMetaFile, cacheContentFile, res);
2024-08-30 09:36:55 +08:00
} else {
2024-08-31 17:51:00 +08:00
try {
2024-10-14 17:13:29 +08:00
// 增加 API 调用次数
viewsInfo.apiCall++;
2024-09-27 15:32:37 +08:00
const apiData = await fetchApiData(reqPath, sign);
2024-08-31 17:51:00 +08:00
if (apiData.code === 200 && apiData.data && apiData.data.url) {
2024-09-27 19:00:46 +08:00
const { url: realUrl, cloudtype, expiration, path, headers, uniqid } = apiData.data;
const data = { realUrl, cloudtype, expiration: expiration * 1000, path, headers, uniqid };
2024-08-31 17:51:00 +08:00
2024-10-12 18:11:22 +08:00
// 修改 pathIndex 记录时,添加时间戳
pathIndex[uniqidhex] = { uniqid: data.uniqid, timestamp: Date.now() };
2024-10-12 17:11:47 +08:00
cacheMetaFile = pathModule.join(cacheDir, `${data.uniqid}.meta`);
cacheContentFile = pathModule.join(cacheDir, `${data.uniqid}.content`);
tempCacheContentFile = pathModule.join(cacheDir, `${data.uniqid}_${crypto.randomBytes(16).toString('hex')}.temp`);
2024-10-12 18:11:22 +08:00
// 重新写入 meta 缓存
fs.writeFileSync(cacheMetaFile, JSON.stringify(data));
2024-09-27 15:32:37 +08:00
2024-10-12 18:11:22 +08:00
// 如果内容缓存存在, 则直接调用
2024-09-27 15:32:37 +08:00
if (fs.existsSync(cacheContentFile)) {
serveFromCache(cacheMetaFile, cacheContentFile, res);
2024-10-13 15:09:25 +08:00
} else {
fetchAndServe(data, tempCacheContentFile, cacheContentFile, res);
2024-08-30 09:36:55 +08:00
}
2024-08-31 17:51:00 +08:00
} else {
2024-08-30 09:36:55 +08:00
res.writeHead(502, { 'Content-Type': 'text/plain' });
2024-08-31 17:51:00 +08:00
res.end(apiData.message || 'Bad Gateway');
2024-08-30 09:36:55 +08:00
}
2024-08-31 17:51:00 +08:00
} catch (error) {
res.writeHead(502, { 'Content-Type': 'text/plain' });
2024-10-13 15:09:25 +08:00
res.end('Bad Gateway: Failed to decode JSON ' + error);
2024-08-31 17:51:00 +08:00
}
}
});
2024-08-30 09:36:55 +08:00
2024-10-13 15:09:25 +08:00
// 检查缓存是否有效
2024-09-27 15:32:37 +08:00
const isCacheValid = (cacheMetaFile, cacheContentFile) => {
if (!fs.existsSync(cacheMetaFile) || !fs.existsSync(cacheContentFile)) return false;
2024-08-31 17:51:00 +08:00
2024-09-27 15:32:37 +08:00
const cacheData = JSON.parse(fs.readFileSync(cacheMetaFile, 'utf8'));
return cacheData.expiration > Date.now();
2024-08-31 17:51:00 +08:00
};
2024-10-13 15:09:25 +08:00
// 从 API 获取数据
2024-09-27 15:32:37 +08:00
const fetchApiData = (reqPath, sign) => {
2024-08-31 17:51:00 +08:00
return new Promise((resolve, reject) => {
2024-09-27 15:32:37 +08:00
const postData = querystring.stringify({ path: reqPath, sign });
2024-08-31 17:51:00 +08:00
const apiReq = https.request(apiEndpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Accept': 'application/json',
'Content-Length': Buffer.byteLength(postData),
'sign': sign
},
2024-10-15 11:08:18 +08:00
timeout: requestTimeout,
rejectUnauthorized: false
2024-08-31 17:51:00 +08:00
}, (apiRes) => {
let data = '';
apiRes.on('data', chunk => data += chunk);
apiRes.on('end', () => {
try {
resolve(JSON.parse(data));
} catch (error) {
reject(error);
}
});
});
2024-08-30 09:36:55 +08:00
2024-08-31 17:51:00 +08:00
apiReq.on('error', reject);
apiReq.write(postData);
apiReq.end();
});
};
2024-08-30 09:36:55 +08:00
2024-10-13 15:09:25 +08:00
// 从真实 URL 获取数据并写入缓存
2024-09-27 15:32:37 +08:00
const fetchAndServe = (data, tempCacheContentFile, cacheContentFile, res) => {
2024-10-15 11:08:18 +08:00
https.get(data.realUrl, { timeout: requestTimeout * 10,rejectUnauthorized: false }, (realRes) => {
2024-09-27 15:32:37 +08:00
const cacheStream = fs.createWriteStream(tempCacheContentFile, { flags: 'w' });
2024-09-27 18:36:23 +08:00
2024-10-12 17:11:47 +08:00
let isVideo = data.path && typeof data.path === 'string' && data.path.includes('.mp4');
data.headers['content-length'] = realRes.headers['content-length'];
2024-08-30 09:36:55 +08:00
res.writeHead(realRes.statusCode, {
2024-09-27 19:00:46 +08:00
...data.headers,
2024-08-31 17:51:00 +08:00
'Cloud-Type': data.cloudtype,
'Cloud-Expiration': data.expiration,
2024-09-27 23:09:57 +08:00
'Content-Type': isVideo ? 'video/mp4' : 'application/octet-stream',
'ETag': data.uniqid || '',
2024-09-27 19:00:46 +08:00
'Cache-Control': 'public, max-age=31536000',
'Expires': new Date(Date.now() + 31536000000).toUTCString(),
2024-09-27 23:09:57 +08:00
'Accept-Ranges': 'bytes',
'Connection': 'keep-alive',
'Date': new Date().toUTCString(),
2024-09-27 19:00:46 +08:00
'Last-Modified': new Date().toUTCString(),
2024-08-30 09:36:55 +08:00
});
2024-09-27 15:32:37 +08:00
realRes.pipe(cacheStream);
2024-08-30 09:36:55 +08:00
realRes.pipe(res);
2024-09-27 15:32:37 +08:00
realRes.on('end', () => {
cacheStream.end();
2024-09-27 23:09:57 +08:00
if (fs.existsSync(tempCacheContentFile)) {
try {
fs.renameSync(tempCacheContentFile, cacheContentFile);
} catch (err) {
console.error(`Error renaming file: ${err}`);
}
}
2024-09-27 15:32:37 +08:00
});
realRes.on('error', (e) => {
2024-10-12 17:11:47 +08:00
handleResponseError(res, tempCacheContentFile, data.realUrl);
2024-09-27 15:32:37 +08:00
});
2024-08-31 17:51:00 +08:00
}).on('error', (e) => {
2024-10-12 17:11:47 +08:00
handleResponseError(res, tempCacheContentFile, data.realUrl);
2024-09-27 15:32:37 +08:00
});
};
2024-10-13 15:09:25 +08:00
// 从缓存中读取数据并返回
2024-09-27 15:32:37 +08:00
const serveFromCache = (cacheMetaFile, cacheContentFile, res) => {
2024-10-15 11:14:11 +08:00
// 增加缓存调用次数
viewsInfo.cacheCall++;
2024-09-27 15:32:37 +08:00
const cacheData = JSON.parse(fs.readFileSync(cacheMetaFile, 'utf8'));
const readStream = fs.createReadStream(cacheContentFile);
2024-09-27 23:09:57 +08:00
2024-10-12 17:11:47 +08:00
let isVideo = cacheData.path && typeof cacheData.path === 'string' && cacheData.path.includes('.mp4');
2024-09-27 23:09:57 +08:00
cacheData.headers['content-length'] = fs.statSync(cacheContentFile).size;
2024-09-27 15:32:37 +08:00
readStream.on('open', () => {
res.writeHead(200, {
2024-09-27 19:00:46 +08:00
...cacheData.headers,
2024-09-27 15:32:37 +08:00
'Cloud-Type': cacheData.cloudtype,
'Cloud-Expiration': cacheData.expiration,
2024-09-27 23:09:57 +08:00
'Content-Type': isVideo ? 'video/mp4' : 'application/octet-stream',
'ETag': cacheData.uniqid || '',
2024-09-27 19:00:46 +08:00
'Cache-Control': 'public, max-age=31536000',
'Expires': new Date(Date.now() + 31536000000).toUTCString(),
2024-09-27 23:09:57 +08:00
'Accept-Ranges': 'bytes',
'Connection': 'keep-alive',
'Date': new Date().toUTCString(),
2024-09-27 19:00:46 +08:00
'Last-Modified': new Date().toUTCString(),
2024-09-27 15:32:37 +08:00
});
readStream.pipe(res);
});
readStream.on('error', (err) => {
2024-10-12 17:11:47 +08:00
handleCacheReadError(res);
2024-08-30 09:36:55 +08:00
});
2024-08-31 17:51:00 +08:00
};
2024-08-30 09:36:55 +08:00
2024-10-13 15:09:25 +08:00
// 处理响应错误
2024-10-12 17:11:47 +08:00
const handleResponseError = (res, tempCacheContentFile, realUrl) => {
if (!res.headersSent) {
res.writeHead(502, { 'Content-Type': 'text/plain' });
res.end(`Bad Gateway: ${realUrl}`);
}
if (fs.existsSync(tempCacheContentFile)) {
fs.unlinkSync(tempCacheContentFile);
}
};
2024-10-13 15:09:25 +08:00
// 处理缓存读取错误
2024-10-12 17:11:47 +08:00
const handleCacheReadError = (res) => {
if (!res.headersSent) {
res.writeHead(500, { 'Content-Type': 'text/plain' });
res.end('Internal Server Error: Unable to read cache content file');
}
};
2024-10-13 15:09:25 +08:00
// 启动服务器
2024-08-31 17:51:00 +08:00
server.listen(port, () => {
console.log(`Proxy server is running on http://localhost:${port}`);
2024-08-30 09:36:55 +08:00
});
2024-10-13 15:09:25 +08:00
// 处理 SIGINT 信号Ctrl+C
2024-08-30 09:36:55 +08:00
process.on('SIGINT', () => {
console.log('Received SIGINT. Shutting down gracefully...');
server.close(() => {
console.log('Server closed.');
process.exit(0);
});
setTimeout(() => {
console.error('Forcing shutdown...');
process.exit(1);
}, 10000);
2024-09-27 15:32:37 +08:00
});