Ejemplo n.º 1
0
        private static void DoSingleFileGet(FtpsClient client, string remotePathName)
        {
            string localPathName = null;
            string localDirName  = null;

            if (_commandArguments.Count > 1)
            {
                if (Directory.Exists(_commandArguments[1]))
                {
                    localDirName = _commandArguments[1];
                }
                else
                {
                    localPathName = _commandArguments[1];
                }
            }
            else
            {
                localDirName = Directory.GetCurrentDirectory();
            }

            if (localPathName == null)
            {
                var remoteFileName = Path.GetFileName(remotePathName);
                localPathName = Path.Combine(localDirName ?? throw new InvalidOperationException($"{nameof(localDirName)} cannot be null"),
                                             remoteFileName ?? throw new InvalidOperationException($"{nameof(remoteFileName)} cannot be null"));
            }

            client.GetFile(remotePathName, localPathName, TransferCallback);
        }
Ejemplo n.º 2
0
        private static void TransferCallback(FtpsClient sender, ETransferActions action, string localObjectName, string remoteObjectName, ulong fileTransmittedBytes, ulong?fileTransferSize, ref bool cancel)
        {
            switch (action)
            {
            case ETransferActions.FileDownloaded:
            case ETransferActions.FileUploaded:
                OnFileTransferCompleted(fileTransmittedBytes, fileTransferSize);
                break;

            case ETransferActions.FileDownloadingStatus:
            case ETransferActions.FileUploadingStatus:
                OnFileTransferStatus(action, localObjectName, remoteObjectName, fileTransmittedBytes, fileTransferSize);
                break;

            case ETransferActions.RemoteDirectoryCreated:
                if (_options.verbose)
                {
                    Console.WriteLine();
                    Console.WriteLine("Remote directory created: " + remoteObjectName);
                }
                break;

            case ETransferActions.LocalDirectoryCreated:
                if (_options.verbose)
                {
                    Console.WriteLine();
                    Console.WriteLine("Local directory created: " + localObjectName);
                }
                break;
            }
        }
Ejemplo n.º 3
0
        private static void DoAppendFile(FtpsClient client)
        {
            var localPathName  = _commandArguments[0];
            var remotePathName = GetRemotePathName(localPathName);

            client.AppendFile(localPathName, remotePathName, TransferCallback);
        }
Ejemplo n.º 4
0
        private static void DoWildCardPut(FtpsClient client, string localPathPattern)
        {
            var localDirName     = Path.GetDirectoryName(localPathPattern);
            var localFilePattern = Path.GetFileName(localPathPattern);

            _filesTransferredCount = 0;

            string remoteDirName = null;

            if (_commandArguments.Count > 1)
            {
                remoteDirName = NormalizeRemotePath(_commandArguments[1]);
            }

            client.PutFiles(localDirName, remoteDirName, localFilePattern, EPatternStyle.Wildcard, _options.recursive, TransferCallback);

            Console.WriteLine();
            if (_filesTransferredCount > 0)
            {
                Console.WriteLine("Uploaded files: {0}", _filesTransferredCount);
            }
            else
            {
                Console.Error.WriteLine("WARNING: No files uploaded");
            }
        }
        private FtpsClient TestConnection(FtpsOptions options)
        {
            FtpsClient client    = new FtpsClient();
            bool       connected = client.Connect(options);

            True(connected, client);
            return(client);
        }
Ejemplo n.º 6
0
 private static void InitLogFile(FtpsClient client)
 {
     if (_options.logFileName != null)
     {
         _swLog                 = new StreamWriter(_options.logFileName);
         client.LogCommand     += client_LogCommand;
         client.LogServerReply += client_LogServerReply;
     }
 }
Ejemplo n.º 7
0
        public void TestUserReportedError2(string host, int port, FtpsSecurityProtocol protocol,
                                           string user, string pwd, string server)
        {
            FtpsClient ftp = new FtpsClient("127.0.0.1", port, protocol);

            ftp.AlwaysAcceptServerCertificate = true;
            ftp.Open("test", "test");
            ftp.GetFile("luna.h", "luna1-2.h", FileAction.Create);
            ftp.Close();
        }
