Esempio n. 1
0
        /// <summary>
        /// Connect to the MySQL server.
        /// </summary>
        private bool connect()
        {
            bool connect = true;

              if (!string.IsNullOrWhiteSpace(this.connection.SSHHostname))
            connect = this.setupSSH();

              if (connect) {
            // Need username?
            if (this.connection.MySQLUsername == "") {
              this.log("Requesting MySQL username from user.");

              fmRequestInput dialog = new fmRequestInput("Enter your MySQL username to connect.");
              DialogResult dialogResult = dialog.ShowDialog(this);

              if (dialogResult == System.Windows.Forms.DialogResult.OK)
            this.connection.MySQLUsername = dialog.tbInput.Text.Trim();
              else {
            this.log("User aborted the connect process.", ICON_WARNING);
            return false;
              }
            }

            // Need password?
            if (this.connection.MySQLPassword == "") {
              this.log("Requesting MySQL password from user.");

              fmRequestInput dialog = new fmRequestInput("Enter your MySQL password to connect.", true);
              DialogResult dialogResult = dialog.ShowDialog(this);

              if (dialogResult == System.Windows.Forms.DialogResult.OK)
            this.connection.MySQLPassword = Encryption.Encrypt(dialog.tbInput.Text);
              else {
            this.log("User aborted the connect process.", ICON_WARNING);
            return false;
              }
            }

            // Connect.
            try {
              this.log("Attempting to connect to MySQL, " + this.connection.MySQLUsername + "@" + this.connection.MySQLHostname + ":" + (this.connection.SSHHostname != "" ? this.connection.ForwardedPort.ToString() : this.connection.MySQLPort.ToString()));

              this.connectorConnection = new MySqlConnection("Data Source=" + this.connection.MySQLHostname + ";Port=" + (this.connection.SSHHostname != "" ? this.connection.ForwardedPort.ToString() : this.connection.MySQLPort.ToString()) + ";User Id=" + this.connection.MySQLUsername + ";Password="******"Connected to " + this.connection.MySQLHostname + ":" + this.connection.MySQLPort.ToString());
            }
            catch (MySqlException mysqlex) {
              this.log("Unable to connect to " + this.connection.MySQLHostname + ":" + (this.connection.SSHHostname != "" ? this.connection.LocalForwardedPort.ToString() : this.connection.MySQLPort.ToString()) + " - MySQL Exception: " + mysqlex.Message, ICON_ERROR);

              MessageBox.Show(
            mysqlex.Message,
            "Error",
            MessageBoxButtons.OK,
            MessageBoxIcon.Error);

              return false;
            }
            catch (Exception ex) {
              this.log("Unable to connect to " + this.connection.MySQLHostname + ":" + (this.connection.SSHHostname != "" ? this.connection.LocalForwardedPort.ToString() : this.connection.MySQLPort.ToString()) + " - Exception: " + ex.Message, ICON_ERROR);

              MessageBox.Show(
            ex.Message,
            "Error",
            MessageBoxButtons.OK,
            MessageBoxIcon.Error);

              return false;
            }
              }
              else
            return false;

              return true;
        }
Esempio n. 2
0
        /// <summary>
        /// Setup the SSH connection, port forwarding, and connect.
        /// </summary>
        private bool setupSSH()
        {
            // Need username?
              if (this.connection.SSHUsername == "") {
            this.log("Requesting SSH username from user.");

            fmRequestInput dialog = new fmRequestInput("Enter your SSH username to connect.");
            DialogResult dialogResult = dialog.ShowDialog(this);

            if (dialogResult == System.Windows.Forms.DialogResult.OK)
              this.connection.SSHUsername = dialog.tbInput.Text.Trim();
            else {
              this.log("User aborted the connect process.", ICON_WARNING);
              return false;
            }
              }

              // Need password?
              if (this.connection.SSHPassword == "") {
            this.log("Requesting SSH password from user.");

            fmRequestInput dialog = new fmRequestInput("Enter your SSH password to connect.", true);
            DialogResult dialogResult = dialog.ShowDialog(this);

            if (dialogResult == System.Windows.Forms.DialogResult.OK)
              this.connection.SSHPassword = Encryption.Encrypt(dialog.tbInput.Text);
            else {
              this.log("User aborted the connect process.", ICON_WARNING);
              return false;
            }
              }

              try {
            this.log("Setting up SSH connection.");

            this.sshClient = new SshClient(
              this.connection.SSHHostname,
              (int)this.connection.SSHPort,
              this.connection.SSHUsername,
              Encryption.Decrypt(this.connection.SSHPassword));

            this.log("Attempting to connect to SSH, " + this.connection.SSHUsername + "@" + this.connection.SSHHostname + ":" + this.connection.SSHPort.ToString());

            this.sshClient.Connect();

            this.log("SSH connected.");
              }
              catch (SshException sshex) {
            this.log("SSH Exception: " + sshex.Message, ICON_ERROR);
            return false;
              }
              catch (Exception ex) {
            this.log("Exception: " + ex.Message, ICON_ERROR);
            return false;
              }

              uint localPort = this.connection.LocalForwardedPort;
              uint attempts = 1;
              uint maxAttempts = 10;

              Random random = new Random();

              while (true) {
            if (this.connection.LocalForwardedPort == 0)
              localPort = (uint)random.Next(10000, 65000);

            try {
              this.log("Attempting (" + attempts.ToString() + " of " + maxAttempts.ToString() + ") to setup port forwarding on local port " + localPort.ToString() + " to remote port " + this.connection.MySQLPort.ToString() + ".");

              ForwardedPortLocal forwardedPort = new ForwardedPortLocal(
            "127.0.0.1",
            localPort,
            "localhost",
            this.connection.MySQLPort);

              this.sshClient.AddForwardedPort(forwardedPort);
              forwardedPort.Start();

              this.log("Port forwarding successfully setup on local port " + localPort.ToString() + ".");
              this.connection.ForwardedPort = localPort;

              break;
            }
            catch (SshException sshex) {
              this.log("SSH Exception: " + sshex.Message, ICON_ERROR);
              break;
            }
            catch (Exception ex) {
              if (ex.Message == "An attempt was made to access a socket in a way forbidden by its access permissions") {
            this.log("Unable to bind local port to " + localPort.ToString() + ". " + ex.Message, ICON_WARNING);
              }
              else {
            this.log("Exception: " + ex.Message, ICON_ERROR);
            break;
              }
            }

            localPort++;
            attempts++;

            if (attempts == maxAttempts) {
              this.log("Unable to bind local port for port forwarding. Aborting.", ICON_ERROR);
              return false;
            }
              }

              return true;
        }