diff --git a/Plugins/Shared/ServerPack/ServerPackWeb.cs b/Plugins/Shared/ServerPack/ServerPackWeb.cs
new file mode 100644
index 0000000..af3c95e
--- /dev/null
+++ b/Plugins/Shared/ServerPack/ServerPackWeb.cs
@@ -0,0 +1,81 @@
+// ServerPackWeb.cs - the host side of ServerWebOpen: open a server's web page in the engine's web
+// panel when it has one, else in the system browser. A server's own code calls OpenWeb on the pump
+// (ServerChatCommand, a RunOnTick action); the answer goes to `done`, which may run on a worker
+// thread (Reply is any-thread). The answer never contains the URL.
+//
+// Never Process.Start inside acclient: the browser opens with ShellExecuteW, as the engine does
+// (ProblemReport.cs), on its own short-lived STA thread so the shell call never blocks the pump.
+// The panel seams (PanelAvailable, TryOpenPanel) are stubs until the engine API is wired.
+using System;
+using System.Runtime.InteropServices;
+using System.Threading;
+
+namespace RynthCore.Plugin.ServerPack;
+
+public sealed partial class ServerPackPlugin
+{
+ [DllImport("shell32.dll", CharSet = CharSet.Unicode, EntryPoint = "ShellExecuteW")]
+ private static extern IntPtr ShellExecuteW(IntPtr hwnd, string? operation, string file, string? parameters, string? directory, int showCmd);
+
+ // Seams for the engine's web panel.
+ private bool PanelAvailable => false;
+
+ private bool TryOpenPanel(WebOpenRequest r, out string error)
+ {
+ error = "The web panel is not available.";
+ return false;
+ }
+
+ /// Opens the page (pump thread). `done` gets the route taken and a message without the URL.
+ internal void OpenWeb(WebOpenRequest r, Action done)
+ {
+ WebOpenRoute route = ServerWebOpen.Route(r, PanelAvailable);
+ if (route == WebOpenRoute.Refused)
+ {
+ done(new WebOpenOutcome(route, false, "That page is not on this server's list of pages, so it was not opened."));
+ return;
+ }
+
+ if (route == WebOpenRoute.Panel)
+ {
+ if (TryOpenPanel(r, out string error))
+ {
+ done(new WebOpenOutcome(route, true, "Opened in the web panel."));
+ return;
+ }
+ // The panel failed: the browser is the fallback, and the report below says it was the browser.
+ LogTagged("web panel did not open: " + error);
+ route = WebOpenRoute.Browser;
+ }
+
+ string url = r.Url;
+ var thread = new Thread(() =>
+ {
+ string message;
+ bool ok;
+ try
+ {
+ long code = ShellExecuteW(IntPtr.Zero, "open", url, null, null, 1).ToInt64();
+ ok = code > 32;
+ message = ok ? "Opened in your browser." : $"Couldn't open your browser (code {code}).";
+ }
+ catch (Exception)
+ {
+ ok = false;
+ message = "Couldn't open your browser (code 0).";
+ }
+ done(new WebOpenOutcome(WebOpenRoute.Browser, ok, message));
+ })
+ {
+ IsBackground = true,
+ Name = "ServerPack web open",
+ };
+ thread.SetApartmentState(ApartmentState.STA);
+ thread.Start();
+ }
+
+ /// Closes the engine's web panel if this plugin opened one. Nothing to close until the panel API is wired.
+ internal void CloseWeb()
+ {
+ }
+}
diff --git a/Plugins/Shared/ServerPack/ServerPlugin.props b/Plugins/Shared/ServerPack/ServerPlugin.props
index 9e8606b..d7abb0d 100644
--- a/Plugins/Shared/ServerPack/ServerPlugin.props
+++ b/Plugins/Shared/ServerPack/ServerPlugin.props
@@ -67,6 +67,9 @@
+
+
+
diff --git a/Plugins/Shared/ServerPack/ServerWebOpen.cs b/Plugins/Shared/ServerPack/ServerWebOpen.cs
new file mode 100644
index 0000000..2100a4d
--- /dev/null
+++ b/Plugins/Shared/ServerPack/ServerWebOpen.cs
@@ -0,0 +1,79 @@
+// 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;
+
+/// A page to open. The URL comes from constants in the plugin, never from chat.
+internal sealed record WebOpenRequest(string Url, IReadOnlyList AllowedHosts, string ProfileKey, string Title);
+
+internal enum WebOpenRoute { Panel, Browser, Refused }
+
+/// What happened. The message never contains the URL.
+internal sealed record WebOpenOutcome(WebOpenRoute Route, bool Ok, string Message);
+
+internal static class ServerWebOpen
+{
+ ///
+ /// 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.
+ ///
+ public static bool IsAllowedUrl(string? url, IReadOnlyList 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;
+ }
+
+ /// ^[a-z0-9-]{1,32}/[0-9a-f]{32}$ - the engine's panel profile key (server/token).
+ 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;
+ }
+
+ /// Refused if the URL is not allowed; Browser if there is no panel or no valid key; else Panel.
+ 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;
+ }
+}
diff --git a/Tools/RynthServerPack.Tests/Program.cs b/Tools/RynthServerPack.Tests/Program.cs
index 0bf26e2..88e690e 100644
--- a/Tools/RynthServerPack.Tests/Program.cs
+++ b/Tools/RynthServerPack.Tests/Program.cs
@@ -22,6 +22,7 @@ internal static class Program
ServerPackCommandsTests.Register(runner);
DreamWeaveSkillsTests.Register(runner);
DreamWeaveTests.Register(runner);
+ ServerWebOpenTests.Register(runner);
return runner.Run("RynthServerPack tests", args.Length > 0 ? args[0] : null);
}
}
diff --git a/Tools/RynthServerPack.Tests/RynthServerPack.Tests.csproj b/Tools/RynthServerPack.Tests/RynthServerPack.Tests.csproj
index cd4e37a..67ccd9a 100644
--- a/Tools/RynthServerPack.Tests/RynthServerPack.Tests.csproj
+++ b/Tools/RynthServerPack.Tests/RynthServerPack.Tests.csproj
@@ -32,6 +32,8 @@
+
+
diff --git a/Tools/RynthServerPack.Tests/ServerWebOpenTests.cs b/Tools/RynthServerPack.Tests/ServerWebOpenTests.cs
new file mode 100644
index 0000000..acbeb10
--- /dev/null
+++ b/Tools/RynthServerPack.Tests/ServerWebOpenTests.cs
@@ -0,0 +1,49 @@
+using System;
+using RynthCore.Plugin.ServerPack;
+using RynthCore.RynthAiTests.Harness;
+
+namespace RynthServerPack.Tests;
+
+// The web opener's pure part: which URLs a server plugin may open, and where the page goes.
+internal static class ServerWebOpenTests
+{
+ public static void Register(Runner r)
+ {
+ r.Add("web open: only https on an allowed host, and the route follows the engine", Rules);
+ }
+
+ private static readonly string[] Hosts = { "www.acdreamweave.com" };
+ private static readonly string[] Wild = { "*.acdreamweave.com" };
+ private const string Key = "dreamweave/0123456789abcdef0123456789abcdef";
+
+ private static void Rules()
+ {
+ Check.True(ServerWebOpen.IsAllowedUrl("https://www.acdreamweave.com/x", Hosts), "https on the host");
+ Check.True(ServerWebOpen.IsAllowedUrl("https://WWW.AcDreamWeave.com/x", Hosts), "host case-insensitive");
+ Check.True(!ServerWebOpen.IsAllowedUrl("http://www.acdreamweave.com/x", Hosts), "http refused");
+ Check.True(!ServerWebOpen.IsAllowedUrl("https://user@www.acdreamweave.com/x", Hosts), "userinfo refused");
+ Check.True(!ServerWebOpen.IsAllowedUrl("https://www.acdreamweave.com:444/x", Hosts), "other port refused");
+ Check.True(!ServerWebOpen.IsAllowedUrl("https://www.acdreamweave.com.evil.com/x", Hosts), "suffix host refused");
+ Check.True(!ServerWebOpen.IsAllowedUrl("javascript:alert(1)", Hosts), "javascript refused");
+ Check.True(!ServerWebOpen.IsAllowedUrl(null, Hosts), "null refused");
+ Check.True(!ServerWebOpen.IsAllowedUrl("/relative", Hosts), "relative refused");
+
+ Check.True(ServerWebOpen.IsAllowedUrl("https://wiki.acdreamweave.com/", Wild), "wildcard matches a subdomain");
+ Check.True(!ServerWebOpen.IsAllowedUrl("https://acdreamweave.com/", Wild), "wildcard excludes the bare domain");
+ Check.True(!ServerWebOpen.IsAllowedUrl("https://evilacdreamweave.com/", Wild), "wildcard needs the dot");
+
+ Check.True(ServerWebOpen.IsValidProfileKey(Key), "dreamweave/ + 32 hex");
+ Check.True(!ServerWebOpen.IsValidProfileKey("dreamweave/0123456789ABCDEF0123456789abcdef"), "uppercase refused");
+ Check.True(!ServerWebOpen.IsValidProfileKey("../0123456789abcdef0123456789abcdef"), "dotdot refused");
+ Check.True(!ServerWebOpen.IsValidProfileKey(Key + "0"), "longer refused");
+ Check.True(!ServerWebOpen.IsValidProfileKey(""), "empty refused");
+ Check.True(!ServerWebOpen.IsValidProfileKey(null), "null refused");
+
+ var ok = new WebOpenRequest("https://www.acdreamweave.com/x", Hosts, Key, "t");
+ Check.Eq(ServerWebOpen.Route(ok, true), WebOpenRoute.Panel, "panel");
+ Check.Eq(ServerWebOpen.Route(ok, false), WebOpenRoute.Browser, "no panel -> browser");
+ Check.Eq(ServerWebOpen.Route(ok with { ProfileKey = "" }, true), WebOpenRoute.Browser, "empty key -> browser");
+ Check.Eq(ServerWebOpen.Route(ok with { ProfileKey = "Bad/Key" }, true), WebOpenRoute.Browser, "bad key -> browser");
+ Check.Eq(ServerWebOpen.Route(ok with { Url = "http://www.acdreamweave.com/x" }, true), WebOpenRoute.Refused, "bad url refused");
+ }
+}