Esempio n. 1
0
        protected override string OnProcess(string sMessage)
        {
            sMessage = sMessage.Trim();

            // Get the dir to list
            string targetToList = GetPath(sMessage);

            // checks the dir name
            if (!FileNameHelpers.IsValid(targetToList))
            {
                return(GetMessage(501, $"\"{sMessage}\" is not a valid directory name"));
            }

            // specify the directory tag
            targetToList = FileNameHelpers.AppendDirTag(targetToList);

            bool targetIsDir = ConnectionObject.FileSystemObject.DirectoryExists(targetToList);

            if (!targetIsDir)
            {
                return(GetMessage(550, $"Directory \"{targetToList}\" not exists"));
            }

            #region Generate response

            StringBuilder response = new StringBuilder();

            string[] files       = ConnectionObject.FileSystemObject.GetFiles(targetToList);
            string[] directories = ConnectionObject.FileSystemObject.GetDirectories(targetToList);

            if (files != null && files.Any())
            {
                foreach (var file in files)
                {
                    var fileInfo = ConnectionObject.FileSystemObject.GetFileInfo(file);

                    response.Append(GenerateEntry(fileInfo));

                    response.Append("\r\n");
                }
            }

            if (directories != null && directories.Any())
            {
                foreach (var dir in directories)
                {
                    var dirInfo = ConnectionObject.FileSystemObject.GetDirectoryInfo(dir);

                    response.Append(GenerateEntry(dirInfo));

                    response.Append("\r\n");
                }
            }

            #endregion

            #region Write response

            var socketData = new FtpDataSocket(ConnectionObject);

            if (!socketData.Loaded)
            {
                return(GetMessage(425, "Unable to establish the data connection"));
            }

            SocketHelpers.Send(ConnectionObject.Socket, $"150 {ConnectionObject.DataType} Opening data connection for MLSD {targetToList}\r\n", ConnectionObject.Encoding);

            try
            {
                // ToDo, send response according to ConnectionObject.DataType, i.e., Ascii or Binary
                socketData.Send(response.ToString(), ConnectionObject.Encoding);
            }
            finally
            {
                socketData.Close();
            }

            #endregion

            return(GetMessage(226, "MLSD successful"));
        }
Esempio n. 2
0
        protected override string OnProcess(string sMessage)
        {
            sMessage = sMessage.Trim();

            string[] asFiles       = null;
            string[] asDirectories = null;

            // Get the file/dir to list
            string targetToList = GetPath(sMessage);

            // checks the file/dir name
            if (!FileNameHelpers.IsValid(targetToList))
            {
                return(GetMessage(501, string.Format("\"{0}\" is not a valid file/directory name", sMessage)));
            }

            // two vars indicating different list results
            bool targetIsFile = false;
            bool targetIsDir  = false;

            // targetToList ends with '/', must be a directory
            if (targetToList.EndsWith(@"/"))
            {
                targetIsFile = false;
                if (ConnectionObject.FileSystemObject.DirectoryExists(targetToList))
                {
                    targetIsDir = true;
                }
            }
            else
            {
                // check whether the target to list is a directory
                if (ConnectionObject.FileSystemObject.DirectoryExists(FileNameHelpers.AppendDirTag(targetToList)))
                {
                    targetIsDir = true;
                }
                // check whether the target to list is a file
                if (ConnectionObject.FileSystemObject.FileExists(targetToList))
                {
                    targetIsFile = true;
                }
            }

            if (targetIsFile)
            {
                asFiles = new string[1] {
                    targetToList
                };
                if (targetIsDir)
                {
                    asDirectories = new string[1] {
                        FileNameHelpers.AppendDirTag(targetToList)
                    }
                }
                ;
            }
            // list a directory
            else if (targetIsDir)
            {
                targetToList  = FileNameHelpers.AppendDirTag(targetToList);
                asFiles       = ConnectionObject.FileSystemObject.GetFiles(targetToList);
                asDirectories = ConnectionObject.FileSystemObject.GetDirectories(targetToList);
            }
            else
            {
                return(GetMessage(550, string.Format("\"{0}\" not exists", sMessage)));
            }

            var socketData = new FtpDataSocket(ConnectionObject);

            if (!socketData.Loaded)
            {
                return(GetMessage(425, "Unable to establish the data connection"));
            }

            // prepare to write response to data channel
            SocketHelpers.Send(ConnectionObject.Socket, string.Format("150 Opening data connection for {0}\r\n", Command), ConnectionObject.Encoding);

            // generate the response
            string sFileList = BuildReply(asFiles, asDirectories);

            // ToDo, send response according to ConnectionObject.DataType, i.e., Ascii or Binary
            socketData.Send(sFileList, Encoding.UTF8);
            socketData.Close();

            return(GetMessage(226, string.Format("{0} successful.", Command)));
        }
        protected override string OnProcess(string sMessage)
        {
            sMessage = sMessage.Trim();
            if (sMessage == "")
            {
                return(GetMessage(501, string.Format("{0} needs a parameter", Command)));
            }

            string sFilePath = GetPath(sMessage);

            if (!ConnectionObject.FileSystemObject.FileExists(sFilePath))
            {
                return(GetMessage(550, string.Format("File \"{0}\" doesn't exist", sMessage)));
            }

            var socketData = new FtpDataSocket(ConnectionObject);

            if (!socketData.Loaded)
            {
                return(GetMessage(425, "Unable to establish the data connection"));
            }

            SocketHelpers.Send(ConnectionObject.Socket, "150 Starting data transfer, please wait...\r\n", ConnectionObject.Encoding);

            IFile file = ConnectionObject.FileSystemObject.OpenFile(sFilePath, false);

            if (file == null)
            {
                return(GetMessage(550, "Couldn't open file"));
            }

            // TYPE I, default
            if (ConnectionObject.DataType == DataType.Image)
            {
                var abBuffer = new byte[m_nBufferSize];

                int nRead = file.Read(abBuffer, m_nBufferSize);

                while (nRead > 0 && socketData.Send(abBuffer, nRead))
                {
                    nRead = file.Read(abBuffer, m_nBufferSize);
                }
            }
            // TYPE A
            else if (ConnectionObject.DataType == DataType.Ascii)
            {
                int writeSize = SocketHelpers.CopyStreamAscii(file.BlobStream, socketData.Socket.GetStream(), m_nBufferSize);
                FtpServerMessageHandler.SendMessage(ConnectionObject.Id, string.Format("Use ascii type success, write {0} chars!", writeSize));
            }
            else // mustn't reach
            {
                file.Close();
                socketData.Close();
                return(GetMessage(451, "Error in transfer data: invalid data type."));
            }

            file.Close();
            socketData.Close();

            return(GetMessage(226, "File download succeeded."));
        }
