using System; 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 { /// /// /// public class GenericSshClient : Device, ISocketStatusWithStreamDebugging, IAutoReconnect { private const string SPlusKey = "Uninitialized SshClient"; public CommunicationStreamDebugging StreamDebugging { get; private set; } /// /// Event that fires when data is received. Delivers args with byte array /// public event EventHandler BytesReceived; /// /// Event that fires when data is received. Delivered as text. /// public event EventHandler TextReceived; /// /// Event when the connection status changes. /// public event EventHandler ConnectionChange; /// /// /// //public event GenericSocketStatusChangeEventDelegate SocketStatusChange; /// /// 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; } /// /// True when the server is connected - when status == 2. /// public bool IsConnected { // returns false if no client or not connected get { return ClientStatus == SocketStatus.SOCKET_STATUS_CONNECTED; } } private bool IsConnecting = false; private bool DisconnectLogged = false; /// /// S+ helper for IsConnected /// public ushort UIsConnected { get { return (ushort)(IsConnected ? 1 : 0); } } /// /// /// public SocketStatus ClientStatus { get { return _ClientStatus; } private set { if (_ClientStatus == value) return; _ClientStatus = value; OnConnectionChange(); } } SocketStatus _ClientStatus; /// /// Contains the familiar Simpl analog status values. This drives the ConnectionChange event /// and IsConnected with be true when this == 2. /// public ushort UStatus { get { return (ushort)_ClientStatus; } } /// /// Determines whether client will attempt reconnection on failure. Default is true /// public bool AutoReconnect { get; set; } /// /// Will be set and unset by connect and disconnect only /// public bool ConnectEnabled { get; private set; } /// /// S+ helper for AutoReconnect /// public ushort UAutoReconnect { get { return (ushort)(AutoReconnect ? 1 : 0); } set { AutoReconnect = value == 1; } } /// /// Millisecond value, determines the timeout period in between reconnect attempts. /// Set to 5000 by default /// public int AutoReconnectIntervalMs { get; set; } SshClient Client; ShellStream TheStream; CTimer ReconnectTimer; //string PreviousHostname; //int PreviousPort; //string PreviousUsername; //string PreviousPassword; /// /// Typical constructor. /// public GenericSshClient(string key, string hostname, int port, string username, string password) : base(key) { StreamDebugging = new CommunicationStreamDebugging(key); CrestronEnvironment.ProgramStatusEventHandler += new ProgramStatusEventHandler(CrestronEnvironment_ProgramStatusEventHandler); Key = key; Hostname = hostname; Port = port; Username = username; Password = password; AutoReconnectIntervalMs = 5000; ReconnectTimer = new CTimer(o => { Connect(); }, Timeout.Infinite); } /// /// S+ Constructor - Must set all properties before calling Connect /// public GenericSshClient() : base(SPlusKey) { StreamDebugging = new CommunicationStreamDebugging(SPlusKey); CrestronEnvironment.ProgramStatusEventHandler += new ProgramStatusEventHandler(CrestronEnvironment_ProgramStatusEventHandler); AutoReconnectIntervalMs = 5000; ReconnectTimer = new CTimer(o => { Connect(); }, Timeout.Infinite); } /// /// Just to help S+ set the key /// public void Initialize(string key) { Key = key; } /// /// Handles closing this up when the program shuts down /// void CrestronEnvironment_ProgramStatusEventHandler(eProgramStatusEventType programEventType) { if (programEventType == eProgramStatusEventType.Stopping) { if (Client != null) { Debug.Console(1, this, "Program stopping. Closing connection"); Disconnect(); } } } /// /// Connect to the server, using the provided properties. /// public void Connect() { if (IsConnecting) { Debug.Console(0, this, Debug.ErrorLogLevel.Warning, "Connection attempt in progress. Exiting Connect()"); return; } IsConnecting = true; ConnectEnabled = true; Debug.Console(1, this, "attempting connect"); // Cancel reconnect if running. ReconnectTimer.Stop(); // Don't try to connect if already if (IsConnected) return; // Don't go unless everything is here if (string.IsNullOrEmpty(Hostname) || Port < 1 || Port > 65535 || Username == null || Password == null) { Debug.Console(1, this, Debug.ErrorLogLevel.Error, "Connect failed. Check hostname, port, username and password are set or not null"); return; } // Cleanup the old client if it already exists if (Client != null) { Debug.Console(1, this, "Cleaning up disconnected client"); Client.ErrorOccurred -= Client_ErrorOccurred; KillClient(SocketStatus.SOCKET_STATUS_BROKEN_LOCALLY); } // This handles both password and keyboard-interactive (like on OS-X, 'nixes) KeyboardInteractiveAuthenticationMethod kauth = new KeyboardInteractiveAuthenticationMethod(Username); kauth.AuthenticationPrompt += new EventHandler(kauth_AuthenticationPrompt); PasswordAuthenticationMethod pauth = new PasswordAuthenticationMethod(Username, Password); Debug.Console(1, this, "Creating new SshClient"); ConnectionInfo connectionInfo = new ConnectionInfo(Hostname, Port, Username, pauth, kauth); Client = new SshClient(connectionInfo); Client.ErrorOccurred -= Client_ErrorOccurred; Client.ErrorOccurred += Client_ErrorOccurred; //Attempt to connect ClientStatus = SocketStatus.SOCKET_STATUS_WAITING; try { Client.Connect(); TheStream = Client.CreateShellStream("PDTShell", 100, 80, 100, 200, 65534); TheStream.DataReceived += Stream_DataReceived; //TheStream.ErrorOccurred += TheStream_ErrorOccurred; Debug.Console(1, this, Debug.ErrorLogLevel.Notice, "Connected"); ClientStatus = SocketStatus.SOCKET_STATUS_CONNECTED; IsConnecting = false; DisconnectLogged = false; return; // Success will not pass here } catch (SshConnectionException e) { var ie = e.InnerException; // The details are inside!! var errorLogLevel = DisconnectLogged == true ? Debug.ErrorLogLevel.None : Debug.ErrorLogLevel.Error; 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})", Username, ie.Message); } else Debug.Console(1, this, errorLogLevel, "Error on connect:\r({0})", e); DisconnectLogged = true; ClientStatus = SocketStatus.SOCKET_STATUS_CONNECT_FAILED; HandleConnectionFailure(); } catch (Exception e) { Debug.Console(1, this, Debug.ErrorLogLevel.Error, "Unhandled exception on connect:\r({0})", e); ClientStatus = SocketStatus.SOCKET_STATUS_CONNECT_FAILED; HandleConnectionFailure(); } ClientStatus = SocketStatus.SOCKET_STATUS_CONNECT_FAILED; HandleConnectionFailure(); } /// /// Disconnect the clients and put away it's resources. /// public void Disconnect() { ConnectEnabled = false; // Stop trying reconnects, if we are ReconnectTimer.Stop(); KillClient(SocketStatus.SOCKET_STATUS_BROKEN_LOCALLY); } /// /// Kills the stream, cleans up the client and sets it to null /// private void KillClient(SocketStatus status) { KillStream(); if (Client != null) { IsConnecting = false; Client.Disconnect(); Client = null; ClientStatus = status; Debug.Console(1, this, "Disconnected"); } } /// /// Anything to do with reestablishing connection on failures /// 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); ReconnectTimer.Reset(AutoReconnectIntervalMs); Debug.Console(1, this, "Attempting connection in {0} seconds", (float) (AutoReconnectIntervalMs/1000)); } } /// /// Kills the stream /// void KillStream() { if (TheStream != null) { TheStream.DataReceived -= Stream_DataReceived; TheStream.Close(); TheStream.Dispose(); TheStream = null; } } /// /// Handles the keyboard interactive authentication, should it be required. /// void kauth_AuthenticationPrompt(object sender, AuthenticationPromptEventArgs e) { foreach (AuthenticationPrompt prompt in e.Prompts) if (prompt.Request.IndexOf("Password:", StringComparison.InvariantCultureIgnoreCase) != -1) prompt.Response = Password; } /// /// Handler for data receive on ShellStream. Passes data across to queue for line parsing. /// void Stream_DataReceived(object sender, Crestron.SimplSharp.Ssh.Common.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)); } } } /// /// Error event handler for client events - disconnect, etc. Will forward those events via ConnectionChange /// event /// void Client_ErrorOccurred(object sender, Crestron.SimplSharp.Ssh.Common.ExceptionEventArgs e) { if (e.Exception is SshConnectionException || e.Exception is System.Net.Sockets.SocketException) Debug.Console(1, this, Debug.ErrorLogLevel.Error, "Disconnected by remote"); else Debug.Console(1, this, Debug.ErrorLogLevel.Error, "Unhandled SSH client error: {0}", e.Exception); ClientStatus = SocketStatus.SOCKET_STATUS_BROKEN_REMOTELY; HandleConnectionFailure(); } /// /// Helper for ConnectionChange event /// void OnConnectionChange() { if (ConnectionChange != null) ConnectionChange(this, new GenericSocketStatusChageEventArgs(this)); } #region IBasicCommunication Members /// /// Sends text to the server /// /// public void SendText(string text) { try { if (Client != null && TheStream != null && IsConnected) { if (StreamDebugging.TxStreamDebuggingIsEnabled) Debug.Console(0, this, "Sending {0} characters of text: '{1}'", text.Length, ComTextHelper.GetDebugText(text)); TheStream.Write(text); TheStream.Flush(); } else { Debug.Console(1, this, "Client is null or disconnected. Cannot Send Text"); } } catch (Exception ex) { Debug.Console(0, "Exception: {0}", ex.Message); Debug.Console(0, "Stack Trace: {0}", ex.StackTrace); Debug.Console(1, this, Debug.ErrorLogLevel.Error, "Stream write failed. Disconnected, closing"); ClientStatus = SocketStatus.SOCKET_STATUS_BROKEN_REMOTELY; HandleConnectionFailure(); } } /// /// Sends Bytes to the server /// /// public void SendBytes(byte[] bytes) { try { if (Client != null && TheStream != null && IsConnected) { if (StreamDebugging.TxStreamDebuggingIsEnabled) Debug.Console(0, this, "Sending {0} bytes: '{1}'", bytes.Length, ComTextHelper.GetEscapedText(bytes)); TheStream.Write(bytes, 0, bytes.Length); TheStream.Flush(); } else { Debug.Console(1, this, "Client is null or disconnected. Cannot Send Bytes"); } } catch { Debug.Console(1, this, Debug.ErrorLogLevel.Error, "Stream write failed. Disconnected, closing"); ClientStatus = SocketStatus.SOCKET_STATUS_BROKEN_REMOTELY; HandleConnectionFailure(); } } #endregion } //***************************************************************************************************** //***************************************************************************************************** /// /// Fired when connection changes /// public class SshConnectionChangeEventArgs : EventArgs { /// /// Connection State /// public bool IsConnected { get; private set; } /// /// Connection Status represented as a ushort /// public ushort UIsConnected { get { return (ushort)(Client.IsConnected ? 1 : 0); } } /// /// The client /// public GenericSshClient Client { get; private set; } /// /// Socket Status as represented by /// public ushort Status { get { return Client.UStatus; } } /// /// S+ Constructor /// public SshConnectionChangeEventArgs() { } /// /// EventArgs class /// /// Connection State /// The Client public SshConnectionChangeEventArgs(bool isConnected, GenericSshClient client) { IsConnected = isConnected; Client = client; } } }