Esempio n. 1
0
        public bool TryUseDbConnection(DatabaseType type, string server, int port, bool useSsh, string sshServer, int?sshPort, SshProxyCredentials credentials, string username, string password, string databaseName, Action <DbConnection> action, out string error)
        {
            error = null;
            bool databaseAccessDenied = false;

            try
            {
                if (useSsh)
                {
                    return(SSHProxyManager.TryUseProxy(server, port, sshServer, sshPort.GetValueOrDefault(22), credentials,
                                                       proxy =>
                    {
                        using (var conn = GetDbConnection(type, proxy.ProxyServer, proxy.ProxyPort, username, password, databaseName))
                        {
                            action(conn);
                        }
                    }, out error));
                }
                else
                {
                    using (var conn = GetDbConnection(type, server, port, username, password, databaseName))
                    {
                        action(conn);
                        return(true);
                    }
                }
            }
            catch (MySqlException e)
            {
                if (e.InnerException != null && e.InnerException.Message != null && e.InnerException.Message.StartsWith("Access denied"))
                {
                    databaseAccessDenied = true;
                }
                else
                {
                    throw;
                }
            }
            catch (SqlException e)
            {
                error = e.Message;
            }
            catch (Exception e)
            {
                error = e.Message;
            }

            if (databaseAccessDenied)
            {
                error = string.Format("Access denied for user='******'. Please check username and password are correct.", username);
            }
            return(error == null);
        }
Esempio n. 2
0
        public void Run()
        {
            try
            {
                using (var sshClient = GetSshClient())
                {
                    bool ended = false;
                    try
                    {
                        sshClient.Connect();

                        int  triesLeft = Retries;
                        bool success   = false;
                        do
                        {
                            ProxyPort = GetPort();
                            triesLeft--;
                            try
                            {
                                using (var forwardedPortLocal = new ForwardedPortLocal(ProxyServer, (uint)ProxyPort, this.Server, (uint)Port))
                                {
                                    sshClient.AddForwardedPort(forwardedPortLocal);
                                    try
                                    {
                                        forwardedPortLocal.Start();

                                        // proxy has started
                                        lock (_usageLock)
                                        {
                                            _isStarted       = true;
                                            _isRunning       = true;
                                            _numCurrentUsers = 0;
                                        }

                                        // signal started
                                        _startedWaitEvent.Set();

                                        // keep alive while running is set to true
                                        while (_isRunning)
                                        {
                                            Thread.Sleep(PollInterval);

                                            lock (_usageLock)
                                            {
                                                TimeSpan timeSinceLastUsed = DateTime.Now - _lastUsed;
                                                // if it's under the min lifetime let it stay alive
                                                // if its other the timeout, kill it regardless
                                                // if its in between check if anybody is using it.
                                                if (sshClient.IsConnected == false)
                                                {
                                                    _isRunning = false;
                                                }
                                                else if (timeSinceLastUsed < MinAliveTime)
                                                {
                                                    _isRunning = true;
                                                }
                                                else if (Timeout <= timeSinceLastUsed)
                                                {
                                                    _isRunning = false;
                                                }
                                                else
                                                {
                                                    _isRunning = _numCurrentUsers > 0;
                                                }

                                                if (_isRunning == false)
                                                {
                                                    ended = true;
                                                }
                                            }
                                        }
                                    }
                                    catch
                                    {
                                        throw;
                                    }
                                    finally
                                    {
                                        lock (_usageLock)
                                        {
                                            _isRunning = false;
                                        }
                                        if (forwardedPortLocal != null && forwardedPortLocal.IsStarted)
                                        {
                                            forwardedPortLocal.Stop();
                                        }
                                    }
                                }
                            }
                            catch (SocketException e)
                            {
                                if (e.SocketErrorCode == SocketError.AddressAlreadyInUse)
                                {
                                    success = false;
                                }
                                else
                                {
                                    Error = e.Message;
                                }
                            }
                        } while (success == false && triesLeft > 0 && ended == false);
                    }
                    catch (SocketException e)
                    {
                        switch (e.SocketErrorCode)
                        {
                        case SocketError.TimedOut: Error = "Connection timed out."; break;

                        case SocketError.AccessDenied: Error = "Access denied."; break;

                        case SocketError.HostNotFound: Error = string.Format("Host='{0}' could not be found.", Server); break;

                        default: Error = e.Message; break;
                        }
                    }
                    catch (SshException e)
                    {
                        if (e.Message == "User cannot be authenticated." || e.Message == "No suitable authentication method found to complete authentication.")
                        {
                            Error = string.Format("Access denied for user='******'.", SshCredentials.Username);
                        }
                        else
                        {
                            Error = e.Message;
                        }
                    }
                    catch (Exception e)
                    {
                        //Elmah.ErrorSignal.FromCurrentContext().Raise(e);
                        Error = "An unexpected error occurred: " + e;
                    }
                    finally
                    {
                        if (sshClient != null && sshClient.IsConnected)
                        {
                            sshClient.Disconnect();
                        }
                    }
                }
            }
            catch
            {
                Error = "An unexpected error occurred.";
            }
            finally
            {
                // signal started
                _startedWaitEvent.Set();
                SSHProxyManager.RemoveProxy(this);
            }
        }