Files
Febe/src/Febe.Desktop.Core/DesktopOptions.cs
T
Alby96andClaude Opus 5.5 c87768766c
verifica / controlli (push) Successful in 22s
verifica / prove (push) Successful in 16m46s
verifica / installatore (push) Successful in 4m43s
verifica / immagine (push) Successful in 21m2s
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>
2026-11-17 16:43:43 +01:00

299 lines
12 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System.Globalization;
using System.Net;
using Febe.App;
using Febe.Browser.Recipes;
namespace Febe.Desktop.Core;
/// <summary>
/// How the browser of Instagram is hosted on Windows: the three attempts of S2, in the order of spec §6.6. The first is
/// the default; the others are fallbacks Alberto chooses with <c>--browser</c> while testing S2 (docs/windows.md).
/// </summary>
public enum BrowserHosting
{
/// <summary>«finestra»: WebView2 in its own window «Browser di Febe», kept open but out of view, occlusion off.</summary>
Window,
/// <summary>«composizione»: the same window, with WebView2 hosted as a DirectComposition visual.</summary>
Composition,
/// <summary>«edge»: Microsoft Edge started as a process and driven like Chromium in Docker (ChromiumProcessHost).</summary>
Edge,
}
/// <summary>Italian names of <see cref="BrowserHosting"/>, for the command line, the setting and the log.</summary>
public static class BrowserHostingNames
{
/// <summary>Setting key (table <c>setting</c>) of the hosting chosen with <c>--browser</c>; it stays for the next starts.</summary>
public const string SettingKey = "desktop.browser";
public static string Name(BrowserHosting hosting) => hosting switch
{
BrowserHosting.Composition => "composizione",
BrowserHosting.Edge => "edge",
_ => "finestra",
};
public static bool TryParse(string? text, out BrowserHosting hosting)
{
switch (text?.Trim().ToLowerInvariant())
{
case "finestra":
hosting = BrowserHosting.Window;
return true;
case "composizione":
hosting = BrowserHosting.Composition;
return true;
case "edge":
hosting = BrowserHosting.Edge;
return true;
default:
hosting = BrowserHosting.Window;
return false;
}
}
}
/// <summary>The folders Windows gives Febe by default (spec §12): tests pass their own.</summary>
/// <param name="LocalAppData"><c>%LOCALAPPDATA%</c>.</param>
/// <param name="Pictures">The user's Pictures folder («Immagini»).</param>
public sealed record DesktopFolders(string LocalAppData, string Pictures)
{
/// <summary>The folders of the current user.</summary>
public static DesktopFolders Current() => new(
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData, Environment.SpecialFolderOption.DoNotVerify),
Environment.GetFolderPath(Environment.SpecialFolder.MyPictures, Environment.SpecialFolderOption.DoNotVerify));
/// <summary>Default Data folder: <c>%LOCALAPPDATA%\Febe</c>.</summary>
public string DefaultDataFolder => Path.Combine(LocalAppData, "Febe");
/// <summary>Default library: <c>Immagini\Febe</c>.</summary>
public string DefaultLibraryFolder => Path.Combine(Pictures, "Febe");
}
/// <summary>Configuration of the Windows application, from its command line (spec §12).</summary>
public sealed record DesktopOptions
{
/// <summary>Data folder chosen with <c>--dati</c> (development, a second account); null: the recorded or default one.</summary>
public string? DataFolder { get; init; }
/// <summary>
/// Library chosen with <c>--libreria</c> (tests on Windows, a second account): the default of the setting instead of
/// <c>Immagini\Febe</c>. A folder chosen later in Impostazioni still wins.
/// </summary>
public string? LibraryFolder { get; init; }
/// <summary><c>--nascosto</c>: start with the icon in the notification area only (start with the computer).</summary>
public bool StartHidden { get; init; }
/// <summary><c>--browser finestra|composizione|edge</c>: the hosting of S2, stored for the next starts.</summary>
public BrowserHosting? Hosting { get; init; }
/// <summary><c>--sviluppo</c>: the developer tools of the interface window (never of the browser of Febe).</summary>
public bool DeveloperTools { get; init; }
/// <summary>Development: the local test site (tools/Febe.SitoDiProva) instead of Instagram, for S1 and S2.</summary>
public Uri? TestSite { get; init; }
/// <summary>Development: the «CDN» of the test site (default: 127.0.0.2, site port + 1).</summary>
public Uri? TestCdn { get; init; }
/// <summary>Development, test site only: opens the site once at start-up so its session cookie exists.</summary>
public bool TestSiteAutoLogin { get; init; }
/// <summary>Development: the first session after this delay instead of 2–10 minutes.</summary>
public TimeSpan? FirstSessionDelay { get; init; }
/// <summary>
/// <c>--raccolta pagine|richieste</c> (issue #36, S4): by the pages (default, as before S4) or by requests with the
/// saved session, only for the operations the site's catalog configures.
/// </summary>
public CollectionRoute Collection { get; init; } = CollectionRoute.Pages;
/// <summary>The fixed DevTools port of Edge (fallback «edge» only), on 127.0.0.1.</summary>
public int EdgeDebugPort { get; init; } = 9222;
/// <summary>Size of the page of the browser of Febe, always the same (spec §6.1).</summary>
public int BrowserWidth { get; init; } = 1280;
public int BrowserHeight { get; init; } = 900;
/// <summary>The site the browser works on: Instagram, or the local test site in development.</summary>
public CollectionSite Site => TestSite is { } site
? CollectionSite.TestSite(site.Authority, (TestCdn ?? DefaultCdn(site)).Authority)
: CollectionSite.Instagram;
/// <summary>The page «Accedi a Instagram» opens: the real login page, or a page of the test site.</summary>
public string LoginUrl => TestSite is { } site ? new Uri(site, "/benvenuto").AbsoluteUri : "https://www.instagram.com/accounts/login/";
/// <summary>The options of the shared composition.</summary>
public FebeAppOptions AppOptions(string dataFolder, DesktopFolders folders) => new()
{
DataFolder = dataFolder,
LibraryFolder = LibraryFolder ?? folders.DefaultLibraryFolder,
LibraryFolderFromSettings = true,
ImportFolder = null,
ZoneId = TimeZoneInfo.Local.Id,
Zone = TimeZoneInfo.Local,
Language = CultureInfo.CurrentUICulture.Name,
Site = Site,
LoginUrl = LoginUrl,
TestSite = TestSite,
TestSiteAutoLogin = TestSiteAutoLogin,
FirstSessionDelay = FirstSessionDelay,
Collection = Collection,
};
private static Uri DefaultCdn(Uri site) => new UriBuilder(site) { Host = "127.0.0.2", Port = site.Port + 1, Path = "/" }.Uri;
}
/// <summary>Reads the command line of Febe.exe. Nothing secret ever passes on it.</summary>
public static class DesktopCommandLine
{
public const string Usage =
"Uso: Febe.exe [--nascosto] [--dati cartella] [--libreria cartella] [--browser finestra|composizione|edge] [--sviluppo]\n"
+ " [--sito-di-prova http://127.0.0.1:8481 [--cdn-di-prova http://127.0.0.2:8482] [--accesso-di-prova]] [--prima-sessione secondi]\n"
+ " [--raccolta pagine|richieste]";
public static bool TryParse(string[] args, out DesktopOptions options, out string problem)
{
ArgumentNullException.ThrowIfNull(args);
options = new DesktopOptions();
problem = string.Empty;
for (var i = 0; i < args.Length; i++)
{
var arg = args[i];
string? Next() => i + 1 < args.Length ? args[++i] : null;
switch (arg.ToLowerInvariant())
{
case "--nascosto":
options = options with { StartHidden = true };
break;
case "--sviluppo":
options = options with { DeveloperTools = true };
break;
case "--dati":
{
var text = Next();
if (string.IsNullOrWhiteSpace(text) || !Path.IsPathFullyQualified(text))
{
problem = "Dopo --dati serve il percorso completo di una cartella.";
return false;
}
options = options with { DataFolder = Path.GetFullPath(text) };
break;
}
case "--libreria":
{
var text = Next();
if (string.IsNullOrWhiteSpace(text) || !Path.IsPathFullyQualified(text))
{
problem = "Dopo --libreria serve il percorso completo di una cartella.";
return false;
}
options = options with { LibraryFolder = Path.GetFullPath(text) };
break;
}
case "--browser":
{
var text = Next();
if (!BrowserHostingNames.TryParse(text, out var hosting))
{
problem = $"Valore non valido dopo --browser («{text}»): finestra, composizione oppure edge.";
return false;
}
options = options with { Hosting = hosting };
break;
}
case "--sito-di-prova":
case "--cdn-di-prova":
{
var text = Next();
if (!TryLocalAddress(text, out var address))
{
problem = $"Indirizzo non valido dopo {arg} («{text}»): serve un indirizzo http di questo computer, per esempio http://127.0.0.1:8481.";
return false;
}
options = arg.Equals("--sito-di-prova", StringComparison.OrdinalIgnoreCase)
? options with { TestSite = address }
: options with { TestCdn = address };
break;
}
case "--accesso-di-prova":
options = options with { TestSiteAutoLogin = true };
break;
case CollectionRouteNames.Option:
{
var text = Next();
if (!CollectionRouteNames.TryParse(text, out var route))
{
problem = CollectionRouteNames.Problem(CollectionRouteNames.Option, text);
return false;
}
options = options with { Collection = route };
break;
}
case "--prima-sessione":
{
var text = Next();
if (!int.TryParse(text, NumberStyles.Integer, CultureInfo.InvariantCulture, out var seconds) || seconds is < 1 or > 3600)
{
problem = $"Valore non valido dopo --prima-sessione («{text}»): secondi fra 1 e 3600.";
return false;
}
options = options with { FirstSessionDelay = TimeSpan.FromSeconds(seconds) };
break;
}
default:
problem = $"Opzione sconosciuta: {arg}.";
return false;
}
}
if (options.TestSiteAutoLogin && options.TestSite is null)
{
problem = "--accesso-di-prova vale solo con --sito-di-prova: con Instagram l'accesso lo fa sempre Alberto, a mano.";
return false;
}
if (options.TestCdn is not null && options.TestSite is null)
{
problem = "--cdn-di-prova vale solo con --sito-di-prova.";
return false;
}
return true;
}
/// <summary>The test site must be on this computer: the switch can never point the browser elsewhere.</summary>
private static bool TryLocalAddress(string? text, out Uri address)
{
address = null!;
if (!Uri.TryCreate(text, UriKind.Absolute, out var uri) || uri.Scheme != Uri.UriSchemeHttp)
{
return false;
}
var local = uri.IsLoopback || (IPAddress.TryParse(uri.Host, out var ip) && IPAddress.IsLoopback(ip));
if (!local)
{
return false;
}
address = new Uri(uri.GetLeftPart(UriPartial.Authority) + "/");
return true;
}
}