diff --git a/Pepperdash Core/Pepperdash Core/Comm/GenericSshClient.cs b/Pepperdash Core/Pepperdash Core/Comm/GenericSshClient.cs index fc4115c..2e431e8 100644 --- a/Pepperdash Core/Pepperdash Core/Comm/GenericSshClient.cs +++ b/Pepperdash Core/Pepperdash Core/Comm/GenericSshClient.cs @@ -8,6 +8,7 @@ using Crestron.SimplSharp.Ssh.Common; namespace PepperDash.Core { + /* /// /// /// @@ -378,6 +379,7 @@ namespace PepperDash.Core /// void Stream_DataReceived(object sender, Crestron.SimplSharp.Ssh.Common.ShellDataEventArgs e) { + var bytes = e.Data; if (bytes.Length > 0) { @@ -537,5 +539,5 @@ namespace PepperDash.Core IsConnected = isConnected; Client = client; } - } + }*/ } diff --git a/Pepperdash Core/Pepperdash Core/Comm/GenericSshClientV2.cs b/Pepperdash Core/Pepperdash Core/Comm/GenericSshClientV2.cs new file mode 100644 index 0000000..5bb768e --- /dev/null +++ b/Pepperdash Core/Pepperdash Core/Comm/GenericSshClientV2.cs @@ -0,0 +1,573 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using Crestron.SimplSharp; +using Crestron.SimplSharp.CrestronSockets; +using Crestron.SimplSharp.Ssh; +using Crestron.SimplSharp.Ssh.Common; + +namespace PepperDash.Core +{ + /// + /// Generic ssh client + /// + public class GenericSshClient : Device, IStreamDebugging, ISocketStatus, IAutoReconnect, IDisposable + { + private static class ConnectionProgress + { + public const int Idle = 0; + public const int InProgress = 1; + } + + private SshClient client; + private ShellStream stream; + private CTimer connectTimer; + private CTimer disconnectTimer; + private int connectionProgress = ConnectionProgress.Idle; + private KeyboardInteractiveAuthenticationMethod keyboardAuth; + private PasswordAuthenticationMethod passwordAuth; + private SocketStatus socketSatus; + + /// + /// Address of server + /// + public string Hostname { get; set; } + + /// + /// Port on server + /// + public int Port { get; set; } + + /// + /// Username for server + /// + public string Username { get; set; } + + /// + /// And... Password for server. That was worth documenting! + /// + public string Password { get; set; } + + /// + /// Client socket status + /// + public SocketStatus ClientStatus + { + get { return socketSatus; } + private set + { + if (socketSatus == value) + return; + socketSatus = value; + OnConnectionChange(); + } + } + + /// + /// Will be set and unset by connect and disconnect only + /// + public bool ConnectEnabled { get; private set; } + + private void OnConnectionChange() + { + var handler = ConnectionChange; + if (handler != null) + handler(this, new GenericSocketStatusChageEventArgs(this)); + } + + private const string SPlusKey = "Uninitialized SshClient"; + + /// + /// S+ Constructor - Must set all properties before calling Connect + /// + public GenericSshClient() + : base(SPlusKey) + { + StreamDebugging = new CommunicationStreamDebugging(SPlusKey); + AutoReconnectIntervalMs = 10000; + + connectTimer = new CTimer(_ => + { + Debug.Console(1, this, "Attempting to reconnect"); + Connect(); + }, Timeout.Infinite); + + disconnectTimer = new CTimer(_ => + { + Debug.Console(1, this, "Attempting to disconnect"); + Disconnect(); + }, Timeout.Infinite); + + CrestronEnvironment.ProgramStatusEventHandler += OnProgramShutdown; + } + + /// + /// Typical constructor. + /// + public GenericSshClient(string key, string hostname, int port, string username, string password) : base(key) + { + Key = key; + Hostname = hostname; + Port = port; + Username = username; + Password = password; + AutoReconnectIntervalMs = 10000; + + connectTimer = new CTimer(_ => + { + Debug.Console(1, this, "Attempting to reconnect"); + Connect(); + }, Timeout.Infinite); + + disconnectTimer = new CTimer(_ => + { + Debug.Console(1, this, "Attempting to disconnect"); + Disconnect(); + }, Timeout.Infinite); + + StreamDebugging = new CommunicationStreamDebugging(key); + CrestronEnvironment.ProgramStatusEventHandler += OnProgramShutdown; + } + + private void OnProgramShutdown(eProgramStatusEventType type) + { + try + { + if (type == eProgramStatusEventType.Stopping) + Disconnect(); + } + catch (Exception ex) + { + Debug.Console(1, this, Debug.ErrorLogLevel.Notice, "Error at shutdown:{0}", ex); + } + } + + /// + /// Just to help S+ set the key + /// + public void Initialize(string key) + { + Key = key; + } + + /// + /// True when the server is connected - when status == 2. + /// + public bool IsConnected + { + get { return client != null && client.IsConnected; } + } + + /// + /// S+ helper for IsConnected + /// + public ushort UIsConnected + { + get { return (ushort)(IsConnected ? 1 : 0); } + } + + /// + /// Event fired when bytes are received on the shell + /// + public event EventHandler BytesReceived; + + /// + /// Event fired when test is received on the shell + /// + public event EventHandler TextReceived; + + /// + /// Sends text to the server + /// + /// + public void SendText(string text) + { + if (CanSend()) + { + if (StreamDebugging.TxStreamDebuggingIsEnabled) + Debug.Console(0, this, "Sending {0} characters of text: '{1}'", text.Length, ComTextHelper.GetDebugText(text.Trim())); + + stream.WriteLine(text); + } + else + { + Debug.Console(1, this, "SendText() called but not connected"); + Disconnect(SocketStatus.SOCKET_STATUS_NO_CONNECT); + } + } + + /// + /// Sends bytes to the server + /// + /// + public void SendBytes(byte[] bytes) + { + if (CanSend()) + { + if (StreamDebugging.TxStreamDebuggingIsEnabled) + Debug.Console(0, this, "Sending {0} bytes: '{1}'", bytes.Length, ComTextHelper.GetEscapedText(bytes)); + + stream.Write(bytes, 0, bytes.Length); + stream.Flush(); + } + else + { + Debug.Console(1, this, "SendText() called but not connected"); + Disconnect(SocketStatus.SOCKET_STATUS_NO_CONNECT); + } + } + + private bool CanSend() + { + return client != null && stream != null && client.IsConnected; + } + + /// + /// Connect to the server, using the provided properties. + /// + public void Connect() + { + ConnectEnabled = true; + DisposeOfTimer(disconnectTimer); + + if ( + Interlocked.CompareExchange(ref connectionProgress, ConnectionProgress.InProgress, + ConnectionProgress.Idle) == ConnectionProgress.Idle) + { + if (client != null && client.IsConnected) + { + Debug.Console(1, this, "Ignoring connection request... already connected"); + Interlocked.Exchange(ref connectionProgress, ConnectionProgress.Idle); + return; + } + + CrestronInvoke.BeginInvoke(_ => + { + const int defaultPort = 22; + var p = Port == default(int) ? defaultPort : Port; + + if (keyboardAuth != null) + { + keyboardAuth.AuthenticationPrompt -= AuthenticationPromptHandler; + keyboardAuth.Dispose(); + } + + if (passwordAuth != null) + { + passwordAuth.Dispose(); + } + + keyboardAuth = new KeyboardInteractiveAuthenticationMethod(Username); + passwordAuth = new PasswordAuthenticationMethod(Username, Password); + keyboardAuth.AuthenticationPrompt += AuthenticationPromptHandler; + + var connectionInfo = new ConnectionInfo(Hostname, p, Username, passwordAuth, keyboardAuth); + + if (connectTimer.Disposed && AutoReconnect) + { + connectTimer = new CTimer(obj => + { + Debug.Console(1, this, "Attempting to reconnect"); + Connect(); + }, Timeout.Infinite); + } + + ConnectInternal(connectionInfo); + }); + } + else + { + + Debug.Console(1, this, "Ingoring connection request while connect/disconnect in progress..."); + if (!connectTimer.Disposed && AutoReconnect) + connectTimer.Reset(10000); + } + } + + private void ConnectInternal(ConnectionInfo connectionInfo) + { + Debug.Console(1, this, "Attempting connection to: " + Hostname); + try + { + // Debug.Console(1, this, Debug.ErrorLogLevel.Notice, "Creating new client..."); + client = new SshClient(connectionInfo); + client.ErrorOccurred += ClientErrorHandler; + client.HostKeyReceived += HostKeyReceivedHandler; + ClientStatus = SocketStatus.SOCKET_STATUS_WAITING; + + // Debug.Console(1, this, Debug.ErrorLogLevel.Notice, "Connecting..."); + client.Connect(); + + // Debug.Console(1, this, Debug.ErrorLogLevel.Notice, "Creating shell..."); + stream = client.CreateShellStream("PDTShell", 100, 80, 100, 200, 65534); + stream.DataReceived += StreamDataReceivedHandler; + stream.ErrorOccurred += StreamErrorOccurredHandler; + // Debug.Console(1, this, Debug.ErrorLogLevel.Notice, "Shell created"); + + if (client.IsConnected) + { + Debug.Console(1, this, Debug.ErrorLogLevel.Notice, "Connected"); + connectTimer.Stop(); + ClientStatus = SocketStatus.SOCKET_STATUS_CONNECTED; + Interlocked.Exchange(ref connectionProgress, ConnectionProgress.Idle); + } + else + { + throw new Exception("Unknown connection error"); + } + } + catch (SshConnectionException e) + { + var ie = e.InnerException; // The details are inside!! + + if (ie is SocketException) + Debug.Console(1, this, Debug.ErrorLogLevel.Error, + "'{0}' CONNECTION failure: Cannot reach host, ({1})", + Key, ie.Message); + else if (ie is System.Net.Sockets.SocketException) + Debug.Console(1, this, Debug.ErrorLogLevel.Error, + "'{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, Debug.ErrorLogLevel.Error, "Authentication failure for username '{0}', ({1})", + Username, ie.Message); + } + else + Debug.Console(1, this, Debug.ErrorLogLevel.Error, "Error on connect:({0})", ie.Message); + + DisconnectInternal(SocketStatus.SOCKET_STATUS_CONNECT_FAILED); + } + catch (Exception e) + { + Debug.Console(1, this, Debug.ErrorLogLevel.Notice, "Connection error: " + e.Message); + DisconnectInternal(SocketStatus.SOCKET_STATUS_CONNECT_FAILED); + } + } + + /// + /// Disconnect the clients and put away it's resources. + /// + public void Disconnect() + { + ConnectEnabled = false; + Disconnect(SocketStatus.SOCKET_STATUS_BROKEN_LOCALLY); + } + + private void Disconnect(SocketStatus status) + { + // kill the reconnect timer if we are disconnecting locally + if (status == SocketStatus.SOCKET_STATUS_BROKEN_LOCALLY) + DisposeOfTimer(connectTimer); + + if ( + Interlocked.CompareExchange(ref connectionProgress, ConnectionProgress.InProgress, + ConnectionProgress.Idle) == ConnectionProgress.Idle) + { + Debug.Console(1, this, "Disconnecting..."); + CrestronInvoke.BeginInvoke(_ => DisconnectInternal(status)); + } + else + { + if (!disconnectTimer.Disposed) + DisposeOfTimer(disconnectTimer); + + disconnectTimer = new CTimer(_ => Disconnect(status), 10000); + Debug.Console(1, this, "Ingoring disconnect request while connect/disconnect in progress... will try again soon"); + } + } + + private void DisconnectInternal(SocketStatus status) + { + DisposeOfStream(); + DisposeOfClient(); + DisposeOfAuthMethods(); + + Debug.Console(1, this, Debug.ErrorLogLevel.Notice, "Disconnect Complete"); + ClientStatus = status; + + DisposeOfTimer(disconnectTimer); + if (!connectTimer.Disposed && AutoReconnect) + { + Debug.Console(1, this, "Autoreconnect try again soon"); + connectTimer.Reset(AutoReconnectIntervalMs); + } + + Interlocked.Exchange(ref connectionProgress, ConnectionProgress.Idle); + } + + private void DisposeOfAuthMethods() + { + try + { + if (keyboardAuth != null) + { + keyboardAuth.AuthenticationPrompt -= AuthenticationPromptHandler; + keyboardAuth.Dispose(); + keyboardAuth = null; + } + + if (passwordAuth != null) + { + passwordAuth.Dispose(); + passwordAuth = null; + } + } + catch (Exception e) + { + Debug.Console(1, this, Debug.ErrorLogLevel.Notice, + "Disconnect() Exception occured freeing auth: " + e.Message); + } + } + + private void DisposeOfClient() + { + try + { + if (client != null) + { + if (client.IsConnected) + client.Disconnect(); + + client.ErrorOccurred -= ClientErrorHandler; + client.HostKeyReceived -= HostKeyReceivedHandler; + client.Dispose(); + } + } + catch (Exception e) + { + Debug.Console(1, this, Debug.ErrorLogLevel.Notice, + "Disconnect() Exception occured freeing client: " + e.Message); + } + } + + private void DisposeOfStream() + { + try + { + if (stream != null) + { + stream.DataReceived -= StreamDataReceivedHandler; + stream.ErrorOccurred -= StreamErrorOccurredHandler; + stream.Close(); + stream.Dispose(); + } + } + catch (Exception e) + { + Debug.Console(1, this, Debug.ErrorLogLevel.Notice, + "Disconnect() exception occured freeing stream: " + e.Message); + } + } + + private static void DisposeOfTimer(CTimer timer) + { + if (timer == null || timer.Disposed) return; + timer.Stop(); + timer.Dispose(); + } + + private void StreamDataReceivedHandler(object sender, ShellDataEventArgs e) + { + var bytes = e.Data; + if (bytes.Length > 0) + { + var bytesHandler = BytesReceived; + if (bytesHandler != null) + { + if (StreamDebugging.RxStreamDebuggingIsEnabled) + { + Debug.Console(0, this, "Received {1} bytes: '{0}'", ComTextHelper.GetEscapedText(bytes), bytes.Length); + } + + bytesHandler(this, new GenericCommMethodReceiveBytesArgs(bytes)); + } + + var textHandler = TextReceived; + if (textHandler != null) + { + var str = Encoding.GetEncoding(28591).GetString(bytes, 0, bytes.Length); + if (StreamDebugging.RxStreamDebuggingIsEnabled) + Debug.Console(0, this, "Received: '{0}'", ComTextHelper.GetDebugText(str)); + + textHandler(this, new GenericCommMethodReceiveTextArgs(str)); + } + } + } + + private void ClientErrorHandler(object sender, ExceptionEventArgs e) + { + Debug.Console(1, this, "SSH client error:{0}", e.Exception); + Disconnect(SocketStatus.SOCKET_STATUS_BROKEN_REMOTELY); + } + + private void StreamErrorOccurredHandler(object sender, EventArgs e) + { + Debug.Console(1, this, "SSH Shellstream error"); + Disconnect(SocketStatus.SOCKET_STATUS_BROKEN_REMOTELY); + } + + private void AuthenticationPromptHandler(object sender, AuthenticationPromptEventArgs e) + { + foreach ( + var prompt in + e.Prompts.Where( + prompt => prompt.Request.IndexOf("Password:", StringComparison.InvariantCultureIgnoreCase) != -1) + ) + prompt.Response = Password; + } + + private static void HostKeyReceivedHandler(object sender, HostKeyEventArgs e) + { + e.CanTrust = true; + } + + private static IEnumerable SplitDataReceived(string str, int maxChunkSize) + { + for (var i = 0; i < str.Length; i += maxChunkSize) + { + yield return str.Substring(i, Math.Min(maxChunkSize, str.Length - i)); + } + } + + /// + /// Stream debugging properties + /// + public CommunicationStreamDebugging StreamDebugging { get; private set; } + + /// + /// Event fired on a connection status change + /// + public event EventHandler ConnectionChange; + + /// + /// Determines if autoreconnect is enabled + /// + public bool AutoReconnect { get; set; } + + /// + /// Millisecond value, determines the timeout period in between reconnect attempts. + /// Set to 10000 by default + /// + public int AutoReconnectIntervalMs { get; set; } + + private bool disposed; + public void Dispose() + { + Dispose(true); + CrestronEnvironment.GC.SuppressFinalize(this); + } + + protected virtual void Dispose(bool disposing) + { + if (disposed) + return; + + if (disposing) + Disconnect(); + + disposed = true; + } + } +} \ No newline at end of file diff --git a/Pepperdash Core/Pepperdash Core/PepperDash_Core.csproj b/Pepperdash Core/Pepperdash Core/PepperDash_Core.csproj index 86ae71d..41f64ed 100644 --- a/Pepperdash Core/Pepperdash Core/PepperDash_Core.csproj +++ b/Pepperdash Core/Pepperdash Core/PepperDash_Core.csproj @@ -72,6 +72,7 @@ +