From 3a47a4fb823f86a681533fb1a63fad0b89ea5d8a Mon Sep 17 00:00:00 2001 From: Greg Date: Wed, 15 Feb 2023 19:02:03 -0700 Subject: [PATCH] Add new IPC that can get around permission issues. --- .../Installer/BootstrapperSetup/Version.wxi | 2 +- .../Generated/OrbisLibraryManager.wxs | 146 +- .../Generated/OrbisNeighborhood.wxs | 2175 +++++++++-------- .../Generated/OrbisPeeknPoke.wxs | 121 +- .../Generated/OrbisSuiteService.wxs | 446 ++-- Windows/Installer/DummyInstaller/Product.wxs | 2 + .../Common/Dispatcher/DispatcherClient.cs | 43 +- Windows/Libraries/OrbisLib2/OrbisLib2.csproj | 2 +- .../Resources/BuildNumber.txt | 2 +- .../Resources/BuildString.txt | 2 +- .../OrbisSuiteService.csproj | 10 +- Windows/OrbisSuiteService/Program.cs | 32 +- .../OrbisSuiteService/Service/Dispatcher.cs | 35 +- .../Service/OrbisConsoleFormatter.cs | 34 + .../Service/TargetEventListener.cs | 9 +- 15 files changed, 1663 insertions(+), 1398 deletions(-) create mode 100644 Windows/OrbisSuiteService/Service/OrbisConsoleFormatter.cs diff --git a/Windows/Installer/BootstrapperSetup/Version.wxi b/Windows/Installer/BootstrapperSetup/Version.wxi index e70bf9e..28a33c0 100644 --- a/Windows/Installer/BootstrapperSetup/Version.wxi +++ b/Windows/Installer/BootstrapperSetup/Version.wxi @@ -2,5 +2,5 @@ - + diff --git a/Windows/Installer/DummyInstaller/Generated/OrbisLibraryManager.wxs b/Windows/Installer/DummyInstaller/Generated/OrbisLibraryManager.wxs index 04b1ac1..7dbe2e0 100644 --- a/Windows/Installer/DummyInstaller/Generated/OrbisLibraryManager.wxs +++ b/Windows/Installer/DummyInstaller/Generated/OrbisLibraryManager.wxs @@ -7,16 +7,25 @@ - + - + - + - + + + + + + + + + + @@ -43,64 +52,91 @@ - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + - + @@ -255,101 +291,101 @@ - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + diff --git a/Windows/Installer/DummyInstaller/Generated/OrbisNeighborhood.wxs b/Windows/Installer/DummyInstaller/Generated/OrbisNeighborhood.wxs index b1d01e4..1804a9d 100644 --- a/Windows/Installer/DummyInstaller/Generated/OrbisNeighborhood.wxs +++ b/Windows/Installer/DummyInstaller/Generated/OrbisNeighborhood.wxs @@ -7,175 +7,184 @@ - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + + + + + + + + + + - + - + @@ -202,169 +211,169 @@ - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + @@ -519,136 +528,136 @@ - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + @@ -1739,1421 +1748,1421 @@ - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + @@ -3308,136 +3317,136 @@ - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + @@ -4528,1244 +4537,1244 @@ - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + diff --git a/Windows/Installer/DummyInstaller/Generated/OrbisPeeknPoke.wxs b/Windows/Installer/DummyInstaller/Generated/OrbisPeeknPoke.wxs index 42487cc..5a08642 100644 --- a/Windows/Installer/DummyInstaller/Generated/OrbisPeeknPoke.wxs +++ b/Windows/Installer/DummyInstaller/Generated/OrbisPeeknPoke.wxs @@ -7,13 +7,13 @@ - + - + - + @@ -109,16 +109,25 @@ - + - + - + - + + + + + + + + + + @@ -145,58 +154,58 @@ - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + @@ -351,101 +360,101 @@ - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + diff --git a/Windows/Installer/DummyInstaller/Generated/OrbisSuiteService.wxs b/Windows/Installer/DummyInstaller/Generated/OrbisSuiteService.wxs index 345e80d..7ecc25f 100644 --- a/Windows/Installer/DummyInstaller/Generated/OrbisSuiteService.wxs +++ b/Windows/Installer/DummyInstaller/Generated/OrbisSuiteService.wxs @@ -7,16 +7,31 @@ - + - + - + - + + + + + + + + + + + + + + + + @@ -43,73 +58,154 @@ - + - + - + - + + + + + + + + + + - + + + + - + + + + + + + + + + - + + + + - + - + - + - + - + - + + + + - + - + - + - + + + + - + - + - + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + - + @@ -264,101 +360,104 @@ - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + + + + - + - + - + - + - + - + - + - + - + - + - + @@ -385,34 +484,43 @@ - + - + - + - + - + - + - + - + - + + + + + + + + + + - + @@ -983,271 +1091,280 @@ - + - + + + + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + + + + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + + + + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + @@ -1282,6 +1399,11 @@ + + + + + @@ -1447,6 +1569,11 @@ + + + + + @@ -1587,6 +1714,11 @@ + + + + + diff --git a/Windows/Installer/DummyInstaller/Product.wxs b/Windows/Installer/DummyInstaller/Product.wxs index 0417768..eaeb93f 100644 --- a/Windows/Installer/DummyInstaller/Product.wxs +++ b/Windows/Installer/DummyInstaller/Product.wxs @@ -13,6 +13,8 @@ _Client; /// /// Call this to subscribe for events from the windows service. /// public static void Subscribe() { - _ServiceMessageBus = new TinyMessageBus("OrbisSuite"); - _ServiceMessageBus.MessageReceived += _ServiceMessageBus_MessageReceived; + _Client = new PipeClient("OrbisSuite"); + _Client.MessageReceived += _Client_MessageReceived; + _Client.ConnectAsync(); } - private static void _ServiceMessageBus_MessageReceived(object? sender, TinyMessageReceivedEventArgs e) + private static void _Client_MessageReceived(object? sender, H.Pipes.Args.ConnectionMessageEventArgs e) { - var Packet = (ForwardPacket)Helper.ByteArrayToObject(e.Message.ToArray()); - - switch (Packet.Type) + switch (e.Message.Type) { default: Console.WriteLine("Invalid Packet..."); @@ -39,45 +30,45 @@ namespace OrbisLib2.Common.Dispatcher break; case ForwardPacket.PacketType.Intercept: - Events.RaiseProcInterceptEvent(Packet.SenderIPAddress); + Events.RaiseProcInterceptEvent(e.Message.SenderIPAddress); break; case ForwardPacket.PacketType.Continue: - Events.RaiseProcContinueEvent(Packet.SenderIPAddress); + Events.RaiseProcContinueEvent(e.Message.SenderIPAddress); break; // Process States case ForwardPacket.PacketType.ProcessDie: - Events.RaiseProcDieEvent(Packet.SenderIPAddress); + Events.RaiseProcDieEvent(e.Message.SenderIPAddress); break; case ForwardPacket.PacketType.ProcessAttach: - Events.RaiseProcAttachEvent(Packet.SenderIPAddress, Packet.ProcessId); + Events.RaiseProcAttachEvent(e.Message.SenderIPAddress, e.Message.ProcessId); break; case ForwardPacket.PacketType.ProcessDetach: - Events.RaiseProcDetachEvent(Packet.SenderIPAddress); + Events.RaiseProcDetachEvent(e.Message.SenderIPAddress); break; // Target State case ForwardPacket.PacketType.TargetSuspend: - Events.RaiseTargetSuspendEvent(Packet.SenderIPAddress); + Events.RaiseTargetSuspendEvent(e.Message.SenderIPAddress); break; case ForwardPacket.PacketType.TargetResume: - Events.RaiseTargetResumeEvent(Packet.SenderIPAddress); + Events.RaiseTargetResumeEvent(e.Message.SenderIPAddress); break; case ForwardPacket.PacketType.TargetShutdown: - Events.RaiseTargetShutdownEvent(Packet.SenderIPAddress); + Events.RaiseTargetShutdownEvent(e.Message.SenderIPAddress); break; case ForwardPacket.PacketType.TargetAvailability: - Events.FireTargetAvailability(Packet.TargetAvailability.Available, Packet.TargetAvailability.Name); + Events.FireTargetAvailability(e.Message.TargetAvailability.Available, e.Message.TargetAvailability.Name); break; case ForwardPacket.PacketType.TargetAPIAvailability: - Events.FireTargetAPIAvailability(Packet.TargetAvailability.Available, Packet.TargetAvailability.Name); + Events.FireTargetAPIAvailability(e.Message.TargetAvailability.Available, e.Message.TargetAvailability.Name); break; // Misc diff --git a/Windows/Libraries/OrbisLib2/OrbisLib2.csproj b/Windows/Libraries/OrbisLib2/OrbisLib2.csproj index 2d731f4..89d16f3 100644 --- a/Windows/Libraries/OrbisLib2/OrbisLib2.csproj +++ b/Windows/Libraries/OrbisLib2/OrbisLib2.csproj @@ -27,11 +27,11 @@ + - diff --git a/Windows/OrbisNeighborHood/Resources/BuildNumber.txt b/Windows/OrbisNeighborHood/Resources/BuildNumber.txt index c7726ec..081381a 100644 --- a/Windows/OrbisNeighborHood/Resources/BuildNumber.txt +++ b/Windows/OrbisNeighborHood/Resources/BuildNumber.txt @@ -1 +1 @@ -2615 +2627 diff --git a/Windows/OrbisNeighborHood/Resources/BuildString.txt b/Windows/OrbisNeighborHood/Resources/BuildString.txt index d2a8c9d..a6754aa 100644 --- a/Windows/OrbisNeighborHood/Resources/BuildString.txt +++ b/Windows/OrbisNeighborHood/Resources/BuildString.txt @@ -1 +1 @@ -Version 3.0.2615 Release Build Tuesday February 14 2023 11:37 PM +Version 3.0.2627 Release Build Wednesday February 15 2023 6:45 PM diff --git a/Windows/OrbisSuiteService/OrbisSuiteService.csproj b/Windows/OrbisSuiteService/OrbisSuiteService.csproj index 419b9cf..6c7f95f 100644 --- a/Windows/OrbisSuiteService/OrbisSuiteService.csproj +++ b/Windows/OrbisSuiteService/OrbisSuiteService.csproj @@ -1,7 +1,7 @@  - WinExe + Exe net6.0-windows enable annotations @@ -13,8 +13,12 @@ - - + + + + + + diff --git a/Windows/OrbisSuiteService/Program.cs b/Windows/OrbisSuiteService/Program.cs index 030dbbb..41d8c39 100644 --- a/Windows/OrbisSuiteService/Program.cs +++ b/Windows/OrbisSuiteService/Program.cs @@ -1,21 +1,45 @@ using OrbisSuiteService.Service; using System.ServiceProcess; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.DependencyInjection; #if DEBUG var service = new Service(); service.OnStartPublic(new string[0]); #else - ServiceBase.Run(new Service()); +ServiceBase.Run(new Service()); #endif class Service : ServiceBase { + private ServiceProvider _serviceProvider; +#if DEBUG bool RunService = true; +#endif + + public Service() + { + IServiceCollection services = new ServiceCollection(); + + services.AddLogging(builder => + { + builder.AddSimpleConsole(options => + { + options.IncludeScopes = false; + options.SingleLine = true; + options.TimestampFormat = "HH:mm:ss "; + }); + }); + + _serviceProvider = services.BuildServiceProvider(); + } public void OnStartPublic(string[] args) { - Console.WriteLine("Starting"); - var dp = new Dispatcher(); + var logger = _serviceProvider.GetService() + .AddFile(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + @"\Orbis Suite\Logging\OrbisServiceLog.txt") + .CreateLogger(); + var dp = new Dispatcher(logger); #if DEBUG while (RunService) { Thread.Sleep(10); } #endif @@ -28,7 +52,9 @@ class Service : ServiceBase protected override void OnStop() { Console.WriteLine("Stopping"); +#if DEBUG RunService = false; +#endif } protected override void OnPause() { diff --git a/Windows/OrbisSuiteService/Service/Dispatcher.cs b/Windows/OrbisSuiteService/Service/Dispatcher.cs index 033485d..ed5fb06 100644 --- a/Windows/OrbisSuiteService/Service/Dispatcher.cs +++ b/Windows/OrbisSuiteService/Service/Dispatcher.cs @@ -1,24 +1,38 @@ -using TinyIpc.Messaging; -using OrbisLib2.Common.Database; -using OrbisLib2.Common.Dispatcher; -using OrbisLib2.Common.Helpers; +using OrbisLib2.Common.Dispatcher; +using Microsoft.Extensions.Logging; +using H.Pipes; +using System.IO.Pipes; +using System.Security.AccessControl; +using System.Security.Principal; +using H.Pipes.AccessControl; namespace OrbisSuiteService.Service { public class Dispatcher { - private TinyMessageBus _ServiceMessageBus; + private IPipeServer _PipeServer; + private ILogger _Logger; private DBWatcher _DBWatcher = new DBWatcher(); - private Settings _Settings = Settings.CreateInstance(); //private SerialComHelper _SerialMonitor = new SerialComHelper(); private TargetWatcher _TargetWatcher; private TargetEventListener _TargetEventListener; - public Dispatcher() + public Dispatcher(ILogger logger) { - _ServiceMessageBus = new TinyMessageBus("OrbisSuite"); + _Logger = logger; + + // Set up the named pipe server. + _PipeServer = new PipeServer("OrbisSuite"); + + // Set the pipe security so userland can interact with us. + var pipeSecurity = new PipeSecurity(); + pipeSecurity.AddAccessRule(new PipeAccessRule(new SecurityIdentifier(WellKnownSidType.BuiltinUsersSid, null), PipeAccessRights.ReadWrite, AccessControlType.Allow)); + _PipeServer.SetPipeSecurity(pipeSecurity); + + // start the pipe server. + _PipeServer.StartAsync(); //Helpers _DBWatcher.DBChanged += _DBWatcher_DBChanged; @@ -26,7 +40,7 @@ namespace OrbisSuiteService.Service _SerialMonitor.Settings.PortName = ""; _SerialMonitor.StartListening();*/ _TargetWatcher = new TargetWatcher(this); - _TargetEventListener = new TargetEventListener(this); + _TargetEventListener = new TargetEventListener(this, _Logger); } private byte[] _SerialDataBuffer = new byte[0]; @@ -54,7 +68,8 @@ namespace OrbisSuiteService.Service public void PublishEvent(ForwardPacket Packet) { - _ServiceMessageBus.PublishAsync(Helper.ObjectToByteArray(Packet)); + _Logger.LogInformation($"Publishing Event: {Packet.Type}."); + _PipeServer.WriteAsync(Packet); } } } diff --git a/Windows/OrbisSuiteService/Service/OrbisConsoleFormatter.cs b/Windows/OrbisSuiteService/Service/OrbisConsoleFormatter.cs new file mode 100644 index 0000000..85e6846 --- /dev/null +++ b/Windows/OrbisSuiteService/Service/OrbisConsoleFormatter.cs @@ -0,0 +1,34 @@ +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Logging.Abstractions; +using Microsoft.Extensions.Logging.Console; +using Microsoft.Extensions.Options; +using System; +using System.Collections.Generic; +using System.Data.Entity.Core.Common.CommandTrees.ExpressionBuilder; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace OrbisSuiteService +{ + internal class OrbisConsoleFormatter : ConsoleFormatter + { + public OrbisConsoleFormatter(string name) : base(name) { } + + public override void Write(in LogEntry logEntry, IExternalScopeProvider? scopeProvider, TextWriter textWriter) + { + string message = + logEntry.Formatter( + logEntry.State, logEntry.Exception); + + if (message == null) + { + return; + } + + textWriter.Write($"{DateTime.Now.ToString("HH:mm:ss")}[OrbisSuiteService]"); + textWriter.Write(message); + textWriter.Write("\n"); + } + } +} diff --git a/Windows/OrbisSuiteService/Service/TargetEventListener.cs b/Windows/OrbisSuiteService/Service/TargetEventListener.cs index 24342c2..0434351 100644 --- a/Windows/OrbisSuiteService/Service/TargetEventListener.cs +++ b/Windows/OrbisSuiteService/Service/TargetEventListener.cs @@ -5,6 +5,7 @@ using OrbisLib2.Common.Dispatcher; using System.Windows.Threading; using System.Net; using System.Text; +using Microsoft.Extensions.Logging; namespace OrbisSuiteService.Service { @@ -24,19 +25,25 @@ namespace OrbisSuiteService.Service { private Listener _TargetListener; private Dispatcher _Dispatcher; + private ILogger _Logger; - public TargetEventListener(Dispatcher Dispatcher) + public TargetEventListener(Dispatcher Dispatcher, ILogger logger) { + _Logger = logger; + _Logger.LogInformation("TargetEventListener\n"); _Dispatcher = Dispatcher; _TargetListener = new Listener(Config.EventPort); _TargetListener.SocketAccepted += _TargetListener_SocketAccepted; _TargetListener.Start(); + + _Logger.LogInformation("TargetEventListener Done\n"); } private void _TargetListener_SocketAccepted(Socket s) { var eventId = s.RecvInt32(); + _Logger.LogInformation($"{eventId}\n"); var ipAddress = ((IPEndPoint)s.RemoteEndPoint).Address.ToString(); switch (eventId) {