Ejemplo n.º 8
0
 public void TestOpenUsing(string host, int port, FtpsSecurityProtocol protocol,
                           string user, string pwd, string server)
 {
     using (FtpsClient c = new FtpsClient(host, port, protocol))
     {
         c.AlwaysAcceptServerCertificate = true;
         c.Open(user, pwd);
         Assert.IsTrue(c.IsConnected);
     }
 }
Ejemplo n.º 9
0
        public void TestPutFileResume(string host, int port, FtpsSecurityProtocol protocol,
                                      string user, string pwd, int fileSize)
        {
            using (FtpsClient c = new FtpsClient(host, port, protocol))
            {
                c.NetworkProtocol = NETWORK_PROTOCOL;
                c.AlwaysAcceptServerCertificate = true;
                c.Open(user, pwd);
                Assert.IsTrue(c.IsConnected);

                MemoryStream m1 = GetMemoryStreamRandom(fileSize);
                MemoryStream m2 = GetMemoryStreamRandom(fileSize);

                byte[] a = m1.ToArray();
                byte[] b = m2.ToArray();
                byte[] l = new byte[a.Length + b.Length];
                Array.Copy(a, l, a.Length);
                Array.Copy(b, 0, l, a.Length, b.Length);

                // m3 is m1 + m2
                MemoryStream m3 = new MemoryStream(l);

                string fname = GetGuidString();

                try
                {
                    c.PutFile(m1, fname, FileAction.Create);

                    Assert.IsTrue(c.Exists(fname));

                    MemoryStream o1 = new MemoryStream();
                    c.GetFile(fname, o1, false);
                    o1.Position = 0;

                    //compare bytes to verify
                    Assert.IsTrue(Compare(m1, o1));

                    // put m3 as a resume file
                    c.PutFile(m3, fname, FileAction.Resume);

                    Assert.IsTrue(c.Exists(fname));

                    MemoryStream o2 = new MemoryStream();
                    c.GetFile(fname, o2, false);
                    o1.Position = 0;

                    //compare bytes to verify
                    Assert.IsTrue(Compare(m3, o2));
                }
                finally
                {
                    c.DeleteFile(fname);
                }
            }
        }
Ejemplo n.º 10
0
        public void TestUserReportedError2()
        {
            // clean up anything that may have been left over from previous tests
            FtpsClient ftp1 = new FtpsClient(FTP_HOST, FTP_STD_PORT, FtpsSecurityProtocol.None);

            ftp1.NetworkProtocol = NETWORK_PROTOCOL;
            ftp1.Open(FTP_USER, FTP_PASS);

            try
            {
                ftp1.ChangeDirectory("test dir");
            }
            catch
            {
            }
            try
            {
                ftp1.DeleteFile("testfile.txt");
            }
            catch
            {
            }

            try
            {
                ftp1.ChangeDirectory("\\");
            }
            catch
            {
            }

            try
            {
                ftp1.DeleteDirectory("test dir");
            }
            catch
            {
            }


            FtpsClient ftp = new FtpsClient(FTP_HOST, FTP_STD_PORT, FtpsSecurityProtocol.Tls12Explicit);

            ftp.NetworkProtocol = NETWORK_PROTOCOL;
            ftp.AlwaysAcceptServerCertificate = true;
            ftp.Open(FTP_USER, FTP_PASS);
            ftp.MakeDirectory("test dir");
            ftp.ChangeDirectory("test dir");
            ftp.PutFile(GetMemoryStreamRandom(100), "testfile.txt", FileAction.Create);
            FtpsItemCollection col = ftp.GetDirList();

            ftp.Close();

            Assert.IsTrue(col.Count == 1);
        }
Ejemplo n.º 11
0
        public void TestOpen(string host, int port, FtpsSecurityProtocol protocol,
                             string user, string pwd)
        {
            FtpsClient c = new FtpsClient(host, port, protocol);

            c.NetworkProtocol = NETWORK_PROTOCOL;
            c.AlwaysAcceptServerCertificate = true;
            c.Open(user, pwd);
            Assert.IsTrue(c.IsConnected);
            c.Close();
        }
Ejemplo n.º 12
0
 public void TestAllocateStorage(string host, int port, FtpsSecurityProtocol protocol,
                                 string user, string pwd)
 {
     using (FtpsClient c = new FtpsClient(host, port, protocol))
     {
         c.NetworkProtocol = NETWORK_PROTOCOL;
         c.AlwaysAcceptServerCertificate = true;
         c.Open(user, pwd);
         Assert.IsTrue(c.IsConnected);
         c.AllocateStorage(DEFAULT_FILE_SIZE * 16);
     }
 }
