Niente più browser nel bot: la sessione di Bidoo si incolla da Chrome (#36)
I cookie di accesso di Bidoo (__stattrb, PHPSESSID) sono HttpOnly: nessuno script o copia della pagina li vede, quindi non esiste un modo più semplice degli strumenti per sviluppatori. Lì Chrome ha già «Copia come cURL», che porta cookie e User-Agent in un colpo. Per questo, come deciso da Alberto, il browser del bot se ne va. - Session/SessionPaste riconosce da solo cURL (bash e cmd, -b o -H cookie, -A o -H user-agent), «Copia come PowerShell», intestazioni della richiesta (anche nome e valore su righe diverse), la riga Cookie, righe della tabella dei cookie e il solo valore di __stattrb; rifiuta un testo senza il cookie di accesso. Lo User-Agent del testo vale più di quello del campo. - La pagina Browser diventa «Accesso a Bidoo» (#/accesso, #/browser ci porta): riquadro da incollare, istruzioni per Chrome, stato della sessione. - Via AssistedLogin, i comandi login.* e i messaggi login.frame/login.state; via chromium e fonts-liberation dall'immagine, e prova-immagine.sh controlla che Chromium non ci sia. All'avvio il bot cancella /config/browser, il profilo della 1.x con una copia dei cookie. - Il client CDP e l'avvio di Chromium passano in tools/Mimante.Schermate, che usa Chrome o Edge del PC. - ADR 0017 (supera la 0003; la 0010 resta per la base Debian), README, architettura, guida, docker.md, stato e CHANGELOG aggiornati. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
f92b8d08f0
commit
88c16568fb
38 files changed
+632
-838
No files matched your search
@@ -207,7 +207,7 @@ public class MonitorAgainstSimulatorTests
|
||||
Assert.True(done.LastDecision!.Shadow);
|
||||
var (ok, why) = await rig.Monitor.BidNowAsync(target.Id, CancellationToken.None);
|
||||
Assert.False(ok);
|
||||
Assert.Contains("Browser", why);
|
||||
Assert.Contains("Accesso a Bidoo", why);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using System.Threading.Channels;
|
||||
using Mimante.Bidoo;
|
||||
using Mimante.Config;
|
||||
using Mimante.Data;
|
||||
@@ -8,7 +7,6 @@ using Mimante.Logging;
|
||||
using Mimante.Ml;
|
||||
using Mimante.Net;
|
||||
using Mimante.Session;
|
||||
using Mimante.Session.Cdp;
|
||||
using Mimante.Settings;
|
||||
using Mimante.Simulator;
|
||||
using Mimante.Telegram;
|
||||
@@ -71,106 +69,134 @@ public class SessionStoreTests
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void I_cookie_incollati_si_normalizzano_e_serve_quello_di_sessione()
|
||||
public void Il_profilo_del_browser_della_1x_si_cancella_all_avvio()
|
||||
{
|
||||
Assert.Equal("__stattrb=1; a=2", SessionStore.NormalizePastedCookie("Cookie: __stattrb=1; a=2\r\n").Cookie);
|
||||
Assert.Contains("__stattrb", SessionStore.NormalizePastedCookie("PHPSESSID=9").Error);
|
||||
Assert.NotNull(SessionStore.NormalizePastedCookie(" ").Error);
|
||||
using var dir = new TempDir();
|
||||
var paths = new ConfigPaths(dir.Path);
|
||||
Assert.False(paths.RemoveOldBrowserProfile());
|
||||
Directory.CreateDirectory(Path.Combine(paths.OldBrowserProfile, "Default"));
|
||||
File.WriteAllText(Path.Combine(paths.OldBrowserProfile, "Default", "Cookies"), "x");
|
||||
Assert.True(paths.RemoveOldBrowserProfile());
|
||||
Assert.False(Directory.Exists(paths.OldBrowserProfile));
|
||||
paths.EnsureAll();
|
||||
Assert.False(Directory.Exists(paths.OldBrowserProfile));
|
||||
}
|
||||
}
|
||||
|
||||
public class CdpClientTests
|
||||
/// <summary>La sessione incollata (ADR 0017): ogni modo in cui Chrome la lascia copiare.</summary>
|
||||
public class SessionPasteTests
|
||||
{
|
||||
private sealed class FakeTransport : ICdpTransport
|
||||
private const string Ua = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36";
|
||||
|
||||
[Fact]
|
||||
public void Copy_as_cURL_per_bash_con_i_cookie_in_b()
|
||||
{
|
||||
private readonly Channel<string> _incoming = Channel.CreateUnbounded<string>();
|
||||
public List<JsonElement> Sent { get; } = new();
|
||||
public Func<JsonElement, string?>? Reply { get; set; }
|
||||
|
||||
public Task SendAsync(string json, CancellationToken ct)
|
||||
{
|
||||
var msg = JsonDocument.Parse(json).RootElement.Clone();
|
||||
lock (Sent) Sent.Add(msg);
|
||||
if (Reply?.Invoke(msg) is { } answer) Push(answer);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public async Task<string?> ReceiveAsync(CancellationToken ct)
|
||||
{
|
||||
try { return await _incoming.Reader.ReadAsync(ct); }
|
||||
catch (ChannelClosedException) { return null; }
|
||||
}
|
||||
|
||||
public void Push(string json) => _incoming.Writer.TryWrite(json);
|
||||
public void Close() => _incoming.Writer.TryComplete();
|
||||
public ValueTask DisposeAsync() { Close(); return ValueTask.CompletedTask; }
|
||||
var text = $"""
|
||||
curl 'https://it.bidoo.com/' \
|
||||
-H 'accept: text/html,application/xhtml+xml' \
|
||||
-H 'accept-language: it-IT,it;q=0.9' \
|
||||
-b '__stattrb=AbC123%3D%3D; PHPSESSID=s3ss10n; _ga=GA1.2.3' \
|
||||
-H 'priority: u=0, i' \
|
||||
-H 'user-agent: {Ua}'
|
||||
""";
|
||||
var p = SessionPaste.Parse(text);
|
||||
Assert.Null(p.Error);
|
||||
Assert.Equal("__stattrb=AbC123%3D%3D; PHPSESSID=s3ss10n; _ga=GA1.2.3", p.Cookie);
|
||||
Assert.Equal(Ua, p.UserAgent);
|
||||
Assert.Equal("cURL", p.Format);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Le_risposte_si_abbinano_per_id_e_gli_errori_diventano_eccezioni()
|
||||
public void Copy_as_cURL_con_l_intestazione_cookie_e_le_virgolette_ansi()
|
||||
{
|
||||
var t = new FakeTransport
|
||||
{
|
||||
Reply = m => m.GetProperty("method").GetString() == "Boom"
|
||||
? JsonSerializer.Serialize(new { id = m.GetProperty("id").GetInt32(), error = new { code = -32000, message = "non va" } })
|
||||
: JsonSerializer.Serialize(new { id = m.GetProperty("id").GetInt32(), result = new { echo = m.GetProperty("method").GetString() } })
|
||||
};
|
||||
await using var cdp = new CdpClient(t);
|
||||
|
||||
var a = cdp.SendAsync("Page.enable", sessionId: "S1");
|
||||
var b = cdp.SendAsync("Target.createTarget", new { url = "about:blank" });
|
||||
Assert.Equal("Page.enable", (await a).GetProperty("echo").GetString());
|
||||
Assert.Equal("Target.createTarget", (await b).GetProperty("echo").GetString());
|
||||
|
||||
var sent = t.Sent.First(x => x.GetProperty("method").GetString() == "Page.enable");
|
||||
Assert.Equal("S1", sent.GetProperty("sessionId").GetString());
|
||||
Assert.Equal("about:blank", t.Sent.First(x => x.GetProperty("method").GetString() == "Target.createTarget").GetProperty("params").GetProperty("url").GetString());
|
||||
|
||||
var ex = await Assert.ThrowsAsync<CdpException>(() => cdp.SendAsync("Boom"));
|
||||
Assert.Contains("non va", ex.Message);
|
||||
var text = "curl 'https://it.bidoo.com/data.php' -H 'Cookie: __stattrb=x1; PHPSESSID=y2' -b $'nota=l\\'altra' -A 'UA di prova' --compressed";
|
||||
var p = SessionPaste.Parse(text);
|
||||
Assert.Equal("__stattrb=x1; PHPSESSID=y2; nota=l'altra", p.Cookie);
|
||||
Assert.Equal("UA di prova", p.UserAgent);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Gli_eventi_portano_la_sessione_e_la_chiusura_sblocca_le_attese()
|
||||
public void Copy_as_cURL_per_cmd_con_gli_accenti_circonflessi()
|
||||
{
|
||||
var t = new FakeTransport();
|
||||
await using var cdp = new CdpClient(t);
|
||||
var got = new TaskCompletionSource<(string, string?, string?)>();
|
||||
cdp.Event += (m, p, s) => got.TrySetResult((m, s, p.GetProperty("url").GetString()));
|
||||
|
||||
t.Push("""{"method":"Page.frameNavigated","params":{"url":"https://it.bidoo.com/"},"sessionId":"S9"}""");
|
||||
var (method, session, url) = await got.Task.WaitAsync(TimeSpan.FromSeconds(5));
|
||||
Assert.Equal("Page.frameNavigated", method);
|
||||
Assert.Equal("S9", session);
|
||||
Assert.Equal("https://it.bidoo.com/", url);
|
||||
|
||||
var pending = cdp.SendAsync("Network.getCookies");
|
||||
t.Close();
|
||||
await Assert.ThrowsAsync<CdpException>(() => pending);
|
||||
Assert.True(cdp.IsClosed);
|
||||
var text = "curl ^\"https://it.bidoo.com/^\" ^\r\n" +
|
||||
" -H ^\"accept: text/html^\" ^\r\n" +
|
||||
" -b ^\"__stattrb=AbC123^%^3D; PHPSESSID=s3ss10n^\" ^\r\n" +
|
||||
" -H ^\"user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64)^\"";
|
||||
var p = SessionPaste.Parse(text);
|
||||
Assert.Null(p.Error);
|
||||
Assert.Equal("__stattrb=AbC123%3D; PHPSESSID=s3ss10n", p.Cookie);
|
||||
Assert.Equal("Mozilla/5.0 (Windows NT 10.0; Win64; x64)", p.UserAgent);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Il_login_si_riconosce_anche_come_finestra_sulla_home()
|
||||
public void Copy_as_PowerShell()
|
||||
{
|
||||
var refs = BidooReferences.Default;
|
||||
Assert.True(AssistedLogin.IsLoginUrl("https://it.bidoo.com/login.php", refs));
|
||||
Assert.True(AssistedLogin.IsLoginUrl("https://it.bidoo.com/?login=true&redirect=index.php", refs));
|
||||
Assert.True(AssistedLogin.IsLoginUrl("", refs));
|
||||
Assert.False(AssistedLogin.IsLoginUrl("https://it.bidoo.com/index.php", refs));
|
||||
var text = """
|
||||
$session = New-Object Microsoft.PowerShell.Commands.WebRequestSession
|
||||
$session.UserAgent = "Mozilla/5.0 (Windows NT 10.0)"
|
||||
$session.Cookies.Add((New-Object System.Net.Cookie("__stattrb", "AbC123%3D%3D", "/", ".bidoo.com")))
|
||||
$session.Cookies.Add((New-Object System.Net.Cookie("PHPSESSID", "s3ss10n", "/", "it.bidoo.com")))
|
||||
Invoke-WebRequest -UseBasicParsing -Uri "https://it.bidoo.com/" `
|
||||
-WebSession $session
|
||||
""";
|
||||
var p = SessionPaste.Parse(text);
|
||||
Assert.Equal("__stattrb=AbC123%3D%3D; PHPSESSID=s3ss10n", p.Cookie);
|
||||
Assert.Equal("Mozilla/5.0 (Windows NT 10.0)", p.UserAgent);
|
||||
Assert.Equal("PowerShell", p.Format);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DevToolsActivePort_da_l_indirizzo_del_browser()
|
||||
public void Le_intestazioni_della_richiesta_in_riga_o_con_il_valore_sotto_il_nome()
|
||||
{
|
||||
using var dir = new TempDir();
|
||||
var file = Path.Combine(dir.Path, "DevToolsActivePort");
|
||||
File.WriteAllText(file, "41233\n/devtools/browser/abc-123\n");
|
||||
Assert.Equal("ws://127.0.0.1:41233/devtools/browser/abc-123", ChromiumProcess.TryReadPort(file)!.ToString());
|
||||
File.WriteAllText(file, "x\n");
|
||||
Assert.Null(ChromiumProcess.TryReadPort(file));
|
||||
Assert.Contains("--headless=new", ChromiumProcess.Arguments("/config/browser"));
|
||||
Assert.Contains("--user-data-dir=/config/browser", ChromiumProcess.Arguments("/config/browser"));
|
||||
var raw = SessionPaste.Parse("GET / HTTP/1.1\r\nHost: it.bidoo.com\r\nCookie: __stattrb=AbC; PHPSESSID=s\r\nUser-Agent: Mozilla/5.0 X\r\n");
|
||||
Assert.Equal("__stattrb=AbC; PHPSESSID=s", raw.Cookie);
|
||||
Assert.Equal("Mozilla/5.0 X", raw.UserAgent);
|
||||
|
||||
var panel = SessionPaste.Parse(":authority\nit.bidoo.com\naccept-language\nit-IT,it;q=0.9\ncookie\n__stattrb=AbC; PHPSESSID=s\nuser-agent:\nMozilla/5.0 Y\n");
|
||||
Assert.Equal("__stattrb=AbC; PHPSESSID=s", panel.Cookie);
|
||||
Assert.Equal("Mozilla/5.0 Y", panel.UserAgent);
|
||||
Assert.Equal("intestazioni", panel.Format);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void La_riga_cookie_da_sola_con_o_senza_prefisso()
|
||||
{
|
||||
Assert.Equal("__stattrb=1; a=2", SessionPaste.Parse("Cookie: __stattrb=1; a=2\r\n").Cookie);
|
||||
var line = SessionPaste.Parse("__stattrb=1; a=2");
|
||||
Assert.Equal("__stattrb=1; a=2", line.Cookie);
|
||||
Assert.Null(line.UserAgent);
|
||||
Assert.Equal("riga Cookie", line.Format);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Le_righe_della_tabella_dei_cookie_e_il_solo_valore()
|
||||
{
|
||||
var table = SessionPaste.Parse("__stattrb\tAbC123\tit.bidoo.com\t/\t2027-01-01T00:00:00.000Z\t15\t✓\n" +
|
||||
"PHPSESSID\ts3ss10n\tit.bidoo.com\t/\tSessione\t17\t✓");
|
||||
Assert.Equal("__stattrb=AbC123; PHPSESSID=s3ss10n", table.Cookie);
|
||||
|
||||
Assert.Equal("__stattrb=AbC123%3D%3D", SessionPaste.Parse(" AbC123%3D%3D \n").Cookie);
|
||||
Assert.Equal("__stattrb=QWxhZGRpbjpvcGVu==", SessionPaste.Parse("QWxhZGRpbjpvcGVu==").Cookie);
|
||||
Assert.Equal("__sess=v", SessionPaste.Parse("v", "__sess").Cookie);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Senza_il_cookie_di_accesso_la_sessione_non_si_accetta()
|
||||
{
|
||||
Assert.Contains("__stattrb", SessionPaste.Parse("PHPSESSID=9").Error);
|
||||
Assert.Contains("__stattrb", SessionPaste.Parse("__stattrb=; PHPSESSID=9").Error);
|
||||
Assert.Contains("__stattrb", SessionPaste.Parse("curl 'https://it.bidoo.com/' -b 'PHPSESSID=9' -H 'user-agent: X'").Error);
|
||||
Assert.NotNull(SessionPaste.Parse("curl 'https://it.bidoo.com/' -H 'accept: */*'").Error);
|
||||
Assert.NotNull(SessionPaste.Parse("ciao, come stai?").Error);
|
||||
Assert.NotNull(SessionPaste.Parse(" ").Error);
|
||||
Assert.NotNull(SessionPaste.Parse(null).Error);
|
||||
Assert.Null(SessionPaste.Parse("PHPSESSID=9").Cookie);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Le_parole_della_riga_di_comando()
|
||||
{
|
||||
Assert.Equal(new[] { "curl", "a b", "c\"d", "e'f", "g\nh", "ij" }, SessionPaste.ShellWords("curl 'a b' \"c\\\"d\" $'e\\'f' $'g\\nh' i\\\n\\j"));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user