public void Cleanup()
 {
     if (_forwardedPort != null)
     {
         _forwardedPort.Dispose();
         _forwardedPort = null;
     }
 }
Ejemplo n.º 2
0
 private void closeAll()
 {
     client.RemoveForwardedPort(fwPort);
     fwPort.Dispose();
     client.Disconnect();
     client.Dispose();
 }
Ejemplo n.º 3
0
 public void DisposeTest()
 {
     uint boundPort = 0; // TODO: Initialize to an appropriate value
     string host = string.Empty; // TODO: Initialize to an appropriate value
     uint port = 0; // TODO: Initialize to an appropriate value
     ForwardedPortLocal target = new ForwardedPortLocal(boundPort, host, port); // TODO: Initialize to an appropriate value
     target.Dispose();
     Assert.Inconclusive("A method that does not return a value cannot be verified.");
 }
 public void Cleanup()
 {
     if (_forwardedPort != null)
     {
         _connectionInfoMock.Setup(p => p.Timeout).Returns(TimeSpan.FromSeconds(1));
         _forwardedPort.Dispose();
         _forwardedPort = null;
     }
 }
        protected void Arrange()
        {
            _closingRegister   = new List <EventArgs>();
            _exceptionRegister = new List <ExceptionEventArgs>();

            _forwardedPort            = new ForwardedPortLocal("boundHost", "host", 22);
            _forwardedPort.Closing   += (sender, args) => _closingRegister.Add(args);
            _forwardedPort.Exception += (sender, args) => _exceptionRegister.Add(args);
            _forwardedPort.Dispose();
        }
Ejemplo n.º 6
0
        public void DisposeTest()
        {
            uint               boundPort = 0;                                             // TODO: Initialize to an appropriate value
            string             host      = string.Empty;                                  // TODO: Initialize to an appropriate value
            uint               port      = 0;                                             // TODO: Initialize to an appropriate value
            ForwardedPortLocal target    = new ForwardedPortLocal(boundPort, host, port); // TODO: Initialize to an appropriate value

            target.Dispose();
            Assert.Inconclusive("A method that does not return a value cannot be verified.");
        }
        protected void Arrange()
        {
            _closingRegister = new List<EventArgs>();
            _exceptionRegister = new List<ExceptionEventArgs>();

            _forwardedPort = new ForwardedPortLocal("boundHost", "host", 22);
            _forwardedPort.Closing += (sender, args) => _closingRegister.Add(args);
            _forwardedPort.Exception += (sender, args) => _exceptionRegister.Add(args);
            _forwardedPort.Dispose();
        }
Ejemplo n.º 8
0
 /// <summary>
 /// If port forwarding is taking place in the sshClient, removes the port forwarding capabilities from it.
 /// </summary>
 private void RemoveForwardPort()
 {
     if (_forwardedPort != null)
     {
         if (_client != null)
         {
             _client.RemoveForwardedPort(_forwardedPort);
         }
         _forwardedPort.Dispose();
     }
 }
Ejemplo n.º 9
0
 public void Dispose()
 {
     if (port != null)
     {
         port.Dispose();
     }
     if (client != null)
     {
         client.Dispose();
     }
 }
 public void Cleanup()
 {
     if (_client != null)
     {
         _client.Dispose();
         _client = null;
     }
     if (_forwardedPort != null)
     {
         _forwardedPort.Dispose();
         _forwardedPort = null;
     }
     if (_channelBindStarted != null)
     {
         _channelBindStarted.Dispose();
         _channelBindStarted = null;
     }
     if (_channelBindCompleted != null)
     {
         _channelBindCompleted.Dispose();
         _channelBindCompleted = null;
     }
 }
        protected void Arrange()
        {
            var random = new Random();
            _closingRegister = new List<EventArgs>();
            _exceptionRegister = new List<ExceptionEventArgs>();
            _localEndpoint = new IPEndPoint(IPAddress.Loopback, 8122);
            _remoteEndpoint = new IPEndPoint(IPAddress.Parse("193.168.1.5"),
                random.Next(IPEndPoint.MinPort, IPEndPoint.MaxPort));

            _forwardedPort = new ForwardedPortLocal(_localEndpoint.Address.ToString(), (uint)_localEndpoint.Port,
                _remoteEndpoint.Address.ToString(), (uint)_remoteEndpoint.Port);
            _forwardedPort.Closing += (sender, args) => _closingRegister.Add(args);
            _forwardedPort.Exception += (sender, args) => _exceptionRegister.Add(args);
            _forwardedPort.Dispose();
        }