Ejemplo n.º 13
0
        private static void DoList(FtpsClient client)
        {
            var remoteDirName = _commandArguments.Count > 0 ? NormalizeRemotePath(_commandArguments[0]) : client.GetCurrentDirectory();

            Console.WriteLine();
            Console.WriteLine("Remote directory: " + remoteDirName);

            // Get the dirList before the WriteLine in order to avoid writing an empty newline in case of exceptions
            var dirList = client.GetDirectoryListUnparsed(remoteDirName);

            Console.WriteLine();
            Console.WriteLine(dirList);
        }
Ejemplo n.º 14
0
 public void TestGetSystemType(string host, int port, FtpsSecurityProtocol protocol,
                               string user, string pwd, string server)
 {
     using (FtpsClient c = new FtpsClient(host, port, protocol))
     {
         c.AlwaysAcceptServerCertificate = true;
         c.Open(user, pwd);
         Assert.IsTrue(c.IsConnected);
         string r = c.GetSystemType();
         Assert.IsNotNullOrEmpty(r);
         Debug.WriteLine("RESULT: " + r);
     }
 }
Ejemplo n.º 15
0
 public void TestUtf8On(string host, int port, FtpsSecurityProtocol protocol,
                        string user, string pwd)
 {
     using (FtpsClient c = new FtpsClient(host, port, protocol))
     {
         c.NetworkProtocol = NETWORK_PROTOCOL;
         c.AlwaysAcceptServerCertificate = true;
         c.Open(user, pwd);
         Assert.IsTrue(c.IsConnected);
         c.SetUtf8On();
         //Assert.IsTrue(c.Encoding == Encoding.UTF8);
     }
 }
Ejemplo n.º 16
0
        private static void DoGet(FtpsClient client)
        {
            var remotePathPattern = _commandArguments[0];

            if (IsWildCardPath(remotePathPattern))
            {
                DoWildCardGet(client, remotePathPattern);
            }
            else
            {
                DoSingleFileGet(client, remotePathPattern);
            }
        }
Ejemplo n.º 17
0
        private static void DoPut(FtpsClient client)
        {
            var localPathPattern = _commandArguments[0];

            if (IsWildCardPath(localPathPattern))
            {
                DoWildCardPut(client, localPathPattern);
            }
            else
            {
                DoSingleFilePut(client, localPathPattern);
            }
        }
Ejemplo n.º 18
0
        public void TestUserReportedError1()
        {
            FtpsClient ftp = new FtpsClient(FTP_HOST, FTP_STD_PORT, FtpsSecurityProtocol.Tls12Explicit);

            ftp.NetworkProtocol = NETWORK_PROTOCOL;
            ftp.AlwaysAcceptServerCertificate = true;
            ftp.Open(FTP_USER, FTP_PASS);
            ftp.PutFile(GetMemoryStreamRandom(100), "testfile.txt", FileAction.Create);
            MemoryStream fout = new MemoryStream();

            ftp.GetFile("testfile.txt", fout, false);
            ftp.DeleteFile("testfile.txt");
            ftp.Close();
        }
Ejemplo n.º 19
0
 public void UploadPackage(string packagePath, string packageVersion)
 {
     _packageFtpsClient = GetNewFtpsClient();
     _packageFtpsClient.TransferProgress      += TransferProgressChangedEventHandler;
     _packageFtpsClient.PutFileAsyncCompleted += UploadPackageFinished;
     _packageFtpsClient.Open(Username, Password.ConvertToInsecureString());
     _packageFtpsClient.ChangeDirectoryMultiPath(Directory);
     _packageFtpsClient.MakeDirectory(packageVersion);
     _packageFtpsClient.ChangeDirectory(packageVersion);
     _packageFtpsClient.PutFileAsync(packagePath, FileAction.Create);
     _uploadPackageResetEvent.WaitOne();
     _packageFtpsClient.Close();
     _uploadPackageResetEvent.Reset();
 }
Ejemplo n.º 20
0
 public void TestQuote(string host, int port, FtpsSecurityProtocol protocol,
                       string user, string pwd)
 {
     using (FtpsClient c = new FtpsClient(host, port, protocol))
     {
         c.NetworkProtocol = NETWORK_PROTOCOL;
         c.AlwaysAcceptServerCertificate = true;
         c.Open(user, pwd);
         Assert.IsTrue(c.IsConnected);
         string result = c.Quote("HELP");
         Assert.IsNotNull(result);
         Assert.IsNotEmpty(result);
     }
 }
