From 77ebaeccfee868033fb20412673d830b28cbcad3 Mon Sep 17 00:00:00 2001 From: RaidMax Date: Tue, 12 Feb 2019 20:34:29 -0600 Subject: [PATCH] update master to allow IW5 to pass validation include version set on manual parser selection update projects to .NET Core 2.2 add middleware to support ip whitelisting (EnableWebfrontConnectionWhitelist and WebfrontConnectionWhitelist) issue #59 --- Application/Application.csproj | 14 +++--- Application/IW4MServer.cs | 1 + Master/Master.pyproj | 3 +- Master/master/resources/instance.py | 3 +- Master/master/schema/serverschema.py | 4 +- .../IW4ScriptCommands.csproj | 6 +-- Plugins/Login/Login.csproj | 6 +-- .../ProfanityDeterment.csproj | 6 +-- Plugins/Stats/Stats.csproj | 6 +-- Plugins/Tests/Tests.csproj | 6 +-- Plugins/Welcome/Welcome.csproj | 6 +-- .../Configuration/ApplicationConfiguration.cs | 2 + SharedLibraryCore/SharedLibraryCore.csproj | 14 +++--- WebfrontCore/Middleware/IPWhitelist.cs | 46 +++++++++++++++++++ WebfrontCore/Startup.cs | 17 +++++-- WebfrontCore/WebfrontCore.csproj | 24 +++++----- 16 files changed, 113 insertions(+), 51 deletions(-) create mode 100644 WebfrontCore/Middleware/IPWhitelist.cs diff --git a/Application/Application.csproj b/Application/Application.csproj index de1ecd9c..3097038e 100644 --- a/Application/Application.csproj +++ b/Application/Application.csproj @@ -2,11 +2,11 @@ Exe - netcoreapp2.1 - 2.1.5 + netcoreapp2.2 + 2.2.1 false RaidMax.IW4MAdmin.Application - 2.2.4.9 + 2.2.5.0 RaidMax Forever None IW4MAdmin @@ -25,14 +25,14 @@ - + true true - 2.2.4.9 - 2.2.4.9 + 2.2.5.0 + 2.2.5.0 @@ -79,7 +79,7 @@ - + diff --git a/Application/IW4MServer.cs b/Application/IW4MServer.cs index 291ba8b6..e60d79c0 100644 --- a/Application/IW4MServer.cs +++ b/Application/IW4MServer.cs @@ -690,6 +690,7 @@ namespace IW4MAdmin { RconParser = Manager.AdditionalRConParsers.FirstOrDefault(_parser => _parser.Version == version.Value) ?? RconParser; EventParser = Manager.AdditionalEventParsers.FirstOrDefault(_parser => _parser.Version == version.Value) ?? EventParser; + Version = RconParser.Version; } var infoResponse = RconParser.Configuration.CommandPrefixes.RConGetInfo != null ? await this.GetInfoAsync() : null; diff --git a/Master/Master.pyproj b/Master/Master.pyproj index c8f24655..6f867b69 100644 --- a/Master/Master.pyproj +++ b/Master/Master.pyproj @@ -11,7 +11,7 @@ . - Web launcher + Standard Python launcher http://localhost . true @@ -25,6 +25,7 @@ script script + False true diff --git a/Master/master/resources/instance.py b/Master/master/resources/instance.py index e6323137..6864757a 100644 --- a/Master/master/resources/instance.py +++ b/Master/master/resources/instance.py @@ -5,6 +5,7 @@ from marshmallow import ValidationError from master.schema.instanceschema import InstanceSchema from master import ctx import json +from netaddr import IPAddress class Instance(Resource): def get(self, id=None): @@ -23,7 +24,7 @@ class Instance(Resource): def put(self, id): try: for server in request.json['servers']: - if 'ip' not in server or server['ip'] == 'localhost': + if 'ip' not in server or IPAddress(server['ip']).is_private() or IPAddress(server['ip']).is_loopback(): server['ip'] = request.remote_addr if 'version' not in server: server['version'] = 'Unknown' diff --git a/Master/master/schema/serverschema.py b/Master/master/schema/serverschema.py index 27ecf488..aa56347b 100644 --- a/Master/master/schema/serverschema.py +++ b/Master/master/schema/serverschema.py @@ -4,7 +4,7 @@ from master.models.servermodel import ServerModel class ServerSchema(Schema): id = fields.Int( required=True, - validate=validate.Range(1, 25525525525565535, 'invalid id') + validate=validate.Range(0, 25525525525565535, 'invalid id') ) ip = fields.Str( required=True @@ -35,7 +35,7 @@ class ServerSchema(Schema): ) map = fields.String( required=True, - validate=validate.Length(1, 32, 'invalid map name') + validate=validate.Length(0, 64, 'invalid map name') ) gametype = fields.String( required=True, diff --git a/Plugins/IW4ScriptCommands/IW4ScriptCommands.csproj b/Plugins/IW4ScriptCommands/IW4ScriptCommands.csproj index cc9b9a2a..a1add8d0 100644 --- a/Plugins/IW4ScriptCommands/IW4ScriptCommands.csproj +++ b/Plugins/IW4ScriptCommands/IW4ScriptCommands.csproj @@ -2,8 +2,8 @@ Library - netcoreapp2.1 - 2.1.5 + netcoreapp2.2 + 2.2.2 Debug;Release;Prerelease @@ -19,7 +19,7 @@ - + diff --git a/Plugins/Login/Login.csproj b/Plugins/Login/Login.csproj index 5375174f..647aa41d 100644 --- a/Plugins/Login/Login.csproj +++ b/Plugins/Login/Login.csproj @@ -2,8 +2,8 @@ Library - netcoreapp2.1 - 2.1.5 + netcoreapp2.2 + 2.2.2 RaidMax.IW4MAdmin.Plugins.Login @@ -22,7 +22,7 @@ - + diff --git a/Plugins/ProfanityDeterment/ProfanityDeterment.csproj b/Plugins/ProfanityDeterment/ProfanityDeterment.csproj index b0db5bdb..1872099b 100644 --- a/Plugins/ProfanityDeterment/ProfanityDeterment.csproj +++ b/Plugins/ProfanityDeterment/ProfanityDeterment.csproj @@ -2,8 +2,8 @@ Library - netcoreapp2.1 - 2.1.5 + netcoreapp2.2 + 2.2.2 RaidMax.IW4MAdmin.Plugins.ProfanityDeterment @@ -20,7 +20,7 @@ - + diff --git a/Plugins/Stats/Stats.csproj b/Plugins/Stats/Stats.csproj index 9a41b152..84280722 100644 --- a/Plugins/Stats/Stats.csproj +++ b/Plugins/Stats/Stats.csproj @@ -2,8 +2,8 @@ Library - netcoreapp2.1 - 2.1.5 + netcoreapp2.2 + 2.2.2 RaidMax.IW4MAdmin.Plugins.Stats @@ -27,7 +27,7 @@ - + diff --git a/Plugins/Tests/Tests.csproj b/Plugins/Tests/Tests.csproj index 2d1e768b..84255c5d 100644 --- a/Plugins/Tests/Tests.csproj +++ b/Plugins/Tests/Tests.csproj @@ -2,8 +2,8 @@ Library - netcoreapp2.1 - 2.1.5 + netcoreapp2.2 + 2.2.2 @@ -23,7 +23,7 @@ - + diff --git a/Plugins/Welcome/Welcome.csproj b/Plugins/Welcome/Welcome.csproj index 66dac03e..010bc58e 100644 --- a/Plugins/Welcome/Welcome.csproj +++ b/Plugins/Welcome/Welcome.csproj @@ -2,8 +2,8 @@ Library - netcoreapp2.1 - 2.1.5 + netcoreapp2.2 + 2.2.2 RaidMax.IW4MAdmin.Plugins.Welcome @@ -20,7 +20,7 @@ - + diff --git a/SharedLibraryCore/Configuration/ApplicationConfiguration.cs b/SharedLibraryCore/Configuration/ApplicationConfiguration.cs index f1a89bef..1daeb4bd 100644 --- a/SharedLibraryCore/Configuration/ApplicationConfiguration.cs +++ b/SharedLibraryCore/Configuration/ApplicationConfiguration.cs @@ -25,6 +25,8 @@ namespace SharedLibraryCore.Configuration public int RConPollRate { get; set; } = 5000; public bool IgnoreBots { get; set; } public TimeSpan MaximumTempBanTime { get; set; } = new TimeSpan(24 * 30, 0, 0); + public bool EnableWebfrontConnectionWhitelist { get; set; } + public List WebfrontConnectionWhitelist { get; set; } public string Id { get; set; } public List Servers { get; set; } public int AutoMessagePeriod { get; set; } diff --git a/SharedLibraryCore/SharedLibraryCore.csproj b/SharedLibraryCore/SharedLibraryCore.csproj index 39c7d82f..dae6cb63 100644 --- a/SharedLibraryCore/SharedLibraryCore.csproj +++ b/SharedLibraryCore/SharedLibraryCore.csproj @@ -2,8 +2,8 @@ Library - netcoreapp2.1 - 2.1.5 + netcoreapp2.2 + 2.2.1 RaidMax.IW4MAdmin.SharedLibraryCore @@ -21,9 +21,9 @@ - - - + + + all runtime; build; native; contentfiles; analyzers @@ -35,12 +35,12 @@ - + - + diff --git a/WebfrontCore/Middleware/IPWhitelist.cs b/WebfrontCore/Middleware/IPWhitelist.cs new file mode 100644 index 00000000..16b34cb8 --- /dev/null +++ b/WebfrontCore/Middleware/IPWhitelist.cs @@ -0,0 +1,46 @@ +using Microsoft.AspNetCore.Http; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace WebfrontCore.Middleware +{ + /// + /// Defines the middleware functioning to whitelist connection from + /// a set of IP Addresses + /// + internal sealed class IPWhitelist + { + private readonly List whitelistedIps; + private readonly RequestDelegate nextRequest; + + /// + /// constructor + /// + /// + /// + /// list of textual ip addresses + public IPWhitelist(RequestDelegate nextRequest, ILogger logger, List whitelistedIps) + { + this.whitelistedIps = whitelistedIps.Select(_ip => System.Net.IPAddress.Parse(_ip).GetAddressBytes()).ToList(); + this.nextRequest = nextRequest; + } + + public async Task Invoke(HttpContext context) + { + bool isAlllowed = whitelistedIps.Any(_ip => _ip.SequenceEqual(context.Connection.RemoteIpAddress.GetAddressBytes())); + + if (isAlllowed) + { + await nextRequest.Invoke(context); + } + + else + { + context.Abort(); + } + } + } +} diff --git a/WebfrontCore/Startup.cs b/WebfrontCore/Startup.cs index aa0f2491..b216b4f8 100644 --- a/WebfrontCore/Startup.cs +++ b/WebfrontCore/Startup.cs @@ -6,6 +6,8 @@ using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using SharedLibraryCore.Database; +using System.Linq; +using WebfrontCore.Middleware; namespace WebfrontCore { @@ -47,16 +49,20 @@ namespace WebfrontCore options.AccessDeniedPath = "/"; options.LoginPath = "/"; }); + +#if DEBUG + services.AddLogging(_builder => + { + _builder.AddDebug(); + }); +#endif } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { - //loggerFactory.AddConsole(Configuration.GetSection("Logging")); - if (env.IsDevelopment()) { - loggerFactory.AddDebug(); app.UseDeveloperExceptionPage(); } @@ -65,6 +71,11 @@ namespace WebfrontCore app.UseExceptionHandler("/Home/Error"); } + if (Program.Manager.GetApplicationSettings().Configuration().EnableWebfrontConnectionWhitelist) + { + app.UseMiddleware(Program.Manager.GetApplicationSettings().Configuration().WebfrontConnectionWhitelist); + } + app.UseStaticFiles(); app.UseAuthentication(); diff --git a/WebfrontCore/WebfrontCore.csproj b/WebfrontCore/WebfrontCore.csproj index bf92a126..384924a3 100644 --- a/WebfrontCore/WebfrontCore.csproj +++ b/WebfrontCore/WebfrontCore.csproj @@ -1,8 +1,8 @@  - netcoreapp2.1 - 2.1.5 + netcoreapp2.2 + 2.2.1 false false true @@ -56,16 +56,16 @@ - - - - - - - - - - + + + + + + + + + +