Files
Febe/src/Febe.Core/Collection/HostClassifier.cs
T
Alby96andClaude Opus 5.5 31bbbf8bb7
verifica / controlli (push) Successful in 2m10s
verifica / prove (push) Successful in 26m16s
verifica / installatore (push) Successful in 5m39s
verifica / immagine (push) Successful in 28m39s
Instagram: Febe legge le risposte vere (campioni dal HAR del 21/10)
Febe non estraeva nulla da Instagram per due motivi. Instagram manda i
dati di storie e raccolte (GraphQL) come text/javascript, e la cattura
leggeva solo le risposte dichiarate JSON: le scartava tutte. E i file di
Instagram (lettori, bersagli) erano scheletri, quindi le ricette si
rifiutavano di girare.

Alberto ha mandato un HAR del suo browser e una guida. La guida descrive
richieste costruite a mano, vietate dalle regole 2-4, e un formato che
nel HAR non compare: non si usa. Dal HAR si è letta solo la forma; le
risposte utili sono diventate campioni grezzi fuori dal repository,
ripuliti e verificati con febe-sonda (tests/Febe.Tests/campioni/instagram).

Che cosa cambia:
- la cattura legge le risposte text/javascript chieste dalla pagina (mai
  gli script) e ogni blocco JSON incorporato nel documento della pagina,
  e avvisa quando un corpo atteso non arriverà;
- le risposte che non parlano di contenuti (promozioni, «storia vista»)
  si ignorano invece di valere come formato non riconosciuto;
- lettori.instagram.json è confermato, con sole chiavi presenti nei
  campioni; identificativi delle raccolte nella forma dell'indirizzo;
- bersagli.instagram.json ha le pagine viste nel traffico vero; i
  bersagli restano vuoti (H11): storie, raccolte, Taggati e Reel
  aspettano lo script tools/bersagli/raccogli-bersagli.js;
- le ricette del profilo reggono dati in risposte diverse e prendono la
  paginazione solo dalla risposta che porta i loro post; l'elenco dei
  seguiti scarta i profili suggeriti; «Tu» si riconosce anche dal solo
  identificativo dell'account collegato;
- il visualizzatore delle raccolte conta i contenuti (l'indirizzo non
  cambia), chiede il permesso prima di ogni contenuto e si chiude sempre;
- una ricetta che la configurazione non lascia girare è «Da configurare»:
  non è un guasto, non si sospende, non si pianifica; le sospensioni
  dovute ai file vecchi si azzerano una volta al cambio dei file;
- la barra laterale dice «Collegato» dal cookie di sessione; i «Post
  singolo» per gli indirizzi scaduti non vengono più annullati;
- febe-sonda da-har; la ripulitura tiene latest_reel_media e mette un
  riempitivo neutro sotto le chiavi simili a segreti (gitleaks).

Prove su Windows: 783 superate, 76 saltate (Chromium e Xvfb, nella CI).
Nulla è stato provato contro Instagram. Dettagli e decisioni in
docs/stato.md e docs/decisioni.md (righe del 25 ottobre).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-25 14:22:16 +01:00

138 lines
5.1 KiB
C#

