public FtpClient GetClient() { if (!(_config is FTPConfiguration baseConfig)) { throw new StorageException("FTP-001", "Invalid FTP Config"); } BasePath = baseConfig.BasePath; FtpClient client; if (baseConfig.ProxyInfo != null) { switch (baseConfig.ProxyInfo.ProxyType) { case ProxyType.HTTP1_1: client = new FtpClientHttp11Proxy(baseConfig.ProxyInfo); break; case ProxyType.USER_AT_HOST: client = new FtpClientUserAtHostProxy(baseConfig.ProxyInfo); break; case ProxyType.BLUE_COAT: client = new FtpClientBlueCoatProxy(baseConfig.ProxyInfo); break; default: client = new FtpClient(); break; } } else { client = new FtpClient(); } client.Host = baseConfig.Host; client.Credentials = new NetworkCredential(baseConfig.User, baseConfig.Password); client.Port = baseConfig.Port; if (_config is FTPSConfiguration ftps) { client.EncryptionMode = FtpEncryptionMode.Auto; client.SslProtocols = ftps.SslProtocol; } if (_config is FTPX509Configuration x509) { client.EncryptionMode = FtpEncryptionMode.Explicit; x509.Certificates.ForEach(c => client.ClientCertificates.Add(c)); client.ValidateCertificate += (ClientOnValidateCertificate); } _clients.Add(client); return(client); }
public static async Task FtpConnect(BotData data, string host, int port = 21, string username = "", string password = "", int timeoutMilliseconds = 10000) { data.Logger.LogHeader(); if (data.UseProxy && data.Proxy is not null && data.Proxy.Type != Models.Proxies.ProxyType.Http) { throw new Exception("Currently, this block only supports HTTP proxies"); } FtpClient client; if (data.UseProxy && data.Proxy is not null) { var proxyInfo = new ProxyInfo { Host = data.Proxy.Host, Port = data.Proxy.Port, Credentials = new NetworkCredential(data.Proxy.Username, data.Proxy.Password) }; client = new FtpClientHttp11Proxy(proxyInfo) { Host = host, Port = port, Credentials = new NetworkCredential(username, password), ConnectTimeout = timeoutMilliseconds, DataConnectionConnectTimeout = timeoutMilliseconds, DataConnectionReadTimeout = timeoutMilliseconds, ReadTimeout = timeoutMilliseconds }; } else { client = new FtpClient(host, port, username, password) { ConnectTimeout = timeoutMilliseconds, DataConnectionConnectTimeout = timeoutMilliseconds, DataConnectionReadTimeout = timeoutMilliseconds, ReadTimeout = timeoutMilliseconds }; } data.SetObject("ftpClient", client); client.OnLogEvent = InitLogger(data); await client.AutoConnectAsync(data.CancellationToken).ConfigureAwait(false); if (!client.IsConnected) { throw new Exception("Failed to connect to the FTP server with the given credentials"); } data.Logger.Log($"Connected to {host}:{port}", LogColors.Maize); }
//[Fact] public void TestListPathWithHttp11Proxy() { using (FtpClient cl = new FtpClientHttp11Proxy(new ProxyInfo { Host = "127.0.0.1", Port = 3128, })) // Credential = new NetworkCredential() { FtpTrace.WriteLine("FTPClient::ConnectionType = '" + cl.ConnectionType + "'"); cl.Credentials = new NetworkCredential(m_user, m_pass); cl.Host = m_host; cl.ValidateCertificate += OnValidateCertificate; cl.DataConnectionType = FtpDataConnectionType.PASV; cl.Connect(); foreach (FtpListItem item in cl.GetListing(null, FtpListOption.SizeModify | FtpListOption.ForceNameList)) { FtpTrace.WriteLine(item.Modified.Kind); FtpTrace.WriteLine(item.Modified); } } }