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>
This commit is contained in:
1 parent
e10c25d12d
commit
ce9691fe1c
19 files changed
+3563
-51
No files matched your search
@@ -38,7 +38,7 @@ public sealed record TestSiteOptions
|
||||
/// fetch JSON in a synthetic format of our own; stop cases are switched through <see cref="Control"/> or the
|
||||
/// <c>/_controllo</c> endpoints. Never Instagram, never anything that looks like it.
|
||||
/// </summary>
|
||||
public sealed class TestSite : IAsyncDisposable
|
||||
public sealed partial class TestSite : IAsyncDisposable
|
||||
{
|
||||
/// <summary>Name of the session cookie of the site.</summary>
|
||||
public const string SessionCookie = "sessione_prova";
|
||||
@@ -165,12 +165,19 @@ public sealed class TestSite : IAsyncDisposable
|
||||
return;
|
||||
}
|
||||
|
||||
if (path.StartsWith("/api/", StringComparison.Ordinal))
|
||||
{
|
||||
await DirectAsync(context, path).ConfigureAwait(false);
|
||||
return;
|
||||
}
|
||||
|
||||
await PageAsync(context, path).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
private async Task PageAsync(HttpContext context, string path)
|
||||
{
|
||||
var mode = Control.Mode;
|
||||
Control.OnPageVisit(path);
|
||||
if (path is "/verifica" or "/verifica/")
|
||||
{
|
||||
await HtmlAsync(context, 200, Pages.Challenge()).ConfigureAwait(false);
|
||||
@@ -195,14 +202,16 @@ public sealed class TestSite : IAsyncDisposable
|
||||
context.Response.Redirect("/verifica?motivo=prova");
|
||||
return;
|
||||
case StopMode.SessionEnded:
|
||||
ExpireSession(context);
|
||||
EndSession(context);
|
||||
context.Response.Redirect("/sessione-scaduta");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!context.Request.Cookies.ContainsKey(SessionCookie))
|
||||
if (!Control.IsSession(context.Request.Cookies[SessionCookie]))
|
||||
{
|
||||
context.Response.Cookies.Append(SessionCookie, "attiva", new CookieOptions { Path = "/", MaxAge = TimeSpan.FromDays(1), SameSite = SameSiteMode.Lax, HttpOnly = true });
|
||||
// No cookie, or one the site ended (a session that expired, a reset): the visit is a new login, with a new
|
||||
// value that the site client accepts too once copied.
|
||||
context.Response.Cookies.Append(SessionCookie, Control.OpenSession(), new CookieOptions { Path = "/", MaxAge = TimeSpan.FromDays(1), SameSite = SameSiteMode.Lax, HttpOnly = true });
|
||||
}
|
||||
|
||||
var segments = path.Split('/', StringSplitOptions.RemoveEmptyEntries).Select(Uri.UnescapeDataString).ToArray();
|
||||
@@ -272,7 +281,7 @@ public sealed class TestSite : IAsyncDisposable
|
||||
await JsonAsync(context, 403, new JsonObject { ["errore"] = "verifica" }).ConfigureAwait(false);
|
||||
return;
|
||||
case StopMode.SessionEnded:
|
||||
ExpireSession(context);
|
||||
EndSession(context);
|
||||
await JsonAsync(context, 401, new JsonObject { ["errore"] = "sessione" }).ConfigureAwait(false);
|
||||
return;
|
||||
case StopMode.EmptyPages:
|
||||
@@ -310,13 +319,28 @@ public sealed class TestSite : IAsyncDisposable
|
||||
return;
|
||||
}
|
||||
|
||||
var result = Lookup(segments, query);
|
||||
if (result is null)
|
||||
{
|
||||
await JsonAsync(context, 404, new JsonObject { ["errore"] = "non-trovato" }).ConfigureAwait(false);
|
||||
return;
|
||||
}
|
||||
|
||||
await JsonAsync(context, 200, result).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The JSON of a data address (<c>/dati/…</c> for the pages, the same under <c>/api/dati/…</c> for the site client),
|
||||
/// by its segments from «dati» on; null when there is nothing there.
|
||||
/// </summary>
|
||||
private JsonObject? Lookup(string[] segments, IQueryCollection query)
|
||||
{
|
||||
var tab = query["scheda"].ToString() switch { "reel" => ProfileTab.Reels, "taggati" => ProfileTab.Tagged, _ => ProfileTab.Posts };
|
||||
var after = query["dopo"].ToString();
|
||||
JsonObject? result;
|
||||
lock (World.Gate)
|
||||
{
|
||||
var api = Api;
|
||||
result = segments switch
|
||||
return segments switch
|
||||
{
|
||||
["dati", "inizio"] => api.Home(),
|
||||
["dati", "inizio", "altri"] => api.HomeMore(after),
|
||||
@@ -329,14 +353,6 @@ public sealed class TestSite : IAsyncDisposable
|
||||
_ => null,
|
||||
};
|
||||
}
|
||||
|
||||
if (result is null)
|
||||
{
|
||||
await JsonAsync(context, 404, new JsonObject { ["errore"] = "non-trovato" }).ConfigureAwait(false);
|
||||
return;
|
||||
}
|
||||
|
||||
await JsonAsync(context, 200, result).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
/// <summary>The answer of the search box, after the waits the control asks for (slow, out of order).</summary>
|
||||
@@ -415,6 +431,28 @@ public sealed class TestSite : IAsyncDisposable
|
||||
case "stato":
|
||||
answer["modo"] = Control.Mode.ToString();
|
||||
answer["richieste"] = Control.DataRequests.Count;
|
||||
answer["dirette"] = Control.DirectRequests.Count;
|
||||
answer["accesso"] = Control.LoginPageVisits;
|
||||
answer["verifica"] = Control.ChallengePageVisits;
|
||||
break;
|
||||
case "diretto":
|
||||
var reply = q["valore"].ToString() switch
|
||||
{
|
||||
"troppe-richieste" => DirectReply.TooManyRequests,
|
||||
"accesso" => DirectReply.RedirectToLogin,
|
||||
"verifica" => DirectReply.RedirectToChallenge,
|
||||
"html" => DirectReply.HtmlInsteadOfJson,
|
||||
"senza-campi" => DirectReply.MissingRootField,
|
||||
"rinnova-cookie" => DirectReply.RenewSessionCookie,
|
||||
"cancella-cookie" => DirectReply.DeleteSessionCookie,
|
||||
"claim" => DirectReply.SetClaim,
|
||||
_ => DirectReply.Normal,
|
||||
};
|
||||
Control.ReplyToDirect(reply, always: q["sempre"].ToString() is "si" or "sì" or "true");
|
||||
answer["risposta"] = reply.ToString();
|
||||
break;
|
||||
case "diretto/ritardo":
|
||||
Control.DirectDelay = TimeSpan.FromMilliseconds(int.Parse(q["ms"].ToString(), CultureInfo.InvariantCulture));
|
||||
break;
|
||||
case "modo":
|
||||
var mode = q["valore"].ToString() switch
|
||||
@@ -509,8 +547,19 @@ public sealed class TestSite : IAsyncDisposable
|
||||
await stream.CopyToAsync(context.Response.Body).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
private static void ExpireSession(HttpContext context) =>
|
||||
/// <summary>
|
||||
/// The session of the request ends on the site (pages and site client alike) and its cookie is deleted: a copy of
|
||||
/// the same value held elsewhere stops working too.
|
||||
/// </summary>
|
||||
private void EndSession(HttpContext context)
|
||||
{
|
||||
if (context.Request.Cookies[SessionCookie] is { Length: > 0 } ended)
|
||||
{
|
||||
Control.EndSession(ended);
|
||||
}
|
||||
|
||||
context.Response.Cookies.Delete(SessionCookie, new CookieOptions { Path = "/" });
|
||||
}
|
||||
|
||||
private static Task HtmlAsync(HttpContext context, int status, string html)
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user