| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 | using System;using System.IO;using System.Reflection;using System.Security.Cryptography.X509Certificates;using InABox.Core;using InABox.IPC;using InABox.Logging;using InABox.Rpc;namespace PRSServer{    public interface IEngine    {        string ServiceName { get; set; }        string Version { get; set; }        void Run();        void Stop();        void Configure(Server settings);        PortStatus[] PortStatusList();    }    public abstract class Engine<TProperties> : IEngine where TProperties : ServerProperties    {                private RpcServerPipeTransport _enginemanager;        public TProperties Properties { get; private set; }        public abstract void Run();        public abstract void Stop();        public virtual PortStatus[] PortStatusList()        {            return new PortStatus[] { };        }        public string ServiceName { get; set; }        public string Version { get; set; }        protected string AppDataFolder { get; set; }        public virtual void Configure(Server server)        {            Properties = server.Properties as TProperties;            AppDataFolder = GetPath(server.Key);            MainLogger.AddLogger(new LogFileLogger(AppDataFolder));            MainLogger.AddLogger(new NamedPipeLogger(server.Key));                        _enginemanager = new RpcServerPipeTransport($"{ServiceName}M");            _enginemanager.AddHandler<IEngine, PortStatusCommand, PortStatusParameters, PortStatusResult>(new PortStatusHandler(this));            _enginemanager.AfterMessage += (transport, args) => MainLogger.Send(LogType.Information,"",$"Engine Manager Message: {args.Message?.Command}");            _enginemanager.Start();        }        public static string GetPath(string key)        {            if (Assembly.GetEntryAssembly() != null)                return Path.Combine(                    Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),                    Path.GetFileNameWithoutExtension(Assembly.GetEntryAssembly().Location),                    key                );            return Path.Combine(                Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),                Path.GetFileNameWithoutExtension(Assembly.GetExecutingAssembly().Location),                key            );        }                public static X509Certificate2? LoadCertificate(string filename)        {            if (!String.IsNullOrWhiteSpace(filename))            {                Logger.Send(LogType.Information, "", "Certificate FileName is {0}", filename);                if (File.Exists(filename))                {                    Logger.Send(LogType.Information, "", "Certificate found; verifying HTTPS Certificate");                    try                    {                        var certificate = new X509Certificate2(filename);                        if (certificate.NotAfter > DateTime.Now)                        {                                                        var names = CertificateEngine.GetDnsNames(certificate);                            Logger.Send(LogType.Information, "", $"Certificate valid for {string.Join(',', names)}");                            return certificate;                        }                        else                        {                            Logger.Send(LogType.Error, "", "HTTPS Certificate has expired, using HTTP instead");                        }                    }                    catch (Exception)                    {                        Logger.Send(LogType.Error, "", "Error validating HTTPS Certificate, using HTTP instead");                    }                }                else                    Logger.Send(LogType.Error, "", "Certificate File does not exist!");            }            return null;        }    }}
 |