|
||
| OnAuthenticationSuccess | Is fired when the client authentication process has been completed. |
| OnAuthenticationFailed | Is fired if the client was unable to authenticate using particular authentication method. In general, this does not mean that the authentication process completely failed ? the client may try several authentication methods consequently and one of them may succeed. |
| OnError | Is fired if some protocol error occurs during the session. Usually this leads to a connection closure. The exact error can be detected via the error code passed to it. |
| OnKeyValidate | Is used to pass the received server key to the application. Please note that incorrect handling of this event may result in a serious security breach. The handler of this event should verify that the passed key corresponds to the remote server (and warn the user if it does not). If the key is valid, the handler should set the Validate parameter to true. The sample does not perform key checkup for the sake of simplicity. |
| OnOpen | Is fired when the SSH connection is established and the component is ready to tunnel data. We will use the handler of this event to kick the MySQL client component. |
| OnClose | Is fired when the SSH connection is closed. |
| OnConnectionOpen | Is fired when a new tunnel is created. The corresponding tunneled connection object is passed as parameter. |
| OnConnectionClose | Is fired when an existing tunnel is closed. |
private void SetupSSHConnection() { // Specifying address and port of SSH server Forwarding.Address = tbSSHAddress.Text; Forwarding.Port = Convert.ToInt32(tbSSHPort.Text); // Setting credentials for authentication on SSH server Forwarding.Username = tbUsername.Text; Forwarding.Password = tbPassword.Text; // Specifying network interface and port number to be opened locally Forwarding.ForwardedHost = ""; Forwarding.ForwardedPort = Convert.ToInt32(tbFwdPort.Text); // Specifying destination host where the server should forward the data to. // Please note, that the destination should be specified according to // SSH servers point of view. E.g., 127.0.0.1 will stand for // SSH servers localhost, not SSH clients one. Forwarding.DestHost = tbDBAddress.Text; Forwarding.DestPort = Convert.ToInt32(tbDBPort.Text); // Opening SSH connection Forwarding.Open(); } |
private void RunQueryThreadFunc() { MySqlConnection MySQLConnection = new MySqlConnection(); // forming connection string string connString = "database=" + tbDBName.Text + ";Connect Timeout=30;user id=" + tbDBUsername.Text + "; pwd=" + tbDBPassword.Text + ";"; if (cbUseTunnelling.Checked) { // specifying local destination if forwarding is enabled connString = connString + "server=127.0.0.1; port=" + tbFwdPort.Text; } else { // specifying real MySQL server location if forwarding is not used connString = connString + "server=" + tbDBAddress.Text + "; port=" + tbDBPort.Text; } MySQLConnection.ConnectionString = connString; try { // opening MySQL connection MySqlCommand cmd = new MySqlCommand(tbQuery.Text, MySQLConnection); Log("Connecting to MySQL server..."); MySQLConnection.Open(); Log("Connection to MySQL server established. Version: " + MySQLConnection.ServerVersion + "."); // reading query results MySqlDataReader reader = cmd.ExecuteReader(); try { for (int i = 0; i < reader.FieldCount; i++) { AddQueryColumn(reader.GetName(i)); } while (reader.Read()) { string[] values = new string[reader.FieldCount]; for (int i = 0; i < reader.FieldCount; i++) { values[i] = reader.GetString(i); } AddQueryValues(values); } } finally { // closing both MySQL and SSH connections Log("Closing MySQL connection"); reader.Close(); MySQLConnection.Close(); Forwarding.Close(); } } catch (Exception ex) { Log("MySQL connection failed (" + ex.Message + ")"); } } |
delegate void LogFunc(string S); private void Log(string S) { if (lvLog.InvokeRequired) { LogFunc d = new LogFunc(Log); Invoke(d, new object[] { S }); } else { ListViewItem item = new ListViewItem(); item.Text = DateTime.Now.ToShortTimeString(); item.SubItems.Add(S); lvLog.Items.Add(item); } } |