Пример #1
0
        public static void start()
        {
            DirectoryInfo  within = new DirectoryInfo (source);
             FileInfo[]     files = within.GetFiles ();
             int            nFiles = files.Length,index = 0;
             float          fraction = 0;
             Credentials    credentials = new Credentials ();

             string target = credentials.bankRelPath + "/staging/";

             FTPSClient client = new FTPSClient ();
             client.Connect (credentials.ftpServer,
                         new NetworkCredential (credentials.username, credentials.password),
                         ESSLSupportMode.CredentialsRequested);

             client.SetTransferMode (ETransferMode.Binary);

             foreach (FileInfo f in files) {
            index += 1;
            fraction = ((float)index) / nFiles;
            try {
               client.PutFile (f.FullName, target + f.Name);
            } catch {
               // Console.WriteLine (" -- putfile exception");
               client.Close ();
               client = new FTPSClient ();
               client.Connect (credentials.ftpServer,
                         new NetworkCredential (credentials.username, credentials.password),
                         ESSLSupportMode.CredentialsRequested);
               client.SetTransferMode (ETransferMode.Binary);
               client.PutFile (f.FullName, target + f.Name);
            }
            Gtk.Application.Invoke (delegate {
               MainClass.getAppWindow ().getTransmissionPBar ().Fraction = fraction;});
            f.Delete() ; // delete the file from within .resolved/
             }
             client.Close ();

             Gtk.Application.Invoke (delegate {
            MainClass.doneTransmitting ();
            MainClass.resetDisplay (); });
        }
Пример #2
0
        public void GetFilesWithFTPNewMode(string host, string remoteLocation)
        {
            FTPSClient client = new FTPSClient();
            try {
                client.Connect(host,ESSLSupportMode.All);
                try {
                    client.SetCurrentDirectory(remoteLocation);
                    ScriptName = client.GetCurrentDirectory();
                    string localDownloadDirectory = downloadDir + ScriptName;

                    client.GetFiles(localDownloadDirectory, "*.log", EPatternStyle.Wildcard, false);

                } catch (Exception ex) {

                }
            } finally {
                client.Close();
                client.Dispose();
            }
        }
