IW4M-Admin/WebfrontCore/Middleware/IPWhitelist.cs
RaidMax 2260d8974d 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
2019-02-12 20:34:29 -06:00

47 lines
1.4 KiB
C#

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();
}
}
}
}