Basic filters, refactorization.
This commit is contained in:
parent
7b95dffd11
commit
2f87a95828
17 changed files with 287 additions and 113 deletions
17
src/Nyangate/Middleware/Authenticator.cs
Normal file
17
src/Nyangate/Middleware/Authenticator.cs
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
21
src/Nyangate/Middleware/Filter.cs
Normal file
21
src/Nyangate/Middleware/Filter.cs
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
17
src/Nyangate/Middleware/IDSFilter.cs
Normal file
17
src/Nyangate/Middleware/IDSFilter.cs
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
17
src/Nyangate/Middleware/IPFilter.cs
Normal file
17
src/Nyangate/Middleware/IPFilter.cs
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
25
src/Nyangate/Middleware/Router.cs
Normal file
25
src/Nyangate/Middleware/Router.cs
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,6 +1,34 @@
|
||||||
|
using Microsoft.AspNetCore.Components.Routing;
|
||||||
|
using Nyangate;
|
||||||
|
using Nyangate.Middleware;
|
||||||
|
using Nyangate.Services;
|
||||||
|
|
||||||
var builder = WebApplication.CreateBuilder(args);
|
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();
|
var app = builder.Build();
|
||||||
|
|
||||||
app.MapGet("/", () => "meow~");
|
app.UseMiddleware<Authenticator>();
|
||||||
|
app.UseMiddleware<Filter>();
|
||||||
|
app.UseMiddleware<Nyangate.Middleware.Router>();
|
||||||
|
|
||||||
app.Run();
|
app.Run();
|
||||||
|
|
11
src/Nyangate/Services/BlacklistRegistry.cs
Normal file
11
src/Nyangate/Services/BlacklistRegistry.cs
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace Nyangate.Services;
|
||||||
|
|
||||||
|
public class BlacklistRegistry
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
11
src/Nyangate/Services/RoutingMap.cs
Normal file
11
src/Nyangate/Services/RoutingMap.cs
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace Nyangate.Services;
|
||||||
|
|
||||||
|
public class RoutingMap
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
21
src/Nyangate/Services/ServerCertificateRegistry.cs
Normal file
21
src/Nyangate/Services/ServerCertificateRegistry.cs
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
|
@ -21,4 +21,10 @@
|
||||||
</PackageReference>
|
</PackageReference>
|
||||||
</ItemGroup>
|
</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>
|
</Project>
|
||||||
|
|
Loading…
Reference in a new issue