Sessione salvata cifrata, copiata a «Ho finito» (#36, S4)
Il client del sito ha bisogno della sessione del browser di Febe: ora la copia c'è, cifrata e dietro un interruttore, senza cambiare nulla di predefinito. - Opzione «raccolta» (pagine predefinita, richieste): solo con «richieste» e almeno un'operazione configurata si costruiscono sessione salvata, client del sito e copia. Con Instagram nulla cambia: le dieci operazioni restano non configurate (mancano __spin_*, __req, __s, __dyn, __csr e altri). - Cookie con dominio, percorso, solo-host, Secure, HttpOnly e scadenza, mandati solo dove il browser li manderebbe (RFC 6265). - Sezione «page» del catalogo: dove la pagina tiene ogni valore; nuovi campi rev, hs, hsi, dpr, ccg, comet_req, entry_nonce. Il caricatore rifiuta un'operazione configurata con un valore di sessione senza fonte nella pagina. - Dati/sessione/sessione.json: DPAPI su Windows, AES-256-GCM in Docker (chiave da FEBE_CHIAVE_SESSIONE o chiave.bin), scrittura atomica, file illeggibile = nessuna sessione. «Scollega» la cancella; chiave.bin resta. - Copia a «Ho finito» con Network.getCookies e una sola Runtime.evaluate in sola lettura, solo da una pagina del sito e finché dura il permesso di guida. - FEBE_PASSWORD e FEBE_CHIAVE_SESSIONE non passano a Chromium, Xvfb, ffmpeg. - Sito di prova: csrftoken, cookie limitato a /api/, registro di moduli suo. - Revisione su tre lenti: 11 punti confermati, tutti corretti. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
3046b2da47
commit
c87768766c
69 files changed
+5936
-415
No files matched your search
@@ -0,0 +1,149 @@
|
||||
using System.Globalization;
|
||||
using Febe.Browser.Session;
|
||||
using Febe.Core.Abstractions;
|
||||
using Febe.Core.Collection.Requests;
|
||||
|
||||
namespace Febe.App;
|
||||
|
||||
/// <summary>How a copy of the browser's session ended (counts and names only).</summary>
|
||||
public enum SessionCopyStatus
|
||||
{
|
||||
/// <summary>Copied and written to the saved session.</summary>
|
||||
Saved,
|
||||
|
||||
/// <summary>Copied; it counts at once, but the disk refused it (tried again with the next write).</summary>
|
||||
SavedInMemoryOnly,
|
||||
|
||||
/// <summary>Not copied: <see cref="SessionCopyOutcome.Refusal"/> says why.</summary>
|
||||
Refused,
|
||||
|
||||
/// <summary>Copied, then thrown away: meanwhile the page was no longer Alberto's (rule 7).</summary>
|
||||
Discarded,
|
||||
}
|
||||
|
||||
/// <summary>What a copy did, for the message of «Ho finito» and the tests. Never a value.</summary>
|
||||
/// <param name="Status">How it ended.</param>
|
||||
/// <param name="Refusal">Why nothing was copied (with <see cref="SessionCopyStatus.Refused"/>).</param>
|
||||
/// <param name="Cookies">Cookies copied.</param>
|
||||
/// <param name="MissingValues">Values of the page it did not find that a configured operation sends.</param>
|
||||
/// <param name="MissingDocIds">doc_id of configured operations it did not find.</param>
|
||||
public sealed record SessionCopyOutcome(SessionCopyStatus Status, CaptureRefusal? Refusal, int Cookies, int MissingValues, int MissingDocIds)
|
||||
{
|
||||
/// <summary>True when the copy now is the saved session.</summary>
|
||||
public bool Saved => Status is SessionCopyStatus.Saved or SessionCopyStatus.SavedInMemoryOnly;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Copies the session of Febe's browser into the saved session (issue #36, S4, spec §6.0): at «Ho finito», and after the
|
||||
/// automatic login of the test site. A new copy replaces the saved one (§6.0 «un nuovo accesso ricopia tutto e
|
||||
/// sostituisce»), also when some value of the page is missing: the requests that send it then do not leave and stop
|
||||
/// the engine as a disconnection until a new copy has it (never a way back to the pages). A refused copy leaves the
|
||||
/// saved session as it is.
|
||||
/// </summary>
|
||||
public interface ISessionCopier
|
||||
{
|
||||
/// <summary>
|
||||
/// Copies the session the page holds now. The caller holds the page (Alberto's lease, the page turn).
|
||||
/// </summary>
|
||||
/// <param name="mayKeep">Asked after reading, before saving: false throws the copy away (the page stopped being Alberto's).</param>
|
||||
/// <param name="ct">Cancels the copy; nothing is saved then.</param>
|
||||
Task<SessionCopyOutcome> CopyAsync(Func<bool> mayKeep, CancellationToken ct);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The copy over the browser's page (<see cref="SessionCapture"/>) into a <see cref="ISavedSiteSession"/>, bounded by
|
||||
/// <see cref="Timeout"/>. Logs one line with counts and names, never a value (rule 10).
|
||||
/// </summary>
|
||||
public sealed class BrowserSessionCopier : ISessionCopier
|
||||
{
|
||||
private readonly ICdpSession _cdp;
|
||||
private readonly SessionCapturePlan _plan;
|
||||
private readonly ISavedSiteSession _store;
|
||||
private readonly IClock _clock;
|
||||
private readonly Action<string> _log;
|
||||
|
||||
/// <param name="cdp">The page of Febe's browser.</param>
|
||||
/// <param name="plan">What to read (from the catalog of the site).</param>
|
||||
/// <param name="store">The saved session the copy replaces.</param>
|
||||
/// <param name="clock">The composition's clock (when the copy was made).</param>
|
||||
/// <param name="log">Receives the Italian log line (counts and names only).</param>
|
||||
public BrowserSessionCopier(ICdpSession cdp, SessionCapturePlan plan, ISavedSiteSession store, IClock clock, Action<string> log)
|
||||
{
|
||||
_cdp = cdp ?? throw new ArgumentNullException(nameof(cdp));
|
||||
_plan = plan ?? throw new ArgumentNullException(nameof(plan));
|
||||
_store = store ?? throw new ArgumentNullException(nameof(store));
|
||||
_clock = clock ?? throw new ArgumentNullException(nameof(clock));
|
||||
_log = log ?? throw new ArgumentNullException(nameof(log));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// How long the reading may last before it counts as a browser error: «Ho finito» holds the commands of the
|
||||
/// connection meanwhile, so a browser that hangs must not hold them for ever (real time).
|
||||
/// </summary>
|
||||
public TimeSpan Timeout { get; set; } = TimeSpan.FromSeconds(10);
|
||||
|
||||
public async Task<SessionCopyOutcome> CopyAsync(Func<bool> mayKeep, CancellationToken ct)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(mayKeep);
|
||||
SessionCaptureResult result;
|
||||
using (var limit = CancellationTokenSource.CreateLinkedTokenSource(ct))
|
||||
{
|
||||
limit.CancelAfter(Timeout);
|
||||
try
|
||||
{
|
||||
result = await SessionCapture.CaptureAsync(_cdp, _plan, _clock, limit.Token).ConfigureAwait(false);
|
||||
}
|
||||
catch (OperationCanceledException) when (!ct.IsCancellationRequested)
|
||||
{
|
||||
_log("Sessione non copiata: il browser non ha risposto in tempo.");
|
||||
return new SessionCopyOutcome(SessionCopyStatus.Refused, CaptureRefusal.BrowserError, 0, 0, 0);
|
||||
}
|
||||
}
|
||||
|
||||
if (result.Values is not { } values)
|
||||
{
|
||||
var refusal = result.Refusal ?? CaptureRefusal.BrowserError;
|
||||
_log($"Sessione non copiata: {Reason(refusal)}. La sessione salvata resta com'era.");
|
||||
return new SessionCopyOutcome(SessionCopyStatus.Refused, refusal, 0, 0, 0);
|
||||
}
|
||||
|
||||
var counts = string.Create(
|
||||
CultureInfo.InvariantCulture,
|
||||
$"{result.Cookies} cookie ({result.DroppedCookies} scartati), {result.ExpectedValues - result.MissingValues.Count} valori della pagina su {result.ExpectedValues}, {result.ExpectedDocIds - result.MissingDocIds.Count} doc_id su {result.ExpectedDocIds}");
|
||||
if (!mayKeep())
|
||||
{
|
||||
_log($"Sessione letta ma non salvata: nel frattempo il browser è tornato al motore ({counts}).");
|
||||
return new SessionCopyOutcome(SessionCopyStatus.Discarded, null, result.Cookies, result.MissingValues.Count, result.MissingDocIds.Count);
|
||||
}
|
||||
|
||||
// Not cancelled from here: the copy was made, and a half-written replacement is never left. It replaces the
|
||||
// saved session also when a value is missing (§6.0): keeping an older copy could keep another login's cookies.
|
||||
var onDisk = await _store.ReplaceAsync(values, CancellationToken.None).ConfigureAwait(false);
|
||||
|
||||
// Missing counts only what a configured operation sends: that request will not leave (a disconnection, §6.0).
|
||||
var neededValues = result.MissingValues.Where(_plan.NeededValues.Contains).ToList();
|
||||
var neededDocIds = result.MissingDocIds.Where(_plan.NeededDocIds.Contains).ToList();
|
||||
var needed = neededValues.Concat(neededDocIds).ToList();
|
||||
var unused = result.MissingValues.Concat(result.MissingDocIds).Except(needed, StringComparer.Ordinal).ToList();
|
||||
_log($"Sessione copiata: {counts}"
|
||||
+ (needed.Count == 0 ? string.Empty : "; mancano " + string.Join(", ", needed) + ", che servono alle operazioni configurate")
|
||||
+ (unused.Count == 0 ? string.Empty : "; mancano anche " + string.Join(", ", unused) + ", che nessuna operazione configurata usa")
|
||||
+ (onDisk ? string.Empty : "; non ancora scritta su disco")
|
||||
+ ".");
|
||||
return new SessionCopyOutcome(
|
||||
onDisk ? SessionCopyStatus.Saved : SessionCopyStatus.SavedInMemoryOnly,
|
||||
null,
|
||||
result.Cookies,
|
||||
neededValues.Count,
|
||||
neededDocIds.Count);
|
||||
}
|
||||
|
||||
/// <summary>Why a copy was refused, in Italian.</summary>
|
||||
public static string Reason(CaptureRefusal refusal) => refusal switch
|
||||
{
|
||||
CaptureRefusal.NotOnSite => "il browser non era su una pagina del sito",
|
||||
CaptureRefusal.CheckpointPage => "il browser era sulla pagina di accesso o di verifica",
|
||||
CaptureRefusal.NoUserAgent => "il browser non ha dato il suo user agent",
|
||||
_ => "il browser non ha risposto",
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user