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>
This commit is contained in:
parent
5c6af0bbe5
commit
dd80216933
6 changed files with 215 additions and 0 deletions
81
Plugins/Shared/ServerPack/ServerPackWeb.cs
Normal file
81
Plugins/Shared/ServerPack/ServerPackWeb.cs
Normal file
|
|
@ -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;
|
||||
}
|
||||
|
||||
/// <summary>Opens the page (pump thread). `done` gets the route taken and a message without the URL.</summary>
|
||||
internal void OpenWeb(WebOpenRequest r, Action<WebOpenOutcome> 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();
|
||||
}
|
||||
|
||||
/// <summary>Closes the engine's web panel if this plugin opened one. Nothing to close until the panel API is wired.</summary>
|
||||
internal void CloseWeb()
|
||||
{
|
||||
}
|
||||
}
|
||||
|
|
@ -67,6 +67,9 @@
|
|||
<Compile Include="$(RynthSuiteRoot)Plugins\Shared\RynthServerQuestPackApi.cs" Link="Shared\RynthServerQuestPackApi.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)ServerPackQuestPack.cs" Link="ServerPack\ServerPackQuestPack.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)ServerPackInterface.cs" Link="ServerPack\ServerPackInterface.cs" />
|
||||
<!-- Open a web page in the engine's panel, else the browser (allowlisted https only). -->
|
||||
<Compile Include="$(MSBuildThisFileDirectory)ServerWebOpen.cs" Link="ServerPack\ServerWebOpen.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)ServerPackWeb.cs" Link="ServerPack\ServerPackWeb.cs" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
|
|
|||
79
Plugins/Shared/ServerPack/ServerWebOpen.cs
Normal file
79
Plugins/Shared/ServerPack/ServerWebOpen.cs
Normal file
|
|
@ -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;
|
||||
|
||||
/// <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;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue