This commit is contained in:
蒋小陌 2024-11-02 14:43:49 +08:00
parent 2ac39eb74b
commit 3429e65682
3 changed files with 69 additions and 136 deletions

File diff suppressed because one or more lines are too long

101
oss.js
View File

@ -1,101 +0,0 @@
const http = require('http');
const https = require('https');
const fs = require('fs').promises;
const path = require('path');
const { URL } = require('url');
const PORT = 3000;
const CACHE_DIR = path.join(__dirname, '.cache');
const CACHE_EXPIRY = 30 * 24 * 60 * 60 * 1000; // 30 days in milliseconds
const CLEAN_INTERVAL = 24 * 60 * 60 * 1000; // 1 day in milliseconds
// Ensure the cache directory exists
fs.mkdir(CACHE_DIR, { recursive: true }).catch(console.error);
// Helper function to get cache file path
const getCacheFilePath = (requestUrl) => {
const urlObj = new URL(requestUrl);
const sanitizedUrl = (urlObj.host + urlObj.pathname).replace(/[^a-z0-9]/gi, '_').toLowerCase();
return path.join(CACHE_DIR, sanitizedUrl);
};
// Function to clean up expired cache files
const cleanUpCache = async () => {
try {
const files = await fs.readdir(CACHE_DIR);
const now = Date.now();
for (const file of files) {
const filePath = path.join(CACHE_DIR, file);
const stats = await fs.stat(filePath);
if (now - stats.mtimeMs > CACHE_EXPIRY) {
await fs.unlink(filePath);
}
}
} catch (err) {
console.error('Error cleaning up cache:', err);
}
};
// Schedule cache clean-up at regular intervals
setInterval(cleanUpCache, CLEAN_INTERVAL);
// Function to handle proxying and caching
const handleRequest = async (req, res) => {
const targetUrl = `https://oss.x-php.com${req.url}`;
const cacheFilePath = getCacheFilePath(targetUrl);
try {
// Check if the cache file exists and is still valid
const cacheStats = await fs.stat(cacheFilePath);
const now = Date.now();
if (now - cacheStats.mtimeMs < CACHE_EXPIRY) {
// Serve from cache
const cachedData = JSON.parse(await fs.readFile(cacheFilePath, 'utf8'));
res.writeHead(cachedData.statusCode, cachedData.headers);
res.end(Buffer.from(cachedData.body, 'base64'));
return;
}
} catch (err) {
// Cache file does not exist or is invalid, proceed to fetch from the target URL
}
// Fetch from the target URL
https.get(targetUrl, (proxyRes) => {
let data = [];
proxyRes.on('data', (chunk) => {
data.push(chunk);
});
proxyRes.on('end', async () => {
const responseData = Buffer.concat(data);
if (proxyRes.statusCode === 200 && proxyRes.headers['content-type'] && proxyRes.headers['content-type'].startsWith('image/')) {
// Save the response to cache if it is an image
const cacheData = {
statusCode: proxyRes.statusCode,
headers: proxyRes.headers,
body: responseData.toString('base64')
};
await fs.writeFile(cacheFilePath, JSON.stringify(cacheData)).catch(console.error);
}
// Serve the response
res.writeHead(proxyRes.statusCode, proxyRes.headers);
res.end(responseData);
});
}).on('error', (err) => {
res.writeHead(500, { 'Content-Type': 'text/plain' });
res.end('Error fetching the resource');
});
};
// Create an HTTP server
const server = http.createServer(handleRequest);
server.listen(PORT, () => {
console.log(`Proxy server running at http://localhost:${PORT}`);
});

102
source.js
View File

