mirror of
https://github.com/RaidMax/IW4M-Admin.git
synced 2025-06-10 15:20:48 -05:00
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
This commit is contained in:
46
WebfrontCore/Middleware/IPWhitelist.cs
Normal file
46
WebfrontCore/Middleware/IPWhitelist.cs
Normal file
@ -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
|
||||
{
|
||||
/// <summary>
|
||||
/// Defines the middleware functioning to whitelist connection from
|
||||
/// a set of IP Addresses
|
||||
/// </summary>
|
||||
internal sealed class IPWhitelist
|
||||
{
|
||||
private readonly List<byte[]> whitelistedIps;
|
||||
private readonly RequestDelegate nextRequest;
|
||||
|
||||
/// <summary>
|
||||
/// constructor
|
||||
/// </summary>
|
||||
/// <param name="nextRequest"></param>
|
||||
/// <param name="logger"></param>
|
||||
/// <param name="whitelistedIps">list of textual ip addresses</param>
|
||||
public IPWhitelist(RequestDelegate nextRequest, ILogger<IPWhitelist> logger, List<string> 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user