From 9b88bedb808d1b80d86a6a6df1fcf3c5ff556308 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E8=92=8B=E5=B0=8F=E9=99=8C?= <xiaomo.drip@gmail.com>
Date: Mon, 14 Oct 2024 17:13:29 +0800
Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=95=B0=E6=8D=AE=E8=AE=BF?=
 =?UTF-8?q?=E9=97=AE=E7=BB=9F=E8=AE=A1?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 README.md | 21 +++++++++++++++++++++
 index.js  | 26 ++++++++++++++++++++++++--
 index.sh  | 46 ----------------------------------------------
 3 files changed, 45 insertions(+), 48 deletions(-)
 create mode 100644 README.md
 delete mode 100644 index.sh

diff --git a/README.md b/README.md
new file mode 100644
index 0000000..ebdfc14
--- /dev/null
+++ b/README.md
@@ -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
+
diff --git a/index.js b/index.js
index 1756b7a..0fd4377 100644
--- a/index.js
+++ b/index.js
@@ -11,6 +11,17 @@ const cacheDir = pathModule.join(__dirname, '.cache');
 const args = process.argv.slice(2);
 const pathIndex = {};
 
+// 增加访问计数器
+const viewsInfo = {
+    // 请求次数
+    request: 0,
+    // 缓存命中次数
+    cacheHit: 0,
+    // API调用次数
+    apiCall: 0,
+};
+
+// 默认端口号和 API 地址
 let port = 9001;
 let apiEndpoint = 'https://oss.x-php.com/alist/link';
 
@@ -41,14 +52,13 @@ setInterval(() => {
             delete pathIndex[key];
         }
     }
-}, 60 * 60 * 1000); // 每隔 1 小时执行一次
+}, 60 * 60 * 1000);  // 每隔 1 小时执行一次
 
 // 处理请求并返回数据
 const server = http.createServer(async (req, res) => {
 
     req.url = req.url.replace(/\/{2,}/g, '/');
 
-
     if (req.url === '/favicon.ico') {
         res.writeHead(204);
         res.end();
@@ -65,6 +75,7 @@ const server = http.createServer(async (req, res) => {
                 port: port,
                 cacheDir: cacheDir,
                 pathIndexCount: Object.keys(pathIndex).length,
+                viewsInfo: viewsInfo
             }
         }));
         return;
@@ -80,6 +91,9 @@ const server = http.createServer(async (req, res) => {
         return;
     }
 
+    // 增加请求次数
+    viewsInfo.request++;
+
     const uniqidhex = crypto.createHash('md5').update(reqPath + sign).digest('hex');
 
     let cacheMetaFile = '';
@@ -92,9 +106,17 @@ const server = http.createServer(async (req, res) => {
     }
 
     if (pathIndex[uniqidhex] && isCacheValid(cacheMetaFile, cacheContentFile)) {
+
+        // 增加缓存命中次数
+        viewsInfo.cacheHit++;
+
         serveFromCache(cacheMetaFile, cacheContentFile, res);
     } else {
         try {
+
+            // 增加 API 调用次数
+            viewsInfo.apiCall++;
+
             const apiData = await fetchApiData(reqPath, sign);
             if (apiData.code === 200 && apiData.data && apiData.data.url) {
                 const { url: realUrl, cloudtype, expiration, path, headers, uniqid } = apiData.data;
diff --git a/index.sh b/index.sh
deleted file mode 100644
index 890de97..0000000
--- a/index.sh
+++ /dev/null
@@ -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