From ec994d51be975daf9bc590a3f4fddf9bed445d61 Mon Sep 17 00:00:00 2001 From: RaidMax Date: Mon, 13 Jan 2020 20:06:57 -0600 Subject: [PATCH 1/2] fix restart command (thanks .net upgrade) reworking a little bit of stuff to allow depedency injection to start creeping in... it's coming --- Application/ApplicationManager.cs | 32 ++++++------------- Application/Main.cs | 24 +++++++++++---- Application/Misc/MiddlewareActionHandler.cs | 34 ++++++++++++++++++--- Plugins/Tests/ManagerFixture.cs | 2 +- 4 files changed, 57 insertions(+), 35 deletions(-) diff --git a/Application/ApplicationManager.cs b/Application/ApplicationManager.cs index 7a1167d96..5d1973eea 100644 --- a/Application/ApplicationManager.cs +++ b/Application/ApplicationManager.cs @@ -42,8 +42,7 @@ namespace IW4MAdmin.Application public CancellationToken CancellationToken => _tokenSource.Token; public string ExternalIPAddress { get; private set; } public bool IsRestartRequested { get; private set; } - public IMiddlewareActionHandler MiddlewareActionHandler { get; private set; } = new MiddlewareActionHandler(); - static ApplicationManager Instance; + public IMiddlewareActionHandler MiddlewareActionHandler { get; } private readonly List Commands; private readonly List MessageTokens; private readonly ClientService ClientSvc; @@ -52,14 +51,15 @@ namespace IW4MAdmin.Application public BaseConfigurationHandler ConfigHandler; GameEventHandler Handler; readonly IPageList PageList; - readonly Dictionary Loggers = new Dictionary(); + private readonly Dictionary _loggers = new Dictionary(); private readonly MetaService _metaService; private readonly TimeSpan _throttleTimeout = new TimeSpan(0, 1, 0); private readonly CancellationTokenSource _tokenSource; private readonly Dictionary> _operationLookup = new Dictionary>(); - private ApplicationManager() + public ApplicationManager(ILogger logger, IMiddlewareActionHandler actionHandler) { + MiddlewareActionHandler = actionHandler; _servers = new ConcurrentBag(); Commands = new List(); MessageTokens = new List(); @@ -74,6 +74,7 @@ namespace IW4MAdmin.Application TokenAuthenticator = new TokenAuthentication(); _metaService = new MetaService(); _tokenSource = new CancellationTokenSource(); + _loggers.Add(0, logger); } public async Task ExecuteEvent(GameEvent newEvent) @@ -156,11 +157,6 @@ namespace IW4MAdmin.Application return Commands; } - public static ApplicationManager GetInstance() - { - return Instance ?? (Instance = new ApplicationManager()); - } - public async Task UpdateServerStates() { // store the server hash code and task for it @@ -681,7 +677,6 @@ namespace IW4MAdmin.Application { _tokenSource.Cancel(); Running = false; - Instance = null; } public void Restart() @@ -692,25 +687,16 @@ namespace IW4MAdmin.Application public ILogger GetLogger(long serverId) { - if (Loggers.ContainsKey(serverId)) + if (_loggers.ContainsKey(serverId)) { - return Loggers[serverId]; + return _loggers[serverId]; } else { - Logger newLogger; + var newLogger = new Logger($"IW4MAdmin-Server-{serverId}"); - if (serverId == 0) - { - newLogger = new Logger("IW4MAdmin-Manager"); - } - else - { - newLogger = new Logger($"IW4MAdmin-Server-{serverId}"); - } - - Loggers.Add(serverId, newLogger); + _loggers.Add(serverId, newLogger); return newLogger; } } diff --git a/Application/Main.cs b/Application/Main.cs index eda7c6fbc..0c1b31af5 100644 --- a/Application/Main.cs +++ b/Application/Main.cs @@ -1,4 +1,5 @@ using IW4MAdmin.Application.Migration; +using IW4MAdmin.Application.Misc; using Microsoft.Extensions.DependencyInjection; using SharedLibraryCore; using SharedLibraryCore.Helpers; @@ -60,7 +61,13 @@ namespace IW4MAdmin.Application restart: try { - ServerManager = ApplicationManager.GetInstance(); + var services = ConfigureServices(); + + using (var builder = services.BuildServiceProvider()) + { + ServerManager = (ApplicationManager)builder.GetRequiredService(); + } + var configuration = ServerManager.GetApplicationSettings().Configuration(); Localization.Configure.Initialize(configuration?.EnableCustomLocale ?? false ? (configuration.CustomLocale ?? "en-US") : "en-US"); @@ -70,7 +77,7 @@ namespace IW4MAdmin.Application ServerManager.Logger.WriteInfo(Utilities.CurrentLocalization.LocalizationIndex["MANAGER_VERSION"].FormatExt(Version)); - ConfigureServices(); + await CheckVersion(); await ServerManager.Init(); } @@ -235,12 +242,17 @@ namespace IW4MAdmin.Application { } } - private static void ConfigureServices() + /// + /// Configures the dependency injection services + /// + private static IServiceCollection ConfigureServices() { var serviceProvider = new ServiceCollection(); - serviceProvider.AddSingleton(ServerManager); - var builder = serviceProvider.BuildServiceProvider(); - builder.Dispose(); + serviceProvider.AddSingleton() + .AddSingleton(_serviceProvider => new Logger("IW4MAdmin-Manager")) + .AddSingleton(); + + return serviceProvider; } } } diff --git a/Application/Misc/MiddlewareActionHandler.cs b/Application/Misc/MiddlewareActionHandler.cs index 8b5a12ada..b038f23ad 100644 --- a/Application/Misc/MiddlewareActionHandler.cs +++ b/Application/Misc/MiddlewareActionHandler.cs @@ -1,15 +1,29 @@ -using SharedLibraryCore.Interfaces; +using SharedLibraryCore; +using SharedLibraryCore.Interfaces; using System; using System.Collections.Generic; -using System.Text; using System.Threading.Tasks; namespace IW4MAdmin.Application.Misc { class MiddlewareActionHandler : IMiddlewareActionHandler { - private static readonly IDictionary> _actions = new Dictionary>(); + private readonly IDictionary> _actions; + private readonly ILogger _logger; + public MiddlewareActionHandler(ILogger logger) + { + _actions = new Dictionary>(); + _logger = logger; + } + + /// + /// Executes the action with the given name + /// + /// Execution return type + /// Input value + /// Name of action to execute + /// public async Task Execute(T value, string name = null) { string key = string.IsNullOrEmpty(name) ? typeof(T).ToString() : name; @@ -22,8 +36,11 @@ namespace IW4MAdmin.Application.Misc { value = await ((IMiddlewareAction)action).Invoke(value); } - // todo: probably log this somewhere - catch { } + catch (Exception e) + { + _logger.WriteWarning($"Failed to invoke middleware action {name}"); + _logger.WriteDebug(e.GetExceptionInfo()); + } } return value; @@ -32,6 +49,13 @@ namespace IW4MAdmin.Application.Misc return value; } + /// + /// Registers an action by name + /// + /// + /// Action type specifier + /// Action to perform + /// Name of action public void Register(T actionType, IMiddlewareAction action, string name = null) { string key = string.IsNullOrEmpty(name) ? typeof(T).ToString() : name; diff --git a/Plugins/Tests/ManagerFixture.cs b/Plugins/Tests/ManagerFixture.cs index 824e46df2..353ce3f7c 100644 --- a/Plugins/Tests/ManagerFixture.cs +++ b/Plugins/Tests/ManagerFixture.cs @@ -20,7 +20,7 @@ namespace Tests File.WriteAllText(logFile, Environment.NewLine); - Manager = ApplicationManager.GetInstance(); + Manager = null; var config = new ApplicationConfiguration { From 943808562fdd921339a1740b024b9fbd3f6a2092 Mon Sep 17 00:00:00 2001 From: RaidMax Date: Tue, 14 Jan 2020 18:56:23 -0600 Subject: [PATCH 2/2] fix error code page for things over than 404s allow request token when not logged in --- Plugins/Login/Plugin.cs | 6 +++++- Plugins/Stats/Cheat/Thresholds.cs | 4 ++-- WebfrontCore/Startup.cs | 13 ++++++++++++- 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/Plugins/Login/Plugin.cs b/Plugins/Login/Plugin.cs index f5e268a03..91fe6c360 100644 --- a/Plugins/Login/Plugin.cs +++ b/Plugins/Login/Plugin.cs @@ -2,6 +2,7 @@ using System.Reflection; using System.Threading.Tasks; using SharedLibraryCore; +using SharedLibraryCore.Commands; using SharedLibraryCore.Configuration; using SharedLibraryCore.Database.Models; using SharedLibraryCore.Exceptions; @@ -42,13 +43,16 @@ namespace IW4MAdmin.Plugins.Login E.Origin.Level == EFClient.Permission.Console) return Task.CompletedTask; - if (((Command)E.Extra).Name == new SharedLibraryCore.Commands.CSetPassword().Name && + if (((Command)E.Extra).Name == new CSetPassword().Name && E.Origin?.Password == null) return Task.CompletedTask; if (((Command)E.Extra).Name == new Commands.CLogin().Name) return Task.CompletedTask; + if (E.Extra.GetType() == typeof(RequestTokenCommand)) + return Task.CompletedTask; + if (!AuthorizedClients[E.Origin.ClientId]) { throw new AuthorizationException(Utilities.CurrentLocalization.LocalizationIndex["PLUGINS_LOGIN_AUTH"]); diff --git a/Plugins/Stats/Cheat/Thresholds.cs b/Plugins/Stats/Cheat/Thresholds.cs index 8ef5df298..37f2c9601 100644 --- a/Plugins/Stats/Cheat/Thresholds.cs +++ b/Plugins/Stats/Cheat/Thresholds.cs @@ -48,8 +48,8 @@ namespace IW4MAdmin.Plugins.Stats.Cheat public const int HighSampleMinKills = 100; public const double KillTimeThreshold = 0.2; public const int LowSampleMinKillsRecoil = 5; - public const double SnapFlagValue = 5.5; - public const double SnapBanValue = 8.7; + public const double SnapFlagValue = 7.6; + public const double SnapBanValue = 11.7; public const double MaxStrainBan = 0.9; diff --git a/WebfrontCore/Startup.cs b/WebfrontCore/Startup.cs index 04e769815..42cc5cfba 100644 --- a/WebfrontCore/Startup.cs +++ b/WebfrontCore/Startup.cs @@ -1,6 +1,7 @@ using Microsoft.AspNetCore.Authentication.Cookies; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc.ApplicationParts; using Microsoft.AspNetCore.Mvc.Razor; using Microsoft.Extensions.Configuration; @@ -8,6 +9,8 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using SharedLibraryCore.Database; using SharedLibraryCore.Interfaces; +using System.Net; +using System.Threading.Tasks; using WebfrontCore.Middleware; namespace WebfrontCore @@ -95,7 +98,15 @@ namespace WebfrontCore // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory) { - app.UseStatusCodePagesWithRedirects("/Home/ResponseStatusCode?statusCode={0}"); + app.UseStatusCodePages(_context => + { + if (_context.HttpContext.Response.StatusCode == (int)HttpStatusCode.NotFound) + { + _context.HttpContext.Response.Redirect($"/Home/ResponseStatusCode?statusCode={_context.HttpContext.Response.StatusCode}"); + } + + return Task.CompletedTask; + }); if (env.EnvironmentName == "Development") {