fix: some fixes to file paths for the reworked websocket

This commit is contained in:
Nick Genovese
2024-11-05 13:16:46 -05:00
parent 1d20f6d1ed
commit 4f01ff1379
4 changed files with 22 additions and 17 deletions

View File

@@ -1,6 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq;
using System.Reflection; using System.Reflection;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using Crestron.SimplSharp; using Crestron.SimplSharp;
@@ -103,7 +104,7 @@ namespace PepperDash.Core
); );
#if NET472 #if NET472
var certPath = IsRunningOnAppliance ? Path.Combine("/", "user", "selfCres.pfx") : Path.Combine("/", "User", "selfCres.pfx"); var certPath = IsRunningOnAppliance ? Path.Combine("/", "user") : Path.Combine("/", "User");
var websocket = new DebugNet472WebSocket(certPath); var websocket = new DebugNet472WebSocket(certPath);
WebsocketUrl = websocket.Url; WebsocketUrl = websocket.Url;
DefaultLoggerConfiguration.WriteTo.Sink(new DebugWebsocketSink(websocket, new JsonFormatter(renderMessage: true)), levelSwitch: WebsocketLoggingLevelSwitch); DefaultLoggerConfiguration.WriteTo.Sink(new DebugWebsocketSink(websocket, new JsonFormatter(renderMessage: true)), levelSwitch: WebsocketLoggingLevelSwitch);
@@ -190,16 +191,17 @@ namespace PepperDash.Core
return; return;
} }
const string pattern = @"^(\d+)(?:\s+--devices\s+([\w,]+))?$"; const string pattern = @"^(\d+)(?:\s+--devices\s+([\w\s,-]+))?$";
var match = Regex.Match(levelString, pattern); var match = Regex.Match(levelString, pattern);
if (match.Success) if (match.Success)
{ {
var level = int.Parse(match.Groups[1].Value); var level = int.Parse(match.Groups[1].Value);
var devices = match.Groups[2].Success var devices = match.Groups[2].Success
? match.Groups[2].Value.Split(',') ? match.Groups[2].Value.Split(',')
.Select(device => device.Trim())
.ToArray()
: []; : [];
if (LogLevels.TryGetValue(level, out var logEventLevel)) if (LogLevels.TryGetValue(level, out var logEventLevel))
{ {
SetConsoleDebugLevel(logEventLevel, devices); SetConsoleDebugLevel(logEventLevel, devices);
@@ -211,7 +213,7 @@ namespace PepperDash.Core
} }
else else
{ {
CrestronConsole.ConsoleCommandResponse($"Error: Unable to parse {levelString} to valid log level"); CrestronConsole.ConsoleCommandResponse($"Error: Unable to parse {levelString} to valid log level:does not match the pattern");
} }
} }
catch catch

View File

@@ -12,7 +12,7 @@ namespace PepperDash.Core.Logging
{ {
AppName = CrestronEnvironment.DevicePlatform switch AppName = CrestronEnvironment.DevicePlatform switch
{ {
eDevicePlatform.Appliance => $"App {InitialParametersClass.ApplicationNumber}", eDevicePlatform.Appliance => $"APP{InitialParametersClass.ApplicationNumber.ToString().PadLeft(2, '0')}",
eDevicePlatform.Server => $"{InitialParametersClass.RoomId}", eDevicePlatform.Server => $"{InitialParametersClass.RoomId}",
_ => string.Empty _ => string.Empty
}; };
@@ -22,7 +22,6 @@ namespace PepperDash.Core.Logging
public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory) public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
{ {
var property = propertyFactory.CreateProperty("App", AppName); var property = propertyFactory.CreateProperty("App", AppName);
logEvent.AddOrUpdateProperty(property); logEvent.AddOrUpdateProperty(property);
} }
} }

View File

@@ -4,6 +4,7 @@ using System.Security.Cryptography.X509Certificates;
using Crestron.SimplSharp; using Crestron.SimplSharp;
#if NET472 #if NET472
using System.IO;
using WebSocketSharp.Net; using WebSocketSharp.Net;
using WebSocketSharp.Server; using WebSocketSharp.Server;
@@ -11,10 +12,10 @@ namespace PepperDash.Core.Logging
{ {
internal class DebugNet472WebSocket : DebugWebSocket internal class DebugNet472WebSocket : DebugWebSocket
{ {
private const string Path = "/debug/join/"; private const string WebsocketPath = "/debug/join/";
public string Url => public string Url =>
$"wss://{CrestronEthernetHelper.GetEthernetParameter(CrestronEthernetHelper.ETHERNET_PARAMETER_TO_GET.GET_CURRENT_IP_ADDRESS, 0)}:{server.Port}{server.WebSocketServices[Path].Path}"; $"wss://{CrestronEthernetHelper.GetEthernetParameter(CrestronEthernetHelper.ETHERNET_PARAMETER_TO_GET.GET_CURRENT_IP_ADDRESS, 0)}:{server.Port}{server.WebSocketServices[WebsocketPath].Path}";
private readonly WebSocketServer server; private readonly WebSocketServer server;
@@ -24,7 +25,8 @@ namespace PepperDash.Core.Logging
if (IsSecure) if (IsSecure)
{ {
server.SslConfiguration = new ServerSslConfiguration(new X509Certificate2(certPath, CertificatePassword)) var filename = Path.Combine(certPath, CertificateName + ".pfx");
server.SslConfiguration = new ServerSslConfiguration(new X509Certificate2(filename, CertificatePassword))
{ {
ClientCertificateRequired = false, ClientCertificateRequired = false,
CheckCertificateRevocation = false, CheckCertificateRevocation = false,
@@ -38,7 +40,7 @@ namespace PepperDash.Core.Logging
}; };
} }
server.AddWebSocketService<DebugClient>(Path); server.AddWebSocketService<DebugClient>(WebsocketPath);
server.Start(); server.Start();
} }

View File

@@ -1,10 +1,7 @@
using System; using System;
using System.IO; using System.IO;
using Crestron.SimplSharp; using Crestron.SimplSharp;
#if NET472
using Org.BouncyCastle.Asn1.X509; using Org.BouncyCastle.Asn1.X509;
#endif
namespace PepperDash.Core.Logging namespace PepperDash.Core.Logging
{ {
@@ -22,8 +19,15 @@ namespace PepperDash.Core.Logging
return; return;
} }
if (!File.Exists(certPath)) if (certPath == null)
{ {
return;
}
var filePath = Path.Combine(certPath, CertificateName + ".pfx");
if (!File.Exists(filePath))
{
Debug.LogInformation("Creating new cert at file path:{Path}", filePath);
CreateCert(certPath); CreateCert(certPath);
} }
} }
@@ -32,7 +36,6 @@ namespace PepperDash.Core.Logging
private static void CreateCert(string filePath) private static void CreateCert(string filePath)
{ {
#if NET472
try try
{ {
var utility = new BouncyCertificate(); var utility = new BouncyCertificate();
@@ -48,7 +51,6 @@ namespace PepperDash.Core.Logging
{ {
Debug.LogError(ex, "WSS failed to create cert"); Debug.LogError(ex, "WSS failed to create cert");
} }
#endif
} }
public abstract bool IsListening { get; } public abstract bool IsListening { get; }