mirror of
https://github.com/DorkSquadRR/DorkNet.git
synced 2026-10-26 07:28:45 -07:00
Split remaining service routes
This commit is contained in:
@@ -3,6 +3,7 @@ using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace DorkNet.ServiceDefaults;
|
||||
|
||||
@@ -21,9 +22,15 @@ public static class DorkNetServiceDefaultsExtensions
|
||||
return builder;
|
||||
}
|
||||
|
||||
public static WebApplication MapDorkNetServiceDefaults(this WebApplication app)
|
||||
public static WebApplication MapDorkNetServiceDefaults(
|
||||
this WebApplication app,
|
||||
bool mapPublicHealth = true)
|
||||
{
|
||||
app.MapHealthChecks("/healthz");
|
||||
if (mapPublicHealth)
|
||||
{
|
||||
app.MapHealthChecks("/healthz");
|
||||
}
|
||||
|
||||
app.MapGet("/internal/healthz", (DorkNetServiceInfo info) =>
|
||||
{
|
||||
return Results.Ok(new ServiceHealthResponse(
|
||||
@@ -34,4 +41,43 @@ public static class DorkNetServiceDefaultsExtensions
|
||||
|
||||
return app;
|
||||
}
|
||||
|
||||
public static WebApplication UseDorkNetRouteOwnershipGuard(
|
||||
this WebApplication app,
|
||||
string serviceName)
|
||||
{
|
||||
app.Use(async (ctx, next) =>
|
||||
{
|
||||
var domain = Environment.GetEnvironmentVariable("DORKNET_DOMAIN")
|
||||
?? ctx.RequestServices.GetService<Microsoft.Extensions.Configuration.IConfiguration>()?["Domain:Apex"]
|
||||
?? "localhost";
|
||||
var path = ctx.Request.Path.Value ?? "/";
|
||||
if (DorkNetRouteOwnership.IsOwnedBy(serviceName, ctx.Request.Host.Host, path, domain))
|
||||
{
|
||||
await next();
|
||||
return;
|
||||
}
|
||||
|
||||
var owner = DorkNetRouteOwnership.ResolvePublicService(ctx.Request.Host.Host, path, domain);
|
||||
var logger = ctx.RequestServices
|
||||
.GetRequiredService<ILoggerFactory>()
|
||||
.CreateLogger("DorkNet.ServiceDefaults.RouteGuard");
|
||||
logger.LogDebug(
|
||||
"[route-guard] service={Service} rejected {Method} {Host}{Path}; owner={Owner}",
|
||||
serviceName,
|
||||
ctx.Request.Method,
|
||||
ctx.Request.Host,
|
||||
ctx.Request.Path,
|
||||
owner);
|
||||
ctx.Response.StatusCode = StatusCodes.Status404NotFound;
|
||||
await ctx.Response.WriteAsJsonAsync(new
|
||||
{
|
||||
error = "route_not_owned_by_service",
|
||||
service = serviceName,
|
||||
owner,
|
||||
});
|
||||
});
|
||||
|
||||
return app;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user