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>
344 lines
12 KiB
C#
344 lines
12 KiB
C#
using System.Collections.Concurrent;
|
|
|
|
namespace Febe.SitoDiProva;
|
|
|
|
/// <summary>The stop cases the site can play (spec §14.2), switched by the tests through <c>/_controllo</c>.</summary>
|
|
public enum StopMode
|
|
{
|
|
/// <summary>Everything works.</summary>
|
|
Normal,
|
|
|
|
/// <summary>Data requests get HTTP 429 (Rallentamento).</summary>
|
|
TooManyRequests,
|
|
|
|
/// <summary>Pages are redirected to <c>/verifica</c>; data requests get 403 and the page moves there (Verifica).</summary>
|
|
Challenge,
|
|
|
|
/// <summary>The session cookie is removed and pages move to <c>/sessione-scaduta</c> (Disconnessione).</summary>
|
|
SessionEnded,
|
|
|
|
/// <summary>Data requests return empty pages.</summary>
|
|
EmptyPages,
|
|
|
|
/// <summary>Data requests return a shape no reader knows (the site «changed»).</summary>
|
|
UnknownFormat,
|
|
}
|
|
|
|
/// <summary>
|
|
/// What the site answers to the next request of the site client (<c>/api/…</c>, issue #36) instead of the data: the stop
|
|
/// cases of spec §7.6 and the renewals the client must keep. The shapes not known from samples are generic (H16).
|
|
/// </summary>
|
|
public enum DirectReply
|
|
{
|
|
/// <summary>The data, after the checks of a real site.</summary>
|
|
Normal,
|
|
|
|
/// <summary>429 with a short JSON and <c>Retry-After: 60</c> (Rallentamento).</summary>
|
|
TooManyRequests,
|
|
|
|
/// <summary>302 to the login page <c>/accesso/</c> (H16-generic).</summary>
|
|
RedirectToLogin,
|
|
|
|
/// <summary>302 to the challenge page <c>/verifica</c> (H16-generic).</summary>
|
|
RedirectToChallenge,
|
|
|
|
/// <summary>200 with an HTML document where JSON is expected (H16-generic).</summary>
|
|
HtmlInsteadOfJson,
|
|
|
|
/// <summary>200 with a JSON that has none of the expected root fields (H16-generic).</summary>
|
|
MissingRootField,
|
|
|
|
/// <summary>The data, with a <c>Set-Cookie</c> that renews the session cookie (new value, the old one stops working).</summary>
|
|
RenewSessionCookie,
|
|
|
|
/// <summary>The data, with a <c>Set-Cookie</c> that deletes the session cookie (the session ends).</summary>
|
|
DeleteSessionCookie,
|
|
|
|
/// <summary>The data, with a new <c>x-ig-set-www-claim</c>.</summary>
|
|
SetClaim,
|
|
}
|
|
|
|
/// <summary>Switches and counters of the test site. Thread-safe.</summary>
|
|
public sealed class SiteControl
|
|
{
|
|
private readonly Lock _gate = new();
|
|
private readonly ConcurrentQueue<string> _dataRequests = new();
|
|
private readonly ConcurrentQueue<string> _directRequests = new();
|
|
private readonly ConcurrentQueue<DirectReply> _directReplies = new();
|
|
private readonly HashSet<string> _sessions = new(StringComparer.Ordinal) { InitialSession };
|
|
private DirectReply _directAlways = DirectReply.Normal;
|
|
private int _directInFlight;
|
|
private int _maxDirectInFlight;
|
|
private int _loginVisits;
|
|
private int _challengeVisits;
|
|
private int _renewals;
|
|
private int _logins;
|
|
private int _claims;
|
|
private int _csrfTokens;
|
|
private StopMode _mode = StopMode.Normal;
|
|
private int _armAfter;
|
|
|
|
/// <summary>How long a photo story stays on screen before the viewer moves on by itself.</summary>
|
|
public TimeSpan StoryDuration { get; set; } = TimeSpan.FromSeconds(5);
|
|
|
|
/// <summary>Validity of the CDN addresses handed out from now on.</summary>
|
|
public TimeSpan AddressLifetime { get; set; } = TimeSpan.FromHours(6);
|
|
|
|
/// <summary>The CDN answers 429 to every file (Rallentamento from the CDN, spec §7.6).</summary>
|
|
public bool CdnTooManyRequests { get; set; }
|
|
|
|
/// <summary>Number of CDN requests answered with 429.</summary>
|
|
public int CdnThrottled => _cdnThrottled;
|
|
|
|
/// <summary>Paths with query of every data request, in arrival order (for the tests).</summary>
|
|
public IReadOnlyList<string> DataRequests => [.. _dataRequests];
|
|
|
|
/// <summary>Number of files served by the CDN.</summary>
|
|
public int CdnFilesServed => _cdnFiles;
|
|
|
|
/// <summary>Number of CDN requests refused because the address had expired.</summary>
|
|
public int CdnExpired => _cdnExpired;
|
|
|
|
private int _cdnFiles;
|
|
private int _cdnExpired;
|
|
private int _cdnThrottled;
|
|
private int _recentSearchWrites;
|
|
|
|
/// <summary>Extra wait before every answer of the search box (slow answers).</summary>
|
|
public TimeSpan SearchDelay { get; set; }
|
|
|
|
/// <summary>
|
|
/// The search box answers shorter texts later than longer ones, so an earlier prefix arrives last (answers out of
|
|
/// order, as on a slow network).
|
|
/// </summary>
|
|
public bool SearchDisorder { get; set; }
|
|
|
|
/// <summary>The page sends no request for a text it already searched, as a page with its own memory does.</summary>
|
|
public bool SearchMemory { get; set; }
|
|
|
|
/// <summary>
|
|
/// Number of «ricerche recenti» the page wrote: clicking a result or pressing Enter in the search box writes one,
|
|
/// as Instagram registers a search. Febe must never cause one.
|
|
/// </summary>
|
|
public int RecentSearchWrites => _recentSearchWrites;
|
|
|
|
/// <summary>
|
|
/// A session already open when the site starts and after <see cref="Reset"/>, as one opened before the test: the
|
|
/// tests that build a saved session without the browser use it. The pages never hand it out: a visit without a live
|
|
/// session gets a new value (<see cref="OpenSession"/>).
|
|
/// </summary>
|
|
public const string InitialSession = "attiva";
|
|
|
|
/// <summary>Method, path and query of every request of the site client (<c>/api/…</c>), in arrival order.</summary>
|
|
public IReadOnlyList<string> DirectRequests => [.. _directRequests];
|
|
|
|
/// <summary>The most requests of the site client the site was answering at the same time.</summary>
|
|
public int MaxDirectInFlight => Volatile.Read(ref _maxDirectInFlight);
|
|
|
|
/// <summary>Extra wait before every answer to the site client (slow answers).</summary>
|
|
public TimeSpan DirectDelay { get; set; }
|
|
|
|
/// <summary>Requests of the login page <c>/accesso…</c> (a redirect followed would land there).</summary>
|
|
public int LoginPageVisits => Volatile.Read(ref _loginVisits);
|
|
|
|
/// <summary>Requests of the challenge page <c>/verifica…</c>.</summary>
|
|
public int ChallengePageVisits => Volatile.Read(ref _challengeVisits);
|
|
|
|
/// <summary>The site client answers to the next request (or, with <paramref name="always"/>, to every one) with <paramref name="reply"/>.</summary>
|
|
public void ReplyToDirect(DirectReply reply, bool always = false)
|
|
{
|
|
if (always)
|
|
{
|
|
lock (_gate)
|
|
{
|
|
_directAlways = reply;
|
|
}
|
|
|
|
_directReplies.Clear();
|
|
return;
|
|
}
|
|
|
|
_directReplies.Enqueue(reply);
|
|
}
|
|
|
|
/// <summary>True when <paramref name="value"/> is a session cookie the site handed out and did not end.</summary>
|
|
public bool IsSession(string? value)
|
|
{
|
|
lock (_gate)
|
|
{
|
|
return value is { Length: > 0 } && _sessions.Contains(value);
|
|
}
|
|
}
|
|
|
|
/// <summary>A new session, as a new login opens: its value works for the pages and for the site client.</summary>
|
|
internal string OpenSession()
|
|
{
|
|
var opened = "aperta-" + Interlocked.Increment(ref _logins).ToString(System.Globalization.CultureInfo.InvariantCulture);
|
|
lock (_gate)
|
|
{
|
|
_sessions.Add(opened);
|
|
}
|
|
|
|
return opened;
|
|
}
|
|
|
|
/// <summary>A new value of the session cookie in place of <paramref name="old"/>, which stops working.</summary>
|
|
internal string RenewSession(string old)
|
|
{
|
|
var renewed = "rinnovata-" + Interlocked.Increment(ref _renewals).ToString(System.Globalization.CultureInfo.InvariantCulture);
|
|
lock (_gate)
|
|
{
|
|
_sessions.Remove(old);
|
|
_sessions.Add(renewed);
|
|
}
|
|
|
|
return renewed;
|
|
}
|
|
|
|
/// <summary>Ends the session <paramref name="value"/>.</summary>
|
|
internal void EndSession(string value)
|
|
{
|
|
lock (_gate)
|
|
{
|
|
_sessions.Remove(value);
|
|
}
|
|
}
|
|
|
|
/// <summary>A new value of the cookie <c>csrftoken</c>, as a visit without one gets.</summary>
|
|
internal string NextCsrf() => "csrf-prova-" + Interlocked.Increment(ref _csrfTokens).ToString(System.Globalization.CultureInfo.InvariantCulture);
|
|
|
|
/// <summary>A new claim value.</summary>
|
|
internal string NextClaim() => "claim-prova-" + Interlocked.Increment(ref _claims).ToString(System.Globalization.CultureInfo.InvariantCulture);
|
|
|
|
/// <summary>
|
|
/// Records a request of the site client and returns the reply it gets and the mode of the site that applies to it:
|
|
/// a request of the site client counts as a data request for <see cref="Set"/> with <c>afterDataRequests</c>, as a
|
|
/// page's <c>/dati/</c> does. Pair with <see cref="OnDirectFinished"/>.
|
|
/// </summary>
|
|
internal (DirectReply Reply, StopMode Mode) OnDirectRequest(string methodPathAndQuery)
|
|
{
|
|
_directRequests.Enqueue(methodPathAndQuery);
|
|
var now = Interlocked.Increment(ref _directInFlight);
|
|
int max;
|
|
while (now > (max = Volatile.Read(ref _maxDirectInFlight)) && Interlocked.CompareExchange(ref _maxDirectInFlight, now, max) != max)
|
|
{
|
|
}
|
|
|
|
StopMode mode;
|
|
DirectReply always;
|
|
lock (_gate)
|
|
{
|
|
if (_armAfter > 0)
|
|
{
|
|
_armAfter--;
|
|
mode = StopMode.Normal;
|
|
}
|
|
else
|
|
{
|
|
mode = _mode;
|
|
}
|
|
|
|
always = _directAlways;
|
|
}
|
|
|
|
return (_directReplies.TryDequeue(out var next) ? next : always, mode);
|
|
}
|
|
|
|
internal void OnDirectFinished() => Interlocked.Decrement(ref _directInFlight);
|
|
|
|
/// <summary>Counts the visits of the login and challenge pages.</summary>
|
|
internal void OnPageVisit(string path)
|
|
{
|
|
if (path.StartsWith("/accesso", StringComparison.Ordinal))
|
|
{
|
|
Interlocked.Increment(ref _loginVisits);
|
|
}
|
|
else if (path.StartsWith("/verifica", StringComparison.Ordinal))
|
|
{
|
|
Interlocked.Increment(ref _challengeVisits);
|
|
}
|
|
}
|
|
|
|
public StopMode Mode
|
|
{
|
|
get
|
|
{
|
|
lock (_gate)
|
|
{
|
|
return _armAfter > 0 ? StopMode.Normal : _mode;
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>Sets a stop case; with <paramref name="afterDataRequests"/> it starts after that many more data requests.</summary>
|
|
public void Set(StopMode mode, int afterDataRequests = 0)
|
|
{
|
|
lock (_gate)
|
|
{
|
|
_mode = mode;
|
|
_armAfter = Math.Max(0, afterDataRequests);
|
|
}
|
|
}
|
|
|
|
/// <summary>Back to normal; counters cleared; default story duration and address lifetime.</summary>
|
|
public void Reset()
|
|
{
|
|
lock (_gate)
|
|
{
|
|
_mode = StopMode.Normal;
|
|
_armAfter = 0;
|
|
}
|
|
|
|
_dataRequests.Clear();
|
|
_directRequests.Clear();
|
|
_directReplies.Clear();
|
|
lock (_gate)
|
|
{
|
|
_directAlways = DirectReply.Normal;
|
|
_sessions.Clear();
|
|
_sessions.Add(InitialSession);
|
|
}
|
|
|
|
DirectDelay = TimeSpan.Zero;
|
|
Interlocked.Exchange(ref _maxDirectInFlight, 0);
|
|
Interlocked.Exchange(ref _loginVisits, 0);
|
|
Interlocked.Exchange(ref _challengeVisits, 0);
|
|
StoryDuration = TimeSpan.FromSeconds(5);
|
|
AddressLifetime = TimeSpan.FromHours(6);
|
|
CdnTooManyRequests = false;
|
|
SearchDelay = TimeSpan.Zero;
|
|
SearchDisorder = false;
|
|
SearchMemory = false;
|
|
Interlocked.Exchange(ref _recentSearchWrites, 0);
|
|
Interlocked.Exchange(ref _cdnFiles, 0);
|
|
Interlocked.Exchange(ref _cdnExpired, 0);
|
|
Interlocked.Exchange(ref _cdnThrottled, 0);
|
|
}
|
|
|
|
public void ClearRequests() => _dataRequests.Clear();
|
|
|
|
/// <summary>Records a data request and returns the mode that applies to it.</summary>
|
|
internal StopMode OnDataRequest(string pathAndQuery)
|
|
{
|
|
_dataRequests.Enqueue(pathAndQuery);
|
|
lock (_gate)
|
|
{
|
|
if (_armAfter > 0)
|
|
{
|
|
_armAfter--;
|
|
return StopMode.Normal;
|
|
}
|
|
|
|
return _mode;
|
|
}
|
|
}
|
|
|
|
internal void OnCdnServed() => Interlocked.Increment(ref _cdnFiles);
|
|
|
|
internal void OnRecentSearchWritten() => Interlocked.Increment(ref _recentSearchWrites);
|
|
|
|
internal void OnCdnExpired() => Interlocked.Increment(ref _cdnExpired);
|
|
|
|
internal void OnCdnThrottled() => Interlocked.Increment(ref _cdnThrottled);
|
|
}
|