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

@ -1,10 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<OutputType>Exe</OutputType> <OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework> <TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
</PropertyGroup> </PropertyGroup>
</Project> </Project>

View file

@ -1 +1 @@
Console.WriteLine("meow~"); Console.WriteLine("meow~");

View file

@ -1,9 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<TargetFramework>net7.0</TargetFramework> <TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
</PropertyGroup> </PropertyGroup>
</Project> </Project>

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,9 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk.Web"> <Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup> <PropertyGroup>
<TargetFramework>net7.0</TargetFramework> <TargetFramework>net7.0</TargetFramework>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup> </PropertyGroup>
</Project> </Project>

View file

@ -1,6 +1,34 @@
var builder = WebApplication.CreateBuilder(args); using Microsoft.AspNetCore.Components.Routing;
var app = builder.Build(); using Nyangate;
using Nyangate.Middleware;
app.MapGet("/", () => "meow~"); using Nyangate.Services;
app.Run(); 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.UseMiddleware<Authenticator>();
app.UseMiddleware<Filter>();
app.UseMiddleware<Nyangate.Middleware.Router>();
app.Run();

View file

@ -1,37 +1,37 @@
{ {
"iisSettings": { "iisSettings": {
"windowsAuthentication": false, "windowsAuthentication": false,
"anonymousAuthentication": true, "anonymousAuthentication": true,
"iisExpress": { "iisExpress": {
"applicationUrl": "http://localhost:17984", "applicationUrl": "http://localhost:17984",
"sslPort": 44337 "sslPort": 44337
} }
}, },
"profiles": { "profiles": {
"http": { "http": {
"commandName": "Project", "commandName": "Project",
"dotnetRunMessages": true, "dotnetRunMessages": true,
"launchBrowser": true, "launchBrowser": true,
"applicationUrl": "http://localhost:5178", "applicationUrl": "http://localhost:5178",
"environmentVariables": { "environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development" "ASPNETCORE_ENVIRONMENT": "Development"
} }
}, },
"https": { "https": {
"commandName": "Project", "commandName": "Project",
"dotnetRunMessages": true, "dotnetRunMessages": true,
"launchBrowser": true, "launchBrowser": true,
"applicationUrl": "https://localhost:7056;http://localhost:5178", "applicationUrl": "https://localhost:7056;http://localhost:5178",
"environmentVariables": { "environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development" "ASPNETCORE_ENVIRONMENT": "Development"
} }
}, },
"IIS Express": { "IIS Express": {
"commandName": "IISExpress", "commandName": "IISExpress",
"launchBrowser": true, "launchBrowser": true,
"environmentVariables": { "environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development" "ASPNETCORE_ENVIRONMENT": "Development"
} }
} }
} }
} }

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

@ -1,8 +1,8 @@
{ {
"Logging": { "Logging": {
"LogLevel": { "LogLevel": {
"Default": "Information", "Default": "Information",
"Microsoft.AspNetCore": "Warning" "Microsoft.AspNetCore": "Warning"
} }
} }
} }

View file

@ -1,9 +1,9 @@
{ {
"Logging": { "Logging": {
"LogLevel": { "LogLevel": {
"Default": "Information", "Default": "Information",
"Microsoft.AspNetCore": "Warning" "Microsoft.AspNetCore": "Warning"
} }
}, },
"AllowedHosts": "*" "AllowedHosts": "*"
} }

View file

@ -1,24 +1,30 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<TargetFramework>net7.0</TargetFramework> <TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<IsPackable>false</IsPackable> <IsPackable>false</IsPackable>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2" /> <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2" />
<PackageReference Include="xunit" Version="2.4.2" /> <PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5"> <PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>
</PackageReference> </PackageReference>
<PackageReference Include="coverlet.collector" Version="3.1.2"> <PackageReference Include="coverlet.collector" Version="3.1.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>
</PackageReference> </PackageReference>
</ItemGroup> </ItemGroup>
</Project> <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>