using System.Text.Json; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http.Features; using Microsoft.Extensions.DependencyInjection; [assembly: HostingStartup(typeof(GrpcDataNodeProbe.WireTapStartup))] namespace GrpcDataNodeProbe; /// /// Rig-only data-listener wire tap (lmxopcua#921). Off unless GRPC_DATA_WIRE_TAP=1; the rig sets it only on a /// pinned-origin SOURCE (rig.py up --source-revision). It writes one JSON line when each request arrives on the /// data listener (port 4057) and one when it completes: the UTC time, the gRPC path, the value of the /// x-otopcua-data-features request header (null when absent), the remote address and, at completion, the /// gRPC status. It records no credential, header other than the feature header, or message body. /// /// /// Self-contained on purpose: it uses ASP.NET Core only, so the rig copies this one file into a pinned older tree's /// node probe and the same tap runs on a Host built from that revision. The arrival line is written before the gRPC /// pipeline runs, so a call the older Host refuses (an unimplemented method, an unauthenticated caller) is recorded /// too. The file is bounded (); past it, a single truncation line is written. /// public sealed class WireTapStartup : IHostingStartup { /// The tap's output file name inside the node's probe directory. public const string FileName = "wire-tap.jsonl"; /// public void Configure(IWebHostBuilder builder) { if (Environment.GetEnvironmentVariable("GRPC_DATA_WIRE_TAP") != "1") return; var directory = Environment.GetEnvironmentVariable("GRPC_DATA_NODE_PROBE_DIR"); if (string.IsNullOrWhiteSpace(directory) || !Directory.Exists(directory)) return; var tap = new WireTapLog(Path.Combine(directory, FileName)); builder.ConfigureServices(services => services.AddSingleton(new WireTapStartupFilter(tap))); } } /// The bounded append-only line log behind the tap. public sealed class WireTapLog(string path) { /// Lines written before the log stops (a soak would otherwise fill the probe directory). public const int MaximumLines = 50_000; private readonly object _gate = new(); private int _lines; /// Appends one record as a JSON line; never throws into the request pipeline. public void Write(object record) { try { lock (_gate) { if (_lines > MaximumLines) return; var line = _lines++ == MaximumLines ? JsonSerializer.Serialize(new { truncated = true }) : JsonSerializer.Serialize(record); File.AppendAllText(path, line + "\n"); } } catch (IOException) { } catch (UnauthorizedAccessException) { } } } /// Records each data-listener request at arrival (before authentication) and at completion. public sealed class WireTapStartupFilter(WireTapLog log) : IStartupFilter { private const int DataListenerPort = 4057; private const string FeaturesHeader = "x-otopcua-data-features"; private static long _sequence; /// public Action Configure(Action next) => app => { app.Use(nextMiddleware => async context => { if (context.Connection.LocalPort != DataListenerPort) { await nextMiddleware(context); return; } var sequence = Interlocked.Increment(ref _sequence); var path = context.Request.Path.Value; string? features = context.Request.Headers.TryGetValue(FeaturesHeader, out var value) ? value.ToString() : null; var remote = context.Connection.RemoteIpAddress?.ToString(); log.Write(new { phase = "arrival", sequence, utc = DateTime.UtcNow.ToString("O"), path, features, remote, protocol = context.Request.Protocol }); try { await nextMiddleware(context); } finally { // A trailers-only answer (e.g. Unimplemented) carries grpc-status in the headers; a normal one in the trailers. var trailers = context.Features.Get()?.Trailers; string? status = trailers is not null && trailers.TryGetValue("grpc-status", out var t) ? t.ToString() : context.Response.Headers.TryGetValue("grpc-status", out var h) ? h.ToString() : null; log.Write(new { phase = "completion", sequence, utc = DateTime.UtcNow.ToString("O"), path, grpcStatus = status, httpStatus = context.Response.StatusCode }); } }); next(app); }; }