This commit is contained in:
蒋小陌 2024-08-31 17:51:00 +08:00
parent 4f4d93a8d6
commit e9b152af47

119
index.js
View File

@ -3,15 +3,24 @@ const https = require('https');
const url = require('url'); const url = require('url');
const querystring = require('querystring'); const querystring = require('querystring');
const apiEndpoint = process.env.url || 'https://oss.x-php.com/alist/link';
const requestTimeout = 10000; // 10 seconds const requestTimeout = 10000; // 10 seconds
const cache = {}; const cache = {};
const args = process.argv.slice(2);
// Get port from environment variable or default to 9001 let port = 9001;
const PORT = process.env.PORT || 9001; let apiEndpoint = 'https://oss.x-php.com/alist/link';
const server = http.createServer((req, res) => { // 解析命令行参数
args.forEach(arg => {
const [key, value] = arg.split('=');
if (key === 'port') {
port = parseInt(value, 10);
} else if (key === 'api') {
apiEndpoint = value;
}
});
const server = http.createServer(async (req, res) => {
if (req.url === '/favicon.ico') { if (req.url === '/favicon.ico') {
res.writeHead(204); res.writeHead(204);
res.end(); res.end();
@ -22,33 +31,52 @@ const server = http.createServer((req, res) => {
const path = parsedUrl.pathname; const path = parsedUrl.pathname;
const sign = parsedUrl.query.sign || ''; const sign = parsedUrl.query.sign || '';
if (sign == '' || path == '/') { if (!sign || path === '/') {
res.writeHead(400, { 'Content-Type': 'text/plain' }); res.writeHead(400, { 'Content-Type': 'text/plain' });
res.end('Bad Request: Missing sign or path'); res.end('Bad Request: Missing sign or path');
return; return;
} }
if (isCacheValid(path)) {
cleanExpiredCache();
fetchAndServe(cache[path], res);
} else {
delete cache[path]; // Remove expired cache entry if exists
try {
const apiData = await fetchApiData(path, sign);
if (apiData.code === 200 && apiData.data && apiData.data.url) {
const { url: realUrl, cloudtype, expiration } = apiData.data;
const data = { realUrl, cloudtype, expiration: expiration * 1000 };
// Check if the data is in cache and not expired if (expiration > 0) {
const cacheEntry = cache[path]; cache[path] = data;
if (cacheEntry && cacheEntry.expiration > Date.now()) { }
// 清理所有过期的缓存 fetchAndServe(data, res);
} else {
res.writeHead(502, { 'Content-Type': 'text/plain' });
res.end(apiData.message || 'Bad Gateway');
}
} catch (error) {
res.writeHead(502, { 'Content-Type': 'text/plain' });
res.end('Bad Gateway: Failed to decode JSON');
}
}
});
const isCacheValid = (path) => cache[path] && cache[path].expiration > Date.now();
const cleanExpiredCache = () => {
Object.keys(cache).forEach(key => { Object.keys(cache).forEach(key => {
if (cache[key].expiration < Date.now()) { if (cache[key].expiration < Date.now()) {
delete cache[key]; delete cache[key];
} }
}); });
};
serveFromCache(cacheEntry, res); const fetchApiData = (path, sign) => {
return; return new Promise((resolve, reject) => {
} else {
delete cache[path]; // Remove expired cache entry if exists
}
// Construct the POST data
const postData = querystring.stringify({ path, sign }); const postData = querystring.stringify({ path, sign });
// Request the real URL from the API
const apiReq = https.request(apiEndpoint, { const apiReq = https.request(apiEndpoint, {
method: 'POST', method: 'POST',
headers: { headers: {
@ -63,64 +91,35 @@ const server = http.createServer((req, res) => {
apiRes.on('data', chunk => data += chunk); apiRes.on('data', chunk => data += chunk);
apiRes.on('end', () => { apiRes.on('end', () => {
try { try {
const apiData = JSON.parse(data); resolve(JSON.parse(data));
if (apiData.code === 200 && apiData.data && apiData.data.url) {
const { url: realUrl, cloudtype, expiration } = apiData.data;
// Cache the response if expiration is greater than 0
if (expiration > 0) {
cache[path] = {
realUrl,
cloudtype,
expiration: Date.now() + expiration * 1000
};
}
fetchAndServe(realUrl, cloudtype, res);
} else {
res.writeHead(502, { 'Content-Type': 'text/plain' });
res.end(apiData.message || 'Bad Gateway');
}
} catch (error) { } catch (error) {
res.writeHead(502, { 'Content-Type': 'text/plain' }); reject(error);
res.end('Bad Gateway: Failed to decode JSON');
} }
}); });
}); });
apiReq.on('error', (e) => { apiReq.on('error', reject);
if (e.code === 'ETIMEDOUT') {
res.writeHead(504, { 'Content-Type': 'text/plain' });
res.end('Gateway Timeout');
} else {
res.writeHead(500, { 'Content-Type': 'text/plain' });
res.end('Internal Server Error');
}
});
apiReq.write(postData); apiReq.write(postData);
apiReq.end(); apiReq.end();
}); });
};
function fetchAndServe(realUrl, cloudtype, res) { const fetchAndServe = (data, res) => {
const realReq = https.get(realUrl, { timeout: requestTimeout * 10 }, (realRes) => { https.get(data.realUrl, { timeout: requestTimeout * 10 }, (realRes) => {
res.writeHead(realRes.statusCode, { res.writeHead(realRes.statusCode, {
...realRes.headers, ...realRes.headers,
'cloudtype': cloudtype 'Cloud-Type': data.cloudtype,
'Cloud-Expiration': data.expiration,
}); });
realRes.pipe(res); realRes.pipe(res);
}); }).on('error', (e) => {
realReq.on('error', (e) => {
res.writeHead(502, { 'Content-Type': 'text/plain' }); res.writeHead(502, { 'Content-Type': 'text/plain' });
res.end(`Bad Gateway: ${realUrl}`); res.end(`Bad Gateway: ${data.realUrl}`);
}); });
} };
function serveFromCache(cacheEntry, res) { server.listen(port, () => {
fetchAndServe(cacheEntry.realUrl, cacheEntry.cloudtype, res); console.log(`Proxy server is running on http://localhost:${port}`);
}
server.listen(PORT, () => {
console.log(`Proxy server is running on http://localhost:${PORT}`);
}); });
// Graceful shutdown // Graceful shutdown