From 08c883e0ff247e0f4dea43b6637abda1327e59f1 Mon Sep 17 00:00:00 2001 From: RaidMax Date: Thu, 3 Jan 2019 14:39:22 -0600 Subject: [PATCH] fix duplicate bot welcomes fix prompt bool incorrect default value rename GameEvent.Remote to GameEvent.IsRemote include NetworkId in webfront claims fix non descript error message appearing when something fails and localization is not initialized --- Application/Application.csproj | 6 +-- Application/IW4MServer.cs | 2 +- Application/Localization/Configure.cs | 2 +- Application/Main.cs | 20 ++++++++-- Application/Manager.cs | 3 +- Plugins/Login/Plugin.cs | 3 +- SharedLibraryCore/Events/GameEvent.cs | 2 +- .../Helpers/BaseConfigurationHandler.cs | 3 +- SharedLibraryCore/Services/ClientService.cs | 10 +---- SharedLibraryCore/Utilities.cs | 13 ++++--- WebfrontCore/Controllers/AccountController.cs | 3 +- WebfrontCore/Controllers/BaseController.cs | 38 +++++++++++-------- WebfrontCore/Controllers/ConsoleController.cs | 3 +- 13 files changed, 62 insertions(+), 46 deletions(-) diff --git a/Application/Application.csproj b/Application/Application.csproj index e328741d7..38ee0c4a1 100644 --- a/Application/Application.csproj +++ b/Application/Application.csproj @@ -6,7 +6,7 @@ 2.1.5 false RaidMax.IW4MAdmin.Application - 2.2.3.1 + 2.2.3.2 RaidMax Forever None IW4MAdmin @@ -31,8 +31,8 @@ true true - 2.2.3.1 - 2.2.3.1 + 2.2.3.2 + 2.2.3.2 diff --git a/Application/IW4MServer.cs b/Application/IW4MServer.cs index e03046b16..86c205ca2 100644 --- a/Application/IW4MServer.cs +++ b/Application/IW4MServer.cs @@ -449,7 +449,7 @@ namespace IW4MAdmin client.Score = origin.Score; // update their IP if it hasn't been set yet - if (client.IPAddress == null) + if (client.IPAddress == null && !client.IsBot) { return client.OnJoin(origin.IPAddress); } diff --git a/Application/Localization/Configure.cs b/Application/Localization/Configure.cs index 2219e6142..bf6a36d81 100644 --- a/Application/Localization/Configure.cs +++ b/Application/Localization/Configure.cs @@ -12,7 +12,7 @@ namespace IW4MAdmin.Application.Localization { public class Configure { - public static void Initialize(string customLocale) + public static void Initialize(string customLocale = null) { string currentLocale = string.IsNullOrEmpty(customLocale) ? CultureInfo.CurrentCulture.Name : customLocale; string[] localizationFiles = Directory.GetFiles(Path.Join(Utilities.OperatingDirectory, "Localization"), $"*.{currentLocale}.json"); diff --git a/Application/Main.cs b/Application/Main.cs index 2c0a06238..7b08fc0a7 100644 --- a/Application/Main.cs +++ b/Application/Main.cs @@ -10,6 +10,7 @@ using System.Text; using System.Threading; using SharedLibraryCore.Localization; using IW4MAdmin.Application.Migration; +using SharedLibraryCore.Exceptions; namespace IW4MAdmin.Application { @@ -38,7 +39,15 @@ namespace IW4MAdmin.Application try { ServerManager = ApplicationManager.GetInstance(); - Localization.Configure.Initialize(ServerManager.GetApplicationSettings().Configuration()?.CustomLocale); + try + { + Localization.Configure.Initialize(ServerManager.GetApplicationSettings().Configuration().CustomLocale); + } + + catch (ServerException) + { + Localization.Configure.Initialize(); + } loc = Utilities.CurrentLocalization.LocalizationIndex; Console.CancelKeyPress += new ConsoleCancelEventHandler(OnCancelKey); @@ -107,7 +116,7 @@ namespace IW4MAdmin.Application var consoleTask = Task.Run(async () => { - String userInput; + string userInput; var Origin = Utilities.IW4MAdminClient(ServerManager.Servers[0]); do @@ -144,13 +153,16 @@ namespace IW4MAdmin.Application catch (Exception e) { - Console.WriteLine(loc["MANAGER_INIT_FAIL"]); + string failMessage = loc == null ? "Failed to initalize IW4MAdmin" : loc["MANAGER_INIT_FAIL"]; + string exitMessage = loc == null ? "Press any key to exit..." : loc["MANAGER_EXIT"]; + + Console.WriteLine(failMessage); while (e.InnerException != null) { e = e.InnerException; } Console.WriteLine(e.Message); - Console.WriteLine(loc["MANAGER_EXIT"]); + Console.WriteLine(exitMessage); Console.ReadKey(); return; } diff --git a/Application/Manager.cs b/Application/Manager.cs index 8b252583e..81dcf08f9 100644 --- a/Application/Manager.cs +++ b/Application/Manager.cs @@ -204,7 +204,8 @@ namespace IW4MAdmin.Application Running = true; #region DATABASE - using (var db = new DatabaseContext(GetApplicationSettings().Configuration()?.ConnectionString, GetApplicationSettings().Configuration()?.DatabaseProvider)) + using (var db = new DatabaseContext(GetApplicationSettings().Configuration()?.ConnectionString, + GetApplicationSettings().Configuration()?.DatabaseProvider)) { await new ContextSeed(db).Seed(); } diff --git a/Plugins/Login/Plugin.cs b/Plugins/Login/Plugin.cs index a1f737e6e..d2deff4c1 100644 --- a/Plugins/Login/Plugin.cs +++ b/Plugins/Login/Plugin.cs @@ -6,7 +6,6 @@ using SharedLibraryCore.Configuration; using SharedLibraryCore.Database.Models; using SharedLibraryCore.Exceptions; using SharedLibraryCore.Interfaces; -using SharedLibraryCore.Objects; namespace IW4MAdmin.Plugins.Login { @@ -23,7 +22,7 @@ namespace IW4MAdmin.Plugins.Login public Task OnEventAsync(GameEvent E, Server S) { - if (E.Remote || Config.RequirePrivilegedClientLogin == false) + if (E.IsRemote || Config.RequirePrivilegedClientLogin == false) return Task.CompletedTask; if (E.Type == GameEvent.EventType.Connect) diff --git a/SharedLibraryCore/Events/GameEvent.cs b/SharedLibraryCore/Events/GameEvent.cs index 4b1a779d9..f068336d1 100644 --- a/SharedLibraryCore/Events/GameEvent.cs +++ b/SharedLibraryCore/Events/GameEvent.cs @@ -188,7 +188,7 @@ namespace SharedLibraryCore public EFClient Origin; public EFClient Target; public Server Owner; - public Boolean Remote = false; + public bool IsRemote { get; set; } = false; public object Extra { get; set; } public ManualResetEventSlim OnProcessed { get; set; } public DateTime Time { get; set; } diff --git a/SharedLibraryCore/Helpers/BaseConfigurationHandler.cs b/SharedLibraryCore/Helpers/BaseConfigurationHandler.cs index ac6289ae5..29656a2a7 100644 --- a/SharedLibraryCore/Helpers/BaseConfigurationHandler.cs +++ b/SharedLibraryCore/Helpers/BaseConfigurationHandler.cs @@ -1,5 +1,6 @@ using Microsoft.Extensions.Configuration; using Newtonsoft.Json; +using SharedLibraryCore.Exceptions; using SharedLibraryCore.Interfaces; using System; using System.Collections.Generic; @@ -40,7 +41,7 @@ namespace SharedLibraryCore.Configuration return File.WriteAllTextAsync(Path.Join(Utilities.OperatingDirectory, "Configuration", $"{Filename}.json"), appConfigJSON); } - public T Configuration() => _configuration; + public T Configuration() => _configuration == null ? throw new ServerException("Configuration is null") : _configuration; public void Set(T config) { diff --git a/SharedLibraryCore/Services/ClientService.cs b/SharedLibraryCore/Services/ClientService.cs index 67214d0bf..8c904b4c0 100644 --- a/SharedLibraryCore/Services/ClientService.cs +++ b/SharedLibraryCore/Services/ClientService.cs @@ -53,13 +53,6 @@ namespace SharedLibraryCore.Services private async Task UpdateAlias(string name, int? ip, EFClient entity, DatabaseContext context) { // entity is the tracked db context item - // todo: move this out -#if DEBUG == false - if (entity.IsBot) - { - return; - } -#endif // get all aliases by IP address and LinkId var iqAliases = context.Aliases .Include(a => a.Link) @@ -377,7 +370,8 @@ namespace SharedLibraryCore.Services ClientId = client.ClientId, Level = client.Level, Password = client.Password, - PasswordSalt = client.PasswordSalt + PasswordSalt = client.PasswordSalt, + NetworkId = client.NetworkId }; #if DEBUG == true diff --git a/SharedLibraryCore/Utilities.cs b/SharedLibraryCore/Utilities.cs index 651ec1048..92626e9cf 100644 --- a/SharedLibraryCore/Utilities.cs +++ b/SharedLibraryCore/Utilities.cs @@ -212,7 +212,7 @@ namespace SharedLibraryCore /// /// Shorthand gametype reported from server /// - public static String GetLocalizedGametype(String input) + public static string GetLocalizedGametype(String input) { switch (input) { @@ -284,7 +284,7 @@ namespace SharedLibraryCore public static int? ConvertToIP(this string str) { bool success = System.Net.IPAddress.TryParse(str, out System.Net.IPAddress ip); - return success && ip.GetAddressBytes().Count(_byte => _byte == 0) != 4 ? + return success && ip.GetAddressBytes().Count(_byte => _byte == 0) != 4 ? (int?)BitConverter.ToInt32(ip.GetAddressBytes(), 0) : null; } @@ -294,12 +294,12 @@ namespace SharedLibraryCore return !ip.HasValue ? "" : new System.Net.IPAddress(BitConverter.GetBytes(ip.Value)).ToString(); } - public static String GetTimePassed(DateTime start) + public static string GetTimePassed(DateTime start) { return GetTimePassed(start, true); } - public static String GetTimePassed(DateTime start, bool includeAgo) + public static string GetTimePassed(DateTime start, bool includeAgo) { TimeSpan Elapsed = DateTime.UtcNow - start; string ago = includeAgo ? $" {CurrentLocalization.LocalizationIndex["WEBFRONT_PENALTY_TEMPLATE_AGO"]}" : ""; @@ -480,10 +480,11 @@ namespace SharedLibraryCore /// description of the question's value /// default value to set if no input is entered /// - public static bool PromptBool(string question, string description = null, char? defaultValue = 'y') + public static bool PromptBool(string question, string description = null, bool defaultValue = true) { Console.Write($"{question}?{(string.IsNullOrEmpty(description) ? "" : $" ({description}) ")}[y/n]: "); - return (Console.ReadLine().ToLower().FirstOrDefault() as char? ?? defaultValue) == 'y'; + char response = Console.ReadLine().ToLower().FirstOrDefault(); + return response != 0 ? response == 'y' : defaultValue; } /// diff --git a/WebfrontCore/Controllers/AccountController.cs b/WebfrontCore/Controllers/AccountController.cs index 7cc598e0e..9465f6586 100644 --- a/WebfrontCore/Controllers/AccountController.cs +++ b/WebfrontCore/Controllers/AccountController.cs @@ -28,7 +28,8 @@ namespace WebfrontCore.Controllers { new Claim(ClaimTypes.NameIdentifier, client.Name), new Claim(ClaimTypes.Role, client.Level.ToString()), - new Claim(ClaimTypes.Sid, client.ClientId.ToString()) + new Claim(ClaimTypes.Sid, client.ClientId.ToString()), + new Claim(ClaimTypes.PrimarySid, client.NetworkId.ToString()) }; var claimsIdentity = new ClaimsIdentity(claims, "login"); diff --git a/WebfrontCore/Controllers/BaseController.cs b/WebfrontCore/Controllers/BaseController.cs index b3c435b88..02225beb1 100644 --- a/WebfrontCore/Controllers/BaseController.cs +++ b/WebfrontCore/Controllers/BaseController.cs @@ -1,14 +1,14 @@ -using System; -using System.Linq; -using System.Security.Claims; -using Microsoft.AspNetCore.Authentication; +using Microsoft.AspNetCore.Authentication; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Filters; - +using SharedLibraryCore; using SharedLibraryCore.Database; using SharedLibraryCore.Database.Models; using SharedLibraryCore.Interfaces; -using SharedLibraryCore.Objects; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Security.Claims; using WebfrontCore.ViewModels; namespace WebfrontCore.Controllers @@ -23,14 +23,19 @@ namespace WebfrontCore.Controllers private static readonly byte[] LocalHost = { 127, 0, 0, 1 }; private static string SocialLink; private static string SocialTitle; + protected List Pages; public BaseController() { if (Manager == null) + { Manager = Program.Manager; + } if (Localization == null) - Localization = SharedLibraryCore.Utilities.CurrentLocalization.LocalizationIndex; + { + Localization = Utilities.CurrentLocalization.LocalizationIndex; + } if (Manager.GetApplicationSettings().Configuration().EnableSocialLink && SocialLink == null) { @@ -38,6 +43,13 @@ namespace WebfrontCore.Controllers SocialTitle = Manager.GetApplicationSettings().Configuration().SocialLinkTitle; } + Pages = Manager.GetPageList().Pages + .Select(page => new Page + { + Name = page.Key, + Location = page.Value + }).ToList(); + ViewBag.Version = Manager.Version; } @@ -55,6 +67,7 @@ namespace WebfrontCore.Controllers try { Client.ClientId = Convert.ToInt32(base.User.Claims.First(c => c.Type == ClaimTypes.Sid).Value); + Client.NetworkId = User.Claims.First(_claim => _claim.Type == ClaimTypes.PrimarySid).Value.ConvertLong(); Client.Level = (EFClient.Permission)Enum.Parse(typeof(EFClient.Permission), User.Claims.First(c => c.Type == ClaimTypes.Role).Value); Client.CurrentAlias = new EFAlias() { Name = User.Claims.First(c => c.Type == ClaimTypes.NameIdentifier).Value }; } @@ -82,18 +95,11 @@ namespace WebfrontCore.Controllers Authorized = Client.ClientId >= 0; ViewBag.Authorized = Authorized; - ViewBag.Url = Manager.GetApplicationSettings().Configuration().WebfrontBindUrl; + ViewBag.Url = Manager.GetApplicationSettings().Configuration().WebfrontUrl; ViewBag.User = Client; ViewBag.SocialLink = SocialLink ?? ""; ViewBag.SocialTitle = SocialTitle; - - // grab the pages from plugins - ViewBag.Pages = Manager.GetPageList().Pages - .Select(page => new Page - { - Name = page.Key, - Location = page.Value - }).ToList(); + ViewBag.Pages = Pages; base.OnActionExecuting(context); } diff --git a/WebfrontCore/Controllers/ConsoleController.cs b/WebfrontCore/Controllers/ConsoleController.cs index 4f7fd2064..e85dacd0b 100644 --- a/WebfrontCore/Controllers/ConsoleController.cs +++ b/WebfrontCore/Controllers/ConsoleController.cs @@ -33,6 +33,7 @@ namespace WebfrontCore.Controllers { ClientId = Client.ClientId, Level = Client.Level, + NetworkId = Client.NetworkId, CurrentServer = server, CurrentAlias = new EFAlias() { @@ -46,7 +47,7 @@ namespace WebfrontCore.Controllers Data = command, Origin = client, Owner = server, - Remote = true + IsRemote = true }; Manager.GetEventHandler().AddEvent(remoteEvent);