Exemplo n.º 1
0
        public async Task connect(String host, int port, String user, String local_path, String remote_path, String password)
        {
            SetLocalPath(local_path);
            if (String.IsNullOrWhiteSpace(user))
            {
                MainWindow.ShowMessage("No user specified", "Missing Username");
                return;
            }
            if (String.IsNullOrWhiteSpace(password))
            {
                var agent = new PageantProtocol();
                var conn  = new AgentConnectionInfo(host, port, user, agent);
                client = new SftpClient(conn);
            }
            else
            {
                client = new SftpClient(host, port, user, password);
            }
            client.KeepAliveInterval = TimeSpan.FromSeconds(30);
            client.ErrorOccurred    += client_ErrorOccurred;
            try {
                client.Connect();
                if (!String.IsNullOrWhiteSpace(remote_path))
                {
                    client.ChangeDirectory(remote_path);
                }
                ConnChanged();
            } catch (SocketException e) {
                await disconnect();

                MainWindow.ShowMessage("Unable to connect due to socket exception of: " + e.Message, "Connection Error");
            } catch (SshAuthenticationException e) {
                await disconnect();

                MainWindow.ShowMessage("Unable to connect due to auth exception of: " + e.Message, "Connection Error");
            } catch (SftpPathNotFoundException) {
                await disconnect();

                MainWindow.ShowMessage("Unable to switch to remote folder of: " + remote_path + " as it doesn't exist", "Connection Error");
            }
        }
Exemplo n.º 2
0
        private void SetupFilesystem()
        {
            Debug.WriteLine("SetupFilesystem {0},{1},{2},{3}", Host, Port, Username, ConnectionType.ToString());

            ProxyTypes pt = ProxyTypes.None;

            switch (ProxyType)
            {
            case 1: pt = ProxyTypes.Http; break;

            case 2: pt = ProxyTypes.Socks4; break;

            case 3: pt = ProxyTypes.Socks5; break;
            }
            int ProxyPort = 8080;
            var Proxy     = ProxyHost;

            if (ProxyHost != null)
            {
                var s = ProxyHost.Split(':');
                if (s.Length > 1)
                {
                    Int32.TryParse(s[1], out ProxyPort);
                    Proxy = s[0];
                }
            }

            ConnectionInfo info;

            switch (ConnectionType)
            {
            case ConnectionType.Pageant:
                var agent = new PageantProtocol();
                if (pt == ProxyTypes.None)
                {
                    info = new AgentConnectionInfo(Host, Port, Username, agent);
                }
                else if (ProxyUser.Length > 0)
                {
                    info = new AgentConnectionInfo(Host, Port, Username, pt, Proxy, ProxyPort, ProxyUser, ProxyPass, agent);
                }
                else
                {
                    info = new AgentConnectionInfo(Host, Port, Username, pt, Proxy, ProxyPort, agent);
                }
                break;

            case ConnectionType.PrivateKey:
                if (pt == ProxyTypes.None)
                {
                    info = new PrivateKeyConnectionInfo(Host, Port, Username, new PrivateKeyFile(PrivateKey, Passphrase));
                }
                else if (ProxyUser.Length > 0)
                {
                    info = new PrivateKeyConnectionInfo(Host, Port, Username, pt, Proxy, ProxyPort, ProxyUser, ProxyPass, new PrivateKeyFile(PrivateKey, Passphrase));
                }
                else
                {
                    info = new PrivateKeyConnectionInfo(Host, Port, Username, pt, Proxy, ProxyPort, new PrivateKeyFile(PrivateKey, Passphrase));
                }
                break;

            default:
                if (pt == ProxyTypes.None)
                {
                    info = new PasswordConnectionInfo(Host, Port, Username, Password);
                }
                else if (ProxyUser.Length > 0)
                {
                    info = new PasswordConnectionInfo(Host, Username, Password, pt, Proxy, ProxyPort, ProxyUser, ProxyPass);
                }
                else
                {
                    info = new PasswordConnectionInfo(Host, Port, Username, Password, pt, Proxy, ProxyPort);
                }
                break;
            }

            _connection = Settings.Default.UseNetworkDrive ? String.Format("\\\\{0}\\{1}\\{2}", info.Host, Root, info.Username) : Name;

            _filesystem = new SftpFilesystem(info, Root, _connection, Settings.Default.UseOfflineAttribute, false, (int)Settings.Default.AttributeCacheTimeout, (int)Settings.Default.DirContentCacheTimeout);
            Debug.WriteLine("Connecting...");
            _filesystem.KeepAliveInterval = new TimeSpan(0, 0, 60);
            _filesystem.Connect();
        }
