Пример #1
0
        public override int Run(string[] paths)
        {
            //this.path = paths[0];
            this.path = paths[0].Replace(@"\", "/");
            Console.WriteLine(path);
            // create a new FtpClient object with the host and port number to use
            // (optionally create a secure SSL connection)
            ftp = new Starksoft.Net.Ftp.FtpClient(host, 21);

            // specify a binary, passive connection with no zlib file compression
            ftp.FileTransferType = TransferType.Binary;
            ftp.DataTransferMode = TransferMode.Passive;

            // open a connection to the ftp server
            ftp.Open(username, password);

            // change to the directory on the ftp server specified in cmd line option
            //ftp.ChangeDirectory(path);

            // retrieve a listing of the files in the directory as a collection of FtpItem objects
            FtpItemCollection col = new FtpItemCollection();

            if (string.IsNullOrEmpty(recurse))
            {
                col = ftp.GetDirList(path);
            }
            else
            {
                col = ftp.GetDirListDeep(path);
            }

            foreach (FtpItem item in col)
            {
                Console.WriteLine("{0}\t{1}\t{2}", item.Modified.ToString(), item.ItemType.ToString(), item.FullPath);
            }

            // close connection to the ftp server
            ftp.Close();

            return 0;
        }
Пример #2
0
        /// <summary>
        /// Connect to the remote servers, with the details from Profile
        /// </summary>
        /// <param name="reconnecting">True if this is an attempt to re-establish a closed connection</param>
        public static void Connect(bool reconnecting = false)
        {
            Notifications.ChangeTrayText(reconnecting ? MessageType.Reconnecting : MessageType.Connecting);
            Log.Write(l.Debug, "{0} client...", reconnecting ? "Reconnecting" : "Connecting");

            if (FTP)
            {
                _ftpc = new FtpClient(Profile.Host, Profile.Port);
                _ftpc.ConnectionClosed += (o, e) =>
                {
                    Notifications.ChangeTrayText(MessageType.Nothing);
                    ConnectionClosed(null, new ConnectionClosedEventArgs { Text = _ftpc.LastResponse.Text });
                    Reconnect();
                };
                _ftpc.TransferProgress += (o, e) =>
                {
                    // if (e.KilobytesPerSecond > 0) Console.Write("\r Transferring at {0,6} kb/s Transferred: {1}\n", e.KilobytesPerSecond, e.BytesTransferred);
                };

                if (Profile.Protocol == FtpProtocol.FTPS)
                {
                    _ftpc.ValidateServerCertificate += (sender, x) =>
                    {
                        // if ValidateCertificate handler isn't set, accept the certificate and move on
                        if (ValidateCertificate == null || Settings.TrustedCertificates.Contains(x.Certificate.Thumbprint))
                        {
                            Log.Write(l.Client, "Trusted: {0}", x.Certificate.Thumbprint);
                            x.IsCertificateValid = true;
                            return;
                        }

                        var e = new ValidateCertificateEventArgs
                        {
                            Fingerprint = x.Certificate.Thumbprint,
                            SerialNumber = x.Certificate.SerialNumber,
                            Algorithm = x.Certificate.SignatureAlgorithm.FriendlyName,
                            ValidFrom = x.Certificate.NotBefore.ToString("MM/dd/yy"),
                            ValidTo = x.Certificate.NotAfter.ToString("MM/dd/yy"),
                            Issuer = x.Certificate.IssuerName.Name
                        };

                        ValidateCertificate(null, e);
                        x.IsCertificateValid = e.IsTrusted;
                    };

                    // Change Security Protocol
                    if (Profile.FtpsInvokeMethod == FtpsMethod.Explicit)
                        _ftpc.SecurityProtocol = FtpSecurityProtocol.Tls1OrSsl3Explicit;
                    else if (Profile.FtpsInvokeMethod == FtpsMethod.Implicit)
                        _ftpc.SecurityProtocol = FtpSecurityProtocol.Tls1OrSsl3Implicit;
                }

                try
                {
                    _ftpc.Open(Profile.Username, Profile.Password);
                    _ftpc.CharacterEncoding = Encoding.Default;
                }
                catch (Exception)
                {
                    if (Profile.FtpsInvokeMethod == FtpsMethod.None)
                        throw;
                    bool connected = false;
                    // Try connecting with the other available Security Protocols
                    foreach (FtpSecurityProtocol p in Enum.GetValues(typeof(FtpSecurityProtocol)))
                    {
                        if ((Profile.FtpsInvokeMethod == FtpsMethod.Explicit && p.ToString().Contains("Explicit"))
                            || (Profile.FtpsInvokeMethod == FtpsMethod.Implicit && p.ToString().Contains("Implicit")))
                        {
                            Log.Write(l.Debug, "Testing with {0}", p.ToString());

                            try {
                                _ftpc.Close();
                                _ftpc.SecurityProtocol = p;
                                _ftpc.Open(Profile.Username, Profile.Password);
                            }
                            catch (Exception exe){
                                Log.Write(l.Warning, "Unable to connect: {0}", exe.GetType().ToString());
                                continue;
                            }
                            connected = true;
                            Profile.SecurityProtocol = p;
                            _ftpc.CharacterEncoding = Encoding.Default;
                            break;
                        }
                    }

                    if (!connected)
                    {
                        Notifications.ChangeTrayText(MessageType.Nothing);
                        throw;
                    }
                }
            }
            else // SFTP
            {
                _sftpc = new SftpClient(Profile.Host, Profile.Port, Profile.Username, Profile.Password);
                _sftpc.ConnectionInfo.AuthenticationBanner += (o, x) => Log.Write(l.Warning, x.BannerMessage);

                _sftpc.HostKeyReceived += (o, x) =>
                {
                    var fingerPrint = x.FingerPrint.GetCertificateData();

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

                    var e = new ValidateCertificateEventArgs
                    {
                        Fingerprint = fingerPrint,
                        Key = x.HostKeyName,
                        KeySize = x.KeyLength.ToString()
                    };
                    ValidateCertificate(null, e);
                    x.CanTrust = e.IsTrusted;
                };

                _sftpc.Connect();

                _sftpc.ErrorOccurred += (o, e) =>
                {
                    if (!isConnected) Notifications.ChangeTrayText(MessageType.Nothing);
                    ConnectionClosed(null, new ConnectionClosedEventArgs { Text = e.Exception.Message });

                    if (e.Exception is Renci.SshNet.Common.SftpPermissionDeniedException)
                        Log.Write(l.Warning, "Permission denied error occured");
                    if (e.Exception is Renci.SshNet.Common.SshConnectionException)
                        Reconnect();
                };
            }

            Profile.HomePath = WorkingDirectory;
            // Profile.HomePath = (Profile.HomePath.StartsWith("/")) ? Profile.HomePath.Substring(1) : Profile.HomePath;

            if (isConnected)
                if (!string.IsNullOrWhiteSpace(Profile.RemotePath) && !Profile.RemotePath.Equals("/"))
                    WorkingDirectory = Profile.RemotePath;

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

            if (Profile.IsDebugMode)
                LogServerInfo();
        }
Пример #3
0
        private void bDone_Click(object sender, EventArgs e)
        {
            bool ftporsftp;
            bool ftps = false;
            bool ftpes = true;

            if (cMode.SelectedIndex == 0)
            {
                ftporsftp = true;
                if (cEncryption.SelectedIndex != 0)
                {
                    ftps = true;
                    if (cEncryption.SelectedIndex == 1)
                    {
                        Log.Write(l.Info, "FTPS Explicit");
                        ftpes = true;
                    }
                    else
                    {
                        Log.Write(l.Info, "FTPS Implicit");
                        ftpes = false;
                    }
                }
                else
                    Log.Write(l.Info, "Plain FTP");
            }
            else
            {
                ftporsftp = false;
                Log.Write(l.Info, "SFTP");
            }

            try
            {
                if (ftporsftp)
                {
                    ftpc = new FtpClient(tHost.Text, Convert.ToInt32(nPort.Value));

                    if (ftps)
                    {
                        if (ftpes)
                            ftpc.SecurityProtocol = FtpSecurityProtocol.Tls1OrSsl3Explicit;
                        else
                            ftpc.SecurityProtocol = FtpSecurityProtocol.Tls1OrSsl3Implicit;
                        ftpc.ValidateServerCertificate += new EventHandler<ValidateServerCertificateEventArgs>(ftp_ValidateServerCertificate);
                    }
                    ftpc.Open(tUsername.Text, tPass.Text);
                    Log.Write(l.Info, "Connected: " + ftpc.IsConnected.ToString());
                    ftpc.Close();
                }
                else
                {
                    string pass = AESEncryption.Encrypt(tPass.Text, decpass, salt, "SHA1", 2, "OFRna73m*aze01xY", 256);
                    ((frmMain)this.Tag).SetPass(pass);
                    //FTPbox.Properties.Settings.Default.ftpPass = tPass.Text;
                    sftp_login();
                    //MessageBox.Show("SFTP Connected");
                    sftpc.quit();
                }

                string hostEncr = AESEncryption.Encrypt(tHost.Text, decpass, salt, "SHA1", 2, "OFRna73m*aze01xY", 256);
                string unEncr = AESEncryption.Encrypt(tUsername.Text, decpass, salt, "SHA1", 2, "OFRna73m*aze01xY", 256);
                string passEncr = AESEncryption.Encrypt(tPass.Text, decpass, salt, "SHA1", 2, "OFRna73m*aze01xY", 256);

                ((frmMain)this.Tag).UpdateAccountInfo(hostEncr, unEncr, passEncr, Convert.ToInt32(nPort.Value), "", ftporsftp, ftps, ftpes);

                //FTPbox.Properties.Settings.Default.ftpHost = tHost.Text;
                //FTPbox.Properties.Settings.Default.ftpPort = Convert.ToInt32(nPort.Value);
                //FTPbox.Properties.Settings.Default.ftpUsername = tUsername.Text;
                //FTPbox.Properties.Settings.Default.ftpPass = tPass.Text;
                //FTPbox.Properties.Settings.Default.FTPorSFTP = ftporsftp;
                //FTPbox.Properties.Settings.Default.timedif = "";
                //FTPbox.Properties.Settings.Default.Save();

                Log.Write(l.Debug, "got new ftp acccount details");
                ((frmMain)this.Tag).ClearLog();
                ((frmMain)this.Tag).UpdateDetails();
                //((frmMain)this.Tag).GetServerTime();
                ((frmMain)this.Tag).loggedIn = true;
                fNewDir fnewdir = new fNewDir();
                fnewdir.ShowDialog();
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Could not connect to FTP server. Check your account details and try again." + Environment.NewLine + " Error message: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                if (ftporsftp)
                    Log.Write(l.Info, "Connected: " + ftpc.IsConnected.ToString());
            }
        }
Пример #4
0
        /// <summary>
        /// Connect to the remote servers, with the details from Profile
        /// </summary>
        /// <param name="reconnecting">True if this is an attempt to re-establish a closed connection</param>
        public void Connect(bool reconnecting = false)
        {
            Notifications.ChangeTrayText(reconnecting ? MessageType.Reconnecting : MessageType.Connecting);
            Log.Write(l.Debug, "{0} client...", reconnecting ? "Reconnecting" : "Connecting");

            if (FTP)
            {
                _ftpc = new FtpClient(controller.Account.Host, controller.Account.Port);
                _ftpc.ConnectionClosed += (o, e) =>
                {
                    Notifications.ChangeTrayText(MessageType.Nothing);
                    if (ConnectionClosed != null) ConnectionClosed(null, new ConnectionClosedEventArgs { Text = _ftpc.LastResponse.Text });
                    Reconnect();
                };

                if (controller.Account.Protocol == FtpProtocol.FTPS)
                {
                    _ftpc.ValidateServerCertificate += (sender, x) =>
                    {
                        // if ValidateCertificate handler isn't set, accept the certificate and move on
                        if (ValidateCertificate == null || Settings.TrustedCertificates.Contains(x.Certificate.Thumbprint))
                        {
                            Log.Write(l.Client, "Trusted: {0}", x.Certificate.Thumbprint);
                            x.IsCertificateValid = true;
                            return;
                        }

                        var e = new ValidateCertificateEventArgs
                        {
                            Fingerprint = x.Certificate.Thumbprint,
                            SerialNumber = x.Certificate.SerialNumber,
                            Algorithm = x.Certificate.SignatureAlgorithm.FriendlyName,
                            ValidFrom = x.Certificate.NotBefore.ToString("MM/dd/yy"),
                            ValidTo = x.Certificate.NotAfter.ToString("MM/dd/yy"),
                            Issuer = x.Certificate.IssuerName.Name
                        };

                        ValidateCertificate(null, e);
                        x.IsCertificateValid = e.IsTrusted;
                    };

                    // Change Security Protocol
                    if (controller.Account.FtpsMethod == FtpsMethod.Explicit)
                        _ftpc.SecurityProtocol = FtpSecurityProtocol.Tls1OrSsl3Explicit;
                    else if (controller.Account.FtpsMethod == FtpsMethod.Implicit)
                        _ftpc.SecurityProtocol = FtpSecurityProtocol.Tls1OrSsl3Implicit;
                }

                try
                {
                    _ftpc.Open(controller.Account.Username, controller.Account.Password);
                }
                catch (Exception)
                {
                    if (controller.Account.FtpsMethod == FtpsMethod.None)
                        throw;
                    bool connected = false;
                    // Try connecting with the other available Security Protocols
                    foreach (FtpSecurityProtocol p in Enum.GetValues(typeof(FtpSecurityProtocol)))
                    {
                        if ((controller.Account.FtpsMethod == FtpsMethod.Explicit && p.ToString().Contains("Explicit"))
                            || (controller.Account.FtpsMethod == FtpsMethod.Implicit && p.ToString().Contains("Implicit")))
                        {
                            Log.Write(l.Debug, "Testing with {0}", p.ToString());

                            try {
                                _ftpc.Close();
                                _ftpc.SecurityProtocol = p;
                                _ftpc.Open(controller.Account.Username, controller.Account.Password);
                            }
                            catch (Exception exe){
                                Log.Write(l.Warning, "Unable to connect: {0}", exe.GetType().ToString());
                                continue;
                            }
                            connected = true;
                            controller.Account.FtpSecurityProtocol = p;
                            break;
                        }
                    }

                    if (!connected)
                    {
                        Notifications.ChangeTrayText(MessageType.Nothing);
                        throw;
                    }
                }
            }
            else // SFTP
            {
                ConnectionInfo connectionInfo;
                if (controller.isPrivateKeyValid)
                    connectionInfo = new PrivateKeyConnectionInfo(controller.Account.Host, controller.Account.Port, controller.Account.Username, new PrivateKeyFile(controller.Account.PrivateKeyFile, controller.Account.Password));
                else
                    connectionInfo = new PasswordConnectionInfo(controller.Account.Host, controller.Account.Port, controller.Account.Username, controller.Account.Password);

                _sftpc = new SftpClient(connectionInfo);
                _sftpc.ConnectionInfo.AuthenticationBanner += (o, x) => Log.Write(l.Warning, x.BannerMessage);

                _sftpc.HostKeyReceived += (o, x) =>
                {
                    var fingerPrint = x.FingerPrint.GetCertificateData();

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

                    var e = new ValidateCertificateEventArgs
                    {
                        Fingerprint = fingerPrint,
                        Key = x.HostKeyName,
                        KeySize = x.KeyLength.ToString()
                    };
                    ValidateCertificate(null, e);
                    x.CanTrust = e.IsTrusted;
                };

                _sftpc.Connect();

                _sftpc.ErrorOccurred += (o, e) =>
                {
                    if (!isConnected) Notifications.ChangeTrayText(MessageType.Nothing);
                    if (ConnectionClosed != null) ConnectionClosed(null, new ConnectionClosedEventArgs { Text = e.Exception.Message });

                    if (e.Exception is Renci.SshNet.Common.SftpPermissionDeniedException)
                        Log.Write(l.Warning, "Permission denied error occured");
                    if (e.Exception is Renci.SshNet.Common.SshConnectionException)
                        Reconnect();
                };
            }

            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();
        }
Пример #5
0
        public static void Connect()
        {
            Console.WriteLine("Connecting client...");
            if (FTP)
            {
                ftpc = new FtpClient(Profile.Host, Profile.Port);

                if (Profile.Protocol == FtpProtocol.FTPS)
                {
                    ftpc.SecurityProtocol = Profile.SecurityProtocol;
                    ftpc.ValidateServerCertificate += new EventHandler<ValidateServerCertificateEventArgs>(ftp_ValidateServerCertificate);
                }

                try
                {
                    ftpc.Open(Profile.Username, Profile.Password);
                    ftpc.CharacterEncoding = System.Text.Encoding.Default;
                }
                catch(Exception ex)
                {
                    if (Profile.FtpsInvokeMethod == FtpsMethod.None)
                        throw ex;
                    bool connected = false;

                    foreach (FtpSecurityProtocol p in Enum.GetValues(typeof(FtpSecurityProtocol)))
                    {
                        if ((Profile.FtpsInvokeMethod == FtpsMethod.Explicit && p.ToString().Contains("Explicit"))
                            || (Profile.FtpsInvokeMethod == FtpsMethod.Implicit && p.ToString().Contains("Implicit")))
                        {
                            Console.WriteLine("Testing with {0}", p.ToString());

                            try {
                                ftpc.Close();
                                ftpc.SecurityProtocol = p;
                                ftpc.Open(Profile.Username, Profile.Password);
                            }
                            catch (Exception exe){
                                Console.WriteLine("Exe: {0}", exe.Message);
                                continue;
                            }
                            connected = true;
                            Profile.SecurityProtocol = p;
                            ftpc.CharacterEncoding = System.Text.Encoding.Default;
                            break;
                        }
                    }

                    if (!connected)
                        throw ex;
                }
            }
            else
            {
                sftpc = new SftpClient(Profile.Host, Profile.Port, Profile.Username, Profile.Password);
                sftpc.Connect();
            }

            Profile.HomePath = WorkingDirectory;
            Profile.HomePath = (Profile.HomePath.StartsWith("/")) ? Profile.HomePath.Substring(1) : Profile.HomePath;

            Console.WriteLine("Client connected sucessfully");

            if (Profile.IsDebugMode)
                LogServerInfo();
        }