增加设置项MaxLoginAttempts(默认3)

This commit is contained in:
2026-02-28 19:39:48 +08:00
parent c5fc081977
commit ec7e0978d3
6 changed files with 64 additions and 5 deletions

View File

@@ -1 +1 @@
2026-02-28 19:13:47 2026-02-28 19:35:58

View File

@@ -1 +1 @@
58df162 c5fc081

View File

@@ -11,7 +11,8 @@ namespace CMLeonOS.Settings
private static Dictionary<string, string> defaultSettings = new Dictionary<string, string> private static Dictionary<string, string> defaultSettings = new Dictionary<string, string>
{ {
{ "LoggerEnabled", "true" } { "LoggerEnabled", "true" },
{ "MaxLoginAttempts", "3" }
}; };
public static bool LoggerEnabled 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() public static void LoadSettings()
{ {
settings.Clear(); settings.Clear();

View File

@@ -1,4 +1,4 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using Sys = Cosmos.System; using Sys = Cosmos.System;

View File

@@ -6,6 +6,7 @@ using System.Text;
using System.Threading; using System.Threading;
using Sys = Cosmos.System; using Sys = Cosmos.System;
using CMLeonOS.Logger; using CMLeonOS.Logger;
using CMLeonOS.Settings;
namespace CMLeonOS namespace CMLeonOS
{ {
@@ -25,6 +26,7 @@ namespace CMLeonOS
public bool fixmode = Kernel.FixMode; public bool fixmode = Kernel.FixMode;
private User currentLoggedInUser; private User currentLoggedInUser;
private static CMLeonOS.Logger.Logger _logger = CMLeonOS.Logger.Logger.Instance; private static CMLeonOS.Logger.Logger _logger = CMLeonOS.Logger.Logger.Instance;
private Dictionary<string, int> loginAttempts = new Dictionary<string, int>();
public User CurrentLoggedInUser public User CurrentLoggedInUser
{ {
@@ -593,13 +595,37 @@ namespace CMLeonOS
if (foundUser.Password != hashedInputPassword) 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); CMLeonOS.UI.TUIHelper.SetColors(global::System.ConsoleColor.Red, global::System.ConsoleColor.Black);
global::System.Console.SetCursorPosition(17, 24); 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); global::System.Threading.Thread.Sleep(1000);
return false; return false;
} }
loginAttempts.Remove(username.ToLower());
CMLeonOS.UI.TUIHelper.SetColors(global::System.ConsoleColor.Green, global::System.ConsoleColor.Black); CMLeonOS.UI.TUIHelper.SetColors(global::System.ConsoleColor.Green, global::System.ConsoleColor.Black);
global::System.Console.SetCursorPosition(17, 24); global::System.Console.SetCursorPosition(17, 24);
global::System.Console.Write("Login successful! "); global::System.Console.Write("Login successful! ");

View File

@@ -58,6 +58,18 @@ namespace CMLeonOS.Commands
Console.WriteLine("Error: LoggerEnabled must be 'true' or 'false'"); 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 else
{ {
SettingsManager.SetSetting(key, value); SettingsManager.SetSetting(key, value);