Exemplo n.º 3
0
        public override WebResponse GetResponse()
        {
            NetworkCredential cred        = (m_cred as NetworkCredential);
            string            strUser     = ((cred != null) ? cred.UserName : null);
            string            strPassword = ((cred != null) ? cred.Password : null);

            BaseClient m_Client = null;

            int l_port = m_uri.Port == -1 ? 22 : m_uri.Port;

            Uri uriTo = null;

            if (m_strMethod == KeePassLib.Serialization.IOConnection.WrmMoveFile)
            {
                uriTo = new Uri(m_whcHeaders.Get(
                                    IOConnection.WrhMoveFileTo));
            }

            MemoryStream reqStream = null;

            if (m_reqBody.Count > 0)
            {
                reqStream = new MemoryStream(m_reqBody.ToArray());
            }

            ConnectionInfo n_con_info;

            if (File.Exists(m_props.Get("SSHKey")))
            {
                using (FileStream keyStream = new FileStream(m_props.Get("SSHKey"), FileMode.Open))
                {
                    PrivateKeyFile v_keyauth;

                    if (strPassword == null)
                    {
                        v_keyauth = new PrivateKeyFile(keyStream);
                    }
                    else
                    {
                        v_keyauth = new PrivateKeyFile(keyStream, strPassword);
                    }

                    n_con_info = new PrivateKeyConnectionInfo(m_uri.Host, l_port, strUser, v_keyauth);
                }
            }
            else if (!String.IsNullOrWhiteSpace(m_props.Get("SSHKey")))
            {
                string keyString = m_props.Get("SSHKey").Replace("\\n", "\n");
                using (MemoryStream keyStream = new MemoryStream(Encoding.ASCII.GetBytes(keyString)))
                {
                    PrivateKeyFile v_keyauth;

                    if (strPassword == null)
                    {
                        v_keyauth = new PrivateKeyFile(keyStream);
                    }
                    else
                    {
                        v_keyauth = new PrivateKeyFile(keyStream, strPassword);
                    }

                    n_con_info = new PrivateKeyConnectionInfo(m_uri.Host, l_port, strUser, v_keyauth);
                }
            }
            else if (String.IsNullOrWhiteSpace(m_props.Get("SSHKey")) &&
                     String.IsNullOrWhiteSpace(strPassword))
            {
                // No password, no keyfile, try pageant
                PageantProtocol agent = new PageantProtocol();
                n_con_info = new AgentConnectionInfo(m_uri.Host, l_port, strUser, agent);
            }
            else
            {
                KeyboardInteractiveAuthenticationMethod v_kauth = new KeyboardInteractiveAuthenticationMethod(strUser);
                v_kauth.AuthenticationPrompt += SftpWebRequest_AuthenticationPrompt;

                PasswordAuthenticationMethod v_pauth = new PasswordAuthenticationMethod(strUser, strPassword);
                n_con_info = new ConnectionInfo(m_uri.Host, l_port, strUser, v_pauth, v_kauth);
            }

            m_Client = new SftpClient(n_con_info);

            //Set timeout to reasonable setting of 30 seconds for default.
            int connectionTimeout = 30000;

            if (m_props.Get("SSHTimeout") != null)
            {
                int.TryParse(m_props.Get("SSHTimeout"), out connectionTimeout);
            }

            m_Client.ConnectionInfo.Timeout = new TimeSpan(0, 0, 0, 0, connectionTimeout);

            if (m_props.Get("HostKey") != null)
            {
                string[] v_ssh_dss_parts = m_props.Get("HostKey").Split(':');
                if (v_ssh_dss_parts.Length != 16)
                {
                    throw new Exception("Input incorrect host fingerprint. Check it. Must look like: 12:34:56:78:90:ab:cd:ef:12:34:56:78:90:ab:cd:ef");
                }
                List <byte> v_ssh_dss_parts_b = new List <byte>();
                foreach (string str in v_ssh_dss_parts)
                {
                    try
                    {
                        v_ssh_dss_parts_b.Add(byte.Parse(str, System.Globalization.NumberStyles.AllowHexSpecifier));
                    }
                    catch (Exception)
                    {
                        throw new Exception("Input incorrect host fingerprint. Check it. Must look like: 12:34:56:78:90:ab:cd:ef:12:34:56:78:90:ab:cd:ef");
                    }
                }
                m_fingerprint             = v_ssh_dss_parts_b.ToArray();
                m_Client.HostKeyReceived += M_Client_HostKeyReceived;
            }

            return(new SftpWebResponse(m_Client, m_strMethod, m_uri, uriTo, reqStream));
        }