diff --git a/src/Config/AppConfig.cs b/src/Config/AppConfig.cs index f82ea0f..f3de6a4 100644 --- a/src/Config/AppConfig.cs +++ b/src/Config/AppConfig.cs @@ -57,6 +57,7 @@ namespace iFileProxy.Config public List BlockedClientIP { get; set; } = []; public List RoutesToTrack { get; set; } = []; public int DailyRequestLimitPerIP { get; set; } = -1; + public bool AllowDifferentIPsForDownload { get; set; } = true; } public partial class Database diff --git a/src/Controllers/iProxyController.cs b/src/Controllers/iProxyController.cs index e6ae664..aec1844 100644 --- a/src/Controllers/iProxyController.cs +++ b/src/Controllers/iProxyController.cs @@ -12,6 +12,7 @@ namespace iFileProxy.Controllers { static readonly TaskManager taskManager = new (); static Dictionary> IPAccessCountDict = []; + static AppConfig? appConfig = AppConfig.GetCurrConfig(); [HttpPost] [Route("/AddOfflineTask")] @@ -51,9 +52,21 @@ namespace iFileProxy.Controllers { string fileName = ""; var d = taskManager.GetTaskListByIpAddr(HttpContext); - if (d.Where(x => x.TaskId == taskID).Any()) + var taskInfo = taskManager.GetTaskInfo(taskID); + + if ((!appConfig.SecurityOptions.AllowDifferentIPsForDownload && d.Where(x => x.TaskId == taskID).Any()) || taskInfo != null) { - fileName = d.Where(x => x.TaskId == taskID).FirstOrDefault()?.FileName; + if (appConfig.SecurityOptions.AllowDifferentIPsForDownload) + { + fileName = taskInfo[0].FileName; + } + else + { + fileName = d.Where(x => x.TaskId == taskID).FirstOrDefault()?.FileName; + } + if (fileName == null) + return Ok(new CommonRsp() { message = "file not exists", retcode = -1 }); + var filePath = Path.Combine(AppConfig.GetCurrConfig().DownloadOptions.SavePath, fileName); if (!System.IO.File.Exists(filePath)) { diff --git a/src/Helpers/DatabaseHelper.cs b/src/Helpers/DatabaseHelper.cs index 55df5b7..55d3c19 100644 --- a/src/Helpers/DatabaseHelper.cs +++ b/src/Helpers/DatabaseHelper.cs @@ -85,25 +85,23 @@ namespace iFileProxy.Helpers /// /// /// - public static string GetTableData(string sql, MySqlConnection conn) + public static string GetTableData(MySqlCommand sqlCmd) { DataTable dataTable = new(); - - using (MySqlCommand getAllData = new (sql, conn)) - { - using (MySqlDataAdapter adapter = new (getAllData)) + using (MySqlDataAdapter adapter = new (sqlCmd)) adapter.Fill(dataTable); - } return JsonConvert.SerializeObject(dataTable); } public string GetTaskListByIP(string ipAddr) { - string sql = $"SELECT * FROM t_tasks_info WHERE client_ip = \"{ipAddr}\""; + string sql = $"SELECT * FROM t_tasks_info WHERE client_ip = @ip_addr"; MySqlConnection conn = GetAndOpenDBConn("iFileProxy_Db"); try { - return GetTableData(sql, conn); + using MySqlCommand sqlCmd = new (sql,conn); + sqlCmd.Parameters.AddWithValue("@ip_addr", ipAddr); + return GetTableData(sqlCmd); } catch (Exception e) { @@ -112,6 +110,25 @@ namespace iFileProxy.Helpers } finally { conn.Close(); } } + public string GetTaskInfoByTid(string tid) + { + string sql = $"SELECT * FROM t_tasks_info WHERE `tid` =@tid"; + MySqlConnection conn = GetAndOpenDBConn("iFileProxy_Db"); + try + { + using MySqlCommand sqlCmd = new(sql, conn); + sqlCmd.Parameters.AddWithValue("@tid", tid); + return GetTableData(sqlCmd); + } + catch (Exception e) + { + _logger.Error($"无法获取任务信息: {e.Message}"); + throw; + } + finally { conn.Close(); } + } + + public bool InsertTaskData(TaskInfo taskInfo) { diff --git a/src/Services/TaskManager.cs b/src/Services/TaskManager.cs index c82ba76..cc2b3bf 100644 --- a/src/Services/TaskManager.cs +++ b/src/Services/TaskManager.cs @@ -157,6 +157,12 @@ namespace iFileProxy.Services return JsonSerializer.Deserialize>(_dbHelper.GetTaskListByIP(clientIp)) ?? []; } + public List GetTaskInfo(string taskId) + { + _logger.Debug(_dbHelper.GetTaskInfoByTid(taskId)); + return JsonSerializer.Deserialize>(_dbHelper.GetTaskInfoByTid(taskId)) ?? [] ; + } + //public bool DeleteTask(HttpContext c) //{ diff --git a/src/iFileProxy.json b/src/iFileProxy.json index 00d86fd..1cc9a30 100644 --- a/src/iFileProxy.json +++ b/src/iFileProxy.json @@ -34,6 +34,7 @@ "RoutesToTrack": [ // 参加IP请求数统计的路径名单 "/Download", "/AddOfflineTask" - ] + ], + "AllowDifferentIPsForDownload": true // 下载者与任务数据提交者IP不同是否允许下载文件 } } \ No newline at end of file diff --git a/src/wwwroot/index.html b/src/wwwroot/index.html index 3713d02..d8eace0 100644 --- a/src/wwwroot/index.html +++ b/src/wwwroot/index.html @@ -17,15 +17,53 @@ a { text-decoration: none; } + + #loading-mask { + position: absolute; + top: 0; + left: 0; + width: 100%; + height: 100%; + background-color: rgba(255, 255, 255, 0.8); + display: flex; + justify-content: center; + align-items: center; + z-index: 1000; + } + + .loading-spinner { + border: 16px solid #f3f3f3; + /* Light grey */ + border-top: 16px solid #3498db; + /* Blue */ + border-radius: 50%; + width: 120px; + height: 120px; + animation: spin 2s linear infinite; + } + + @keyframes spin { + 0% { + transform: rotate(0deg); + } + + 100% { + transform: rotate(360deg); + } + } +
-
+
+
Github文件下载加速
@@ -58,9 +96,22 @@