Пример #3
0
        public static bool load(string qrc)
        {
            // qrc for the just detected QR Code
             string   manifest = qrc.Substring(0,6) ;

             if (Folder.GetFiles(manifest).Length == 0) { // manifest not present locally
            Credentials   credentials = new Credentials() ;
            FTPSClient  ftp = new FTPSClient() ;

            ftp.Connect(credentials.ftpServer,
               new NetworkCredential(credentials.username, credentials.password),
               ESSLSupportMode.CredentialsRequested) ;
            ftp.GetFile(credentials.bankRelPath + "/atm/" + manifest + "/keyFile",
                        Folder.FullName + Path.DirectorySeparatorChar + manifest) ;
            ftp.Close() ;
             }

             FileInfo[]  m = Folder.GetFiles(manifest) ; // there should be just one
             foreach (FileInfo f in m) Manifest.load(f) ;

             return true ;
        }
        /// <summary>
        /// FTPS procedure to upload or download files
        /// </summary>
        /// <param name="blShuttingDown"></param>
        public void FTPs(ref bool blShuttingDown)
        {
            FTPSClient FTPS = null;
            bool blFileFound = false;
            bool blOverwriteFile = false;
            try
            {
                FTPS = new FTPSClient();
                AES256 aes = new AES256(ep);
                string upassword = aes.Decrypt(_password);

                if (Protocol == ProtocolOptions.FTP)
                {
                    FTPS.Connect(Host, Port, new NetworkCredential(Username, upassword), ESSLSupportMode.ClearText, null, null, 0, 0, 0, Timeout);
                }
                else
                {
                    if (AllowAnyCertificate)
                    {
                        if (Protocol == ProtocolOptions.FTPsExplicit)
                        {
                            FTPS.Connect(Host, Port, new NetworkCredential(Username, upassword), ESSLSupportMode.CredentialsRequired | ESSLSupportMode.DataChannelRequested, new RemoteCertificateValidationCallback(ValidateTestServerCertificate), null, 0, 0, 0, Timeout);

                        }
                        else
                        {
                            FTPS.Connect(Host, Port, new NetworkCredential(Username, upassword), ESSLSupportMode.Implicit, new RemoteCertificateValidationCallback(ValidateTestServerCertificate), null, 0, 0, 0, Timeout);

                        }

                    }
                    else
                    {
                        if (Protocol == ProtocolOptions.FTPsExplicit)
                        {
                            FTPS.Connect(Host, Port, new NetworkCredential(Username, upassword), ESSLSupportMode.CredentialsRequired | ESSLSupportMode.DataChannelRequested, new RemoteCertificateValidationCallback(ValidateServerCertificate), null, 0, 0, 0, Timeout);
                        }
                        else
                        {
                            FTPS.Connect(Host, Port, new NetworkCredential(Username, upassword), ESSLSupportMode.Implicit, new RemoteCertificateValidationCallback(ValidateServerCertificate), null, 0, 0, 0, Timeout);
                        }
                    }
                }

                _evt.WriteEntry("Remote Sync: FTPS Connected Successfully to: " + Host, System.Diagnostics.EventLogEntryType.Information, 2005, 20);

                upassword = "";
                switch (TransferDirection)
                {
                    case TransferDirectionOptions.Upload:
                        BackupFolder = BackupFolder.Replace("\\\\", "\\");
                        IList<DirectoryListItem> RemoteFilesU=null;

                        try
                        {
                            FTPS.SetCurrentDirectory(RemoteDirectory);
                            RemoteFilesU = FTPS.GetDirectoryList();
                            UploadFiles = Common.WalkDirectory(BackupFolder, ref blShuttingDown, FileNameFilter);

                            //CreateRemote Directories
                            foreach (DirectoryInfo dir in Common.GetAllDirectories(BackupFolder))
                            {
                                string strRemotePath = "";
                                strRemotePath=Common.RemotePathCombine(RemoteDirectory, dir.FullName, BackupFolder);
                                if (blShuttingDown)
                                {
                                    throw new Exception("Shutting Down");
                                }

                                if (blShuttingDown)
                                {

                                    _evt.WriteEntry("Remote Sync: Shutting down about to possibly create a folder on Host: " + Host + " Folder: " + strRemotePath, System.Diagnostics.EventLogEntryType.Information, 2040, 20);
                                    return;
                                }

                                try
                                {
                                    FTPS.MakeDir(strRemotePath);
                                    _evt.WriteEntry("Remote Sync: Folder Created on " + Host + " : " + strRemotePath, System.Diagnostics.EventLogEntryType.Information, 2040, 20);
                                }
                                catch (Exception)
                                {
                                }

                                //FTPS.SetCurrentDirectory(strRemotePath);
                            }

                            //Upload Each File
                            foreach (FileInfo fileU in UploadFiles)
                            {
                                if (blShuttingDown)
                                {
                                    throw new Exception("Shutting Down");
                                }
                                try
                                {
                                    string strRemotePath = "";

                                    strRemotePath = Common.RemotePathCombine(RemoteDirectory, fileU.DirectoryName, BackupFolder);
                                    strRemotePath = Common.RemotePathCombine(strRemotePath, fileU.Name);

                                    blFileFound = false;
                                    blOverwriteFile = false;
                                    try
                                    {

                                        if (FTPS.GetFileTransferSize(strRemotePath) > 0)
                                        {
                                            blFileFound = true;
                                            if (!(/*(fileU.LastAccessTimeUtc == FTPS.GetFileModificationTime(strRemotePath)) &&*/ ((ulong)fileU.Length == FTPS.GetFileTransferSize(strRemotePath))))
                                            {
                                                blOverwriteFile = true;
                                            }

                                        }
                                    }
                                    catch (Exception)
                                    {
                                        //File Not found No Action Necessary on Error
                                    }

                                    try
                                    {
                                        if (FTPS.GetFileTransferSize(strRemotePath + ".7z") > 0)
                                        {
                                            blFileFound = true;
                                            //7z file exists but there is no current way to compare the 7zipped file vs the non compressed file so the user overwrite option will force the appropriate action
                                            blOverwriteFile = true;
                                            /*
                                            if (!((fileU.LastAccessTimeUtc == FTPS.GetFileModificationTime(strRemotePath + ".7z")) && ((ulong)fileU.Length == FTPS.GetFileTransferSize(strRemotePath + ".7z"))))
                                            {
                                                blOverwriteFile = true;
                                            }
                                            */

                                        }
                                    }
                                    catch (Exception)
                                    {
                                        //File Not Found No Action Necessary on Error
                                    }
                                    if (blShuttingDown)
                                    {

                                        _evt.WriteEntry("Remote Sync FTPS: Shutting Down about to possible upload a file: " + fileU.FullName + " Host: " + Host + " To: " + strRemotePath, System.Diagnostics.EventLogEntryType.Information, 2010, 20);
                                        return;
                                    }

                                    if ((!blFileFound || blOverwriteFile || (Overwrite == OverwriteOptions.ForceOverwrite && blFileFound)) && !(Overwrite == OverwriteOptions.NoOverwrite && blFileFound))
                                    {
                                        //This Uploads the file
                                        FTPS.PutFile(fileU.FullName, strRemotePath);
                                        _evt.WriteEntry("Remote Sync FTPS: File Uploaded: " + fileU.FullName + " Host: " + Host + " To: " + strRemotePath, System.Diagnostics.EventLogEntryType.Information, 2010, 20);

                                    }

                                }
                                catch (Exception exp)
                                {
                                    _evt.WriteEntry("Remote Sync FTPS: Host:" + Host + " Upload FileName: " + fileU.FullName + " Error: " + exp.Message, System.Diagnostics.EventLogEntryType.Error, 2010, 20);
                                }

                            }
                        }
                        catch (Exception exu)
                        {
                            _evt.WriteEntry("Remote Sync FTPS: Upload Error on Host:" + Host + " Error: " + exu.Message, System.Diagnostics.EventLogEntryType.Error, 2010, 20);
                        }
                        finally
                        {
                            if (RemoteFilesU != null)
                            {
                                RemoteFilesU.Clear();
                            }
                            if (UploadFiles != null)
                            {
                                UploadFiles.Clear();
                            }
                            RemoteFilesU = null;
                            UploadFiles = null;

                        }
                        break;
                    case TransferDirectionOptions.Download:

                        List<RemoteFile> RemoteFilesD=null;

                        try
                        {
                            FTPS.SetCurrentDirectory(RemoteDirectory);
                            RemoteFilesD = Common.GetRemoteDirectories(RemoteDirectory, FTPS, "");
                            Common.CreateLocalFolderStructure(RemoteFilesD, BackupFolder);
                            foreach (RemoteFile FileD in RemoteFilesD)
                            {
                                string strLocalFile = "";
                                string strRemoteFile = "";

                                if (blShuttingDown)
                                {
                                    throw new Exception("Shutting Down");
                                }
                                try
                                {
                                    if (!FileD.IsDirectory)
                                    {

                                        strLocalFile = Common.WindowsPathCombine(BackupFolder, FileD.ParentDirectory);
                                        strLocalFile = Common.WindowsPathCombine(strLocalFile, FileD.Name);
                                        strRemoteFile = FileD.FullName;

                                        if (Common.DownloadFile(strLocalFile, strRemoteFile, FileD, Overwrite))
                                        {
                                            if ((!blFileFound || blOverwriteFile || (Overwrite == OverwriteOptions.ForceOverwrite && blFileFound)) && !(Overwrite == OverwriteOptions.NoOverwrite && blFileFound))
                                            {
                                                if (Common.FixNullstring(FileNameFilter) != "" && Common.VerifyPattern(FileNameFilter))
                                                {
                                                    if (Common.FileNameMatchesPattern(FileNameFilter, FileD.Name))
                                                    {
                                                        FTPS.GetFile(strRemoteFile, strLocalFile);
                                                        _evt.WriteEntry("Remote Sync FTPS: File Downloaded: " + strRemoteFile + " Host: " + Host + " To: " + strLocalFile, System.Diagnostics.EventLogEntryType.Information, 2020, 20);
                                                    }
                                                }
                                                else
                                                {
                                                    FTPS.GetFile(strRemoteFile, strLocalFile);
                                                    _evt.WriteEntry("Remote Sync FTPS: File Downloaded: " + strRemoteFile + " Host: " + Host + " To: " + strLocalFile, System.Diagnostics.EventLogEntryType.Information, 2020, 20);
                                                }
                                            }

                                        }

                                    }
                                }
                                catch (Exception exdi)
                                {
                                    _evt.WriteEntry("Remote Sync FTPS: File Download Error: " + strRemoteFile + " Host: " + Host + " To: " + strLocalFile + " Error: " + exdi.Message, System.Diagnostics.EventLogEntryType.Error, 2020, 20);
                                }

                            }
                        }
                        catch (Exception exd)
                        {
                            _evt.WriteEntry("Remote Sync FTPS: Download Error: Host: " + Host + " Error: " + exd.Message, System.Diagnostics.EventLogEntryType.Error, 2020, 20);
                        }
                        finally
                        {
                            if (RemoteFilesD !=null)
                            {
                                RemoteFilesD.Clear();
                            }
                            RemoteFilesD = null;
                        }
                        break;

                }
            }
            catch (Exception ex)
            {
                _evt.WriteEntry("Remote Sync FTPS: Error: Host: " + Host + " Error: " + ex.Message, System.Diagnostics.EventLogEntryType.Error, 2000, 20);
            }
            finally
            {
                if (FTPS != null)
                {
                    try
                    {
                        FTPS.Close();
                    }
                    catch (Exception)
                    {

                    }

                    FTPS.Dispose();

                }
                FTPS = null;
            }
        }
