mirror of
https://github.com/DorkSquadRR/DorkNet.git
synced 2026-10-25 23:18:44 -07:00
Restrict gateway to configured public hosts
This commit is contained in:
@@ -26,6 +26,20 @@ var app = builder.Build();
|
||||
|
||||
app.MapDorkNetServiceDefaults();
|
||||
|
||||
var publicHosts = BuildPublicHosts(apexDomain);
|
||||
app.Use(async (ctx, next) =>
|
||||
{
|
||||
if (DorkNetRouteOwnership.IsInfrastructurePath(ctx.Request.Path.Value ?? "/")
|
||||
|| IsAllowedPublicHost(ctx.Request.Host.Host, publicHosts))
|
||||
{
|
||||
await next();
|
||||
return;
|
||||
}
|
||||
|
||||
ctx.Response.StatusCode = StatusCodes.Status404NotFound;
|
||||
await ctx.Response.WriteAsync("Unknown DorkNet host.");
|
||||
});
|
||||
|
||||
app.MapGet("/internal/services", (IOptions<DorkNetServiceMapOptions> options) =>
|
||||
Results.Ok(options.Value.Endpoints()));
|
||||
app.MapGet("/internal/routes", () =>
|
||||
@@ -190,6 +204,33 @@ static string[] WebBrowserHosts(string apexDomain)
|
||||
return [apexDomain, SubdomainHost("www", apexDomain)];
|
||||
}
|
||||
|
||||
static HashSet<string> BuildPublicHosts(string apexDomain)
|
||||
{
|
||||
var hosts = new HashSet<string>(StringComparer.OrdinalIgnoreCase)
|
||||
{
|
||||
apexDomain,
|
||||
"localhost",
|
||||
"127.0.0.1",
|
||||
};
|
||||
|
||||
foreach (var subdomain in DorkNetRouteOwnership.PublicSubdomains)
|
||||
{
|
||||
hosts.Add(SubdomainHost(subdomain, apexDomain));
|
||||
}
|
||||
|
||||
return hosts;
|
||||
}
|
||||
|
||||
static bool IsAllowedPublicHost(string? host, HashSet<string> publicHosts)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(host))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
return publicHosts.Contains(host.Split(':', 2)[0].Trim());
|
||||
}
|
||||
|
||||
static void AddHostRoute(
|
||||
List<RouteConfig> routes,
|
||||
ref int order,
|
||||
|
||||
Reference in New Issue
Block a user