namespace Febe.Core.Collection;
/// <summary>An address reduced to what may be kept: scheme, host and path. Never query string or fragment.</summary>
/// <param name="Url">The sanitized address, e.g. «https://www.example.org/a/b/».</param>
/// <param name="Scheme">Scheme in lower case («https», «about», «data»...).</param>
/// <param name="Host">Host in lower case, with the port when it is not the default one; empty when there is none.</param>
/// <param name="Path">Path without query and fragment; empty for opaque addresses (data:, blob:).</param>
public sealed record SanitizedUrl(string Url, string Scheme, string Host, string Path)
{
public override string ToString() => Url;
}
/// <summary>Strips query strings, fragments and credentials from addresses (spec §7.6, §13.2).</summary>
public static class UrlSanitizer
{
public static SanitizedUrl Sanitize(string? url)
{
if (string.IsNullOrWhiteSpace(url))
{
return new SanitizedUrl(string.Empty, string.Empty, string.Empty, string.Empty);
}
if (!Uri.TryCreate(url, UriKind.Absolute, out var uri))
{
// Not an absolute address: keep nothing that could carry data.
return new SanitizedUrl("(indirizzo non valido)", string.Empty, string.Empty, string.Empty);
}
var scheme = uri.Scheme.ToLowerInvariant();
if (scheme is "http" or "https" or "ws" or "wss")
{
var host = HostWithPort(uri);
var path = string.IsNullOrEmpty(uri.AbsolutePath) ? "/" : uri.AbsolutePath;
return new SanitizedUrl($"{scheme}://{host}{path}", scheme, host, path);
}
if (scheme == "about")
{
// about:blank, about:srcdoc: no data inside.
var opaque = uri.AbsolutePath.ToLowerInvariant();
return new SanitizedUrl($"about:{opaque}", scheme, string.Empty, opaque);
}
// data:, blob:, chrome:, file: ... only the scheme survives.
return new SanitizedUrl($"{scheme}:", scheme, string.Empty, string.Empty);
}
internal static string HostWithPort(Uri uri)
{
var host = uri.IdnHost.ToLowerInvariant();
if (uri.HostNameType == UriHostNameType.IPv6 && !host.StartsWith('['))
{
host = $"[{host}]";
}
return uri.IsDefaultPort ? host : $"{host}:{uri.Port}";
}
}
/// <summary>
/// Tells site, CDN and other hosts apart. Entries are host names that also match their sub-domains
/// («instagram.com» matches «www.instagram.com»), or exact «host:port» pairs (used by the local test site).
/// </summary>
public sealed class HostClassifier
{
private readonly string[] _site;
private readonly string[] _cdn;
public HostClassifier(IEnumerable<string> siteHosts, IEnumerable<string> cdnHosts)
{
_site = Normalize(siteHosts);
_cdn = Normalize(cdnHosts);
}
/// <summary>
/// Instagram's public host names: the site and the two CDN families its pages load files from. In the HAR of
/// 21 October 2026 the data came from www.instagram.com (logging from graph.instagram.com), the video files from
/// scontent-*.cdninstagram.com and the scripts from static.cdninstagram.com; fbcdn.net is public knowledge.
/// </summary>
public static HostClassifier Instagram { get; } = new(
["instagram.com"],
["cdninstagram.com", "fbcdn.net"]);
public IReadOnlyList<string> SiteHosts => _site;
public IReadOnlyList<string> CdnHosts => _cdn;
public HostKind Classify(string? url) =>
Uri.TryCreate(url, UriKind.Absolute, out var uri) ? Classify(uri) : HostKind.Other;
public HostKind Classify(Uri uri)
{
ArgumentNullException.ThrowIfNull(uri);
if (uri.Scheme is not ("http" or "https"))
{
return HostKind.Other;
}
var host = uri.IdnHost.ToLowerInvariant();
var hostWithPort = UrlSanitizer.HostWithPort(uri);
var explicitPort = $"{host}:{uri.Port}";
// CDN first: a CDN entry is more specific than a site entry when both could match.
if (Matches(_cdn, host, hostWithPort, explicitPort))
{
return HostKind.Cdn;
}
return Matches(_site, host, hostWithPort, explicitPort) ? HostKind.Site : HostKind.Other;
}
private static bool Matches(string[] entries, string host, string hostWithPort, string explicitPort)
{
foreach (var entry in entries)
{
if (entry.Contains(':', StringComparison.Ordinal))
{
if (entry == hostWithPort || entry == explicitPort)
{
return true;
}
}
else if (host == entry || host.EndsWith("." + entry, StringComparison.Ordinal))
{
return true;
}
}
return false;
}
private static string[] Normalize(IEnumerable<string> hosts)
{
ArgumentNullException.ThrowIfNull(hosts);
return [.. hosts.Select(h => h.Trim().TrimStart('.').ToLowerInvariant()).Where(h => h.Length > 0).Distinct()];
}
}