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