Ejemplo n.º 1
0
        /// <summary>
        /// Detect the FTP Server based on the welcome message sent by the server after getting the 220 connection command.
        /// Its the primary method.
        /// </summary>
        public static FtpServer DetectFtpServer(FtpClient client, FtpReply HandshakeReply)
        {
            var serverType = client.ServerType;

            if (HandshakeReply.Success && (HandshakeReply.Message != null || HandshakeReply.InfoMessages != null))
            {
                var message = (HandshakeReply.Message ?? "") + (HandshakeReply.InfoMessages ?? "");

                // try to detect any of the servers
                foreach (var server in AllServers)
                {
                    if (server.DetectByWelcome(message))
                    {
                        serverType = server.ToEnum();
                        break;
                    }
                }

                // trace it
                if (serverType != FtpServer.Unknown)
                {
                    client.LogLine(FtpTraceLevel.Info, "Status:   Detected FTP server: " + serverType.ToString());
                }
            }

            return(serverType);
        }
Ejemplo n.º 2
0
        private FtpReply GetProxyReply(FtpSocketStream stream)
        {
            FtpReply reply = new FtpReply();
            string   buf;

            lock (Lock) {
                if (!IsConnected)
                {
                    throw new InvalidOperationException("No connection to the server has been established.");
                }

                stream.ReadTimeout = ReadTimeout;
                while ((buf = stream.ReadLine(Encoding)) != null)
                {
                    Match m;

                    FtpTrace.WriteLine(buf);

                    if ((m = Regex.Match(buf, @"^HTTP/.*\s(?<code>[0-9]{3}) (?<message>.*)$")).Success)
                    {
                        reply.Code    = m.Groups["code"].Value;
                        reply.Message = m.Groups["message"].Value;
                        break;
                    }

                    reply.InfoMessages += string.Format("{0}\n", buf);
                }
            }

            return(reply);
        }
Ejemplo n.º 3
0
        public override void SetModifiedTime(SyncQueueItem i, DateTime time)
        {
            string command;
            var    reply         = new FtpReply();
            var    timeFormatted = time.ToString("yyyyMMddHHMMss");

            if (_ftpc.Capabilities.HasFlag(FtpCapability.MFF))
            {
                command = string.Format("MFF Modify={0}; {1}", timeFormatted, i.CommonPath);
                reply   = _ftpc.Execute(command);
            }
            if (!reply.Success && _ftpc.Capabilities.HasFlag(FtpCapability.MFMT))
            {
                command = string.Format("MFMT {0} {1}", timeFormatted, i.CommonPath);
                reply   = _ftpc.Execute(command);
            }
            if (!reply.Success)
            {
                command = string.Format("SITE UTIME {0} {1}", timeFormatted, i.CommonPath);
                reply   = _ftpc.Execute(command);
            }

            if (!reply.Success)
            {
                Log.Write(l.Error, "SetModTime failed, file: {0} msg: {1}", i.CommonPath, reply.ErrorMessage);
            }
        }
