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">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

View file

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

View file

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

View file

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

View file

@ -1,37 +1,37 @@
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:17984",
"sslPort": 44337
}
},
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "http://localhost:5178",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "https://localhost:7056;http://localhost:5178",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:17984",
"sslPort": 44337
}
},
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "http://localhost:5178",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "https://localhost:7056;http://localhost:5178",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"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": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}

View file

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

View file

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