Ejemplo n.º 12
0
        protected void Arrange()
        {
            var random = new Random();

            _closingRegister   = new List <EventArgs>();
            _exceptionRegister = new List <ExceptionEventArgs>();
            _localEndpoint     = new IPEndPoint(IPAddress.Loopback, 8122);
            _remoteEndpoint    = new IPEndPoint(IPAddress.Parse("193.168.1.5"),
                                                random.Next(IPEndPoint.MinPort, IPEndPoint.MaxPort));

            _forwardedPort = new ForwardedPortLocal(_localEndpoint.Address.ToString(), (uint)_localEndpoint.Port,
                                                    _remoteEndpoint.Address.ToString(), (uint)_remoteEndpoint.Port);
            _forwardedPort.Closing   += (sender, args) => _closingRegister.Add(args);
            _forwardedPort.Exception += (sender, args) => _exceptionRegister.Add(args);
            _forwardedPort.Dispose();
        }
        protected void Arrange()
        {
            _closingRegister = new List<EventArgs>();
            _exceptionRegister = new List<ExceptionEventArgs>();

            _sessionMock = new Mock<ISession>(MockBehavior.Strict);
            _connectionInfoMock = new Mock<IConnectionInfo>(MockBehavior.Strict);

            var sequence = new MockSequence();
            _sessionMock.InSequence(sequence).Setup(p => p.IsConnected).Returns(true);
            _sessionMock.InSequence(sequence).Setup(p => p.ConnectionInfo).Returns(_connectionInfoMock.Object);
            _connectionInfoMock.InSequence(sequence).Setup(p => p.Timeout).Returns(TimeSpan.FromSeconds(30));

            _forwardedPort = new ForwardedPortLocal(IPAddress.Loopback.ToString(), "host", 22);
            _forwardedPort.Closing += (sender, args) => _closingRegister.Add(args);
            _forwardedPort.Exception += (sender, args) => _exceptionRegister.Add(args);
            _forwardedPort.Session = _sessionMock.Object;
            _forwardedPort.Start();
            _forwardedPort.Dispose();
        }
        protected void Arrange()
        {
            _closingRegister   = new List <EventArgs>();
            _exceptionRegister = new List <ExceptionEventArgs>();

            _sessionMock        = new Mock <ISession>(MockBehavior.Strict);
            _connectionInfoMock = new Mock <IConnectionInfo>(MockBehavior.Strict);

            var sequence = new MockSequence();

            _sessionMock.InSequence(sequence).Setup(p => p.IsConnected).Returns(true);
            _sessionMock.InSequence(sequence).Setup(p => p.ConnectionInfo).Returns(_connectionInfoMock.Object);
            _connectionInfoMock.InSequence(sequence).Setup(p => p.Timeout).Returns(TimeSpan.FromSeconds(30));

            _forwardedPort            = new ForwardedPortLocal(IPAddress.Loopback.ToString(), "host", 22);
            _forwardedPort.Closing   += (sender, args) => _closingRegister.Add(args);
            _forwardedPort.Exception += (sender, args) => _exceptionRegister.Add(args);
            _forwardedPort.Session    = _sessionMock.Object;
            _forwardedPort.Start();
            _forwardedPort.Dispose();
        }
Ejemplo n.º 15
0
 public void Dispose()
 {
     _client?.Dispose();
     _forwardedPort?.Dispose();
 }
Ejemplo n.º 16
0
        /// <summary>
        /// Starts an ssh tunnel, where conntection to a local port (which this will listen on), transparently connects it to a remote port.
        /// </summary>
        /// <returns><c>true</c>, if tunnel was started, <c>false</c> otherwise.</returns>
        /// <param name="TunnelPortLocal">Tunnel port local.</param>
        /// <param name="TunnelPortRemote">Tunnel port remote.</param>
        /// <param name="LocalNetwork">Local network.</param>
        public bool StartTunnel(UInt32 TunnelPortLocal, UInt32 TunnelPortRemote, String LocalNetwork = null)
        {
            if (!ConnectSSH())
            {
                return(false);
            }

            try
            {
                if (LocalNetwork == null)
                {
                    LocalNetwork = IPAddress.Loopback.ToString();
                }

                Write("ssh tunnel: {0}:{1} -> {2}:{3}...", LocalNetwork, TunnelPortLocal, "localhost", TunnelPortRemote);
                if (forwardPort != null)
                {
                    if (forwardPort.BoundPort == TunnelPortLocal && forwardPort.Port == TunnelPortRemote)
                    {
                        if (forwardPort.IsStarted)
                        {
                            WriteLine("Tunnel already connencted");
                            return(true);
                        }
                        else
                        {
                            forwardPort.Dispose();
                        }
                    }
                    else
                    {
                        forwardPort.Dispose();
                    }
                }

                forwardPort = new ForwardedPortLocal(LocalNetwork, TunnelPortLocal, "localhost", TunnelPortRemote);

                sshClient.AddForwardedPort(forwardPort);

                forwardPort.Exception += (object sender, ExceptionEventArgs e) =>
                {
                    WriteLine("Tunnel error: {0}", e.Exception.Message);
                };

                forwardPort.RequestReceived += (object sender, PortForwardEventArgs e) =>
                {
                    WriteLine("Tunnel connection: {0}->{1}", e.OriginatorHost, e.OriginatorPort);
                };
                forwardPort.Start();
                WriteLine("OK");

                return(forwardPort.IsStarted);
            }
            catch (SocketException ex)
            {
                if (ex.SocketErrorCode == SocketError.AccessDenied)
                {
                    WriteLine("FAILED\r\nAccess Denied - Cannot create port redirect. Try running Monodevelop with higher privileges.");
                }
                else
                {
                    WriteLine("FAILED\r\nTunnel Error: {0}", ex);
                }
            }
            catch (Exception ex)
            {
                WriteLine("Tunnel Error: {0}", ex);
            }
            return(false);
        }
