From ec7e0978d30ccddc920108037754f5c71dcd47ef Mon Sep 17 00:00:00 2001 From: Leonmmcoset Date: Sat, 28 Feb 2026 19:39:48 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E8=AE=BE=E7=BD=AE=E9=A1=B9Ma?= =?UTF-8?q?xLoginAttempts=EF=BC=88=E9=BB=98=E8=AE=A43=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- BuildTime.txt | 2 +- GitCommit.txt | 2 +- Settings/Settings.cs | 23 ++++++++++++++++++- System/Process.cs | 2 +- System/UserSystem.cs | 28 +++++++++++++++++++++++- shell/Commands/System/SettingsCommand.cs | 12 ++++++++++ 6 files changed, 64 insertions(+), 5 deletions(-) diff --git a/BuildTime.txt b/BuildTime.txt index e206e17..738e3e0 100644 --- a/BuildTime.txt +++ b/BuildTime.txt @@ -1 +1 @@ -2026-02-28 19:13:47 \ No newline at end of file +2026-02-28 19:35:58 \ No newline at end of file diff --git a/GitCommit.txt b/GitCommit.txt index 1ecd060..02e284d 100644 --- a/GitCommit.txt +++ b/GitCommit.txt @@ -1 +1 @@ -58df162 \ No newline at end of file +c5fc081 \ No newline at end of file diff --git a/Settings/Settings.cs b/Settings/Settings.cs index fc409fa..b94db6e 100644 --- a/Settings/Settings.cs +++ b/Settings/Settings.cs @@ -11,7 +11,8 @@ namespace CMLeonOS.Settings private static Dictionary defaultSettings = new Dictionary { - { "LoggerEnabled", "true" } + { "LoggerEnabled", "true" }, + { "MaxLoginAttempts", "3" } }; public static bool LoggerEnabled @@ -31,6 +32,26 @@ namespace CMLeonOS.Settings } } + public static int MaxLoginAttempts + { + get + { + if (settings.TryGetValue("MaxLoginAttempts", out string value)) + { + if (int.TryParse(value, out int result)) + { + return result; + } + } + return 3; + } + set + { + settings["MaxLoginAttempts"] = value.ToString(); + SaveSettings(); + } + } + public static void LoadSettings() { settings.Clear(); diff --git a/System/Process.cs b/System/Process.cs index fe75121..1f6e2eb 100644 --- a/System/Process.cs +++ b/System/Process.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using Sys = Cosmos.System; diff --git a/System/UserSystem.cs b/System/UserSystem.cs index 197d5a1..beba310 100644 --- a/System/UserSystem.cs +++ b/System/UserSystem.cs @@ -6,6 +6,7 @@ using System.Text; using System.Threading; using Sys = Cosmos.System; using CMLeonOS.Logger; +using CMLeonOS.Settings; namespace CMLeonOS { @@ -25,6 +26,7 @@ namespace CMLeonOS public bool fixmode = Kernel.FixMode; private User currentLoggedInUser; private static CMLeonOS.Logger.Logger _logger = CMLeonOS.Logger.Logger.Instance; + private Dictionary loginAttempts = new Dictionary(); public User CurrentLoggedInUser { @@ -593,13 +595,37 @@ namespace CMLeonOS if (foundUser.Password != hashedInputPassword) { + string usernameLower = username.ToLower(); + + if (!loginAttempts.ContainsKey(usernameLower)) + { + loginAttempts[usernameLower] = 0; + } + + loginAttempts[usernameLower]++; + int attempts = loginAttempts[usernameLower]; + int maxAttempts = Settings.SettingsManager.MaxLoginAttempts; + + if (attempts >= maxAttempts) + { + CMLeonOS.UI.TUIHelper.SetColors(global::System.ConsoleColor.Red, global::System.ConsoleColor.Black); + global::System.Console.SetCursorPosition(17, 24); + global::System.Console.Write($"Maximum login attempts ({maxAttempts}) reached. "); + global::System.Console.SetCursorPosition(17, 25); + global::System.Console.Write("Please restart to enter password again. "); + global::System.Threading.Thread.Sleep(3000); + Sys.Power.Reboot(); + } + CMLeonOS.UI.TUIHelper.SetColors(global::System.ConsoleColor.Red, global::System.ConsoleColor.Black); global::System.Console.SetCursorPosition(17, 24); - global::System.Console.Write("Invalid password. "); + global::System.Console.Write($"Invalid password. Attempts: {attempts}/{maxAttempts} "); global::System.Threading.Thread.Sleep(1000); return false; } + loginAttempts.Remove(username.ToLower()); + CMLeonOS.UI.TUIHelper.SetColors(global::System.ConsoleColor.Green, global::System.ConsoleColor.Black); global::System.Console.SetCursorPosition(17, 24); global::System.Console.Write("Login successful! "); diff --git a/shell/Commands/System/SettingsCommand.cs b/shell/Commands/System/SettingsCommand.cs index f9ec4bc..31d8a86 100644 --- a/shell/Commands/System/SettingsCommand.cs +++ b/shell/Commands/System/SettingsCommand.cs @@ -58,6 +58,18 @@ namespace CMLeonOS.Commands Console.WriteLine("Error: LoggerEnabled must be 'true' or 'false'"); } } + else if (key.ToLower() == "maxloginattempts") + { + if (int.TryParse(value, out int attempts) && attempts > 0) + { + SettingsManager.MaxLoginAttempts = attempts; + Console.WriteLine($"MaxLoginAttempts set to {attempts}"); + } + else + { + Console.WriteLine("Error: MaxLoginAttempts must be a positive integer"); + } + } else { SettingsManager.SetSetting(key, value);