// MaverickPipeTap.exe — espera o pipe aparecer, conecta na hora e grava TUDO. // gfn-ccpipe e de instancia unica e transitoria: quem chega depois fica sem. // csc /nologo /optimize+ /out:MaverickPipeTap.exe MaverickPipeTap.cs // MaverickPipeTap.exe --pipe gfn-ccpipe --wait 180 --hold 90 // MaverickPipeTap.exe --pipe gfn-ccpipe --send "{\"jsonrpc\":\"2.0\",\"id\":1,\"method\":\"rpc.discover\"}" using System; using System.Collections.Generic; using System.IO; using System.IO.Pipes; using System.Text; using System.Threading; internal static class MaverickPipeTap { static readonly StringBuilder LogBuf = new StringBuilder(); static string LogPath = "MaverickPipeTap.log"; static void Log(string m) { string line = DateTime.Now.ToString("HH:mm:ss.fff") + " " + m; LogBuf.AppendLine(line); Console.WriteLine(line); try { File.WriteAllText(LogPath, LogBuf.ToString()); } catch { } } static bool PipeExists(string name) { try { foreach (string p in Directory.GetFiles(@"\\.\pipe\")) if (string.Equals(p.Substring(9), name, StringComparison.OrdinalIgnoreCase)) return true; } catch { } return false; } static string Render(byte[] buf, int n) { var hex = new StringBuilder(); for (int i = 0; i < Math.Min(n, 64); i++) hex.Append(buf[i].ToString("X2")).Append(' '); string txt = Encoding.UTF8.GetString(buf, 0, n); var clean = new StringBuilder(); foreach (char c in txt) clean.Append(c >= 32 && c < 127 ? c : '.'); return string.Format("[{0} B] {1} | {2}", n, hex.ToString().Trim(), clean); } static int Main(string[] args) { string pipe = "gfn-ccpipe"; int wait = 180, hold = 90; string send = null; for (int i = 0; i < args.Length; i++) { if (args[i] == "--pipe" && i + 1 < args.Length) pipe = args[++i]; else if (args[i] == "--wait" && i + 1 < args.Length) int.TryParse(args[++i], out wait); else if (args[i] == "--hold" && i + 1 < args.Length) int.TryParse(args[++i], out hold); else if (args[i] == "--send" && i + 1 < args.Length) send = args[++i]; } string dir = Path.GetDirectoryName(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName); LogPath = Path.Combine(dir ?? ".", "MaverickPipeTap.log"); Log("=== MaverickPipeTap pipe=" + pipe + " wait=" + wait + "s hold=" + hold + "s"); Log("user=" + Environment.UserName + " session=" + System.Diagnostics.Process.GetCurrentProcess().SessionId); DateTime waitEnd = DateTime.UtcNow.AddSeconds(wait); bool exists = false; while (DateTime.UtcNow < waitEnd) { if (PipeExists(pipe)) { exists = true; Log("pipe apareceu: " + pipe); break; } Thread.Sleep(200); } if (!exists) { Log("pipe nao apareceu em " + wait + "s. inicie a sessao/streaming e rode de novo."); return 2; } NamedPipeClientStream client; try { client = new NamedPipeClientStream(".", pipe, PipeDirection.InOut, PipeOptions.None); client.Connect(1500); Log("conectado. mantendo " + hold + "s."); } catch (Exception ex) { Log("conectar falhou: " + ex.GetType().Name + " " + ex.Message); return 3; } using (client) { if (send != null) { byte[] data = Encoding.UTF8.GetBytes(send); try { client.Write(data, 0, data.Length); client.Flush(); Log("--> " + send); } catch (Exception ex) { Log("enviar falhou: " + ex.Message); } } var buf = new byte[8192]; var end = DateTime.UtcNow.AddSeconds(hold); int total = 0; while (DateTime.UtcNow < end) { try { var task = client.ReadAsync(buf, 0, buf.Length); if (!task.Wait(400)) continue; int n = task.Result; if (n <= 0) { Log("servidor fechou a conexao."); break; } total += n; Log("<-- " + Render(buf, n)); } catch (Exception ex) { Log("read encerrou: " + ex.GetType().Name); break; } } Log("fim. bytes recebidos: " + total); } return 0; } }