示例#1
0
        private List <string> DownloadFromFtpSvr(System.Net.FtpClient.FtpClient ftpClient, string serverpath, string localDire)
        {
            var downloadFiles = new List <string>();

            foreach (var ftpItem in ftpClient.GetListing(serverpath, FtpListOption.Modify | FtpListOption.Size))
            {
                var descPath = $@"{localDire}\{ftpItem.Name}";
                if (File.Exists(descPath))
                {
                    File.Decrypt(descPath);
                }

                using (var ftpStream = ftpClient.OpenRead(ftpItem.FullName))
                {
                    using (var fileStream = File.Create(descPath, (int)ftpStream.Length))
                    {
                        var buffer = new byte[200 * 1024];
                        int count;

                        while ((count = ftpStream.Read(buffer, 0, buffer.Length)) > 0)
                        {
                            fileStream.Write(buffer, 0, count);
                        }
                    }

                    downloadFiles.Add(ftpItem.Name);
                }
            }

            return(downloadFiles);
        }
示例#2
0
 private static void Open()
 {
     Close();
     if (commandParams.Count == 0)
     {
         Console.WriteLine("Usage:open host [port]");
     }
     else
     {
         string str    = commandParams[0];
         int    result = 0;
         if (commandParams.Count == 1)
         {
             commandParams.Add("21");
         }
         if (!int.TryParse(commandParams[1], out result))
         {
             Console.WriteLine("unknow port!");
         }
         else
         {
             client      = new System.Net.FtpClient.FtpClient();
             client.Host = str;
             client.Port = result;
             client.DataConnectionType = FtpDataConnectionType.PASV;
             client.Connect();
             client.ValidateCertificate += new FtpSslValidation(Program.client_ValidateCertificate);
         }
     }
 }
示例#3
0
 /// <summary>
 ///     登陆
 /// </summary>
 /// <returns>登陆是否成功</returns>
 public bool Login()
 {
     using (var ftp = new System.Net.FtpClient.FtpClient())
     {
         ftp.Host        = ServerHost;
         ftp.Credentials = new NetworkCredential(UserName, UserPassword);
         ftp.Connect();
         return(ftp.IsConnected);
     }
 }
示例#4
0
        private System.Net.FtpClient.FtpClient CreateFtpClient()
        {
            var ftpClient = new System.Net.FtpClient.FtpClient
            {
                Host        = ServerHost,
                Credentials = new NetworkCredential(UserName, UserPassword)
            };

            ftpClient.Connect();
            return(ftpClient);
        }
示例#5
0
 private static void client_ValidateCertificate(System.Net.FtpClient.FtpClient control, FtpSslValidationEventArgs e)
 {
     if (e.Accept)
     {
         Console.WriteLine("certificate accept.");
     }
     else
     {
         Console.WriteLine("error:" + e.PolicyErrors.ToString());
     }
     throw new NotImplementedException();
 }
示例#6
0
        static void Main(string[] args)
        {
            //var ftpClient = new System.Net.FtpClient.FtpClient();
            //ftpClient.Host = "192.168.1.80";
            //ftpClient.Port = 21;

            //ftpClient.

            var ftpClient = new System.Net.FtpClient.FtpClient();

            ftpClient.Host        = "192.168.147.133";
            ftpClient.Port        = 9904;
            ftpClient.Credentials = new NetworkCredential("kerry", "P@$$w0rd");
            ftpClient.UngracefullDisconnection = true;

            ftpClient.Connect();

            //var localFile = new FileInfo(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $"{Guid.NewGuid().ToString("N")}.text"));
            //using (var fs = localFile.Create())
            //{
            //    var content = Encoding.UTF8.GetBytes(Guid.NewGuid().ToString());
            //    fs.Write(content, 0, content.Length);
            //    fs.Flush();
            //}

            var localFile = new FileInfo(@"F:\Users\Administrator\Desktop\com.action98.drmp.mobilemonitor.apk");

            var remotePath = @"\A\com.action98.drmp.mobilemonitor.apk";

            //构建异步参数
            var asyncState = new Dictionary <string, object>();

            asyncState.Add("LocalPath", localFile.FullName);
            asyncState.Add("FtpClient", ftpClient);
            asyncState.Add("RemotePath", remotePath);

            //开始异步上传
            ftpClient.BeginOpenWrite(remotePath, FtpDataType.Binary, BeginOpenWriteCallback, asyncState);

            Console.ReadKey();
        }
示例#7
0
        public override async Task Connect(bool reconnecting = false)
        {
            Notifications.ChangeTrayText(reconnecting ? MessageType.Reconnecting : MessageType.Connecting);
            Log.Write(l.Debug, "{0} client...", reconnecting ? "Reconnecting" : "Connecting");

            _ftpc = new System.Net.FtpClient.FtpClient
            {
                Host = Controller.Account.Host,
                Port = Controller.Account.Port
            };

            // Add accepted certificates
            _ftpc.ClientCertificates.AddRange(_certificates);

            if (Controller.Account.Protocol == FtpProtocol.FTPS)
            {
                _ftpc.SslProtocols         = SslProtocols.Tls11 | SslProtocols.Tls12;
                _ftpc.ValidateCertificate += (sender, x) =>
                {
                    var certificate = new X509Certificate2(x.Certificate);

                    Log.Write(l.Warning, $"Policy errors: {x.PolicyErrors}");

                    // if ValidateCertificate handler isn't set, accept the certificate and move on
                    if (ValidateCertificate == null || Settings.TrustedCertificates.Contains(certificate.Thumbprint))
                    {
                        Log.Write(l.Client, "Trusted: {0}", certificate.Thumbprint);
                        x.Accept = true;
                        return;
                    }

                    var e = new ValidateCertificateEventArgs
                    {
                        cert        = certificate,
                        Fingerprint = certificate.Thumbprint
                    };

                    // Prompt user to validate
                    ValidateCertificate?.Invoke(null, e);

                    if (e.IsTrusted)
                    {
                        _certificates.Add(x.Certificate);
                    }

                    x.Accept = e.IsTrusted;
                };
            }

            // Change Security Protocol
            _ftpc.EncryptionMode = (FtpEncryptionMode)Controller.Account.FtpsMethod;

            _ftpc.Credentials = new NetworkCredential(Controller.Account.Username, Controller.Account.Password);

            try
            {
                await _ftpc.ConnectAsync();
            }
            catch (AuthenticationException ex) when(ex.Message.StartsWith("The remote certificate is invalid"))
            {
                throw new CertificateDeclinedException(ex.Message, ex);
            }
            catch (FtpCommandException ex)
            {
                ex.LogException();
                throw new AuthenticationException(ex.Message, ex.InnerException);
            }
            catch (Exception ex)
            {
                ex.LogException();
                throw;
            }

            _ftpc.Encoding = this.Charset;

            Controller.HomePath = WorkingDirectory;

            if (IsConnected)
            {
                if (!string.IsNullOrWhiteSpace(Controller.Paths.Remote) && !Controller.Paths.Remote.Equals("/"))
                {
                    WorkingDirectory = Controller.Paths.Remote;
                }
            }

            Log.Write(l.Debug, "Client connected sucessfully");
            Notifications.ChangeTrayText(MessageType.Ready);

            if (Settings.IsDebugMode)
            {
                LogServerInfo();
            }

            // Periodically send NOOP (KeepAlive) to server if a non-zero interval is set
            SetKeepAlive();
        }