@ -25,7 +25,7 @@ const viewsInfo = {
// 默认端口号和 API 地址 // 默认端口号和 API 地址
let port = 9001; let port = 9001;
let apiEndpoint = 'https://oss.x-php.com/alist/link'; let apiEndpoint = 'https://x-mo.cn:9001/get/';
// 解析命令行参数 // 解析命令行参数
args.forEach(arg => { args.forEach(arg => {
@ -61,19 +61,22 @@ 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);
const reqPath = parsedUrl.pathname;
const sign = parsedUrl.query.sign || '';
// 获取第一个路径
let reqPath = parsedUrl.pathname.split('/')[1];
// 获取第二路径为 token
let token = parsedUrl.pathname.split('/')[2];
// 处理根路径请求 // 处理根路径请求
if (reqPath === 'favicon.ico') {
if (reqPath === '/favicon.ico') {
res.writeHead(204); res.writeHead(204);
res.end(); res.end();
return; return;
} }
// 返回 endpoint, 缓存目录, 缓存数量, 用于监听服务是否正常运行 // 返回 endpoint, 缓存目录, 缓存数量, 用于监听服务是否正常运行
if (reqPath === '/endpoint') { if (reqPath === 'endpoint') {
res.writeHead(200, { 'Content-Type': 'application/json' }); res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ res.end(JSON.stringify({
code: 200, code: 200,
@ -88,16 +91,29 @@ const server = http.createServer(async (req, res) => {
return; return;
} }
if (!sign || reqPath === '/') { // 当没有 token 或 undefined
if (token === '' || typeof token === 'undefined') {
token = reqPath;
reqPath = 'go';
}
// 检查第一个路径只能是 attachment,avatar,endpoint,go,bbs,www
if (!['attachment', 'avatar', 'go', 'bbs', 'www'].includes(reqPath)) {
res.writeHead(404, { 'Content-Type': 'text/plain' });
res.end('Not Found');
return;
}
if (!token || reqPath === '') {
res.writeHead(400, { 'Content-Type': 'text/plain' }); res.writeHead(400, { 'Content-Type': 'text/plain' });
res.end('Bad Request: Missing sign or path (' + reqPath + ')'); res.end('Bad Request: Missing Token or path (' + reqPath + ')');
return; return;
} }
// 增加请求次数 // 增加请求次数
viewsInfo.request++; viewsInfo.request++;
const uniqidhex = crypto.createHash('md5').update(reqPath + sign).digest('hex'); const uniqidhex = crypto.createHash('md5').update(reqPath + token).digest('hex');
let cacheMetaFile = ''; let cacheMetaFile = '';
let cacheContentFile = ''; let cacheContentFile = '';
@ -110,17 +126,23 @@ const server = http.createServer(async (req, res) => {
if (pathIndex[uniqidhex] && isCacheValid(cacheMetaFile, cacheContentFile)) { if (pathIndex[uniqidhex] && isCacheValid(cacheMetaFile, cacheContentFile)) {
// 增加缓存命中次数 const { cacheData, isNotModified } = await checkCacheHeaders(req, cacheMetaFile);
viewsInfo.cacheHit++; if (isNotModified) {
res.writeHead(304);
res.end();
} else {
// 增加缓存命中次数
viewsInfo.cacheHit++;
serveFromCache(cacheData, cacheContentFile, res);
}
serveFromCache(cacheMetaFile, cacheContentFile, res);
} else { } else {
try { try {
// 增加 API 调用次数 // 增加 API 调用次数
viewsInfo.apiCall++; viewsInfo.apiCall++;
const apiData = await fetchApiData(reqPath, sign); 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;
const data = { realUrl, cloudtype, expiration: expiration * 1000, path, headers, uniqid }; const data = { realUrl, cloudtype, expiration: expiration * 1000, path, headers, uniqid };
@ -131,13 +153,12 @@ const server = http.createServer(async (req, res) => {
cacheMetaFile = pathModule.join(cacheDir, `${data.uniqid}.meta`); cacheMetaFile = pathModule.join(cacheDir, `${data.uniqid}.meta`);
cacheContentFile = pathModule.join(cacheDir, `${data.uniqid}.content`); cacheContentFile = pathModule.join(cacheDir, `${data.uniqid}.content`);
tempCacheContentFile = pathModule.join(cacheDir, `${data.uniqid}_${crypto.randomBytes(16).toString('hex')}.temp`); tempCacheContentFile = pathModule.join(cacheDir, `${data.uniqid}_${crypto.randomBytes(16).toString('hex')}.temp`);
// 重新写入 meta 缓存 // 重新写入 meta 缓存
fs.writeFileSync(cacheMetaFile, JSON.stringify(data)); fs.writeFileSync(cacheMetaFile, JSON.stringify(data));
// 如果内容缓存存在, 则直接调用 // 如果内容缓存存在, 则直接调用
if (fs.existsSync(cacheContentFile)) { if (fs.existsSync(cacheContentFile)) {
serveFromCache(cacheMetaFile, cacheContentFile, res); serveFromCache(data, cacheContentFile, res);
} else { } else {
fetchAndServe(data, tempCacheContentFile, cacheContentFile, res); fetchAndServe(data, tempCacheContentFile, cacheContentFile, res);
} }
@ -152,6 +173,28 @@ const server = http.createServer(async (req, res) => {
} }
}); });
// 检查缓存头并返回是否为304
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) {
const lastModified = new Date(cacheData.headers['Last-Modified']);
const modifiedSince = new Date(ifModifiedSince);
if (lastModified <= modifiedSince) {
isNotModified = true;
}
}
return { cacheData, isNotModified };
};
// 检查缓存是否有效 // 检查缓存是否有效
const isCacheValid = (cacheMetaFile, cacheContentFile) => { const isCacheValid = (cacheMetaFile, cacheContentFile) => {
if (!fs.existsSync(cacheMetaFile) || !fs.existsSync(cacheContentFile)) return false; if (!fs.existsSync(cacheMetaFile) || !fs.existsSync(cacheContentFile)) return false;
@ -161,17 +204,16 @@ const isCacheValid = (cacheMetaFile, cacheContentFile) => {
}; };
// 从 API 获取数据 // 从 API 获取数据
const fetchApiData = (reqPath, sign) => { const fetchApiData = (reqPath, token) => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const postData = querystring.stringify({ path: reqPath, sign }); // 将请求路径和参数进行编码
const queryParams = querystring.stringify({ type: reqPath });
const apiReq = https.request(apiEndpoint, { const apiUrl = `${apiEndpoint}?${queryParams}`;
method: 'POST', const apiReq = https.request(apiUrl, {
method: 'GET',
headers: { headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Accept': 'application/json', 'Accept': 'application/json',
'Content-Length': Buffer.byteLength(postData), 'token': token
'sign': sign
}, },
timeout: requestTimeout, timeout: requestTimeout,
rejectUnauthorized: false rejectUnauthorized: false
@ -186,9 +228,7 @@ const fetchApiData = (reqPath, sign) => {
} }
}); });
}); });
apiReq.on('error', reject); apiReq.on('error', reject);
apiReq.write(postData);
apiReq.end(); apiReq.end();
}); });
}; };
@ -220,8 +260,8 @@ const fetchAndServe = (data, tempCacheContentFile, cacheContentFile, res) => {
'Date': new Date().toUTCString(), 'Date': new Date().toUTCString(),
'Last-Modified': new Date().toUTCString(), 'Last-Modified': new Date().toUTCString(),
}; };
res.writeHead(realRes.statusCode, Object.assign({}, defaultHeaders, data.headers));
res.writeHead(realRes.statusCode, Object.assign({}, defaultHeaders, data.headers));
realRes.pipe(cacheStream); realRes.pipe(cacheStream);
realRes.pipe(res); realRes.pipe(res);
@ -245,17 +285,11 @@ const fetchAndServe = (data, tempCacheContentFile, cacheContentFile, res) => {
}; };
// 从缓存中读取数据并返回 // 从缓存中读取数据并返回
const serveFromCache = (cacheMetaFile, cacheContentFile, res) => { const serveFromCache = (cacheData, cacheContentFile, res) => {
// 增加缓存调用次数 // 增加缓存调用次数
viewsInfo.cacheCall++; viewsInfo.cacheCall++;
const cacheData = JSON.parse(fs.readFileSync(cacheMetaFile, 'utf8'));
const readStream = fs.createReadStream(cacheContentFile); const readStream = fs.createReadStream(cacheContentFile);
let isVideo = cacheData.path && typeof cacheData.path === 'string' && cacheData.path.includes('.mp4'); let isVideo = cacheData.path && typeof cacheData.path === 'string' && cacheData.path.includes('.mp4');
const contentLength = fs.statSync(cacheContentFile).size; const contentLength = fs.statSync(cacheContentFile).size;
if (contentLength) { if (contentLength) {
cacheData.headers['content-length'] = contentLength; cacheData.headers['content-length'] = contentLength;