增加数据访问统计

This commit is contained in:
蒋小陌 2024-10-14 17:13:29 +08:00
parent ef878eade1
commit 9b88bedb80
3 changed files with 45 additions and 48 deletions

21
README.md Normal file
View File

@ -0,0 +1,21 @@
本指南将帮助你安装和运行 AList 代理服务。AList 是一个支持多种存储的文件列表程序,提供网页浏览和 WebDAV 服务。
安装步骤
1. 下载代理脚本
使用以下命令下载代理脚本到本地:
bash
curl -o index.js https://x-mo.cn:9009/XiaoMo/alist-proxy/raw/branch/master/index.js
2. 安装依赖
确保你的系统已经安装了 Node.js。如果没有请访问 Node.js 官网 下载并安装。
3. 运行代理服务
下载完成后,使用以下命令运行代理服务:
bash
node index.js
4. 访问 AList 界面
代理服务运行后,你可以通过浏览器访问 AList 的 Web 界面。默认情况下AList 监听在 http://localhost:9001

View File

@ -11,6 +11,17 @@ const cacheDir = pathModule.join(__dirname, '.cache');
const args = process.argv.slice(2); const args = process.argv.slice(2);
const pathIndex = {}; const pathIndex = {};
// 增加访问计数器
const viewsInfo = {
// 请求次数
request: 0,
// 缓存命中次数
cacheHit: 0,
// API调用次数
apiCall: 0,
};
// 默认端口号和 API 地址
let port = 9001; let port = 9001;
let apiEndpoint = 'https://oss.x-php.com/alist/link'; let apiEndpoint = 'https://oss.x-php.com/alist/link';
@ -48,7 +59,6 @@ const server = http.createServer(async (req, res) => {
req.url = req.url.replace(/\/{2,}/g, '/'); req.url = req.url.replace(/\/{2,}/g, '/');
if (req.url === '/favicon.ico') { if (req.url === '/favicon.ico') {
res.writeHead(204); res.writeHead(204);
res.end(); res.end();
@ -65,6 +75,7 @@ const server = http.createServer(async (req, res) => {
port: port, port: port,
cacheDir: cacheDir, cacheDir: cacheDir,
pathIndexCount: Object.keys(pathIndex).length, pathIndexCount: Object.keys(pathIndex).length,
viewsInfo: viewsInfo
} }
})); }));
return; return;
@ -80,6 +91,9 @@ const server = http.createServer(async (req, res) => {
return; return;
} }
// 增加请求次数
viewsInfo.request++;
const uniqidhex = crypto.createHash('md5').update(reqPath + sign).digest('hex'); const uniqidhex = crypto.createHash('md5').update(reqPath + sign).digest('hex');
let cacheMetaFile = ''; let cacheMetaFile = '';
@ -92,9 +106,17 @@ const server = http.createServer(async (req, res) => {
} }
if (pathIndex[uniqidhex] && isCacheValid(cacheMetaFile, cacheContentFile)) { if (pathIndex[uniqidhex] && isCacheValid(cacheMetaFile, cacheContentFile)) {
// 增加缓存命中次数
viewsInfo.cacheHit++;
serveFromCache(cacheMetaFile, cacheContentFile, res); serveFromCache(cacheMetaFile, cacheContentFile, res);
} else { } else {
try { try {
// 增加 API 调用次数
viewsInfo.apiCall++;
const apiData = await fetchApiData(reqPath, sign); const apiData = await fetchApiData(reqPath, sign);
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;

View File

@ -1,46 +0,0 @@
#!/bin/bash
# 检查是否传入端口参数,如果没有则使用默认端口 6001
if [ $# -eq 0 ]; then
PORT=6001
echo "未提供端口号,使用默认端口 $PORT"
else
PORT=$1 # 从命令行参数获取端口号
fi
# 定义文件
FILE="index.js"
# 检查 index.js 是否存在,如果存在则删除
if [ -f "$FILE" ]; then
echo "$FILE 存在,正在删除..."
rm -f "$FILE"
echo "$FILE 已删除"
fi
# 检查指定端口是否被占用
if lsof -i:$PORT; then
echo "端口 $PORT 被占用,正在杀死相关进程..."
# 获取占用端口的进程 ID 并杀死它
lsof -ti:$PORT | xargs kill -9
echo "端口 $PORT 已释放"
else
echo "端口 $PORT 未被占用"
fi
# 检查 Node.js 是否安装
if ! command -v node &> /dev/null; then
echo "Node.js 未安装,请先安装 Node.js。"
exit 1
fi
# 下载 index.js 并执行
echo "正在下载 index.js..."
curl -o $FILE https://x-mo.cn:9009/XiaoMo/alist-proxy/raw/branch/master/index.js
if [ $? -eq 0 ]; then
echo "下载完成,正在执行 node $FILE --port=$PORT ..."
node $FILE --port=$PORT # 使用 --port=6001 传递参数
else
echo "下载失败"
fi