Ejemplo n.º 4
0
 private static void User()
 {
     if ((client == null) || !client.IsConnected)
     {
         Console.WriteLine("not connected.");
     }
     else if (commandParams.Count == 0)
     {
         Console.WriteLine("Usage:user naem [password]");
     }
     else
     {
         client.Credentials = new NetworkCredential(commandParams[0], (commandParams.Count == 1) ? "" : commandParams[1]);
         FtpReply reply = client.Execute("User " + commandParams[0]);
         if ((commandParams.Count == 1) && !reply.Success)
         {
             Console.WriteLine(reply.Code + " " + reply.Message);
         }
         if (commandParams.Count > 1)
         {
             reply = client.Execute("PASS " + commandParams[1]);
         }
         Console.WriteLine(reply.Code + " " + reply.Message);
     }
 }
        private static async Task <string> GetFtpServerInfoInnerAsync(WebDirectory webDirectory, string username, string password, CancellationToken cancellationToken)
        {
            if (string.IsNullOrWhiteSpace(username) && string.IsNullOrWhiteSpace(password))
            {
                GetCredentials(webDirectory, out string username1, out string password1);

                username = username1;
                password = password1;
            }

            // Try multiple possible options, the AutoDetect and AutoConnectAsync are not working (reliably)
            foreach (FtpEncryptionMode ftpEncryptionMode in Enum.GetValues(typeof(FtpEncryptionMode)))
            {
                try
                {
                    Logger.Warn($"Try FTP(S) connection with EncryptionMode {ftpEncryptionMode}");

                    FtpClient ftpClient = new FtpClient(webDirectory.Uri.Host, webDirectory.Uri.Port, username, password)
                    {
                        EncryptionMode = ftpEncryptionMode
                    };

                    ftpClient.ValidateAnyCertificate = true;
                    await ftpClient.ConnectAsync(cancellationToken);

                    OpenDirectoryIndexer.Session.Parameters[Constants.Parameters_FtpEncryptionMode] = ftpEncryptionMode.ToString();

                    FtpReply connectReply = ftpClient.LastReply;

                    FtpReply helpReply = await ftpClient.ExecuteAsync("HELP", cancellationToken);

                    FtpReply statusReply = await ftpClient.ExecuteAsync("STAT", cancellationToken);

                    FtpReply systemReply = await ftpClient.ExecuteAsync("SYST", cancellationToken);

                    await ftpClient.DisconnectAsync(cancellationToken);

                    return
                        ($"Connect Respones: {connectReply.InfoMessages}{Environment.NewLine}" +
                         $"ServerType: {ftpClient.ServerType}{Environment.NewLine}" +
                         $"Help response: {helpReply.InfoMessages}{Environment.NewLine}" +
                         $"Status response: {statusReply.InfoMessages}{Environment.NewLine}" +
                         $"System response: {systemReply.InfoMessages}{Environment.NewLine}");
                }
                catch (Exception ex)
                {
                    if (ex is FtpCommandException ftpCommandException)
                    {
                        if (IsMaxThreads(ftpCommandException))
                        {
                            return(null);
                        }
                    }

                    Logger.Error($"FTP EncryptionMode {ftpEncryptionMode} failed: {ex.Message}");
                }
            }

            return(null);
        }
        public void DownloadFile(string sourceFilePath, System.IO.Stream destinationStream)
        {
            FtpClient conn = null;

            try
            {
                conn = OpenClient(sourceFilePath);

                sourceFilePath = AdjustPath(conn.Host, sourceFilePath);

                using (System.IO.Stream sStream = conn.OpenRead(sourceFilePath, FtpDataType.Binary))
                {
                    sStream.CopyTo(destinationStream);
                }

                FtpReply reply = conn.GetReply();

                if (!reply.Success)
                {
                    throw new Exception("There was an error reading from the FTP server: " + reply.Message);
                }
            }
            catch (Exception ex)
            {
                DisposeClient(conn);
                conn = null;

                throw ex;
            }
            finally
            {
                RecycleClient(conn);
            }
        }
        public void UploadFile(System.IO.Stream sourceStream, string destinationFilePath)
        {
            FtpClient conn = null;

            try
            {
                conn = OpenClient(destinationFilePath);

                destinationFilePath = AdjustPath(conn.Host, destinationFilePath);

                using (System.IO.Stream dStream = conn.OpenWrite(destinationFilePath, FtpDataType.Binary))
                {
                    sourceStream.CopyTo(dStream);
                }

                FtpReply reply = conn.GetReply();

                if (!reply.Success)
                {
                    throw new Exception("There was an error writing to the FTP server: " + reply.Message);
                }
            }
            catch (Exception ex)
            {
                DisposeClient(conn);
                conn = null;

                throw ex;
            }
            finally
            {
                RecycleClient(conn);
            }
        }
Ejemplo n.º 8
0
        //[Fact]
        public async Task StreamResponsesAsync()
        {
            using (FtpClient cl = NewFtpClient())
            {
                cl.EncryptionMode       = FtpEncryptionMode.None;
                cl.ValidateCertificate += OnValidateCertificate;

                using (FtpDataStream s = (FtpDataStream)await cl.OpenWriteAsync("test.txt"))
                {
                    FtpReply r = s.CommandStatus;

                    FtpTrace.WriteLine("");
                    FtpTrace.WriteLine("Response to STOR:");
                    FtpTrace.WriteLine("Code: " + r.Code);
                    FtpTrace.WriteLine("Message: " + r.Message);
                    FtpTrace.WriteLine("Informational: " + r.InfoMessages);

                    r = s.Close();
                    FtpTrace.WriteLine("");
                    FtpTrace.WriteLine("Response after close:");
                    FtpTrace.WriteLine("Code: " + r.Code);
                    FtpTrace.WriteLine("Message: " + r.Message);
                    FtpTrace.WriteLine("Informational: " + r.InfoMessages);
                }
            }
        }
