RynthServers/Plugins/Shared/ServerPack/ServerWebOpen.cs
Daniel Wallace dd80216933 ServerPack: open a web page in the engine's panel, else the browser
Pure allowlist/route rules (ServerWebOpen) plus the host opener (ServerPackWeb):
ShellExecuteW on a background STA thread; panel seams stubbed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-08 13:18:45 -08:00

79 lines
3.4 KiB
C#

// ServerWebOpen.cs - the rules for opening a server's web page from a server plugin: which URLs are
// allowed and where the page goes (the engine's web panel when it has one, else the system browser).
// Pure (no engine, no threads): RynthServerPack.Tests covers it. The host side is ServerPackWeb.cs.
// No Regex: the plugins are NativeAOT and Regex costs size, so the key is parsed by hand.
using System;
using System.Collections.Generic;
namespace RynthCore.Plugin.ServerPack;
/// <summary>A page to open. The URL comes from constants in the plugin, never from chat.</summary>
internal sealed record WebOpenRequest(string Url, IReadOnlyList<string> AllowedHosts, string ProfileKey, string Title);
internal enum WebOpenRoute { Panel, Browser, Refused }
/// <summary>What happened. The message never contains the URL.</summary>
internal sealed record WebOpenOutcome(WebOpenRoute Route, bool Ok, string Message);
internal static class ServerWebOpen
{
/// <summary>
/// Absolute https, no userinfo, the default port, and a host equal to an allowed entry
/// (OrdinalIgnoreCase). An entry "*.x" matches subdomains of x only, never x itself.
/// </summary>
public static bool IsAllowedUrl(string? url, IReadOnlyList<string> allowedHosts)
{
if (string.IsNullOrEmpty(url) || allowedHosts == null) return false;
if (!Uri.TryCreate(url, UriKind.Absolute, out Uri? uri)) return false;
if (!string.Equals(uri.Scheme, "https", StringComparison.Ordinal)) return false;
if (uri.UserInfo.Length != 0 || !uri.IsDefaultPort) return false;
string host = uri.Host;
if (host.Length == 0) return false;
foreach (string entry in allowedHosts)
{
if (string.IsNullOrEmpty(entry)) continue;
if (entry.StartsWith("*.", StringComparison.Ordinal))
{
string suffix = entry.Substring(1); // ".x"
if (suffix.Length > 1
&& host.Length > suffix.Length
&& host.EndsWith(suffix, StringComparison.OrdinalIgnoreCase))
return true;
}
else if (string.Equals(host, entry, StringComparison.OrdinalIgnoreCase))
{
return true;
}
}
return false;
}
/// <summary>^[a-z0-9-]{1,32}/[0-9a-f]{32}$ - the engine's panel profile key (server/token).</summary>
public static bool IsValidProfileKey(string? key)
{
if (key == null) return false;
int slash = key.IndexOf('/');
if (slash < 1 || slash > 32) return false;
if (key.Length - slash - 1 != 32) return false;
for (int i = 0; i < slash; i++)
{
char c = key[i];
if (!((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || c == '-')) return false;
}
for (int i = slash + 1; i < key.Length; i++)
{
char c = key[i];
if (!((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f'))) return false;
}
return true;
}
/// <summary>Refused if the URL is not allowed; Browser if there is no panel or no valid key; else Panel.</summary>
public static WebOpenRoute Route(WebOpenRequest r, bool panelAvailable)
{
if (!IsAllowedUrl(r.Url, r.AllowedHosts)) return WebOpenRoute.Refused;
if (!panelAvailable || !IsValidProfileKey(r.ProfileKey)) return WebOpenRoute.Browser;
return WebOpenRoute.Panel;
}
}