using Crestron.SimplSharp; using Org.BouncyCastle.Asn1.X509; using Serilog; using Serilog.Configuration; using Serilog.Core; using Serilog.Events; using Serilog.Formatting; using Serilog.Formatting.Json; using System; using System.IO; using System.Security.Authentication; using WebSocketSharp; using WebSocketSharp.Server; using X509Certificate2 = System.Security.Cryptography.X509Certificates.X509Certificate2; namespace PepperDash.Core; /// /// Provides a WebSocket-based logging sink for debugging purposes, allowing log events to be broadcast to connected /// WebSocket clients. /// /// This class implements the interface and is designed to send /// formatted log events to WebSocket clients connected to a secure WebSocket server. The server is hosted locally /// and uses a self-signed certificate for SSL/TLS encryption. public class DebugWebsocketSink : ILogEventSink, IKeyed { private HttpServer _httpsServer; private readonly string _path = "/debug/join/"; private const string _certificateName = "selfCres"; private const string _certificatePassword = "cres12345"; /// /// Gets the port number on which the HTTPS server is currently running. /// public int Port { get { if(_httpsServer == null) return 0; return _httpsServer.Port; } } /// /// Gets the WebSocket URL for the current server instance. /// /// The URL is dynamically constructed based on the server's current IP address, port, /// and WebSocket path. public string Url { get { if (_httpsServer == null) return ""; return $"wss://{CrestronEthernetHelper.GetEthernetParameter(CrestronEthernetHelper.ETHERNET_PARAMETER_TO_GET.GET_CURRENT_IP_ADDRESS, 0)}:{_httpsServer.Port}{_httpsServer.WebSocketServices[_path].Path}"; } } /// /// Gets a value indicating whether the HTTPS server is currently listening for incoming connections. /// public bool IsRunning { get => _httpsServer?.IsListening ?? false; } /// public string Key => "DebugWebsocketSink"; private readonly ITextFormatter _textFormatter; /// /// Initializes a new instance of the class with the specified text formatter. /// /// This constructor initializes the WebSocket sink and ensures that a certificate is /// available for secure communication. If the required certificate does not exist, it will be created /// automatically. Additionally, the sink is configured to stop the server when the program is /// stopping. /// The text formatter used to format log messages. If null, a default JSON formatter is used. public DebugWebsocketSink(ITextFormatter formatProvider) { _textFormatter = formatProvider ?? new JsonFormatter(); if (!File.Exists($"\\user\\{_certificateName}.pfx")) CreateCert(); CrestronEnvironment.ProgramStatusEventHandler += type => { if (type == eProgramStatusEventType.Stopping) { StopServer(); } }; } private static void CreateCert() { try { var utility = new BouncyCertificate(); var ipAddress = CrestronEthernetHelper.GetEthernetParameter(CrestronEthernetHelper.ETHERNET_PARAMETER_TO_GET.GET_CURRENT_IP_ADDRESS, 0); var hostName = CrestronEthernetHelper.GetEthernetParameter(CrestronEthernetHelper.ETHERNET_PARAMETER_TO_GET.GET_HOSTNAME, 0); var domainName = CrestronEthernetHelper.GetEthernetParameter(CrestronEthernetHelper.ETHERNET_PARAMETER_TO_GET.GET_DOMAIN_NAME, 0); CrestronConsole.PrintLine(string.Format("DomainName: {0} | HostName: {1} | {1}.{0}@{2}", domainName, hostName, ipAddress)); var certificate = utility.CreateSelfSignedCertificate(string.Format("CN={0}.{1}", hostName, domainName), [string.Format("{0}.{1}", hostName, domainName), ipAddress], [KeyPurposeID.id_kp_serverAuth, KeyPurposeID.id_kp_clientAuth]); //Crestron fails to let us do this...perhaps it should be done through their Dll's but haven't tested var separator = Path.DirectorySeparatorChar; utility.CertificatePassword = _certificatePassword; utility.WriteCertificate(certificate, @$"{separator}user{separator}", _certificateName); } catch (Exception ex) { //Debug.Console(0, "WSS CreateCert Failed\r\n{0}\r\n{1}", ex.Message, ex.StackTrace); CrestronConsole.PrintLine("WSS CreateCert Failed\r\n{0}\r\n{1}", ex.Message, ex.StackTrace); } } /// /// Sends a log event to all connected WebSocket clients. /// /// The log event is formatted using the configured text formatter and then broadcasted /// to all clients connected to the WebSocket server. If the WebSocket server is not initialized or not /// listening, the method exits without performing any action. /// The log event to be formatted and broadcasted. Cannot be null. public void Emit(LogEvent logEvent) { if (_httpsServer == null || !_httpsServer.IsListening) return; var sw = new StringWriter(); _textFormatter.Format(logEvent, sw); _httpsServer.WebSocketServices[_path].Sessions.Broadcast(sw.ToString()); } /// /// Starts the WebSocket server on the specified port and configures it with the appropriate certificate. /// /// This method initializes the WebSocket server and binds it to the specified port. It /// also applies the server's certificate for secure communication. Ensure that the port is not already in use /// and that the certificate file is accessible. /// The port number on which the WebSocket server will listen. Must be a valid, non-negative port number. public void StartServerAndSetPort(int port) { Debug.Console(0, "Starting Websocket Server on port: {0}", port); Start(port, $"\\user\\{_certificateName}.pfx", _certificatePassword); } private void Start(int port, string certPath = "", string certPassword = "") { try { _httpsServer = new HttpServer(port, true); if (!string.IsNullOrWhiteSpace(certPath)) { Debug.Console(0, "Assigning SSL Configuration"); _httpsServer.SslConfiguration.ServerCertificate = new X509Certificate2(certPath, certPassword); _httpsServer.SslConfiguration.ClientCertificateRequired = false; _httpsServer.SslConfiguration.CheckCertificateRevocation = false; _httpsServer.SslConfiguration.EnabledSslProtocols = SslProtocols.Tls12; //this is just to test, you might want to actually validate _httpsServer.SslConfiguration.ClientCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => { Debug.Console(0, "HTTPS ClientCerticateValidation Callback triggered"); return true; }; } Debug.Console(0, "Adding Debug Client Service"); _httpsServer.AddWebSocketService(_path); Debug.Console(0, "Assigning Log Info"); _httpsServer.Log.Level = LogLevel.Trace; _httpsServer.Log.Output = (d, s) => { uint level; switch(d.Level) { case WebSocketSharp.LogLevel.Fatal: level = 3; break; case WebSocketSharp.LogLevel.Error: level = 2; break; case WebSocketSharp.LogLevel.Warn: level = 1; break; case WebSocketSharp.LogLevel.Info: level = 0; break; case WebSocketSharp.LogLevel.Debug: level = 4; break; case WebSocketSharp.LogLevel.Trace: level = 5; break; default: level = 4; break; } Debug.Console(level, "{1} {0}\rCaller:{2}\rMessage:{3}\rs:{4}", d.Level.ToString(), d.Date.ToString(), d.Caller.ToString(), d.Message, s); }; Debug.Console(0, "Starting"); _httpsServer.Start(); Debug.Console(0, "Ready"); } catch (Exception ex) { Debug.Console(0, "WebSocket Failed to start {0}", ex.Message); } } /// /// Stops the WebSocket server if it is currently running. /// /// This method halts the WebSocket server and releases any associated resources. After /// calling this method, the server will no longer accept or process incoming connections. public void StopServer() { Debug.Console(0, "Stopping Websocket Server"); _httpsServer?.Stop(); _httpsServer = null; } } /// /// Configures the logger to write log events to a debug WebSocket sink. /// /// This extension method allows you to direct log events to a WebSocket sink for debugging /// purposes. public static class DebugWebsocketSinkExtensions { /// /// Configures a logger to write log events to a debug WebSocket sink. /// /// This method adds a sink that writes log events to a WebSocket for debugging purposes. /// It is typically used during development to stream log events in real-time. /// The logger sink configuration to apply the WebSocket sink to. /// An optional text formatter to format the log events. If not provided, a default formatter will be used. /// A object that can be used to further configure the logger. public static LoggerConfiguration DebugWebsocketSink( this LoggerSinkConfiguration loggerConfiguration, ITextFormatter formatProvider = null) { return loggerConfiguration.Sink(new DebugWebsocketSink(formatProvider)); } } /// /// Represents a WebSocket client for debugging purposes, providing connection lifecycle management and message /// handling functionality. /// /// The class extends to handle /// WebSocket connections, including events for opening, closing, receiving messages, and errors. It tracks the /// duration of the connection and logs relevant events for debugging. public class DebugClient : WebSocketBehavior { private DateTime _connectionTime; /// /// Gets the duration of time the WebSocket connection has been active. /// public TimeSpan ConnectedDuration { get { if (Context.WebSocket.IsAlive) { return DateTime.Now - _connectionTime; } else { return new TimeSpan(0); } } } /// /// Initializes a new instance of the class. /// /// This constructor creates a new instance and logs its /// creation using the method with a debug level of 0. public DebugClient() { Debug.Console(0, "DebugClient Created"); } /// protected override void OnOpen() { base.OnOpen(); var url = Context.WebSocket.Url; Debug.Console(0, Debug.ErrorLogLevel.Notice, "New WebSocket Connection from: {0}", url); _connectionTime = DateTime.Now; } /// protected override void OnMessage(MessageEventArgs e) { base.OnMessage(e); Debug.Console(0, "WebSocket UiClient Message: {0}", e.Data); } /// protected override void OnClose(CloseEventArgs e) { base.OnClose(e); Debug.Console(0, Debug.ErrorLogLevel.Notice, "WebSocket UiClient Closing: {0} reason: {1}", e.Code, e.Reason); } /// protected override void OnError(WebSocketSharp.ErrorEventArgs e) { base.OnError(e); Debug.Console(2, Debug.ErrorLogLevel.Notice, "WebSocket UiClient Error: {0} message: {1}", e.Exception, e.Message); } }