Ejemplo n.º 9
0
        /// <summary>
        ///		Envía el comando LIST al servidor
        /// </summary>
        internal override FtpReply Send()
        {
            FtpReply reply = new FtpReply(0);

            // Asigna la protección
            Connection.CheckProtection(Parameters.FtpClientParameters.FtpProtection.ControlChannel);
            // Obtiene los archivos
            using (Network.FtpStream data = new Network.FtpStreamFactory().OpenDataStream(Connection, FtpClient.FtpTransferMode.Binary))
            {
                // Envía el comando
                reply = Expect(SendCommand("MLSD", Connection.Server.ServerPlatform.EscapePath(Path.ToString())),
                               125, 150, 425);
                // Si ha contestado correctamente, se interpretan los archivos del directorio
                if (reply.IsSuccess)
                {
                    using (StreamReader reader = new StreamReader(data.Validated(), Connection.Client.ClientParameters.Encoding))
                    {
                        string line;

                        // Lee las líneas
                        while ((line = reader.ReadLine()) != null)
                        {
                            AddFileEntry(line);
                        }
                    }
                }
                else
                {
                    data.Abort();
                    ThrowException(reply);
                }
            }
            // Devuelve la respuesta
            return(reply);
        }
Ejemplo n.º 10
0
        static void StreamResponses()
        {
            using (FtpClient cl = new FtpClient()) {
                cl.Credentials          = new NetworkCredential(m_user, m_pass);
                cl.Host                 = m_host;
                cl.EncryptionMode       = FtpEncryptionMode.None;
                cl.ValidateCertificate += new FtpSslValidation(delegate(FtpClient control, FtpSslValidationEventArgs e) {
                    e.Accept = true;
                });

                using (FtpDataStream s = (FtpDataStream)cl.OpenWrite("test.txt")) {
                    FtpReply r = s.CommandStatus;

                    Console.WriteLine();
                    Console.WriteLine("Response to STOR:");
                    Console.WriteLine("Code: {0}", r.Code);
                    Console.WriteLine("Message: {0}", r.Message);
                    Console.WriteLine("Informational: {0}", r.InfoMessages);

                    r = s.Close();
                    Console.WriteLine();
                    Console.WriteLine("Response after close:");
                    Console.WriteLine("Code: {0}", r.Code);
                    Console.WriteLine("Message: {0}", r.Message);
                    Console.WriteLine("Informational: {0}", r.InfoMessages);
                }
            }
        }
