mirror of
https://github.com/DorkSquadRR/DorkNet.git
synced 2026-10-27 16:08:47 -07:00
Fix notification URLs in HTTP deployments
Fixes #19 by making the December server advertise notification and service URLs with the configured deployment scheme.
This commit is contained in:
@@ -15,5 +15,5 @@ public class ServiceUrlsController(DomainConfig domain) : ControllerBase
|
|||||||
{
|
{
|
||||||
[HttpGet("services")]
|
[HttpGet("services")]
|
||||||
public ActionResult<Dictionary<string, string>> Get()
|
public ActionResult<Dictionary<string, string>> Get()
|
||||||
=> Ok(ConfigService.BuildServiceUrlMap(domain.Apex));
|
=> Ok(ConfigService.BuildServiceUrlMap(domain));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ public class NotificationController(DomainConfig domain) : ControllerBase
|
|||||||
public IActionResult Connect() => StatusCode(StatusCodes.Status410Gone, new
|
public IActionResult Connect() => StatusCode(StatusCodes.Status410Gone, new
|
||||||
{
|
{
|
||||||
error = "use_signalr_hub",
|
error = "use_signalr_hub",
|
||||||
hint = $"connect to {domain.Sub("notify")}/hub/v1 instead",
|
hint = $"connect to {domain.Url("notify", "/hub/v1")} instead",
|
||||||
});
|
});
|
||||||
|
|
||||||
/// <summary>GET <c>api/notification/hub/v1</c> — returns the
|
/// <summary>GET <c>api/notification/hub/v1</c> — returns the
|
||||||
@@ -27,7 +27,7 @@ public class NotificationController(DomainConfig domain) : ControllerBase
|
|||||||
[HttpGet("/api/notification/hub/v1")]
|
[HttpGet("/api/notification/hub/v1")]
|
||||||
public IActionResult HubInfo() => Ok(new
|
public IActionResult HubInfo() => Ok(new
|
||||||
{
|
{
|
||||||
Url = $"https://{domain.Sub("notify")}/hub/v1",
|
Url = domain.Url("notify", "/hub/v1"),
|
||||||
Protocol = "signalr",
|
Protocol = "signalr",
|
||||||
Version = 1,
|
Version = 1,
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -37,13 +37,13 @@ public class NsController : ControllerBase
|
|||||||
// code: locker)" in BootSequence.
|
// code: locker)" in BootSequence.
|
||||||
[HttpGet("/", Order = -1000)]
|
[HttpGet("/", Order = -1000)]
|
||||||
public ActionResult<Dictionary<string, string>> Root() =>
|
public ActionResult<Dictionary<string, string>> Root() =>
|
||||||
Ok(ConfigService.BuildServiceUrlMap(_domain.Apex));
|
Ok(ConfigService.BuildServiceUrlMap(_domain));
|
||||||
|
|
||||||
// Some builds fetch /v1 or /v1/services directly
|
// Some builds fetch /v1 or /v1/services directly
|
||||||
[HttpGet("/v1")]
|
[HttpGet("/v1")]
|
||||||
[HttpGet("/v1/services")]
|
[HttpGet("/v1/services")]
|
||||||
public ActionResult<Dictionary<string, string>> Services() =>
|
public ActionResult<Dictionary<string, string>> Services() =>
|
||||||
Ok(ConfigService.BuildServiceUrlMap(_domain.Apex));
|
Ok(ConfigService.BuildServiceUrlMap(_domain));
|
||||||
|
|
||||||
// Ping / health used by the client keep-alive loop
|
// Ping / health used by the client keep-alive loop
|
||||||
[HttpGet("/v1/ping")]
|
[HttpGet("/v1/ping")]
|
||||||
|
|||||||
@@ -25,9 +25,9 @@ public class ConfigService(IConfiguration config, DomainConfig domain)
|
|||||||
return new RecRoomConfig
|
return new RecRoomConfig
|
||||||
{
|
{
|
||||||
MessageOfTheDay = config["Server:MOTD"] ?? "Welcome to the private server!",
|
MessageOfTheDay = config["Server:MOTD"] ?? "Welcome to the private server!",
|
||||||
CdnBaseUri = $"https://{domain.Sub("cdn")}",
|
CdnBaseUri = domain.Url("cdn"),
|
||||||
PhotonConfig = photon,
|
PhotonConfig = photon,
|
||||||
ServiceUrls = BuildServiceUrlMap(apex),
|
ServiceUrls = BuildServiceUrlMap(domain),
|
||||||
ConfigTable = new Dictionary<string, string>
|
ConfigTable = new Dictionary<string, string>
|
||||||
{
|
{
|
||||||
// Season keys the 2019/2020 client checks at startup
|
// Season keys the 2019/2020 client checks at startup
|
||||||
@@ -110,12 +110,14 @@ public class ConfigService(IConfiguration config, DomainConfig domain)
|
|||||||
/// <see cref="DomainConfig.Apex"/>) so the map stays a single
|
/// <see cref="DomainConfig.Apex"/>) so the map stays a single
|
||||||
/// source of truth.</summary>
|
/// source of truth.</summary>
|
||||||
public static Dictionary<string, string> BuildServiceUrlMap(string apex)
|
public static Dictionary<string, string> BuildServiceUrlMap(string apex)
|
||||||
|
=> BuildServiceUrlMap(new DomainConfig(apex));
|
||||||
|
|
||||||
|
public static Dictionary<string, string> BuildServiceUrlMap(DomainConfig domain)
|
||||||
{
|
{
|
||||||
var map = new Dictionary<string, string>(ServiceSubdomains.Length);
|
var map = new Dictionary<string, string>(ServiceSubdomains.Length);
|
||||||
foreach (var (service, sub) in ServiceSubdomains)
|
foreach (var (service, sub) in ServiceSubdomains)
|
||||||
{
|
{
|
||||||
var host = string.IsNullOrEmpty(sub) ? apex : $"{sub}.{apex}";
|
map[service] = domain.Url(sub);
|
||||||
map[service] = $"https://{host}";
|
|
||||||
}
|
}
|
||||||
return map;
|
return map;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,8 +11,28 @@ namespace DorkNet.Server.Services;
|
|||||||
public sealed class DomainConfig
|
public sealed class DomainConfig
|
||||||
{
|
{
|
||||||
public string Apex { get; }
|
public string Apex { get; }
|
||||||
public DomainConfig(string apex) { Apex = apex; }
|
public string Scheme { get; }
|
||||||
|
|
||||||
|
public DomainConfig(string apex, string scheme = "https")
|
||||||
|
{
|
||||||
|
Apex = apex;
|
||||||
|
Scheme = NormalizeScheme(scheme);
|
||||||
|
}
|
||||||
|
|
||||||
public string Sub(string prefix) => $"{prefix}.{Apex}";
|
public string Sub(string prefix) => $"{prefix}.{Apex}";
|
||||||
|
public string Url(string prefix, string path = "")
|
||||||
|
{
|
||||||
|
var host = string.IsNullOrEmpty(prefix) ? Apex : Sub(prefix);
|
||||||
|
if (string.IsNullOrEmpty(path)) return $"{Scheme}://{host}";
|
||||||
|
return $"{Scheme}://{host}{(path.StartsWith('/') ? path : "/" + path)}";
|
||||||
|
}
|
||||||
|
|
||||||
public static bool MatchesSubdomain(string host, string prefix)
|
public static bool MatchesSubdomain(string host, string prefix)
|
||||||
=> host.StartsWith(prefix + ".", StringComparison.OrdinalIgnoreCase);
|
=> host.StartsWith(prefix + ".", StringComparison.OrdinalIgnoreCase);
|
||||||
|
|
||||||
|
private static string NormalizeScheme(string? value)
|
||||||
|
{
|
||||||
|
var scheme = string.IsNullOrWhiteSpace(value) ? "https" : value.Trim().TrimEnd(':', '/', '\\').ToLowerInvariant();
|
||||||
|
return scheme is "http" or "https" ? scheme : "https";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -70,7 +70,7 @@ public static class MiddlewarePipelineExtensions
|
|||||||
if (ctx.Request.Host.Host.Equals(domainCfg.Sub("api"), StringComparison.OrdinalIgnoreCase) &&
|
if (ctx.Request.Host.Host.Equals(domainCfg.Sub("api"), StringComparison.OrdinalIgnoreCase) &&
|
||||||
(ctx.Request.Path == "/admin" || ctx.Request.Path.StartsWithSegments("/admin")))
|
(ctx.Request.Path == "/admin" || ctx.Request.Path.StartsWithSegments("/admin")))
|
||||||
{
|
{
|
||||||
ctx.Response.Redirect($"https://{domainCfg.Sub("admin")}/", permanent: false);
|
ctx.Response.Redirect(domainCfg.Url("admin", "/"), permanent: false);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
await next();
|
await next();
|
||||||
|
|||||||
@@ -106,7 +106,8 @@ public static class ServiceCollectionExtensions
|
|||||||
builder.Configuration["Domain:Apex"]
|
builder.Configuration["Domain:Apex"]
|
||||||
?? Environment.GetEnvironmentVariable("DORKNET_DOMAIN")
|
?? Environment.GetEnvironmentVariable("DORKNET_DOMAIN")
|
||||||
?? "localhost";
|
?? "localhost";
|
||||||
builder.Services.AddSingleton(new DomainConfig(apex));
|
var scheme = builder.Configuration["Domain:Scheme"] ?? "https";
|
||||||
|
builder.Services.AddSingleton(new DomainConfig(apex, scheme));
|
||||||
builder.Services.Configure<Microsoft.AspNetCore.HostFiltering.HostFilteringOptions>(opt =>
|
builder.Services.Configure<Microsoft.AspNetCore.HostFiltering.HostFilteringOptions>(opt =>
|
||||||
{
|
{
|
||||||
opt.AllowedHosts = new[] { apex, $"*.{apex}", "localhost", "127.0.0.1" };
|
opt.AllowedHosts = new[] { apex, $"*.{apex}", "localhost", "127.0.0.1" };
|
||||||
|
|||||||
Reference in New Issue
Block a user