Ejemplo n.º 21
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);
        }
Ejemplo n.º 22
0
 public void TestGetSystemType(string host, int port, FtpsSecurityProtocol protocol,
                               string user, string pwd)
 {
     using (FtpsClient c = new FtpsClient(host, port, protocol))
     {
         c.NetworkProtocol = NETWORK_PROTOCOL;
         c.AlwaysAcceptServerCertificate = true;
         c.Open(user, pwd);
         Assert.IsTrue(c.IsConnected);
         string r = c.GetSystemType();
         Assert.IsNotNull(r);
         Assert.IsNotEmpty(r);
         Console.WriteLine("RESULT: " + r);
     }
 }
Ejemplo n.º 23
0
 public void TestRepeatedOpen(string host, int port, FtpsSecurityProtocol protocol,
                              string user, string pwd, string server, int connections)
 {
     for (int i = 0; i < connections; i++)
     {
         using (FtpsClient c = new FtpsClient(host, port, protocol))
         {
             Debug.WriteLine("********** START **********");
             c.AlwaysAcceptServerCertificate = true;
             c.Open(user, pwd);
             Assert.IsTrue(c.IsConnected);
             Debug.WriteLine("********** PASSED **********");
         }
     }
 }
Ejemplo n.º 24
0
        public void TestPutFileUnique(string host, int port, FtpsSecurityProtocol protocol,
                                      string user, string pwd, string server)
        {
            using (FtpsClient c = new FtpsClient(host, port, protocol))
            {
                c.AlwaysAcceptServerCertificate = true;
                c.Open(user, pwd);
                Assert.IsTrue(c.IsConnected);

                byte[]       bytes = new byte[1024];
                MemoryStream m     = new MemoryStream(bytes);
                string       fname = c.PutFileUnique(m);
                c.DeleteFile(fname);
            }
        }
Ejemplo n.º 25
0
        public void TestChangeDirectory(string host, int port, FtpsSecurityProtocol protocol,
                                        string user, string pwd)
        {
            using (FtpsClient c = new FtpsClient(host, port, protocol))
            {
                c.NetworkProtocol = NETWORK_PROTOCOL;
                c.AlwaysAcceptServerCertificate = true;
                c.Open(user, pwd);
                Assert.IsTrue(c.IsConnected);
                string dir1 = Guid.NewGuid().ToString();
                string dir2 = Guid.NewGuid().ToString();
                string dir3 = Guid.NewGuid().ToString();
                string dir4 = Guid.NewGuid().ToString();

                // create the directories and change into them
                c.MakeDirectory(dir1);
                c.ChangeDirectory(dir1);
                c.MakeDirectory(dir2);
                c.ChangeDirectory(dir2);
                c.MakeDirectory(dir3);
                c.ChangeDirectory(dir3);
                c.MakeDirectory(dir4);

                c.ChangeDirectoryUp();
                c.ChangeDirectoryUp();
                c.ChangeDirectoryUp();
                c.ChangeDirectoryUp();

                // try changing directory using a full path which does
                // not work for all FTP servers
                c.ChangeDirectory(String.Format("{0}/{1}/{2}/{3}", dir1, dir2, dir3, dir4));
                c.ChangeDirectory("//");

                // try changing directory using multipath command which should work
                // for all FTP servers
                c.ChangeDirectoryMultiPath(String.Format("{0}/{1}/{2}/{3}", dir1, dir2, dir3, dir4));
                c.ChangeDirectoryUp();

                // delete the temporary directories
                c.DeleteDirectory(dir4);
                c.ChangeDirectoryUp();
                c.DeleteDirectory(dir3);
                c.ChangeDirectoryUp();
                c.DeleteDirectory(dir2);
                c.ChangeDirectoryUp();
                c.DeleteDirectory(dir1);
            }
        }