Пример #5
0
        public static bool load(string qrc)
        {
            // qrc for the just detected QR Code
              mutex.WaitOne();

              string manifest = qrc.Substring(0, 6);
              bool sthLoaded = false;

              if (Folder.GetFiles(manifest).Length == 0) { // manifest not present locally
            Credentials credentials = new Credentials();
            FTPSClient ftp = new FTPSClient();

            ftp.Connect(credentials.ftpServer,
               new NetworkCredential(credentials.username, credentials.password),
               ESSLSupportMode.CredentialsRequested);
            ftp.GetFile(credentials.root + "/atm/" + manifest + "/keyFile",
                    Folder.FullName + Path.DirectorySeparatorChar + manifest);
            ftp.Close();
              }

              FileInfo[] m = Folder.GetFiles(manifest); // there should be just one
              foreach (FileInfo f in m)
            sthLoaded |= Manifest.load(f); // returns false if manifest already in DOM

              mutex.ReleaseMutex();
              return sthLoaded;
        }
Пример #6
0
        static void Main(string[] args)
        {
            foreach (string arg in args)
            {
                // display help
                if (arg.Equals("/?", StringComparison.CurrentCultureIgnoreCase) | arg.Equals("/help", StringComparison.CurrentCultureIgnoreCase))
                {
                    DisplayHelp();
                }

                // switches
                if (arg.StartsWith("-"))
                {
                    if (arg.Equals("-list", StringComparison.CurrentCultureIgnoreCase))
                    {
                        _command = "list";
                    }

                    if (arg.Equals("-get", StringComparison.CurrentCultureIgnoreCase))
                    {
                        _command = "get";
                    }

                    if (arg.Equals("-import", StringComparison.CurrentCultureIgnoreCase))
                    {
                        _command = "import";
                    }
                }
                else if (arg.StartsWith("/"))
                {
                    if (arg.Contains(":"))
                    {
                        string key = arg.Split(':')[0];
                        string value = arg.Split(':')[1];

                        switch (key.ToLower())
                        {
                            case "host":
                                _host = value;

                                break;
                            case "port":
                                _port = int.Parse(value);

                                break;
                            case "username":
                                _username = value;

                                break;
                            case "password":
                                _password = value;

                                break;
                        }

                        //Console.WriteLine(key);
                        //Console.WriteLine(value);
                    }
                    //else
                    //    Console.WriteLine("{arg} - Invalid parameter!");
                }
                else
                {
                    _commandArgument = arg;
                }

            }

            FTPSClient client = new FTPSClient();

            client.Connect(_host, new NetworkCredential(_username, _password), ESSLSupportMode.ClearText);

            switch (_command)
            {
                case "import":
                    ImportPackage(client, _commandArgument);

                    break;
                case "get":
                    GetFile(client, _commandArgument);

                    break;
                case "list":
                    ListFiles(client);

                    break;
                default:
                    break;
            }

            client.Close();

            Console.ReadLine();

        }
