Merge pull request #181 from PepperDash/feature-2/ssh-move-away-from-crestron

Migrate to Renci SSH.NET
This commit is contained in:
Neil Dorin
2025-02-19 17:13:16 -07:00
committed by GitHub
4 changed files with 76 additions and 87 deletions

View File

@@ -1,10 +1,11 @@
using System; using System;
using System.Linq;
using System.Text; using System.Text;
using System.Threading;
using Crestron.SimplSharp; using Crestron.SimplSharp;
using Crestron.SimplSharp.CrestronSockets; using Crestron.SimplSharp.CrestronSockets;
using Crestron.SimplSharp.Ssh; using PepperDash.Core.Logging;
using Crestron.SimplSharp.Ssh.Common; using Renci.SshNet;
using Renci.SshNet.Common;
namespace PepperDash.Core namespace PepperDash.Core
{ {
@@ -133,7 +134,8 @@ namespace PepperDash.Core
CTimer ReconnectTimer; CTimer ReconnectTimer;
//Lock object to prevent simulatneous connect/disconnect operations //Lock object to prevent simulatneous connect/disconnect operations
private CCriticalSection connectLock = new CCriticalSection(); //private CCriticalSection connectLock = new CCriticalSection();
private SemaphoreSlim connectLock = new SemaphoreSlim(1);
private bool DisconnectLogged = false; private bool DisconnectLogged = false;
@@ -158,7 +160,7 @@ namespace PepperDash.Core
{ {
Connect(); Connect();
} }
}, Timeout.Infinite); }, System.Threading.Timeout.Infinite);
} }
/// <summary> /// <summary>
@@ -176,7 +178,7 @@ namespace PepperDash.Core
{ {
Connect(); Connect();
} }
}, Timeout.Infinite); }, System.Threading.Timeout.Infinite);
} }
/// <summary> /// <summary>
@@ -196,7 +198,7 @@ namespace PepperDash.Core
{ {
if (Client != null) if (Client != null)
{ {
Debug.Console(1, this, "Program stopping. Closing connection"); this.LogDebug("Program stopping. Closing connection");
Disconnect(); Disconnect();
} }
} }
@@ -211,7 +213,7 @@ namespace PepperDash.Core
if (string.IsNullOrEmpty(Hostname) || Port < 1 || Port > 65535 if (string.IsNullOrEmpty(Hostname) || Port < 1 || Port > 65535
|| Username == null || Password == null) || Username == null || Password == null)
{ {
Debug.Console(0, this, Debug.ErrorLogLevel.Error, "Connect failed. Check hostname, port, username and password are set or not null"); this.LogError("Connect failed. Check hostname, port, username and password are set or not null");
return; return;
} }
@@ -219,14 +221,14 @@ namespace PepperDash.Core
try try
{ {
connectLock.Enter(); connectLock.Wait();
if (IsConnected) if (IsConnected)
{ {
Debug.Console(1, this, "Connection already connected. Exiting Connect()"); this.LogDebug("Connection already connected. Exiting Connect");
} }
else else
{ {
Debug.Console(1, this, "Attempting connect"); this.LogDebug("Attempting connect");
// Cancel reconnect if running. // Cancel reconnect if running.
ReconnectTimer.Stop(); ReconnectTimer.Stop();
@@ -234,7 +236,7 @@ namespace PepperDash.Core
// Cleanup the old client if it already exists // Cleanup the old client if it already exists
if (Client != null) if (Client != null)
{ {
Debug.Console(1, this, "Cleaning up disconnected client"); this.LogDebug("Cleaning up disconnected client");
KillClient(SocketStatus.SOCKET_STATUS_BROKEN_LOCALLY); KillClient(SocketStatus.SOCKET_STATUS_BROKEN_LOCALLY);
} }
@@ -243,7 +245,7 @@ namespace PepperDash.Core
kauth.AuthenticationPrompt += new EventHandler<AuthenticationPromptEventArgs>(kauth_AuthenticationPrompt); kauth.AuthenticationPrompt += new EventHandler<AuthenticationPromptEventArgs>(kauth_AuthenticationPrompt);
PasswordAuthenticationMethod pauth = new PasswordAuthenticationMethod(Username, Password); PasswordAuthenticationMethod pauth = new PasswordAuthenticationMethod(Username, Password);
Debug.Console(1, this, "Creating new SshClient"); this.LogDebug("Creating new SshClient");
ConnectionInfo connectionInfo = new ConnectionInfo(Hostname, Port, Username, pauth, kauth); ConnectionInfo connectionInfo = new ConnectionInfo(Hostname, Port, Username, pauth, kauth);
Client = new SshClient(connectionInfo); Client = new SshClient(connectionInfo);
@@ -257,7 +259,7 @@ namespace PepperDash.Core
Client.Connect(); Client.Connect();
TheStream = Client.CreateShellStream("PDTShell", 100, 80, 100, 200, 65534); TheStream = Client.CreateShellStream("PDTShell", 100, 80, 100, 200, 65534);
TheStream.DataReceived += Stream_DataReceived; TheStream.DataReceived += Stream_DataReceived;
Debug.Console(1, this, Debug.ErrorLogLevel.Notice, "Connected"); this.LogInformation("Connected");
ClientStatus = SocketStatus.SOCKET_STATUS_CONNECTED; ClientStatus = SocketStatus.SOCKET_STATUS_CONNECTED;
DisconnectLogged = false; DisconnectLogged = false;
} }
@@ -267,35 +269,52 @@ namespace PepperDash.Core
var errorLogLevel = DisconnectLogged == true ? Debug.ErrorLogLevel.None : Debug.ErrorLogLevel.Error; var errorLogLevel = DisconnectLogged == true ? Debug.ErrorLogLevel.None : Debug.ErrorLogLevel.Error;
if (ie is SocketException) if (ie is SocketException)
Debug.Console(1, this, errorLogLevel, "'{0}' CONNECTION failure: Cannot reach host, ({1})", Key, ie.Message);
else if (ie is System.Net.Sockets.SocketException)
Debug.Console(1, this, errorLogLevel, "'{0}' Connection failure: Cannot reach host '{1}' on port {2}, ({3})",
Key, Hostname, Port, ie.GetType());
else if (ie is SshAuthenticationException)
{ {
Debug.Console(1, this, errorLogLevel, "Authentication failure for username '{0}', ({1})", this.LogException(ie, "CONNECTION failure: Cannot reach host, ({1})", Key, ie.Message);
}
if (ie is System.Net.Sockets.SocketException socketException)
{
this.LogException(ie, "'{0}' Connection failure: Cannot reach host '{1}' on port {2}, ({3})",
Key, Hostname, Port, ie.GetType());
}
if (ie is SshAuthenticationException)
{
this.LogException(ie, "Authentication failure for username '{0}', ({1})", this,
Username, ie.Message); Username, ie.Message);
} }
else else
Debug.Console(1, this, errorLogLevel, "Error on connect:\r({0})", ie.Message); this.LogException(ie, "Error on connect");
DisconnectLogged = true; DisconnectLogged = true;
KillClient(SocketStatus.SOCKET_STATUS_CONNECT_FAILED); KillClient(SocketStatus.SOCKET_STATUS_CONNECT_FAILED);
if (AutoReconnect) if (AutoReconnect)
{ {
Debug.Console(1, this, "Checking autoreconnect: {0}, {1}ms", AutoReconnect, AutoReconnectIntervalMs); this.LogDebug("Checking autoreconnect: {0}, {1}ms", AutoReconnect, AutoReconnectIntervalMs);
ReconnectTimer.Reset(AutoReconnectIntervalMs);
}
}
catch(SshOperationTimeoutException ex)
{
this.LogWarning("Connection attempt timed out: {message}", ex.Message);
DisconnectLogged = true;
KillClient(SocketStatus.SOCKET_STATUS_CONNECT_FAILED);
if (AutoReconnect)
{
this.LogDebug("Checking autoreconnect: {0}, {1}ms", AutoReconnect, AutoReconnectIntervalMs);
ReconnectTimer.Reset(AutoReconnectIntervalMs); ReconnectTimer.Reset(AutoReconnectIntervalMs);
} }
} }
catch (Exception e) catch (Exception e)
{ {
var errorLogLevel = DisconnectLogged == true ? Debug.ErrorLogLevel.None : Debug.ErrorLogLevel.Error; var errorLogLevel = DisconnectLogged == true ? Debug.ErrorLogLevel.None : Debug.ErrorLogLevel.Error;
Debug.Console(1, this, errorLogLevel, "Unhandled exception on connect:\r({0})", e.Message); this.LogException(e, "Unhandled exception on connect");
DisconnectLogged = true; DisconnectLogged = true;
KillClient(SocketStatus.SOCKET_STATUS_CONNECT_FAILED); KillClient(SocketStatus.SOCKET_STATUS_CONNECT_FAILED);
if (AutoReconnect) if (AutoReconnect)
{ {
Debug.Console(1, this, "Checking autoreconnect: {0}, {1}ms", AutoReconnect, AutoReconnectIntervalMs); this.LogDebug("Checking autoreconnect: {0}, {1}ms", AutoReconnect, AutoReconnectIntervalMs);
ReconnectTimer.Reset(AutoReconnectIntervalMs); ReconnectTimer.Reset(AutoReconnectIntervalMs);
} }
} }
@@ -303,7 +322,7 @@ namespace PepperDash.Core
} }
finally finally
{ {
connectLock.Leave(); connectLock.Release();
} }
} }
@@ -335,35 +354,7 @@ namespace PepperDash.Core
Client.Disconnect(); Client.Disconnect();
Client = null; Client = null;
ClientStatus = status; ClientStatus = status;
Debug.Console(1, this, "Disconnected"); this.LogDebug("Disconnected");
}
}
/// <summary>
/// Anything to do with reestablishing connection on failures
/// </summary>
void HandleConnectionFailure()
{
KillClient(SocketStatus.SOCKET_STATUS_CONNECT_FAILED);
Debug.Console(1, this, "Client nulled due to connection failure. AutoReconnect: {0}, ConnectEnabled: {1}", AutoReconnect, ConnectEnabled);
if (AutoReconnect && ConnectEnabled)
{
Debug.Console(1, this, "Checking autoreconnect: {0}, {1}ms", AutoReconnect, AutoReconnectIntervalMs);
if (ReconnectTimer == null)
{
ReconnectTimer = new CTimer(o =>
{
Connect();
}, AutoReconnectIntervalMs);
Debug.Console(1, this, "Attempting connection in {0} seconds",
(float) (AutoReconnectIntervalMs/1000));
}
else
{
Debug.Console(1, this, "{0} second reconnect cycle running",
(float) (AutoReconnectIntervalMs/1000));
}
} }
} }
@@ -378,7 +369,7 @@ namespace PepperDash.Core
TheStream.Close(); TheStream.Close();
TheStream.Dispose(); TheStream.Dispose();
TheStream = null; TheStream = null;
Debug.Console(1, this, "Disconnected stream"); this.LogDebug("Disconnected stream");
} }
} }
@@ -395,7 +386,7 @@ namespace PepperDash.Core
/// <summary> /// <summary>
/// Handler for data receive on ShellStream. Passes data across to queue for line parsing. /// Handler for data receive on ShellStream. Passes data across to queue for line parsing.
/// </summary> /// </summary>
void Stream_DataReceived(object sender, Crestron.SimplSharp.Ssh.Common.ShellDataEventArgs e) void Stream_DataReceived(object sender, ShellDataEventArgs e)
{ {
var bytes = e.Data; var bytes = e.Data;
if (bytes.Length > 0) if (bytes.Length > 0)
@@ -405,7 +396,7 @@ namespace PepperDash.Core
{ {
if (StreamDebugging.RxStreamDebuggingIsEnabled) if (StreamDebugging.RxStreamDebuggingIsEnabled)
{ {
Debug.Console(0, this, "Received {1} bytes: '{0}'", ComTextHelper.GetEscapedText(bytes), bytes.Length); this.LogInformation("Received {1} bytes: '{0}'", ComTextHelper.GetEscapedText(bytes), bytes.Length);
} }
bytesHandler(this, new GenericCommMethodReceiveBytesArgs(bytes)); bytesHandler(this, new GenericCommMethodReceiveBytesArgs(bytes));
} }
@@ -415,7 +406,7 @@ namespace PepperDash.Core
{ {
var str = Encoding.GetEncoding(28591).GetString(bytes, 0, bytes.Length); var str = Encoding.GetEncoding(28591).GetString(bytes, 0, bytes.Length);
if (StreamDebugging.RxStreamDebuggingIsEnabled) if (StreamDebugging.RxStreamDebuggingIsEnabled)
Debug.Console(0, this, "Received: '{0}'", ComTextHelper.GetDebugText(str)); this.LogInformation("Received: '{0}'", ComTextHelper.GetDebugText(str));
textHandler(this, new GenericCommMethodReceiveTextArgs(str)); textHandler(this, new GenericCommMethodReceiveTextArgs(str));
} }
@@ -427,27 +418,26 @@ namespace PepperDash.Core
/// Error event handler for client events - disconnect, etc. Will forward those events via ConnectionChange /// Error event handler for client events - disconnect, etc. Will forward those events via ConnectionChange
/// event /// event
/// </summary> /// </summary>
void Client_ErrorOccurred(object sender, Crestron.SimplSharp.Ssh.Common.ExceptionEventArgs e) void Client_ErrorOccurred(object sender, ExceptionEventArgs e)
{ {
CrestronInvoke.BeginInvoke(o => CrestronInvoke.BeginInvoke(o =>
{ {
if (e.Exception is SshConnectionException || e.Exception is System.Net.Sockets.SocketException) if (e.Exception is SshConnectionException || e.Exception is System.Net.Sockets.SocketException)
Debug.Console(1, this, Debug.ErrorLogLevel.Error, "Disconnected by remote"); this.LogError("Disconnected by remote");
else else
Debug.Console(1, this, Debug.ErrorLogLevel.Error, "Unhandled SSH client error: {0}", e.Exception); this.LogException(e.Exception, "Unhandled SSH client error");
try try
{ {
connectLock.Enter(); connectLock.Wait();
KillClient(SocketStatus.SOCKET_STATUS_BROKEN_REMOTELY); KillClient(SocketStatus.SOCKET_STATUS_BROKEN_REMOTELY);
} }
finally finally
{ {
connectLock.Leave(); connectLock.Release();
} }
if (AutoReconnect && ConnectEnabled) if (AutoReconnect && ConnectEnabled)
{ {
Debug.Console(1, this, "Checking autoreconnect: {0}, {1}ms", AutoReconnect, AutoReconnectIntervalMs); this.LogDebug("Checking autoreconnect: {0}, {1}ms", AutoReconnect, AutoReconnectIntervalMs);
ReconnectTimer.Reset(AutoReconnectIntervalMs); ReconnectTimer.Reset(AutoReconnectIntervalMs);
} }
}); });
@@ -475,7 +465,7 @@ namespace PepperDash.Core
if (Client != null && TheStream != null && IsConnected) if (Client != null && TheStream != null && IsConnected)
{ {
if (StreamDebugging.TxStreamDebuggingIsEnabled) if (StreamDebugging.TxStreamDebuggingIsEnabled)
Debug.Console(0, this, "Sending {0} characters of text: '{1}'", text.Length, ComTextHelper.GetDebugText(text)); this.LogInformation("Sending {0} characters of text: '{1}'", text.Length, ComTextHelper.GetDebugText(text));
TheStream.Write(text); TheStream.Write(text);
TheStream.Flush(); TheStream.Flush();
@@ -483,15 +473,12 @@ namespace PepperDash.Core
} }
else else
{ {
Debug.Console(1, this, "Client is null or disconnected. Cannot Send Text"); this.LogDebug("Client is null or disconnected. Cannot Send Text");
} }
} }
catch (Exception ex) catch (Exception ex)
{ {
Debug.Console(0, "Exception: {0}", ex.Message); this.LogException(ex, "Exception sending text: {message}", text);
Debug.Console(0, "Stack Trace: {0}", ex.StackTrace);
Debug.Console(1, this, Debug.ErrorLogLevel.Error, "Stream write failed. Disconnected, closing");
} }
} }
@@ -506,19 +493,19 @@ namespace PepperDash.Core
if (Client != null && TheStream != null && IsConnected) if (Client != null && TheStream != null && IsConnected)
{ {
if (StreamDebugging.TxStreamDebuggingIsEnabled) if (StreamDebugging.TxStreamDebuggingIsEnabled)
Debug.Console(0, this, "Sending {0} bytes: '{1}'", bytes.Length, ComTextHelper.GetEscapedText(bytes)); this.LogInformation("Sending {0} bytes: '{1}'", bytes.Length, ComTextHelper.GetEscapedText(bytes));
TheStream.Write(bytes, 0, bytes.Length); TheStream.Write(bytes, 0, bytes.Length);
TheStream.Flush(); TheStream.Flush();
} }
else else
{ {
Debug.Console(1, this, "Client is null or disconnected. Cannot Send Bytes"); this.LogDebug("Client is null or disconnected. Cannot Send Bytes");
} }
} }
catch catch (Exception ex)
{ {
Debug.Console(1, this, Debug.ErrorLogLevel.Error, "Stream write failed. Disconnected, closing"); this.LogException(ex, "Exception sending bytes: {message}", ComTextHelper.GetEscapedText(bytes));
} }
} }

