Strato browser (M0/M2): client CDP con elenco dei comandi ammessi, ChromiumProcessHost e Xvfb sorvegliati, gesti umani, mappa dei bersagli, cattura delle risposte, segnali di arresto, scaricamento dal browser, vista remota, distribuzioni del ritmo; prove unitarie e d'insieme con Chromium su Xvfb; esito locale di S5 e S6
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Mzbb6zeFTjT6Rp8b92AVkA
This commit is contained in:
44 files changed
+8396
No files matched your search
@@ -0,0 +1,242 @@
|
||||
using System.Text.Json;
|
||||
using System.Text.RegularExpressions;
|
||||
using Febe.Core.Abstractions;
|
||||
|
||||
namespace Febe.Core.Collection;
|
||||
|
||||
/// <summary>
|
||||
/// Turns what the page sees (responses, navigations, JSON bodies, empty pages, the session cookie) into stop
|
||||
/// signals (spec §7.6) and reports them to the circuit breaker. It never logs or keeps bodies.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// The same signal code is reported at most once per <see cref="DedupeWindow"/>: a page that receives ten 429
|
||||
/// responses in a second is one throttle, not ten. Thread-safe.
|
||||
/// </remarks>
|
||||
public sealed class SignalDetector : IResponseObserver, IResponseConsumer
|
||||
{
|
||||
private readonly SignalRules _rules;
|
||||
private readonly ICircuitBreaker _breaker;
|
||||
private readonly IClock _clock;
|
||||
private readonly Dictionary<string, DateTimeOffset> _lastReported = new(StringComparer.Ordinal);
|
||||
private readonly Lock _gate = new();
|
||||
private int _consecutiveEmpty;
|
||||
|
||||
public SignalDetector(SignalRules rules, ICircuitBreaker breaker, IClock clock, TimeSpan? dedupeWindow = null)
|
||||
{
|
||||
_rules = rules ?? throw new ArgumentNullException(nameof(rules));
|
||||
_breaker = breaker ?? throw new ArgumentNullException(nameof(breaker));
|
||||
_clock = clock ?? throw new ArgumentNullException(nameof(clock));
|
||||
DedupeWindow = dedupeWindow ?? TimeSpan.FromSeconds(60);
|
||||
}
|
||||
|
||||
public TimeSpan DedupeWindow { get; }
|
||||
|
||||
/// <summary>Raised after a signal has been reported to the breaker.</summary>
|
||||
public event EventHandler<Signal>? SignalRaised;
|
||||
|
||||
void IResponseObserver.OnResponse(ResponseDescriptor response) => ObserveResponse(response);
|
||||
|
||||
void IResponseObserver.OnNavigation(NavigationDescriptor navigation) => ObserveNavigation(navigation);
|
||||
|
||||
ValueTask IResponseConsumer.ConsumeAsync(ResponseDescriptor response, JsonDocument body, CancellationToken ct)
|
||||
{
|
||||
ObserveJsonBody(response, body.RootElement);
|
||||
return ValueTask.CompletedTask;
|
||||
}
|
||||
|
||||
/// <summary>Status and path rules on a response.</summary>
|
||||
public Signal? ObserveResponse(ResponseDescriptor response)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(response);
|
||||
var scope = response.IsMainFrameDocument ? SignalScope.Document : SignalScope.Subresource;
|
||||
foreach (var rule in _rules.Rules)
|
||||
{
|
||||
if (!rule.HostKinds.Contains(response.HostKind) || (rule.Scope & scope) == 0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if ((rule.Statuses.Count > 0 && rule.Statuses.Contains(response.Status))
|
||||
|| (rule.Path is not null && SafeIsMatch(rule.Path, response.Path)))
|
||||
{
|
||||
return Raise(rule.Level, rule.Code, response.Url);
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>Path rules on a main-frame navigation.</summary>
|
||||
public Signal? ObserveNavigation(NavigationDescriptor navigation)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(navigation);
|
||||
foreach (var rule in _rules.Rules)
|
||||
{
|
||||
if (rule.Path is null || (rule.Scope & SignalScope.Navigation) == 0 || !rule.HostKinds.Contains(navigation.HostKind))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (SafeIsMatch(rule.Path, navigation.Path))
|
||||
{
|
||||
return Raise(rule.Level, rule.Code, navigation.Url);
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>Text rules on the string values of a short JSON response.</summary>
|
||||
public Signal? ObserveJsonBody(ResponseDescriptor response, JsonElement root)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(response);
|
||||
foreach (var rule in _rules.Rules)
|
||||
{
|
||||
if (rule.Text is null || !rule.HostKinds.Contains(response.HostKind))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (ContainsText(root, rule.Text, rule.MaxTextChars))
|
||||
{
|
||||
return Raise(rule.Level, rule.Code, response.Url);
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A recipe loaded a page (or a page of data) that turned out empty. After the configured number of
|
||||
/// consecutive empty pages a throttle signal is raised. The caller decides what «empty» means.
|
||||
/// </summary>
|
||||
public Signal? ObserveEmptyPage(string? url = null)
|
||||
{
|
||||
if (_rules.EmptyPages is not { } rule)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
lock (_gate)
|
||||
{
|
||||
_consecutiveEmpty++;
|
||||
if (_consecutiveEmpty < rule.Consecutive)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
_consecutiveEmpty = 0;
|
||||
}
|
||||
|
||||
return Raise(rule.Level, rule.Code, url is null ? null : UrlSanitizer.Sanitize(url).Url);
|
||||
}
|
||||
|
||||
/// <summary>A recipe loaded a page with content: resets the empty-page counter.</summary>
|
||||
public void ObserveNonEmptyPage()
|
||||
{
|
||||
lock (_gate)
|
||||
{
|
||||
_consecutiveEmpty = 0;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Result of the session-cookie check: a missing cookie is a disconnection (spec §7.6).</summary>
|
||||
public Signal? ObserveSessionCookie(bool present) =>
|
||||
present ? null : Raise(SignalLevel.Disconnect, SignalRules.SessionCookieMissingCode, null);
|
||||
|
||||
private Signal? Raise(SignalLevel level, string code, string? url)
|
||||
{
|
||||
var now = _clock.Now;
|
||||
lock (_gate)
|
||||
{
|
||||
if (_lastReported.TryGetValue(code, out var last) && now - last < DedupeWindow)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
_lastReported[code] = now;
|
||||
}
|
||||
|
||||
var signal = new Signal(level, code, url, now);
|
||||
_breaker.Report(signal);
|
||||
SignalRaised?.Invoke(this, signal);
|
||||
return signal;
|
||||
}
|
||||
|
||||
private static bool SafeIsMatch(Regex regex, string input)
|
||||
{
|
||||
try
|
||||
{
|
||||
return regex.IsMatch(input);
|
||||
}
|
||||
catch (RegexMatchTimeoutException)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Looks for <paramref name="regex"/> in the string values, giving up when they exceed <paramref name="maxChars"/>.</summary>
|
||||
private static bool ContainsText(JsonElement root, Regex regex, int maxChars)
|
||||
{
|
||||
var budget = maxChars;
|
||||
var strings = new List<string>();
|
||||
if (!Collect(root, strings, ref budget, depth: 0))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
foreach (var s in strings)
|
||||
{
|
||||
if (SafeIsMatch(regex, s))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private static bool Collect(JsonElement element, List<string> strings, ref int budget, int depth)
|
||||
{
|
||||
if (depth > 32)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
switch (element.ValueKind)
|
||||
{
|
||||
case JsonValueKind.String:
|
||||
var value = element.GetString() ?? string.Empty;
|
||||
budget -= value.Length;
|
||||
if (budget < 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
strings.Add(value);
|
||||
return true;
|
||||
case JsonValueKind.Object:
|
||||
foreach (var property in element.EnumerateObject())
|
||||
{
|
||||
if (!Collect(property.Value, strings, ref budget, depth + 1))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
case JsonValueKind.Array:
|
||||
foreach (var item in element.EnumerateArray())
|
||||
{
|
||||
if (!Collect(item, strings, ref budget, depth + 1))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
default:
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user