Ejemplo n.º 26
0
        public void TestPutFileCreateNew(string host, int port, FtpsSecurityProtocol protocol,
                                         string user, string pwd, int fileSize)
        {
            using (FtpsClient c = new FtpsClient(host, port, protocol))
            {
                c.NetworkProtocol = NETWORK_PROTOCOL;
                c.AlwaysAcceptServerCertificate = true;
                c.Open(user, pwd);
                Assert.IsTrue(c.IsConnected);

                MemoryStream m1    = GetMemoryStreamRandom(fileSize);
                string       fname = GetGuidString();
                bool         fail  = false;

                try
                {
                    c.PutFile(m1, fname, FileAction.Create);

                    Assert.IsTrue(c.Exists(fname));

                    MemoryStream o1 = new MemoryStream();
                    c.GetFile(fname, o1, false);
                    o1.Position = 0;

                    //compare bytes to verify
                    Assert.IsTrue(Compare(m1, o1));

                    c.PutFile(m1, fname, FileAction.Create);

                    try
                    {
                        // put a second time which should cause an exception to be thrown
                        // since there is an existing file already on the server
                        c.PutFile(m1, fname, FileAction.CreateNew);
                    }
                    catch (FtpsDataTransferException)
                    {
                        fail = true;
                    }

                    Assert.IsTrue(fail);
                }
                finally
                {
                    c.DeleteFile(fname);
                }
            }
        }
Ejemplo n.º 27
0
 /// <summary>
 ///     Connects to a FTP server trying every methods
 /// </summary>
 private void ConnectOrReconnectFtp(FtpsClient ftp, string userName, string passWord, string host, int port)
 {
     // try to connect!
     if (!ftp.Connected)
     {
         ConnectFtp(ftp, userName, passWord, host, port);
     }
     else
     {
         try {
             ftp.GetCurrentDirectory();
         } catch (Exception) {
             ConnectFtp(ftp, userName, passWord, host, port);
         }
     }
 }
Ejemplo n.º 28
0
        private static void DoPutUniqueFile(FtpsClient client)
        {
            var localPathName = _commandArguments[0];

            if (_commandArguments.Count > 1)
            {
                var remoteDirName = NormalizeRemotePath(_commandArguments[1]);
                client.SetCurrentDirectory(remoteDirName);
            }

            string remoteFileName;

            client.PutUniqueFile(localPathName, out remoteFileName, TransferCallback);

            Console.WriteLine("Unique file uploaded. File name: \"" + remoteFileName + "\"");
        }
Ejemplo n.º 29
0
        public void TestGetFile(string host, int port, FtpsSecurityProtocol protocol, string user, string pwd)
        {
            using (FtpsClient c = new FtpsClient(host, port, protocol))
            {
                c.NetworkProtocol = NETWORK_PROTOCOL;
                c.AlwaysAcceptServerCertificate = true;
                c.TcpBufferSize = 80000;
                c.Open(user, pwd);

                Assert.IsTrue(c.IsConnected);

                MemoryStream msfile = new MemoryStream();
                c.GetFile("/gcrypt/gnupg/index.html", msfile, false);

                Assert.IsTrue(msfile.Length != 0);
            }
        }
Ejemplo n.º 30
0
        public void TestPutFileCreate(string host, int port, FtpsSecurityProtocol protocol,
                                      string user, string pwd, int fileSize)
        {
            using (FtpsClient c = new FtpsClient(host, port, protocol))
            {
                c.NetworkProtocol = NETWORK_PROTOCOL;
                c.AlwaysAcceptServerCertificate = true;
                c.NetworkProtocol = NetworkVersion.IPv6;
                c.Open(user, pwd);
                Assert.IsTrue(c.IsConnected);

                MemoryStream m1 = GetMemoryStreamRandom(fileSize);

                string fname = GetGuidString();

                try
                {
                    c.PutFile(m1, fname, FileAction.Create);

                    Assert.IsTrue(c.Exists(fname));

                    MemoryStream o1 = new MemoryStream();
                    c.GetFile(fname, o1, false);
                    o1.Position = 0;

                    // compare bytes to verify
                    Assert.IsTrue(Compare(m1, o1));

                    // put a second file which should overwrite the first file
                    c.PutFile(m1, fname, FileAction.Create);

                    Assert.IsTrue(c.Exists(fname));

                    MemoryStream o2 = new MemoryStream();
                    c.GetFile(fname, o2, false);
                    o1.Position = 0;

                    // compare bytes to verify
                    Assert.IsTrue(Compare(m1, o2));
                }
                finally
                {
                    c.DeleteFile(fname);
                }
            }
        }