Пример #7
0
        public static void start()
        {
            DirectoryInfo within = new DirectoryInfo(source);
              FileInfo[] files = within.GetFiles();
              int nFiles = files.Length, processed = 0;
              int percentDone = 0;
              Credentials credentials = new Credentials();

              string target = credentials.target + "/staging/";
              Stopwatch stopWatch = new Stopwatch();
              FTPSClient client = new FTPSClient();
              string welcomeMsg = null;

              System.Diagnostics.Debug.WriteLine("[Transmitter]: Thread start()");

              try {
            welcomeMsg = client.Connect(credentials.ftpServer,
                    new NetworkCredential(credentials.username, credentials.password),
                    ESSLSupportMode.CredentialsRequested);
              } catch (SocketException e) {
            Program.Gui.Invoke((MethodInvoker)(delegate {
              Program.EstTransmission.Text = "No Net?";
              Label noNet = Program.Gui.NetConnectionStatus;

              noNet.Text = "Just make sure you're connected to the Net. I will try again later";
              noNet.Visible = true;
            }));
            client.Close();
            return;
              }

              // Have net ... Try logging in to FTP server

              if (welcomeMsg.StartsWith("Login successful")) {
            Program.Gui.Invoke((MethodInvoker)(delegate {
              Program.EstTransmission.Text = "Connected";
              Program.Gui.NetConnectionStatus.Visible = false;
            }));
              } else {
            Program.Gui.Invoke((MethodInvoker)(delegate {
              Program.EstTransmission.Text = "Couldn't login";
              Label noNet = Program.Gui.NetConnectionStatus;

              noNet.Text = "Happens sometimes. Not to worry. I will try again later";
              noNet.Visible = true;
            }));
            client.Close();
            return;
              }

              // Otherwise, we are good to go
              client.SetTransferMode(ETransferMode.Binary);
              stopWatch.Start();

              foreach (FileInfo f in files) {
            if (!Program.GuiAlive) {
              System.Diagnostics.Debug.WriteLine("[Transmitter]: Gui Dead!!");
              break;
            }

            ulong bytesSent = 0;
            ulong size = (ulong)f.Length;

            try {
              bytesSent = client.PutFile(f.FullName, target + f.Name);
            } catch {
              // Console.WriteLine (" -- putfile exception");
              client.Close();
              client = new FTPSClient();
              client.Connect(credentials.ftpServer,
                    new NetworkCredential(credentials.username, credentials.password),
                    ESSLSupportMode.CredentialsRequested);
              client.SetTransferMode(ETransferMode.Binary);
              bytesSent = client.PutFile(f.FullName, target + f.Name);
            }
            processed += 1;
            percentDone = (int)((processed * 100) / nFiles); // percentage

            TimeSpan elapsed = stopWatch.Elapsed;
            string estimate = Program.EstTimeToCompletion(elapsed, processed, nFiles);

            // Update the transmission progress bar
            if (Program.GuiAlive) {
              Program.Gui.Invoke((MethodInvoker)(delegate {
            Program.TransmissionBar.Value = percentDone;
            Program.EstTransmission.Text = estimate;
              }));
            }

            if (bytesSent == size)
              f.Delete();
            // delete file from .resolved/ only if it was transferred completely
              }

              stopWatch.Stop();
              client.Close();
              System.Diagnostics.Debug.WriteLine("[Transmitter]: Thread end()");

              if (Program.GuiAlive) {
            Program.Gui.Invoke((MethodInvoker)(delegate {
              Program.DoneTransmitting();
              Program.ResetDisplay();
            }));
              }
        }