private FtpsClient TestConnection(FtpsOptions options)
        {
            FtpsClient client    = new FtpsClient();
            bool       connected = client.Connect(options);

            True(connected, client);
            return(client);
        }
Exemple #2
0
        private static void DoConnect(FtpsClient client)
        {
            WriteCredentialsEncryptionWarning();

            CheckPassword();

            var port = _options.port;

            if (port == 0)
            {
                port = (_options.SslRequestSupportMode & ESSLSupportMode.Implicit) == ESSLSupportMode.Implicit ? 990 : 21;
            }

            NetworkCredential credential = null;

            if (!string.IsNullOrEmpty(_options.UserName))
            {
                credential = new NetworkCredential(_options.UserName, _options.password);
            }

            X509Certificate x509ClientCert = null;

            if (_options.sslClientCertPath != null)
            {
                x509ClientCert = X509Certificate.CreateFromCertFile(_options.sslClientCertPath);
            }

            client.Connect(_options.hostName, port,
                           credential,
                           _options.SslRequestSupportMode,
                           ValidateTestServerCertificate,
                           x509ClientCert,
                           _options.sslMinKeyExchangeAlgStrength,
                           _options.sslMinCipherAlgStrength,
                           _options.sslMinHashAlgStrength,
                           _options.timeout * 1000,
                           _options.useCtrlEndPointAddressForData,
                           _options.dataConnectionMode);

            // client.Connect already sets binary by default
            if (_options.transferMode != ETransferMode.Binary)
            {
                client.SetTransferMode(_options.transferMode);
            }

            WriteConnectionInfo(client);

            WriteSslStatus(client);
        }
Exemple #3
0
        /// <summary>
        ///     Connects to a FTP server trying every methods
        /// </summary>
        private void ConnectFtp(FtpsClient ftp, string userName, string passWord, string host, int port)
        {
            NetworkCredential credential = null;

            if (!string.IsNullOrEmpty(userName))
            {
                credential = new NetworkCredential(userName, passWord ?? "");
            }

            var modes = new List <EsslSupportMode>();

            typeof(EsslSupportMode).ForEach <EsslSupportMode>((s, l) => {
                modes.Add((EsslSupportMode)l);
            });

            var sb = new StringBuilder();

            ftp.DataConnectionMode = EDataConnectionMode.Passive;
            while (!ftp.Connected && ftp.DataConnectionMode == EDataConnectionMode.Passive)
            {
                foreach (var mode in modes.OrderByDescending(mode => mode))
                {
                    try {
                        var curPort = port > 0 ? port : (mode & EsslSupportMode.Implicit) == EsslSupportMode.Implicit ? 990 : 21;
                        ftp.Connect(host, curPort, credential, mode, 1800);
                        ftp.Connected = true;
                        if (!ftp.Connected)
                        {
                            ftp.Close();
                        }
                        break;
                    } catch (Exception e) {
                        sb.AppendLine($"{mode} >> {e.Message}");
                    }
                }
                ftp.DataConnectionMode = EDataConnectionMode.Active;
            }

            // failed?
            if (!ftp.Connected)
            {
                throw new ArchiverException($"Failed to connect to a FTP server with : Username : {userName ?? "none"}, Password : {passWord ?? "none"}, Host : {host}, Port : {(port == 0 ? 21 : port)}", new Exception(sb.ToString()));
            }
        }
Exemple #4
0
        private static bool ConnectFtp(FtpsClient ftp, string userName, string passWord, string server, int port, string serverUri)
        {
            NetworkCredential credential = null;

            if (!string.IsNullOrEmpty(userName))
            {
                credential = new NetworkCredential(userName, passWord);
            }
            foreach (var mode in EsslSupportMode.ClearText.GetEnumValues <EsslSupportMode>().OrderByDescending(mode => mode))
            {
                try {
                    var curPort = port > -1 ? port : ((mode & EsslSupportMode.Implicit) == EsslSupportMode.Implicit ? 990 : 21);
                    ftp.Connect(server, curPort, credential, mode, 1800);
                    ftp.Connected = true;
                    break;
                } catch (Exception) {
                    //ignored
                }
            }

            // failed?
            if (!ftp.Connected)
            {
                if (!IsSpamming(serverUri, 2000, true))
                {
                    UserCommunication.Notify(string.Format(@"Failed to connect to the FTP server!<br><br>The connexion used was:
                            <br>- Username : {0}
                            <br>- Password : {1}
                            <br>- Host : {2}
                            <br>- Port : {3}
                            ", userName ?? "none", passWord ?? "none", server, port == -1 ? 21 : port), MessageImg.MsgError, "Ftp connexion", "Failed");
                }
                return(false);
            }
            return(true);
        }