Skip to content

Commit

Permalink
fix: some fixes to file paths for the reworked websocket
Browse files Browse the repository at this point in the history
  • Loading branch information
ngenovese11 committed Nov 5, 2024
1 parent 1d20f6d commit 4f01ff1
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 17 deletions.
12 changes: 7 additions & 5 deletions src/Pepperdash.Core/Debug.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text.RegularExpressions;
using Crestron.SimplSharp;
Expand Down Expand Up @@ -103,7 +104,7 @@ static Debug()
);

#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);
WebsocketUrl = websocket.Url;
DefaultLoggerConfiguration.WriteTo.Sink(new DebugWebsocketSink(websocket, new JsonFormatter(renderMessage: true)), levelSwitch: WebsocketLoggingLevelSwitch);
Expand Down Expand Up @@ -190,16 +191,17 @@ private static void SetDebugFromConsole(string levelString)
return;
}

const string pattern = @"^(\d+)(?:\s+--devices\s+([\w,]+))?$";
var match = Regex.Match(levelString, pattern);
const string pattern = @"^(\d+)(?:\s+--devices\s+([\w\s,-]+))?$";
var match = Regex.Match(levelString, pattern);
if (match.Success)
{
var level = int.Parse(match.Groups[1].Value);
var devices = match.Groups[2].Success
? match.Groups[2].Value.Split(',')
.Select(device => device.Trim())
.ToArray()
: [];


if (LogLevels.TryGetValue(level, out var logEventLevel))
{
SetConsoleDebugLevel(logEventLevel, devices);
Expand All @@ -211,7 +213,7 @@ private static void SetDebugFromConsole(string levelString)
}
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
Expand Down
3 changes: 1 addition & 2 deletions src/Pepperdash.Core/Logging/CrestronEnricher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ static CrestronEnricher()
{
AppName = CrestronEnvironment.DevicePlatform switch
{
eDevicePlatform.Appliance => $"App {InitialParametersClass.ApplicationNumber}",
eDevicePlatform.Appliance => $"APP{InitialParametersClass.ApplicationNumber.ToString().PadLeft(2, '0')}",
eDevicePlatform.Server => $"{InitialParametersClass.RoomId}",
_ => string.Empty
};
Expand All @@ -22,7 +22,6 @@ static CrestronEnricher()
public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
{
var property = propertyFactory.CreateProperty("App", AppName);

logEvent.AddOrUpdateProperty(property);
}
}
Expand Down
10 changes: 6 additions & 4 deletions src/Pepperdash.Core/Logging/DebugNet472WebSocket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@
using Crestron.SimplSharp;

#if NET472
using System.IO;
using WebSocketSharp.Net;
using WebSocketSharp.Server;

namespace PepperDash.Core.Logging
{
internal class DebugNet472WebSocket : DebugWebSocket
{
private const string Path = "/debug/join/";
private const string WebsocketPath = "/debug/join/";

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;

Expand All @@ -24,7 +25,8 @@ public DebugNet472WebSocket(string certPath = "") : base(certPath)

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,
CheckCertificateRevocation = false,
Expand All @@ -38,7 +40,7 @@ public DebugNet472WebSocket(string certPath = "") : base(certPath)
};
}

server.AddWebSocketService<DebugClient>(Path);
server.AddWebSocketService<DebugClient>(WebsocketPath);
server.Start();
}

Expand Down
14 changes: 8 additions & 6 deletions src/Pepperdash.Core/Logging/DebugWebSocket.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
using System;
using System.IO;
using Crestron.SimplSharp;

#if NET472
using Org.BouncyCastle.Asn1.X509;
#endif

namespace PepperDash.Core.Logging
{
Expand All @@ -22,8 +19,15 @@ protected DebugWebSocket(string certPath = "")
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);
}
}
Expand All @@ -32,7 +36,6 @@ protected DebugWebSocket(string certPath = "")

private static void CreateCert(string filePath)
{
#if NET472
try
{
var utility = new BouncyCertificate();
Expand All @@ -48,7 +51,6 @@ private static void CreateCert(string filePath)
{
Debug.LogError(ex, "WSS failed to create cert");
}
#endif
}

public abstract bool IsListening { get; }
Expand Down

0 comments on commit 4f01ff1

Please sign in to comment.