Files
duplicati/Duplicati/WebserverCore/Extensions/WebApplicationExtensions.cs
T
Kenneth Skovhede 2b55b3449d Add custom hostname validation
This adds a manually implemented hostname check that is applied as an endpoint filter.
With the implemented filter, the validation rules are similar to those before Kestrel was introduced.
This fixes #5469
2024-08-19 14:07:48 +02:00

38 lines
1.2 KiB
C#

using System.Reflection;
using Duplicati.WebserverCore.Abstractions;
using Duplicati.WebserverCore.Middlewares;
namespace Duplicati.WebserverCore.Extensions;
public static class WebApplicationExtensions
{
public static WebApplication AddEndpoints(this WebApplication application)
{
return AddV1(application);
}
private static WebApplication AddV1(WebApplication application)
{
var mapperInterfaceType = typeof(IEndpointV1);
var endpoints =
typeof(WebApplicationExtensions).Assembly.DefinedTypes
.Where(t => t.ImplementedInterfaces.Contains(mapperInterfaceType))
.ToArray();
if (endpoints.Length == 0)
{
return application;
}
foreach (var endpoint in endpoints)
{
var group = application.MapGroup("/api/v1")
.AddEndpointFilter<LanguageFilter>()
.AddEndpointFilter<HostnameFilter>();
var methodMap = endpoint.GetMethod(nameof(IEndpointV1.Map), BindingFlags.Static | BindingFlags.Public);
methodMap!.Invoke(null, [group]);
}
return application;
}
}