Files
Febe/tools/Febe.SitoDiProva/SiteControl.cs
T
Alby96andClaude Opus 5.5 ce9691fe1c
verifica / controlli (push) Successful in 17s
verifica / prove (push) Successful in 15m4s
verifica / installatore (push) Successful in 4m22s
verifica / immagine (push) Successful in 15m55s
Client del sito contro il sito di prova (#36, S2)
HttpSiteRequester è l'unica classe che parla col sito: va solo a
https://www.instagram.com/ o a un sito di prova di loopback, non segue i
rinvii, una richiesta per volta col permesso del Governatore, solo le
intestazioni del catalogo con l'user agent della sessione, e registra solo
operazione, forma del percorso e stato. Cookie rinnovati e claim finiscono
nella sessione (ISiteSession, per ora in memoria; la copia cifrata è S4).
SiteAnswerSignals traduce le risposte nei segnali di §7.6: 429 rallenta;
rinvii, 401/403, HTML o campi mancanti valgono come Verifica (H16).
Il sito di prova risponde alle richieste dirette come dice
richieste.prova.json e simula i casi d'arresto; una visita senza sessione
viva apre una sessione nuova anche per il client. Nulla è ancora collegato
al motore (S3).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 18:34:12 +01:00

340 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 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 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);
}