diff --git a/src/Febe.Web/Connection/ConfigurationProblems.cs b/src/Febe.Web/Connection/ConfigurationProblems.cs new file mode 100644 index 0000000..df6f83b --- /dev/null +++ b/src/Febe.Web/Connection/ConfigurationProblems.cs @@ -0,0 +1,23 @@ +namespace Febe.Web.Connection; + +/// A configuration problem that keeps the engine from starting (spec §8.2, §11). Texts in Italian. +/// Stable code, e.g. "password-mancante", "dati-non-scrivibili", "dati-in-rete", "webview2-assente". +/// What is wrong. +/// What Febe did (the engine did not start) and why. +/// What Alberto must do. +public sealed record ConfigurationProblem(string Code, string Title, string Message, string Remedy); + +/// +/// Problems found at startup. When the list is not empty the interface shows only the page that explains them +/// (field problems of GET /api/capabilities) and /salute answers 503. +/// +public interface IConfigurationProblems +{ + IReadOnlyList Current { get; } +} + +/// No problems (default registration). +public sealed class NoConfigurationProblems : IConfigurationProblems +{ + public IReadOnlyList Current => []; +} diff --git a/src/Febe.Web/Connection/IConnectionService.cs b/src/Febe.Web/Connection/IConnectionService.cs new file mode 100644 index 0000000..ebaaddc --- /dev/null +++ b/src/Febe.Web/Connection/IConnectionService.cs @@ -0,0 +1,60 @@ +namespace Febe.Web.Connection; + +/// State of the link to Instagram shown at the bottom of the sidebar and on the Collegamento page (spec §4.1). +public enum ConnectionState +{ + /// No session in the browser profile: «Non collegato». + NotConnected, + + /// Session cookie present: «Collegato». + Connected, + + /// The engine stopped for a challenge or a disconnection: «Verifica richiesta». + VerificationRequired, + + /// The browser is not available (starting, crashed, demo without browser). + BrowserUnavailable, +} + +/// What the interface knows about the link to Instagram. +/// Current state. +/// Connected account (from the data the page received, hypothesis H10), when known. +/// Display name, when known. +/// True while Alberto is using the browser (login page, remote view, browser window): the engine is still. +/// Address shown in the remote view's address bar, without query string. +public sealed record ConnectionStatus( + ConnectionState State, + string? Username, + string? DisplayName, + bool UserIsDriving, + string? CurrentUrl); + +/// Result of «Ho finito» on the Collegamento page. +/// True when the session cookie now exists. +/// Italian message for the page (what happened, what Febe did, what Alberto should do). +public sealed record FinishResult(bool Connected, string Message); + +/// +/// The link to Instagram (spec §4.1, §10.3 «Collegamento» and «Browser» pages). Implemented by the server/desktop +/// composition over the real browser host, and by the demo mode with a fake driven by the scene. +/// Opening the login page here is not an engine action (rule 7): Alberto is using the browser and the engine is still. +/// +public interface IConnectionService +{ + Task GetStatusAsync(CancellationToken ct); + + /// + /// Alberto starts using the browser: the engine stops acting (UserIsDriving), and when + /// is true the browser loads the real Instagram login page. Windows: the browser window is brought to the front. + /// + Task BeginUserDrivingAsync(bool openLogin, CancellationToken ct); + + /// «Ho finito»: checks that the session cookie exists (value never read), hides the browser, ends user driving. + Task FinishAsync(CancellationToken ct); + + /// «Annulla»: ends user driving and puts the browser on the empty rest page. + Task CancelAsync(CancellationToken ct); + + /// «Scollega» (after confirmation in the UI): stops the engine and deletes the browser profile. + Task DisconnectAsync(CancellationToken ct); +} diff --git a/src/Febe.Web/Connection/PROTOCOLLO.md b/src/Febe.Web/Connection/PROTOCOLLO.md new file mode 100644 index 0000000..2a18ee5 --- /dev/null +++ b/src/Febe.Web/Connection/PROTOCOLLO.md @@ -0,0 +1,44 @@ +# Collegamento, vista remota e problemi di configurazione + +Contratto fra l'interfaccia (pagine Collegamento, Browser, errore di configurazione) e la composizione del server. +Lo usano due parti scritte in parallelo: non cambiarlo senza aggiornare entrambe. + +## Collegamento (`IConnectionService`) + +| Endpoint | Corpo e risposta | +|---|---| +| `GET /api/connection` | `{ state: "notConnected"\|"connected"\|"verificationRequired"\|"browserUnavailable", username, displayName, userIsDriving, currentUrl }` | +| `POST /api/connection/begin` | `{ openLogin: bool }` → 204. Alberto usa il browser; con `openLogin` il browser carica la pagina di accesso vera | +| `POST /api/connection/finish` | → `{ connected: bool, message }` («Ho finito») | +| `POST /api/connection/cancel` | → 204 («Annulla») | +| `POST /api/connection/disconnect` | → 204 («Scollega», dopo la conferma nell'interfaccia) | + +In modalità dimostrativa la scena `scollegato` dà `notConnected`, la scena `fermo` dà `verificationRequired`. + +## Vista remota (`/api/vista`, solo veste Docker) + +WebSocket sullo stesso host dell'interfaccia; si apre solo da una sessione autenticata e con `Origin` uguale +all'host (§13.1). Aprirla mette il motore fermo (`UserIsDriving`); chiuderla lo libera. + +Dal server al browser di Alberto: + +- messaggio di testo `{ "type": "meta", "width": 1280, "height": 900, "url": "https://www.instagram.com/accounts/login/" }` + all'apertura e a ogni cambio di indirizzo o dimensione (l'indirizzo senza parametri di ricerca); +- messaggi binari: un fotogramma JPEG ciascuno (da `Page.startScreencast`), da disegnare su un ``. + +Dal browser di Alberto al server, solo messaggi di testo JSON: + +- `{ "type": "mouse", "action": "move"|"down"|"up"|"wheel", "x": 0..1, "y": 0..1, "button": "left"|"right"|"middle", "deltaX": n, "deltaY": n, "clickCount": n }` + con coordinate relative al fotogramma; +- `{ "type": "key", "action": "down"|"up"|"char", "key": "a", "code": "KeyA", "text": "a", "modifiers": n }` + (i tasti non si registrano mai, né sul server né nel browser di Alberto); +- `{ "type": "navigate", "action": "back"|"reload" }`. + +## Problemi di configurazione + +`GET /api/capabilities` ha il campo `problems`: elenco di `{ code, title, message, remedy }` in italiano. +Se non è vuoto il motore non parte e l'interfaccia mostra solo la pagina che spiega il problema (§8.2, §11): +password mancante (`FEBE_PASSWORD`), cartella dei dati non scrivibile o su un disco di rete, runtime WebView2 assente +(Windows). In modalità dimostrativa la scena `errore` ne simula uno. + +`GET /salute` (senza autenticazione) risponde `200 {"stato":"ok"}` o `503 {"stato":"problema"}`, senza altri dettagli.