Esempio n. 4
0
        protected override string OnProcess(string sMessage)
        {
            sMessage = sMessage.Trim();
            if (sMessage == "")
            {
                return(GetMessage(501, $"{Command} needs a parameter"));
            }

            string sFilePath = GetPath(sMessage);

            Trace.TraceInformation($"RETR {sFilePath} - BEGIN");
            Stopwatch sw = new Stopwatch();

            sw.Start();

            if (!ConnectionObject.FileSystemObject.FileExists(sFilePath))
            {
                FtpServer.LogWrite(this, sMessage, 550, sw.ElapsedMilliseconds);
                return(GetMessage(550, $"File \"{sMessage}\" doesn't exist"));
            }

            var   socketData = new FtpDataSocket(ConnectionObject);
            IFile file       = null;

            try
            {
                if (!socketData.Loaded)
                {
                    FtpServer.LogWrite(this, sMessage, 425, sw.ElapsedMilliseconds);
                    return(GetMessage(425, "Unable to establish the data connection"));
                }

                SocketHelpers.Send(ConnectionObject.Socket, "150 Starting data transfer, please wait...\r\n",
                                   ConnectionObject.Encoding);

                file = ConnectionObject.FileSystemObject.OpenFile(sFilePath, false);
                if (file == null)
                {
                    return(GetMessage(550, "Couldn't open file"));
                }

                // TYPE I, default
                if (ConnectionObject.DataType == DataType.Image)
                {
                    var abBuffer = new byte[m_nBufferSize];

                    int nRead = file.Read(abBuffer, m_nBufferSize);

                    while (nRead > 0 && socketData.Send(abBuffer, nRead))
                    {
                        nRead = file.Read(abBuffer, m_nBufferSize);
                    }
                }
                // TYPE A
                else if (ConnectionObject.DataType == DataType.Ascii)
                {
                    int writeSize = SocketHelpers.CopyStreamAscii(file.BlobStream, socketData.Socket.GetStream(),
                                                                  m_nBufferSize);
                    FtpServerMessageHandler.SendMessage(ConnectionObject.Id,
                                                        $"Use ascii type success, write {writeSize} chars!");
                }
                else // mustn't reach
                {
                    file.Close();
                    socketData.Close();
                    FtpServer.LogWrite(this, sMessage, 451, sw.ElapsedMilliseconds);
                    return(GetMessage(451, "Error in transfer data: invalid data type."));
                }



                sw.Stop();
                Trace.TraceInformation($"RETR {sFilePath} - END, Time {sw.ElapsedMilliseconds} ms");

                FtpServer.LogWrite(this, sMessage, 226, sw.ElapsedMilliseconds);
                return(GetMessage(226, $"File download succeeded. Time {sw.ElapsedMilliseconds} ms"));
            }
            finally
            {
                file?.Close();
                socketData.Close();
            }
        }