using System.IO.Pipes; using System.Reflection; using System.Runtime.ExceptionServices; using System.Runtime.InteropServices; using System.Security.Principal; using Microsoft.Win32.SafeHandles; using XFEToolBox.Client.Core.Search.Service; using var currentIdentity = WindowsIdentity.GetCurrent(); if (new WindowsPrincipal(currentIdentity).IsInRole(WindowsBuiltInRole.Administrator)) { Console.Error.WriteLine("Ordinary (non-elevated) terminal required to validate the actual client trust boundary."); return 2; } AppDomain.CurrentDomain.FirstChanceException += (_, e) => { if (e.Exception is IOException or UnauthorizedAccessException or OperationCanceledException or System.ComponentModel.Win32Exception) Console.WriteLine("FirstChance type=" + e.Exception.GetType().Name + " hresult=" + e.Exception.HResult.ToString("X")); }; using var raw = Native.Open(); Console.WriteLine("PipeOpen=" + !raw.IsInvalid + " error=" + (raw.IsInvalid ? Marshal.GetLastWin32Error() : 0)); if (!raw.IsInvalid) { Console.WriteLine(Native.DescribeSession(raw)); var trust = typeof(IndexingServiceClient).Assembly.GetType("XFEToolBox.Client.Core.Search.Service.IndexingServiceTrust")! .GetMethod("IsTrustedServer", BindingFlags.NonPublic | BindingFlags.Static)!; var trusted = (bool)trust.Invoke(null, [raw])!; Console.WriteLine("Trusted=" + trusted); if (trusted) { using var client = new NamedPipeClientStream(PipeDirection.InOut, true, true, raw); using var timeout = new CancellationTokenSource(TimeSpan.FromSeconds(2)); try { await IndexingServiceProtocol.WriteAsync(client, new IndexingServiceRequest(1, "status"), 16384, timeout.Token); Console.WriteLine("RequestWrite=ok"); var response = await IndexingServiceProtocol.ReadAsync(client, 16 * 1024 * 1024, timeout.Token); Console.WriteLine($"Response version={response.Version} errorPresent={response.Error is not null} ready={response.Status.ReadyVolumes}/{response.Status.Volumes}"); } catch (Exception error) { Console.WriteLine("ResponseException=" + error.GetType().Name + " hresult=" + error.HResult.ToString("X")); } } } var status = await new IndexingServiceClient().GetStatusAsync(); Console.WriteLine($"PublicClient statusAvailable={status is not null}"); return status is not null ? 0 : 1; static class Native { public static string DescribeSession(SafePipeHandle pipe) { var pidOk = GetNamedPipeServerProcessId(pipe, out var pid); var pidError = pidOk ? 0 : Marshal.GetLastWin32Error(); var pipeSessionOk = GetNamedPipeServerSessionId(pipe, out var pipeSession); var pipeSessionError = pipeSessionOk ? 0 : Marshal.GetLastWin32Error(); var processSessionOk = ProcessIdToSessionId(pid, out var processSession); var processSessionError = processSessionOk ? 0 : Marshal.GetLastWin32Error(); return $"ServerPID ok={pidOk} pid={pid} error={pidError}; " + $"PipeSession ok={pipeSessionOk} session={pipeSession} error={pipeSessionError}; " + $"ProcessSession ok={processSessionOk} session={processSession} error={processSessionError}"; } public static SafePipeHandle Open() => CreateFileW(@"\\.\pipe\XFEToolBox.Indexing.v1", 0x0012019B, 0, IntPtr.Zero, 3, 0x40120000, IntPtr.Zero); [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)] private static extern SafePipeHandle CreateFileW(string path, uint access, uint share, IntPtr security, uint create, uint flags, IntPtr template); [DllImport("kernel32.dll", SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] private static extern bool GetNamedPipeServerProcessId(SafePipeHandle pipe, out uint pid); [DllImport("kernel32.dll", SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] private static extern bool GetNamedPipeServerSessionId(SafePipeHandle pipe, out uint session); [DllImport("kernel32.dll", SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] private static extern bool ProcessIdToSessionId(uint pid, out uint session); }