feat: add port forwarding functionality for debug websocket in DebugSessionRequestHandler

This commit is contained in:
jkdevito 2026-06-12 11:26:56 -05:00
commit 6a4e3d5be2
3 changed files with 109 additions and 3 deletions

View file

@ -7,6 +7,7 @@ using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Reflection; using System.Reflection;
using System.Text.RegularExpressions;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace PepperDash.Essentials.Core namespace PepperDash.Essentials.Core
@ -176,7 +177,20 @@ namespace PepperDash.Essentials.Core
{ {
if (!conversionType.IsEnum) if (!conversionType.IsEnum)
{ {
return Convert.ChangeType(value, conversionType, System.Globalization.CultureInfo.InvariantCulture); if (conversionType == typeof(byte[]) && value is string byteString)
{
var unescaped = UnescapeString(byteString);
return System.Text.Encoding.GetEncoding(28591).GetBytes(unescaped);
}
var converted = Convert.ChangeType(value, conversionType, System.Globalization.CultureInfo.InvariantCulture);
if (conversionType == typeof(string) && converted is string s)
{
return UnescapeString(s);
}
return converted;
} }
var stringValue = Convert.ToString(value); var stringValue = Convert.ToString(value);
@ -189,6 +203,32 @@ namespace PepperDash.Essentials.Core
return Enum.Parse(conversionType, stringValue, true); return Enum.Parse(conversionType, stringValue, true);
} }
/// <summary>
/// Processes escape sequences in a string, converting sequences like \r, \n, \t, \xHH
/// to their corresponding non-printable ASCII characters.
/// </summary>
private static string UnescapeString(string input)
{
if (string.IsNullOrEmpty(input))
return input;
return Regex.Replace(input, @"\\(r|n|t|\\|x[0-9A-Fa-f]{2})", match =>
{
var seq = match.Groups[1].Value;
switch (seq)
{
case "r": return "\r";
case "n": return "\n";
case "t": return "\t";
case "\\": return "\\";
default:
// \xHH hex escape
var hex = seq.Substring(1);
return ((char)Convert.ToInt32(hex, 16)).ToString();
}
});
}
/// <summary> /// <summary>
/// Gets the properties on a device /// Gets the properties on a device
/// </summary> /// </summary>

View file

@ -348,9 +348,9 @@ namespace PepperDash.Essentials.Core
var sources = DeviceManager.AllDevices.OfType<IRoutingOutputs>() var sources = DeviceManager.AllDevices.OfType<IRoutingOutputs>()
.Where(d => !(d is IRoutingInputsOutputs)).ToList(); .Where(d => !(d is IRoutingInputsOutputs)).ToList();
foreach (var sink in sinks) foreach (var sink in sinks.Where(d => !(d is IRoutingInputsOutputs)))
{ {
foreach (var source in sources) foreach (var source in sources.Where(d => !(d is IRoutingInputsOutputs)))
{ {
foreach (var inputPort in sink.InputPorts) foreach (var inputPort in sink.InputPorts)
{ {
@ -363,6 +363,10 @@ namespace PepperDash.Essentials.Core
continue; continue;
} }
Debug.LogVerbose("AudioOrSingleRoute Found: {audioRoute}", audioOrSingleRoute);
Debug.LogVerbose("VideoRoute Found: {videoRoute}", videoRoute);
if (audioOrSingleRoute != null) if (audioOrSingleRoute != null)
{ {
// Only add routes that have actual switching steps // Only add routes that have actual switching steps

View file

@ -57,6 +57,36 @@ namespace PepperDash.Essentials.Core.Web.RequestHandlers
// Start the WS Server // Start the WS Server
Debug.WebsocketSink.StartServerAndSetPort(port); Debug.WebsocketSink.StartServerAndSetPort(port);
Debug.SetWebSocketMinimumDebugLevel(Serilog.Events.LogEventLevel.Verbose); Debug.SetWebSocketMinimumDebugLevel(Serilog.Events.LogEventLevel.Verbose);
// Attempt to forward the port to the CS LAN
try
{
var csAdapterId = CrestronEthernetHelper.GetAdapterdIdForSpecifiedAdapterType(
EthernetAdapterType.EthernetCSAdapter);
var csIp = CrestronEthernetHelper.GetEthernetParameter(
CrestronEthernetHelper.ETHERNET_PARAMETER_TO_GET.GET_CURRENT_IP_ADDRESS, csAdapterId);
var result = CrestronEthernetHelper.AddPortForwarding(
(ushort)port, (ushort)port, csIp,
CrestronEthernetHelper.ePortMapTransport.TCP);
if (result != CrestronEthernetHelper.PortForwardingUserPatRetCodes.NoErr)
{
Debug.LogMessage(LogEventLevel.Warning, "Error adding port forwarding for debug websocket: {0}", result);
}
else
{
Debug.LogMessage(LogEventLevel.Information, "Port {0} forwarded to CS LAN for debug websocket", port);
}
}
catch (ArgumentException)
{
Debug.LogMessage(LogEventLevel.Debug, "This processor does not have a CS LAN adapter; skipping port forwarding");
}
catch (Exception ex)
{
Debug.LogMessage(LogEventLevel.Warning, "Error automatically forwarding debug websocket port to CS LAN: {0}", ex.Message);
}
} }
var url = Debug.WebsocketSink.Url; var url = Debug.WebsocketSink.Url;
@ -90,8 +120,40 @@ namespace PepperDash.Essentials.Core.Web.RequestHandlers
/// <param name="context"></param> /// <param name="context"></param>
protected override void HandlePost(HttpCwsContext context) protected override void HandlePost(HttpCwsContext context)
{ {
var port = Debug.WebsocketSink.Port;
Debug.WebsocketSink.StopServer(); Debug.WebsocketSink.StopServer();
// Remove the port forwarding entry
try
{
var csAdapterId = CrestronEthernetHelper.GetAdapterdIdForSpecifiedAdapterType(
EthernetAdapterType.EthernetCSAdapter);
var csIp = CrestronEthernetHelper.GetEthernetParameter(
CrestronEthernetHelper.ETHERNET_PARAMETER_TO_GET.GET_CURRENT_IP_ADDRESS, csAdapterId);
var result = CrestronEthernetHelper.RemovePortForwarding(
(ushort)port, (ushort)port, csIp,
CrestronEthernetHelper.ePortMapTransport.TCP);
if (result != CrestronEthernetHelper.PortForwardingUserPatRetCodes.NoErr)
{
Debug.LogMessage(LogEventLevel.Warning, "Error removing port forwarding for debug websocket: {0}", result);
}
else
{
Debug.LogMessage(LogEventLevel.Information, "Port forwarding for port {0} removed", port);
}
}
catch (ArgumentException)
{
Debug.LogMessage(LogEventLevel.Debug, "This processor does not have a CS LAN adapter; skipping port forwarding removal");
}
catch (Exception ex)
{
Debug.LogMessage(LogEventLevel.Warning, "Error removing debug websocket port forwarding: {0}", ex.Message);
}
context.Response.StatusCode = 200; context.Response.StatusCode = 200;
context.Response.StatusDescription = "OK"; context.Response.StatusDescription = "OK";
context.Response.End(); context.Response.End();