View File

@@ -1,11 +1,16 @@
using Serilog; using Serilog;
using Serilog.Events; using Serilog.Events;
using System;
using Log = PepperDash.Core.Debug; using Log = PepperDash.Core.Debug;
namespace PepperDash.Core.Logging namespace PepperDash.Core.Logging
{ {
public static class DebugExtensions public static class DebugExtensions
{ {
public static void LogException(this IKeyed device, Exception ex, string message, params object[] args)
{
Log.LogMessage(ex, message, device, args);
}
public static void LogVerbose(this IKeyed device, string message, params object[] args) public static void LogVerbose(this IKeyed device, string message, params object[] args)
{ {
Log.LogMessage(LogEventLevel.Verbose, device, message, args); Log.LogMessage(LogEventLevel.Verbose, device, message, args);

View File

@@ -32,13 +32,14 @@
<Reference Include="System.Net.Http" /> <Reference Include="System.Net.Http" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="BouncyCastle" Version="1.8.9" /> <PackageReference Include="BouncyCastle.Cryptography" Version="2.4.0" />
<PackageReference Include="Crestron.SimplSharp.SDK.Library" Version="2.20.66" /> <PackageReference Include="Crestron.SimplSharp.SDK.Library" Version="2.20.66" />
<PackageReference Include="Serilog" Version="3.1.1" /> <PackageReference Include="Serilog" Version="3.1.1" />
<PackageReference Include="Serilog.Expressions" Version="4.0.0" /> <PackageReference Include="Serilog.Expressions" Version="4.0.0" />
<PackageReference Include="Serilog.Formatting.Compact" Version="2.0.0" /> <PackageReference Include="Serilog.Formatting.Compact" Version="2.0.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="5.0.1" /> <PackageReference Include="Serilog.Sinks.Console" Version="5.0.1" />
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" /> <PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
<PackageReference Include="SSH.NET" Version="2024.2.0" />
<PackageReference Include="WebSocketSharp" Version="1.0.3-rc11" /> <PackageReference Include="WebSocketSharp" Version="1.0.3-rc11" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View File

@@ -9,7 +9,6 @@ using Org.BouncyCastle.Asn1.X509;
using Org.BouncyCastle.Crypto; using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Generators; using Org.BouncyCastle.Crypto.Generators;
using Org.BouncyCastle.Crypto.Prng; using Org.BouncyCastle.Crypto.Prng;
using Org.BouncyCastle.Math;
using Org.BouncyCastle.Pkcs; using Org.BouncyCastle.Pkcs;
using Org.BouncyCastle.Security; using Org.BouncyCastle.Security;
using Org.BouncyCastle.Utilities; using Org.BouncyCastle.Utilities;
@@ -17,10 +16,7 @@ using Org.BouncyCastle.X509;
using X509Certificate2 = System.Security.Cryptography.X509Certificates.X509Certificate2; using X509Certificate2 = System.Security.Cryptography.X509Certificates.X509Certificate2;
using X509KeyStorageFlags = System.Security.Cryptography.X509Certificates.X509KeyStorageFlags; using X509KeyStorageFlags = System.Security.Cryptography.X509Certificates.X509KeyStorageFlags;
using X509ContentType = System.Security.Cryptography.X509Certificates.X509ContentType; using X509ContentType = System.Security.Cryptography.X509Certificates.X509ContentType;
using System.Text;
using Org.BouncyCastle.Crypto.Operators; using Org.BouncyCastle.Crypto.Operators;
using System.Numerics;
using System.Security.Cryptography.X509Certificates;
using BigInteger = Org.BouncyCastle.Math.BigInteger; using BigInteger = Org.BouncyCastle.Math.BigInteger;
using X509Certificate = Org.BouncyCastle.X509.X509Certificate; using X509Certificate = Org.BouncyCastle.X509.X509Certificate;
@@ -284,7 +280,7 @@ namespace PepperDash.Core
// Now to convert the Bouncy Castle certificate to a .NET certificate. // Now to convert the Bouncy Castle certificate to a .NET certificate.
// See http://web.archive.org/web/20100504192226/http://www.fkollmann.de/v2/post/Creating-certificates-using-BouncyCastle.aspx // See http://web.archive.org/web/20100504192226/http://www.fkollmann.de/v2/post/Creating-certificates-using-BouncyCastle.aspx
// ...but, basically, we create a PKCS12 store (a .PFX file) in memory, and add the public and private key to that. // ...but, basically, we create a PKCS12 store (a .PFX file) in memory, and add the public and private key to that.
var store = new Pkcs12Store(); var store = new Pkcs12StoreBuilder().Build();
// What Bouncy Castle calls "alias" is the same as what Windows terms the "friendly name". // What Bouncy Castle calls "alias" is the same as what Windows terms the "friendly name".
string friendlyName = certificate.SubjectDN.ToString(); string friendlyName = certificate.SubjectDN.ToString();