feat(server): API v1 with auth, uploads, sync, jobs, render nodes and events
- Security: PBKDF2-SHA512 (210,000 iterations) hashes, opaque bearer tokens stored as SHA-256 with sliding expiry and revocation, HttpOnly/Secure/ SameSite=Strict admin cookie with a session-bound CSRF token, sign-in rate limit and lockout, role/permission metadata on every endpoint, audit log, first-run administrator bootstrap. - Sequences with keyset paging, ETags and settings_version conflicts; sync manifest; frames, proxies, thumbnails and exports streamed with Range. - Chunked resumable uploads with hash short-circuit, quota and disk checks, staging on the archive volume, SHA-256 verification and IArchiveIngest. - Job engine host, jobs API, render nodes with heartbeat timeout and reassignment, WebSocket events, LAN discovery beacon, upload cleanup. - Windows service host over advapi32 (--service, --install-service, --uninstall-service); fails fast without the archive module services. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
56 files changed
+4726
-108
No files matched your search
@@ -0,0 +1,117 @@
|
||||
using System.ComponentModel;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Runtime.Versioning;
|
||||
using static Titano.Server.Hosting.Windows.ServiceNative;
|
||||
|
||||
namespace Titano.Server.Hosting.Windows;
|
||||
|
||||
/// <summary>
|
||||
/// Runs the server as a Windows service with the SCM API directly (StartServiceCtrlDispatcherW,
|
||||
/// RegisterServiceCtrlHandlerExW, SetServiceStatus). Stop and shutdown requests stop the web host gracefully: running
|
||||
/// jobs keep their checkpoints and resume at the next start.
|
||||
/// </summary>
|
||||
[SupportedOSPlatform("windows")]
|
||||
public static class WindowsServiceHost
|
||||
{
|
||||
public const string ServiceName = "TitanoServer";
|
||||
private const uint StartWaitHint = 60_000;
|
||||
private const uint StopWaitHint = 35_000;
|
||||
|
||||
private static readonly ServiceStatusTracker Status = new();
|
||||
private static Func<WebApplication>? _build;
|
||||
private static WebApplication? _app;
|
||||
private static nint _statusHandle;
|
||||
|
||||
/// <summary>
|
||||
/// Hands the process to the Service Control Manager and returns when the service has stopped. False when the
|
||||
/// process was not started by the SCM (for example from a console).
|
||||
/// </summary>
|
||||
public static unsafe bool Run(Func<WebApplication> build)
|
||||
{
|
||||
_build = build;
|
||||
fixed (char* name = ServiceName)
|
||||
{
|
||||
var table = stackalloc ServiceTableEntry[2];
|
||||
table[0] = new ServiceTableEntry { ServiceName = name, ServiceProc = &ServiceMain };
|
||||
table[1] = default;
|
||||
if (StartServiceCtrlDispatcherW(table)) return true;
|
||||
}
|
||||
var error = Marshal.GetLastPInvokeError();
|
||||
if (error == ErrorFailedServiceControllerConnect) return false;
|
||||
throw new Win32Exception(error);
|
||||
}
|
||||
|
||||
[UnmanagedCallersOnly(CallConvs = [typeof(CallConvStdcall)])]
|
||||
private static unsafe void ServiceMain(uint argc, char** argv)
|
||||
{
|
||||
_statusHandle = RegisterServiceCtrlHandlerExW(ServiceName, &Handler, 0);
|
||||
if (_statusHandle == 0) return;
|
||||
Report(Status.StartPending(StartWaitHint));
|
||||
try
|
||||
{
|
||||
_app = _build!();
|
||||
_app.StartAsync().GetAwaiter().GetResult();
|
||||
Report(Status.Running());
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.Error.WriteLine(ex);
|
||||
Report(Status.Stopped(ErrorExceptionInService));
|
||||
}
|
||||
}
|
||||
|
||||
[UnmanagedCallersOnly(CallConvs = [typeof(CallConvStdcall)])]
|
||||
private static unsafe uint Handler(uint control, uint eventType, nint eventData, nint context)
|
||||
{
|
||||
switch (control)
|
||||
{
|
||||
case ControlStop or ControlShutdown:
|
||||
if (Status.Current.State is ServiceState.Running or ServiceState.StartPending)
|
||||
{
|
||||
Report(Status.StopPending(StopWaitHint));
|
||||
// The handler must return at once; the host stops on the thread pool.
|
||||
_ = Task.Run(StopAsync);
|
||||
}
|
||||
return NoError;
|
||||
case ControlInterrogate:
|
||||
Report(Status.Current);
|
||||
return NoError;
|
||||
default:
|
||||
return ErrorCallNotImplemented;
|
||||
}
|
||||
}
|
||||
|
||||
private static async Task StopAsync()
|
||||
{
|
||||
uint exitCode = 0;
|
||||
try
|
||||
{
|
||||
if (_app is { } app)
|
||||
{
|
||||
await app.StopAsync();
|
||||
await app.DisposeAsync();
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.Error.WriteLine(ex);
|
||||
exitCode = ErrorExceptionInService;
|
||||
}
|
||||
Report(Status.Stopped(exitCode));
|
||||
}
|
||||
|
||||
private static unsafe void Report(ServiceStatusReport report)
|
||||
{
|
||||
var status = new ServiceStatus
|
||||
{
|
||||
ServiceType = ServiceWin32OwnProcess,
|
||||
CurrentState = (uint)report.State,
|
||||
ControlsAccepted = report.ControlsAccepted,
|
||||
Win32ExitCode = report.ExitCode,
|
||||
CheckPoint = report.CheckPoint,
|
||||
WaitHint = report.WaitHintMilliseconds,
|
||||
};
|
||||
SetServiceStatus(_statusHandle, &status);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user