From 3323eef81eb644a9ba1bee0bc005eca5046a30f4 Mon Sep 17 00:00:00 2001 From: root Date: Wed, 20 Nov 2024 22:13:27 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=E9=A1=B5=E9=9D=A2=E5=86=85?= =?UTF-8?q?=E5=AE=B9=EF=BC=8C=E6=94=AF=E6=8C=81=E8=B7=AF=E5=BE=84=E5=8C=B9?= =?UTF-8?q?=E9=85=8D=E8=AE=A1=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 13 +- src/Config/AppConfig.cs | 1 + src/Helpers/DatabaseHelper.cs | 2 +- src/Middleware/IPAccessLimitMiddleware.cs | 22 ++ .../PublishProfiles/FolderProfile.pubxml | 2 +- src/Services/TaskManager.cs | 2 +- src/iFileProxy.json | 6 +- src/wwwroot/index.html | 179 +++++--------- src/wwwroot/query_download_task.html | 229 ++++++++++++++++++ 9 files changed, 326 insertions(+), 130 deletions(-) create mode 100644 src/wwwroot/query_download_task.html diff --git a/README.md b/README.md index 67e61b5..fffbf3c 100644 --- a/README.md +++ b/README.md @@ -1 +1,12 @@ -### 文件代理下载工具 \ No newline at end of file +### 文件代理下载工具 + +#### 功能列表 +- [x] 代理下载文件 +- [x] 浏览器下载文件 +- [x] 文件名黑名单 +- [x] 目标Host黑名单 +- [x] 文件大小限制 +- [x] 基于IP查询提交的任务状态 +- [x] 基于IP和路由的请求次数限制 +- [ ] 已经存在对应文件时候直接跳过代理下载 直接下载已经缓存的内容 +- [ ] 捐赠 \ No newline at end of file diff --git a/src/Config/AppConfig.cs b/src/Config/AppConfig.cs index b08c948..f82ea0f 100644 --- a/src/Config/AppConfig.cs +++ b/src/Config/AppConfig.cs @@ -55,6 +55,7 @@ namespace iFileProxy.Config public List BlockedHost { get; set; } = []; public List BlockedFileName { get; set; } = []; public List BlockedClientIP { get; set; } = []; + public List RoutesToTrack { get; set; } = []; public int DailyRequestLimitPerIP { get; set; } = -1; } diff --git a/src/Helpers/DatabaseHelper.cs b/src/Helpers/DatabaseHelper.cs index b2f2763..55df5b7 100644 --- a/src/Helpers/DatabaseHelper.cs +++ b/src/Helpers/DatabaseHelper.cs @@ -176,7 +176,7 @@ namespace iFileProxy.Helpers public bool UpdateTaskStatus(TaskInfo taskInfo) { - string sql = @"UPDATE t_tasks_info set `status` = @status WHERE `tid` = @tid"; + string sql = @"UPDATE t_tasks_info set `status` = @status , update_time = Now() WHERE `tid` = @tid"; MySqlConnection conn = GetAndOpenDBConn("iFileProxy_Db"); try { diff --git a/src/Middleware/IPAccessLimitMiddleware.cs b/src/Middleware/IPAccessLimitMiddleware.cs index ba7760c..d030c65 100644 --- a/src/Middleware/IPAccessLimitMiddleware.cs +++ b/src/Middleware/IPAccessLimitMiddleware.cs @@ -1,5 +1,6 @@ namespace iFileProxy.Middleware { + using iFileProxy.Config; using iFileProxy.Helpers; using iFileProxy.Models; using Microsoft.AspNetCore.Http; @@ -13,6 +14,7 @@ private readonly RequestDelegate _next; private readonly Dictionary> _IPAccessCountDict; private readonly int _dailyRequestLimitPerIP; + private readonly AppConfig _appConfig = AppConfig.GetCurrConfig(); public IPAccessLimitMiddleware(RequestDelegate next, Dictionary> IPAccessCountDict, int dailyRequestLimitPerIP) { @@ -23,6 +25,26 @@ public async Task InvokeAsync(HttpContext context) { + // 获取需要跟踪的路由列表 + var routesToTrack = _appConfig.SecurityOptions.RoutesToTrack; + + // 检查当前请求的路径是否在需要跟踪的路由列表中 + foreach (var p in routesToTrack) + { + if (context.Request.Path.ToString().StartsWith(p, StringComparison.OrdinalIgnoreCase)) + { + // 如果匹配到需要跟踪的路由,继续处理请求 + break; + } + } + + // 如果没有匹配到需要跟踪的路由,直接调用下一个中间件 + if (!routesToTrack.Any(p => context.Request.Path.ToString().StartsWith(p, StringComparison.OrdinalIgnoreCase))) + { + await _next(context); + return; + } + string dateStr = DateTime.Now.ToString("yyyy-MM-dd"); string ipStr = MasterHelper.GetClientIPAddr(context); diff --git a/src/Properties/PublishProfiles/FolderProfile.pubxml b/src/Properties/PublishProfiles/FolderProfile.pubxml index 8285a16..1296e0c 100644 --- a/src/Properties/PublishProfiles/FolderProfile.pubxml +++ b/src/Properties/PublishProfiles/FolderProfile.pubxml @@ -15,7 +15,7 @@ https://go.microsoft.com/fwlink/?LinkID=208121. <_TargetId>Folder net8.0 - win-x64 + linux-x64 e343bd8a-27ed-47e2-b50d-e3000730e65e false true diff --git a/src/Services/TaskManager.cs b/src/Services/TaskManager.cs index 9e316e9..c82ba76 100644 --- a/src/Services/TaskManager.cs +++ b/src/Services/TaskManager.cs @@ -37,7 +37,7 @@ namespace iFileProxy.Services public TaskAddState AddTask(HttpContext c) { string? clientIp = MasterHelper.GetClientIPAddr(c); - string? t_url = c.Request.Query["url"].FirstOrDefault(); + string? t_url = c.Request.Query["url"].FirstOrDefault() ?? c.Request.Form["url"].FirstOrDefault(); if (_appConfig.DownloadOptions.MaxParallelTasks != 0 && runningTasks.Count >= _appConfig.DownloadOptions.MaxParallelTasks) return TaskAddState.ErrMaxParallelTasksLimit; diff --git a/src/iFileProxy.json b/src/iFileProxy.json index a502070..00d86fd 100644 --- a/src/iFileProxy.json +++ b/src/iFileProxy.json @@ -30,6 +30,10 @@ "BlockedClientIP": [ // 禁止使用服务的客户端IP "127.0.0.1" ], - "DailyRequestLimitPerIP": 200 // 单个IP每日最大请求次数限制 + "DailyRequestLimitPerIP": 200, // 单个IP每日最大请求次数限制 + "RoutesToTrack": [ // 参加IP请求数统计的路径名单 + "/Download", + "/AddOfflineTask" + ] } } \ No newline at end of file diff --git a/src/wwwroot/index.html b/src/wwwroot/index.html index b09bd3a..3713d02 100644 --- a/src/wwwroot/index.html +++ b/src/wwwroot/index.html @@ -1,70 +1,19 @@ - + - 离线下载任务管理 + Github文件下载加速 + + + + + + +
+

离线下载任务管理

+
+ + + + + + + + + + + + + + +
文件名大小提交时间状态哈希
+

返回主页 | 捐赠

+ +
+
+ + + + + + + + + \ No newline at end of file