Files
Heimdall/src/Heimdall.Agent/Program.cs
T
Arnaud Dartois e5b035745d feat(agent): découverte mDNS/HomeKit — probe multicast _hap._tcp (Fixes #62)
- HomeKitMdnsProbe : paquet DNS PTR brut sur 224.0.0.251:5353, écoute 2 s
  avec timeout explicite Task.WhenAny (jamais de hang, cf. 491a253)
- Parseur DNS complet : PTR/SRV/TXT/A avec pointeurs de compression, TXT
  (ci/id/md/sf), dédup par IP, mapping IP via SRV target
- Filtre catégories 17 (camera) et 18 (doorbell) — Strix pkg/probe/mdns.go
- Intégré au scan broadcast de NetworkCameraScanner ; devices HomeKit avec
  HomeKitName/HomeKitDeviceId/HomeKitModel remplis → classés HomeKit côté central
- Les devices HomeKit ne génèrent plus de fausses URLs RTSP (flux HAP)
- Tests : 4 (parseur PTR/SRV/TXT/A, filtrage catégories, dédup, no-hang)
2026-08-11 22:16:19 +02:00

108 lines
3.9 KiB
C#

using Sentry.AspNetCore;
using Heimdall.Agent;
using Heimdall.Agent.Logging;
using Heimdall.Agent.Services;
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
var builder = WebApplication.CreateSlimBuilder(args);
// ── Sentry (GlitchTip) — DSN from config pipeline with legacy env var fallback ──
// Primary: Sentry:Dsn from appsettings.json or env var Sentry__Dsn
// Fallback: legacy SENTRY_DSN env var (Docker backward compat)
// Neither set → DSN is null → Sentry SDK no-ops gracefully
var sentryDsn = builder.Configuration["Sentry:Dsn"]
?? Environment.GetEnvironmentVariable("SENTRY_DSN");
if (!string.IsNullOrEmpty(sentryDsn))
{
builder.WebHost.UseSentry((SentryAspNetCoreOptions options) =>
{
options.Dsn = sentryDsn;
options.MinimumBreadcrumbLevel = LogLevel.Debug;
options.MinimumEventLevel = LogLevel.Error;
options.TracesSampleRate = 0.05;
options.MaxRequestBodySize = Sentry.Extensibility.RequestSize.Small;
options.ProfilesSampleRate = 0.05;
});
}
// Strongly-typed configuration with validation
builder.Services.AddOptions<AgentOptions>()
.BindConfiguration(AgentOptions.SectionName)
.ValidateDataAnnotations()
.ValidateOnStart();
// HTTP clients
builder.Services.AddHttpClient("Central", (sp, client) =>
{
var options = sp.GetRequiredService<IOptions<AgentOptions>>().Value;
var baseUrl = options.CentralUrl.EndsWith('/') ? options.CentralUrl : options.CentralUrl + "/";
client.BaseAddress = new Uri(baseUrl);
client.Timeout = TimeSpan.FromSeconds(10);
});
builder.Services.AddHttpClient("IpResolver", client =>
{
client.Timeout = TimeSpan.FromSeconds(5);
});
// Agent services
builder.Services.AddSingleton<AgentState>(sp =>
{
var state = new AgentState();
var options = sp.GetRequiredService<IOptions<AgentOptions>>().Value;
var stateDir = options.StateDirectory
?? (OperatingSystem.IsLinux() ? "/var/lib/heimdall" : Directory.GetCurrentDirectory());
state.ConfigureStateDirectory(stateDir);
return state;
});
builder.Services.AddSingleton<SystemMetricsProvider>();
builder.Services.AddSingleton<NetworkCameraScanner>();
builder.Services.AddSingleton<HomeKitMdnsProbe>();
builder.Services.AddSingleton<DiscoveryScanTrigger>();
builder.Services.AddSingleton<AgentCredentialStore>();
builder.Services.AddSingleton<RuntimeLogLevel>();
builder.Services.AddHostedService<AgentRegistrationService>();
builder.Services.AddHostedService<ConfigReceiverService>();
builder.Services.AddHostedService<AgentDiscoveryService>();
// Video capture pipeline (ticket #1): FFmpeg capture per assigned camera,
// bounded snapshot channel, adaptive interval, frames → FrameAnalyzerService.
builder.Services.AddSingleton<SceneStateTracker>();
builder.Services.AddSingleton<CameraConfigStore>();
builder.Services.AddSingleton<FrameAnalyzerService>();
builder.Services.AddSingleton<CameraStreamService>();
builder.Services.AddHostedService<SnapshotPipelineService>();
// Log capture & upload to Central (ticket #50)
builder.Services.AddSingleton<LogBufferService>();
builder.Services.AddSingleton<AgentLoggingProvider>();
builder.Services.AddSingleton<ILoggerProvider>(sp => sp.GetRequiredService<AgentLoggingProvider>());
builder.Services.AddHostedService<LogUploadService>();
// Health checks — used by Docker HEALTHCHECK
builder.Services.AddHealthChecks()
.AddCheck<AgentHealthCheck>("agent_health", tags: ["ready"]);
var app = builder.Build();
// Health endpoint
app.MapHealthChecks("/healthz", new HealthCheckOptions
{
Predicate = _ => true,
ResultStatusCodes =
{
[HealthStatus.Healthy] = StatusCodes.Status200OK,
[HealthStatus.Degraded] = StatusCodes.Status200OK,
[HealthStatus.Unhealthy] = StatusCodes.Status503ServiceUnavailable,
}
});
await app.RunAsync();