Files
Febe/src/Febe.Web/FebeWebOptions.cs
T

108 lines
4.9 KiB
C#

using System.Net;
using System.Reflection;
namespace Febe.Web;
/// <summary>Which of the two shells serves the interface (spec §1, §5). The interface adapts to it (capabilities).</summary>
public enum FebeEdition
{
/// <summary>Container on the home server: folders read-only in the interface, «Scarica» instead of «Mostra nella cartella».</summary>
Docker,
/// <summary>Windows application: native folder picker, «Mostra nella cartella», «Avvio» block, browser in its own window.</summary>
Desktop,
}
/// <summary>How requests are admitted (spec §13.1).</summary>
public enum AccessMode
{
/// <summary>No authentication: only for the demo, which listens on 127.0.0.1 and holds no real data.</summary>
None,
/// <summary>Docker: password, HttpOnly SameSite=Strict session cookie, lockout after repeated failures.</summary>
Password,
/// <summary>Windows: a token generated at every start, known to the window, plus Host and Origin checks.</summary>
Token,
}
/// <summary>Repeated wrong passwords lock the login for a while (spec §13.1 «blocco dopo tentativi ripetuti»).</summary>
public sealed record LockoutOptions
{
/// <summary>Wrong attempts from one client within <see cref="Window"/> that lock that client.</summary>
public int AttemptsPerClient { get; init; } = 5;
/// <summary>Wrong attempts from all clients within <see cref="Window"/> that lock every new login.</summary>
public int AttemptsOverall { get; init; } = 20;
public TimeSpan Window { get; init; } = TimeSpan.FromMinutes(15);
public TimeSpan LockFor { get; init; } = TimeSpan.FromMinutes(15);
/// <summary>A session lasts this long after the login.</summary>
public TimeSpan SessionLifetime { get; init; } = TimeSpan.FromDays(30);
}
/// <summary>Configuration of the web layer. Services (storage, library, engine...) are registered separately.</summary>
public sealed record FebeWebOptions
{
public FebeEdition Edition { get; init; } = FebeEdition.Docker;
public AccessMode Access { get; init; } = AccessMode.Password;
/// <summary>Docker: the interface password (FEBE_PASSWORD). Never logged.</summary>
public string? Password { get; init; }
/// <summary>Windows: the token of this start (see <see cref="Security.AccessToken.Create"/>). Never logged.</summary>
public string? AccessToken { get; init; }
/// <summary>
/// Values accepted in the Host header (host, or host:port), case-insensitive. Empty means any host: right for Docker,
/// reached by the server's name or address on the home network; the password and the Origin check protect it.
/// The desktop and the demo pass the loopback names with their port (DNS rebinding cannot reach them).
/// </summary>
public IReadOnlyList<string> AllowedHosts { get; init; } = [];
/// <summary>Trust X-Forwarded-Host for the Origin check (behind a reverse proxy with HTTPS). Off by default.</summary>
public bool TrustForwardedHost { get; init; }
public LockoutOptions Lockout { get; init; } = new();
/// <summary>
/// Development only: serve the interface from this folder (src/Febe.Web/wwwroot) instead of the copy embedded in
/// the assembly, so that pages can be edited and reloaded without rebuilding. Null in every real start.
/// </summary>
public string? InterfaceFolder { get; init; }
/// <summary>Demo mode (spec §10.6): fake engine, temporary library, scenes chosen by address.</summary>
public bool Demo { get; init; }
/// <summary>
/// Docker: root of the import sources, the read-only volume (spec §9.1, §11). Used when the composition does not
/// register its own <c>ImportService</c>; ignored on the desktop, where any folder can be chosen.
/// </summary>
public string ImportRoot { get; init; } = "/importa";
/// <summary>Program version shown in the interface.</summary>
public string Version { get; init; } = typeof(FebeWebOptions).Assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion?.Split('+')[0] ?? "0.0.0";
/// <summary>Loopback host names with a port, for <see cref="AllowedHosts"/>; port 0 accepts any port (<c>host:*</c>).</summary>
public static IReadOnlyList<string> LoopbackHosts(int port)
{
var p = port == 0 ? "*" : port.ToString(System.Globalization.CultureInfo.InvariantCulture);
return [$"127.0.0.1:{p}", $"localhost:{p}", $"[::1]:{p}"];
}
}
/// <summary>Where Kestrel listens.</summary>
/// <param name="Address">IP address (127.0.0.1 for the desktop and the demo; any address in Docker).</param>
/// <param name="Port">Port; 0 picks a free one (tests, desktop).</param>
public sealed record FebeListen(IPAddress Address, int Port)
{
public const int DefaultPort = 8480;
public static FebeListen Loopback(int port = DefaultPort) => new(IPAddress.Loopback, port);
public static FebeListen Any(int port = DefaultPort) => new(IPAddress.Any, port);
}