Ejemplo n.º 11
0
        public static void GenNewScript()
        {
            FtpClient Client = new FtpClient("192.168.2.110");

            Client.Credentials    = new NetworkCredential("astronet", "P@ssw0rd");
            Client.ConnectTimeout = 1000;
            Client.Connect();

            //-----------------------------------------------------------------------------------Test Json Write-----------------------------------------------------------------------------------------
            List <ScriptStructureNew> ScriptList = new List <ScriptStructureNew>();

            int    station_random = new Random().Next(1, 4);
            String stationName    = null;

            /*
             * if(station_random == 1)
             * {
             *  stationName = "AIRFORCE";
             *  ScriptList.Add(new ScriptStructureNew(DateTime.UtcNow.Ticks.ToString(), DateTime.UtcNow.Ticks.ToString(), "30", STATIONNAME.AIRFORCE.ToString(), DEVICENAME.AIRFORCE_TS700MM.ToString(), TS700MMSET.TS700MM_MOUNT_SETENABLE.ToString(), new List<String> { }, SCRIPTSTATE.WAITINGSERVER.ToString(), DateTime.UtcNow.AddMinutes(-2).Ticks.ToString(), DateTime.UtcNow.AddMinutes(+2).Ticks.ToString()));
             *  ScriptList.Add(new ScriptStructureNew(DateTime.UtcNow.Ticks.ToString(), DateTime.UtcNow.Ticks.ToString(), "30", STATIONNAME.AIRFORCE.ToString(), DEVICENAME.AIRFORCE_IMAGING.ToString(), IMAGINGSET.IMAGING_CCD_EXPOSE.ToString(), new List<String> { "FileName", "12.0", "true" }, SCRIPTSTATE.WAITINGSERVER.ToString(), DateTime.UtcNow.AddMinutes(-2).Ticks.ToString(), DateTime.UtcNow.AddMinutes(+2).Ticks.ToString()));
             * }
             * else if(station_random == 2)
             * {
             *  stationName = "ASTROPARK";
             *  ScriptList.Add(new ScriptStructureNew(DateTime.UtcNow.Ticks.ToString(), DateTime.UtcNow.Ticks.ToString(), "30", STATIONNAME.ASTROPARK.ToString(), DEVICENAME.ASTROPARK_TS700MM.ToString(), TS700MMSET.TS700MM_MOUNT_SETENABLE.ToString(), new List<String> { }, SCRIPTSTATE.WAITINGSERVER.ToString(), DateTime.UtcNow.AddMinutes(-2).Ticks.ToString(), DateTime.UtcNow.AddMinutes(+2).Ticks.ToString()));
             *  ScriptList.Add(new ScriptStructureNew(DateTime.UtcNow.Ticks.ToString(), DateTime.UtcNow.Ticks.ToString(), "30", STATIONNAME.ASTROPARK.ToString(), DEVICENAME.ASTROPARK_IMAGING.ToString(), IMAGINGSET.IMAGING_CCD_EXPOSE.ToString(), new List<String> { "FileName", "12.0", "true" }, SCRIPTSTATE.WAITINGSERVER.ToString(), DateTime.UtcNow.AddMinutes(-2).Ticks.ToString(), DateTime.UtcNow.AddMinutes(+2).Ticks.ToString()));
             * }
             * else if (station_random == 3)
             * {
             *  stationName = "USA";
             *  ScriptList.Add(new ScriptStructureNew(DateTime.UtcNow.Ticks.ToString(), DateTime.UtcNow.Ticks.ToString(), "30", STATIONNAME.USA.ToString(), DEVICENAME.USA_TS700MM.ToString(), TS700MMSET.TS700MM_MOUNT_SETENABLE.ToString(), new List<String> { }, SCRIPTSTATE.WAITINGSERVER.ToString(), DateTime.UtcNow.AddMinutes(-2).Ticks.ToString(), DateTime.UtcNow.AddMinutes(+2).Ticks.ToString()));
             *  ScriptList.Add(new ScriptStructureNew(DateTime.UtcNow.Ticks.ToString(), DateTime.UtcNow.Ticks.ToString(), "30", STATIONNAME.USA.ToString(), DEVICENAME.USA_IMAGING.ToString(), IMAGINGSET.IMAGING_CCD_EXPOSE.ToString(), new List<String> { "FileName", "12.0", "true" }, SCRIPTSTATE.WAITINGSERVER.ToString(), DateTime.UtcNow.AddMinutes(-2).Ticks.ToString(), DateTime.UtcNow.AddMinutes(+2).Ticks.ToString()));
             * }
             */
            stationName = "ASTROPARK";
            ScriptList.Add(new ScriptStructureNew(DateTime.UtcNow.Ticks.ToString(), DateTime.UtcNow.Ticks.ToString(), DateTime.UtcNow.AddMilliseconds(+1).Ticks.ToString(), "30", stationName, stationName + "_" + "TS700MM", TS700MMSET.TS700MM_MOUNT_SETENABLE.ToString(), new List <String> {
            }, SCRIPTSTATE.WAITINGSERVER.ToString(), DateTime.UtcNow.AddMinutes(+1).Ticks.ToString(), DateTime.UtcNow.AddMinutes(+2).Ticks.ToString(), "", "", "CHAMP"));
            ScriptList.Add(new ScriptStructureNew(DateTime.UtcNow.Ticks.ToString(), DateTime.UtcNow.Ticks.ToString(), DateTime.UtcNow.AddMilliseconds(+2).Ticks.ToString(), "30", stationName, stationName + "_" + "TS700MM", TS700MMSET.TS700MM_MOUNT_SLEWRADEC.ToString(), new List <String> {
                "4 55 23.32", "+14 32 54.12"
            }, SCRIPTSTATE.WAITINGSERVER.ToString(), DateTime.UtcNow.AddMinutes(+1).Ticks.ToString(), DateTime.UtcNow.AddMinutes(+2).Ticks.ToString(), "", "", "CHAMP"));
            ScriptList.Add(new ScriptStructureNew(DateTime.UtcNow.Ticks.ToString(), DateTime.UtcNow.Ticks.ToString(), DateTime.UtcNow.AddMilliseconds(+3).Ticks.ToString(), "30", stationName, stationName + "_" + "IMAGING", IMAGINGSET.IMAGING_CCD_EXPOSE.ToString(), new List <String> {
                "FileName", "1.0", "true", "TigerStar"
            }, SCRIPTSTATE.WAITINGSERVER.ToString(), DateTime.UtcNow.AddMinutes(+1).Ticks.ToString(), DateTime.UtcNow.AddMinutes(+2).Ticks.ToString(), "", "", "CHAMP"));

            String DataJsonTest = JsonConvert.SerializeObject(ScriptList);

            var remoteFileStream = Client.OpenWrite(@"\Script\" + stationName + "\\" + DateTime.UtcNow.Ticks.ToString() + ".txt");

            Byte[] DataByte = System.Text.Encoding.UTF8.GetBytes(DataJsonTest);
            remoteFileStream.Write(DataByte, 0, DataByte.Count());
            remoteFileStream.Close();
            FtpReply status = Client.GetReply();

            Client.Disconnect();
            //-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
        }
Ejemplo n.º 12
0
 public void ReadMultilineTest()
 {
     var ftpReply = new FtpReply();
     Assert.IsTrue(ftpReply.ParseLine("220---------- Welcome to Pure-FTPd [privsep] [TLS] ----------"));
     Assert.IsTrue(ftpReply.ParseLine("220-You are user number 1 of 1000 allowed."));
     Assert.IsTrue(ftpReply.ParseLine("220-Local time is now 01:29. Server port: 21."));
     Assert.IsTrue(ftpReply.ParseLine("220-This is a private system - No anonymous login"));
     Assert.IsTrue(ftpReply.ParseLine("220-IPv6 connections are also welcome on this server."));
     Assert.IsFalse(ftpReply.ParseLine("220 You will be disconnected after 15 minutes of inactivity."));
 }
Ejemplo n.º 13
0
        //Returns true if successful
        public static bool RemoteCommand(string Command, bool ShowError = true)
        {
            FtpReply reply = Client.Execute("RCMD " + Command);

            if (ShowError)
            {
                HandleError(reply.Code, reply.ErrorMessage);
            }

            return(reply.Success);
        }
Ejemplo n.º 14
0
        public void ReadMultilineTest()
        {
            var ftpReply = new FtpReply();

            Assert.IsTrue(ftpReply.ParseLine("220---------- Welcome to Pure-FTPd [privsep] [TLS] ----------"));
            Assert.IsTrue(ftpReply.ParseLine("220-You are user number 1 of 1000 allowed."));
            Assert.IsTrue(ftpReply.ParseLine("220-Local time is now 01:29. Server port: 21."));
            Assert.IsTrue(ftpReply.ParseLine("220-This is a private system - No anonymous login"));
            Assert.IsTrue(ftpReply.ParseLine("220-IPv6 connections are also welcome on this server."));
            Assert.IsFalse(ftpReply.ParseLine("220 You will be disconnected after 15 minutes of inactivity."));
        }
Ejemplo n.º 15
0
 private static void Binary()
 {
     if ((client == null) || !client.IsConnected)
     {
         Console.WriteLine("not connected.");
     }
     else
     {
         FtpReply reply = client.Execute("TYPE I");
         Console.WriteLine(reply.Code + " " + reply.Message);
     }
 }
Ejemplo n.º 16
0
 /// <summary>
 ///		Lanza la excepción adecuada para una respuesta
 /// </summary>
 protected void ThrowException(FtpReply reply)
 {
     if (reply.Class == FtpReply.FtpReplyCodeClass.Filesystem)
     {
         throw new Exceptions.FtpFileException($"File error. Code={reply.Code} ('{reply.Lines[0]}')", reply);
     }
     if (reply.Class == FtpReply.FtpReplyCodeClass.Connections)
     {
         throw new Exceptions.FtpTransportException($"Connection error. Code={reply.Code} ('{reply.Lines[0]}')");
     }
     throw new Exceptions.FtpProtocolException($"Expected other reply than {reply.Code} ('{reply.Lines[0]}')", reply);
 }
Ejemplo n.º 17
0
        /// <summary>
        ///		Envía el comando
        /// </summary>
        internal override FtpReply Send()
        {
            FtpReply reply = Expect(SendCommand("SYST", null), 215);

            // Obtiene el sistema a partir de la respuesta
            if (reply.IsSuccess)
            {
                System = reply.Lines[0];
            }
            // Devuelve la respuesta del servidro
            return(reply);
        }
        /// <summary>
        ///		Obtiene el directorio actual del cliente
        /// </summary>
        internal override FtpReply Send()
        {
            FtpReply reply = Expect(SendCommand("PWD", null), 257, 550);

            // Si ha devuelto al menos una línea, obtiene el directorio de la primera
            if (reply.Lines.Length > 0)
            {
                ActualPath = GetPath(reply.Lines[0]);
            }
            // Devuelve el resultado
            return(reply);
        }
Ejemplo n.º 19
0
 private static void Execute(string command, bool appendParams = false)
 {
     if (CheckClient() && (!appendParams || HasParam("parameter invalidate.")))
     {
         if (appendParams)
         {
             command = command + " " + commandParams.Aggregate <string>((s, s1) => (s + " " + s1));
         }
         FtpReply reply = client.Execute(command);
         Console.WriteLine(reply.Code + " " + reply.Message);
     }
 }
Ejemplo n.º 20
0
Archivo: FTP.cs Proyecto: zz110/ShareX
        private bool UploadData2(Stream localStream, string remotePath)
        {
            bool result;

            using (Stream remoteStream = client.OpenWrite(remotePath))
            {
                result = TransferData(localStream, remoteStream);
            }
            FtpReply ftpReply = client.GetReply();

            return(result && ftpReply.Success);
        }
Ejemplo n.º 21
0
        /// <summary>
        ///		Envía los comandos RNFR y RNTO
        /// </summary>
        internal override FtpReply Send()
        {
            FtpReply reply = Expect(SendCommand("RNFR", Path.ToString()), 350);

            // Si ha podido enviar el comando RNFR, envía el comando RNTO
            if (reply.IsSuccess)
            {
                reply = Expect(SendCommand("RNTO", Target.ToString()), 250);
            }
            // Devuelve la última respuesta
            return(reply);
        }
Ejemplo n.º 22
0
        private bool UploadDataInternal(Stream localStream, string remotePath)
        {
            bool result;

#pragma warning disable 0618
            using (Stream remoteStream = client.OpenWrite(remotePath))
            {
                result = TransferData(localStream, remoteStream);
            }
#pragma warning restore 0618
            FtpReply ftpReply = client.GetReply();
            return(result && ftpReply.Success);
        }
Ejemplo n.º 23
0
        public void Handle(TestMessage.FtpMessage message)
        {
            Console.WriteLine("Message Received With The Following\n\n");

            Console.WriteLine("ID: " + message.ID.ToString());
            Console.WriteLine("Name: " + message.Name);

            FtpReply rep = new FtpReply {
                ID = 500, OtherData = Guid.NewGuid(), IsThisCool = true, Description = "What the?"
            };

            this.Bus.Reply(rep);
        }
Ejemplo n.º 24
0
        private FtpReply GetProxyReply(FtpSocketStream stream)
        {
            var    reply = new FtpReply();
            string buf;

#if !CORE14
            lock (Lock) {
#endif
            if (!IsConnected)
            {
                throw new InvalidOperationException("No connection to the server has been established.");
            }

            stream.ReadTimeout = ReadTimeout;
            while ((buf = stream.ReadLine(Encoding)) != null)
            {
                Match m;

                LogLine(FtpTraceLevel.Info, buf);

                if ((m = Regex.Match(buf, @"^HTTP/.*\s(?<code>[0-9]{3}) (?<message>.*)$")).Success)
                {
                    reply.Code    = m.Groups["code"].Value;
                    reply.Message = m.Groups["message"].Value;
                    break;
                }

                reply.InfoMessages += buf + "\n";
            }

            // fixes #84 (missing bytes when downloading/uploading files through proxy)
            while ((buf = stream.ReadLine(Encoding)) != null)
            {
                LogLine(FtpTraceLevel.Info, buf);

                if (FtpExtensions.IsNullOrWhiteSpace(buf))
                {
                    break;
                }

                reply.InfoMessages += buf + "\n";
            }

#if !CORE14
        }
#endif

            return(reply);
        }
        /// <summary>
        ///		Lee los parámetros de EPSV
        /// </summary>
        private void ReadParametersEpsv(FtpReply reply)
        {
            // Si responde con código 229, recoge el puerto, si no, lo intenta de nuevo (puede haber dado
            // un error de tipo "421 - Demasiadas conexiones abiertas")
            if (reply.Code == 229)
            {
                Match match = EpsvEx.Match(reply.Lines[0]);

                // Obtiene los datos
                Host = Connection.Client.Uri.Host;
                Port = int.Parse(match.Groups["port"].Value);
                // Indica que se ha pasado a modo pasivo
                IsPassive = true;
            }
        }
Ejemplo n.º 26
0
 /// <summary>
 ///		Comprueba los códigos espeficcados en una respuesta
 /// </summary>
 protected FtpReply Expect(FtpReply reply, params int[] codes)
 {
     // Si no existe ninguno de los códigos especificados
     if (!ExistsCode(reply.Code, codes))
     {
         // Cuando se encuentra el código 214 no se trata, puede que se dé una inconsistencia entre
         // comandos y respuestas así que lo mejor es desconectar para reiniciar las parejas comando / respuesta
         if (reply.Code == 214)
         {
             Connection.Disconnect();
         }
         // Lanza la excepción adecuada
         ThrowException(reply);
     }
     // Si todo ha ido bien, devuelve la respuesta leída
     return(reply);
 }
        /// <summary>
        ///		Lee los parámetros de PSV
        /// </summary>
        private void ReadParametersPsv(FtpReply reply)
        {
            // Si se encuentra el código 227, recoge el host y puerto, si no, lo intenta de nuevo (puede haber dado un
            // error del tipo "421 - Demasiadas conexiones abiertas")
            if (reply.Code == 227)
            {
                Match match = PasvEx.Match(reply.Lines[0]);

                // Obtiene el host y el puerto de la expresión regular
                Host = string.Format("{0}.{1}.{2}.{3}",
                                     match.Groups["ip1"], match.Groups["ip2"],
                                     match.Groups["ip3"], match.Groups["ip4"]);
                Port = int.Parse(match.Groups["portHi"].Value) * 256 + int.Parse(match.Groups["portLo"].Value);
                // Indica que se ha pasado a modo pasivo
                IsPassive = true;
            }
        }
Ejemplo n.º 28
0
        /// <summary>
        ///		Envía el comando RMD
        /// </summary>
        internal override FtpReply Send()
        {
            FtpReply reply = null;

            // Borra el directorio o el archivo
            if (IsDirectory ?? true)
            {
                reply = DeletePath();
            }
            // Si era un archivo o hemos intentado borrar el directorio y no hemos podido
            if (reply == null || !reply.IsSuccess)
            {
                reply = DeleteFile();
            }
            // Devuelve la respuesta
            return(reply);
        }
Ejemplo n.º 29
0
        /// <summary> Redefine the first dialog: auth with proxy information </summary>
        protected override void Handshake()
        {
            // Proxy authentication eventually needed.
            if (Proxy.Credentials != null)
            {
                Authenticate(Proxy.Credentials.UserName, Proxy.Credentials.Password);
            }

            // Connection USER@Host means to change user name to add host.
            Credentials.UserName = Credentials.UserName + "@" + Host;

            FtpReply reply = GetReply();

            if (reply.Code == "220")
            {
                FtpTrace.WriteLine(FtpTraceLevel.Info, "Status: Server is ready for the new client");
            }
        }
Ejemplo n.º 30
0
        /// <summary> Redefine the first dialog: auth with proxy information </summary>
        protected override void Handshake()
        {
            // Proxy authentication eventually needed.
            if (Proxy.Credentials != null)
            {
                Authenticate(Proxy.Credentials.UserName, Proxy.Credentials.Password);
            }
            // Connection USER@Host means to change user name to add host.
            Credentials.UserName = Credentials.UserName + "@" + Host;

            FtpReply reply = GetReply();

            if (reply.Code == "220")
            {
                this.LogLine(FtpTraceLevel.Info, "Status: Server is ready for the new client");
            }
            // TO TEST: if we are able to detect the actual FTP server software from this reply
            HandshakeReply = reply;
        }
Ejemplo n.º 31
0
        public void SendResponse(FtpReply ftpReply, bool IsRawReply)
        {
            if (disposedValue)
            {
                return;
            }

            var bytesMessage = ServerEncoding.GetBytes(ftpReply.Message);

            if (IsRawReply)
            {
                ClientCommandStream.Write(bytesMessage, 0, bytesMessage.Length);
                return;
            }

            var reply = ServerEncoding.GetBytes((int)ftpReply.ReplyCode + " " + ftpReply.Message + "\r\n");

            ClientCommandStream.Write(reply, 0, reply.Length);
        }