Basic filters, refactorization.

This commit is contained in:
femsci 2023-03-27 15:39:09 +02:00
parent 7b95dffd11
commit 2f87a95828
Signed by: femsci
GPG key ID: 08F7911F0E650C67
17 changed files with 287 additions and 113 deletions

View file

@ -0,0 +1,17 @@
namespace Nyangate.Middleware;
public class Authenticator
{
private readonly RequestDelegate _next;
public Authenticator(RequestDelegate next)
{
_next = next;
}
public async Task Invoke(HttpContext ctx)
{
//do smth
await _next.Invoke(ctx);
}
}

View file

@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Nyangate.Middleware;
public class Filter
{
private readonly RequestDelegate _next;
public Filter(RequestDelegate next)
{
this._next = next;
}
public async Task Invoke(HttpContext ctx)
{
//do smth
await _next.Invoke(ctx);
}
}

View file

@ -0,0 +1,17 @@
namespace Nyangate.Middleware;
public class IDSFilter
{
private readonly RequestDelegate _next;
public IDSFilter(RequestDelegate next)
{
_next = next;
}
public async Task Invoke(HttpContext ctx)
{
//do smth
await _next.Invoke(ctx);
}
}

View file

@ -0,0 +1,17 @@
namespace Nyangate.Middleware;
public class IPFilter
{
private readonly RequestDelegate _next;
public IPFilter(RequestDelegate next)
{
_next = next;
}
public async Task Invoke(HttpContext ctx)
{
//do smth
await _next.Invoke(ctx);
}
}

View file

@ -0,0 +1,25 @@
using Nyangate.Services;
namespace Nyangate.Middleware
{
public class Router
{
private readonly RequestDelegate _next;
private readonly RoutingMap _map;
public Router(RequestDelegate next, RoutingMap map)
{
_next = next;
_map = map;
}
public async Task Invoke(HttpContext ctx)
{
string host = ctx.Request.Host.Host;
//check ctx.Request.IsHttps if strict https required
Console.WriteLine($"meow: {ctx.Connection.RemoteIpAddress}");
ctx.Response.StatusCode = 200;
await _next.Invoke(ctx);
}
}
}

View file

@ -1,6 +1,34 @@
using Microsoft.AspNetCore.Components.Routing;
using Nyangate;
using Nyangate.Middleware;
using Nyangate.Services;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddSingleton<ServerCertificateRegistry>();
builder.Services.AddSingleton<RoutingMap>();
//TODO: Replace with DI
var certs = new ServerCertificateRegistry();
builder.WebHost.UseKestrel(o =>
{
o.AddServerHeader = false;
o.ConfigureHttpsDefaults(https =>
{
https.ServerCertificateSelector = (_, name) =>
name is not null ? certs.GetOrDefault(name) : certs.Default();
});
o.ConfigureEndpointDefaults(ep =>
{
ep.UseConnectionLogging();
});
});
var app = builder.Build();
app.MapGet("/", () => "meow~");
app.UseMiddleware<Authenticator>();
app.UseMiddleware<Filter>();
app.UseMiddleware<Nyangate.Middleware.Router>();
app.Run();

View file

@ -0,0 +1,11 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Nyangate.Services;
public class BlacklistRegistry
{
}

View file

@ -0,0 +1,11 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Nyangate.Services;
public class RoutingMap
{
}

View file

@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Threading.Tasks;
namespace Nyangate.Services;
public class ServerCertificateRegistry
{
private readonly Dictionary<string, X509Certificate2> _registry = new();
public X509Certificate2 GetOrDefault(string hostname)
{
return _registry.GetValueOrDefault(hostname, Default());
}
public X509Certificate2 Default()
{
throw new NotImplementedException();
}
}

View file

@ -21,4 +21,10 @@
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\Nyangate.Lib\Nyangate.Lib.csproj" />
<ProjectReference Include="..\..\src\Nyangate\Nyangate.csproj" />
<ProjectReference Include="..\..\src\Nyangate.Cli\Nyangate.Cli.csproj" />
</ItemGroup>
</Project>