Ejemplo n.º 17
0
        private void btnConnect_Click(object sender, EventArgs e)
        {
            iSFtp = new SftpClient(txtSFtpIP.Text, Convert.ToInt32(numUpDownSFtpPort.Value), txtSFtpUsername.Text, txtSFtpPassword.Text);

            try {
                iSFtp.Connect();
            } catch (Exception ex) {
                MessageBox.Show(ex.Message, "Failed!", MessageBoxButtons.OK, MessageBoxIcon.Error);

                iSFtp.Dispose();
                iSFtp = null;

                FlashState();
                return;
            }

            if (checkBoxUseSSH.Checked)
            {
                // Setup SSH connection.
                iSSHClient = new SshClient(txtSSHTunnelIP.Text, Convert.ToInt32(numUpDownSSHTunnelPort.Value), txtSSHTunnelUsername.Text, txtSSHTunnelPassword.Text);

                try {
                    iSSHClient.Connect();
                } catch (Exception ex) {
                    MessageBox.Show(ex.Message, "Failed!", MessageBoxButtons.OK, MessageBoxIcon.Error);

                    iSSHClient.Dispose();
                    iSSHClient = null;

                    iSFtp.Disconnect();
                    iSFtp.Dispose();
                    iSFtp = null;

                    FlashState();
                    return;
                }

                // Setup port forwarding.
                iSSHTunnel = new ForwardedPortLocal("127.0.0.1", 0, txtDBIP.Text, Convert.ToUInt32(numUpDownDBPort.Value));
                iSSHClient.AddForwardedPort(iSSHTunnel);

                try {
                    iSSHTunnel.Start();
                } catch (Exception ex) {
                    MessageBox.Show(ex.Message, "Failed!", MessageBoxButtons.OK, MessageBoxIcon.Error);

                    iSSHClient.RemoveForwardedPort(iSSHTunnel);

                    iSSHTunnel.Dispose();
                    iSSHTunnel = null;

                    iSSHClient.Disconnect();
                    iSSHClient.Dispose();
                    iSSHClient = null;

                    iSFtp.Disconnect();
                    iSFtp.Dispose();
                    iSFtp = null;

                    FlashState();
                    return;
                }
            } // End if(checkBoxUseSSH.Checked)

            MySqlConnectionStringBuilder connStrBuilder = new MySqlConnectionStringBuilder();

            connStrBuilder.Server   = checkBoxUseSSH.Checked ? iSSHTunnel.BoundHost : txtDBIP.Text;
            connStrBuilder.Port     = checkBoxUseSSH.Checked ? iSSHTunnel.BoundPort : Convert.ToUInt32(numUpDownDBPort.Value);
            connStrBuilder.UserID   = txtDBUsername.Text;
            connStrBuilder.Password = txtDBPassword.Text;

            MysqlConn = new MySqlConnection(connStrBuilder.GetConnectionString(true));

            try {
                MysqlConn.Open();
            } catch (Exception ex) {
                MessageBox.Show(ex.Message, "Failed!", MessageBoxButtons.OK, MessageBoxIcon.Error);

                MysqlConn.Dispose();
                MysqlConn = null;

                if (checkBoxUseSSH.Checked)
                {
                    iSSHTunnel.Stop();

                    iSSHClient.RemoveForwardedPort(iSSHTunnel);

                    iSSHTunnel.Dispose();
                    iSSHTunnel = null;

                    iSSHClient.Disconnect();
                    iSSHClient.Dispose();
                    iSSHClient = null;
                }

                iSFtp.Disconnect();
                iSFtp.Dispose();
                iSFtp = null;

                FlashState();
                return;
            }

            // Connect Done.
            Connected = true;
            FlashState();
        }