1111
This commit is contained in:
102
source.js
102
source.js
@@ -25,7 +25,7 @@ const viewsInfo = {
|
||||
|
||||
// 默认端口号和 API 地址
|
||||
let port = 9001;
|
||||
let apiEndpoint = 'https://oss.x-php.com/alist/link';
|
||||
let apiEndpoint = 'https://x-mo.cn:9001/get/';
|
||||
|
||||
// 解析命令行参数
|
||||
args.forEach(arg => {
|
||||
@@ -61,19 +61,22 @@ const server = http.createServer(async (req, res) => {
|
||||
|
||||
req.url = req.url.replace(/\/{2,}/g, '/');
|
||||
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.end();
|
||||
return;
|
||||
}
|
||||
|
||||
// 返回 endpoint, 缓存目录, 缓存数量, 用于监听服务是否正常运行
|
||||
if (reqPath === '/endpoint') {
|
||||
if (reqPath === 'endpoint') {
|
||||
res.writeHead(200, { 'Content-Type': 'application/json' });
|
||||
res.end(JSON.stringify({
|
||||
code: 200,
|
||||
@@ -88,16 +91,29 @@ const server = http.createServer(async (req, res) => {
|
||||
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.end('Bad Request: Missing sign or path (' + reqPath + ')');
|
||||
res.end('Bad Request: Missing Token or path (' + reqPath + ')');
|
||||
return;
|
||||
}
|
||||
|
||||
// 增加请求次数
|
||||
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 cacheContentFile = '';
|
||||
@@ -110,17 +126,23 @@ const server = http.createServer(async (req, res) => {
|
||||
|
||||
if (pathIndex[uniqidhex] && isCacheValid(cacheMetaFile, cacheContentFile)) {
|
||||
|
||||
// 增加缓存命中次数
|
||||
viewsInfo.cacheHit++;
|
||||
const { cacheData, isNotModified } = await checkCacheHeaders(req, cacheMetaFile);
|
||||
if (isNotModified) {
|
||||
res.writeHead(304);
|
||||
res.end();
|
||||
} else {
|
||||
// 增加缓存命中次数
|
||||
viewsInfo.cacheHit++;
|
||||
serveFromCache(cacheData, cacheContentFile, res);
|
||||
}
|
||||
|
||||
serveFromCache(cacheMetaFile, cacheContentFile, res);
|
||||
} else {
|
||||
try {
|
||||
|
||||
// 增加 API 调用次数
|
||||
viewsInfo.apiCall++;
|
||||
|
||||
const apiData = await fetchApiData(reqPath, sign);
|
||||
const apiData = await fetchApiData(reqPath, token);
|
||||
if (apiData.code === 200 && apiData.data && apiData.data.url) {
|
||||
const { url: realUrl, cloudtype, expiration, path, headers, uniqid } = apiData.data;
|
||||
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`);
|
||||
cacheContentFile = pathModule.join(cacheDir, `${data.uniqid}.content`);
|
||||
tempCacheContentFile = pathModule.join(cacheDir, `${data.uniqid}_${crypto.randomBytes(16).toString('hex')}.temp`);
|
||||
|
||||
// 重新写入 meta 缓存
|
||||
fs.writeFileSync(cacheMetaFile, JSON.stringify(data));
|
||||
|
||||
// 如果内容缓存存在, 则直接调用
|
||||
|
||||
if (fs.existsSync(cacheContentFile)) {
|
||||
serveFromCache(cacheMetaFile, cacheContentFile, res);
|
||||
serveFromCache(data, cacheContentFile, res);
|
||||
} else {
|
||||
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) => {
|
||||
if (!fs.existsSync(cacheMetaFile) || !fs.existsSync(cacheContentFile)) return false;
|
||||
@@ -161,17 +204,16 @@ const isCacheValid = (cacheMetaFile, cacheContentFile) => {
|
||||
};
|
||||
|
||||
// 从 API 获取数据
|
||||
const fetchApiData = (reqPath, sign) => {
|
||||
const fetchApiData = (reqPath, token) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
const postData = querystring.stringify({ path: reqPath, sign });
|
||||
|
||||
const apiReq = https.request(apiEndpoint, {
|
||||
method: 'POST',
|
||||
// 将请求路径和参数进行编码
|
||||
const queryParams = querystring.stringify({ type: reqPath });
|
||||
const apiUrl = `${apiEndpoint}?${queryParams}`;
|
||||
const apiReq = https.request(apiUrl, {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'Content-Type': 'application/x-www-form-urlencoded',
|
||||
'Accept': 'application/json',
|
||||
'Content-Length': Buffer.byteLength(postData),
|
||||
'sign': sign
|
||||
'token': token
|
||||
},
|
||||
timeout: requestTimeout,
|
||||
rejectUnauthorized: false
|
||||
@@ -186,9 +228,7 @@ const fetchApiData = (reqPath, sign) => {
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
apiReq.on('error', reject);
|
||||
apiReq.write(postData);
|
||||
apiReq.end();
|
||||
});
|
||||
};
|
||||
@@ -220,8 +260,8 @@ const fetchAndServe = (data, tempCacheContentFile, cacheContentFile, res) => {
|
||||
'Date': 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(res);
|
||||
|
||||
@@ -245,17 +285,11 @@ const fetchAndServe = (data, tempCacheContentFile, cacheContentFile, res) => {
|
||||
};
|
||||
|
||||
// 从缓存中读取数据并返回
|
||||
const serveFromCache = (cacheMetaFile, cacheContentFile, res) => {
|
||||
|
||||
const serveFromCache = (cacheData, cacheContentFile, res) => {
|
||||
// 增加缓存调用次数
|
||||
viewsInfo.cacheCall++;
|
||||
|
||||
|
||||
const cacheData = JSON.parse(fs.readFileSync(cacheMetaFile, 'utf8'));
|
||||
const readStream = fs.createReadStream(cacheContentFile);
|
||||
|
||||
let isVideo = cacheData.path && typeof cacheData.path === 'string' && cacheData.path.includes('.mp4');
|
||||
|
||||
const contentLength = fs.statSync(cacheContentFile).size;
|
||||
if (contentLength) {
|
||||
cacheData.headers['content-length'] = contentLength;
|
||||
|
||||
Reference in New Issue
Block a user