Beispiel #1
0
        private void DoDataConnectionOperation(IAsyncResult result)
        {
            HandleAsyncResult(result);

            DataConnectionOperation op = result.AsyncState as DataConnectionOperation;

            string response;

            using (NetworkStream dataStream = _dataClient.GetStream())
            {
                response = op.Operation(dataStream, op.Arguments);
            }

            _dataClient.Close();
            _dataClient = null;
            try
            {
                _controlWriter.WriteLine(response);
                _controlWriter.Flush();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
Beispiel #2
0
        /// <summary>
        /// LIST Command - RFC 959 - Section 4.1.3
        /// <para>列出当前目录下的文件详细信息</para>
        /// </summary>
        /// <param name="pathname"></param>
        /// <returns></returns>
        private Response List(string pathname)
        {
            //if (pathname.Trim().ToLower().StartsWith("-a") || pathname.Trim().ToLower().StartsWith("-l"))
            //{
            //    pathname = pathname.Trim().Remove(0, 2).Trim();
            //}

            if (_dataEndpoint == null && _dataConnectionType == DataConnectionType.Active)
            {
                return(GetResponse(FtpResponses.BAD_SEQUENCE));
            }

            if (_virtualFileSystem.ExistsDirectory(pathname) || _virtualFileSystem.ExistsEntity(pathname))
            {
                var state = new DataConnectionOperation {
                    Arguments = pathname, Operation = ListOperation
                };

                SetupDataConnectionOperation(state);

                return(GetResponse(FtpResponses.OPENING_DATA_TRANSFER.SetData(_dataConnectionType, "LIST" + (_protected ? withSsl : ""))));
            }

            return(GetResponse(FtpResponses.FILE_ACTION_NOT_TAKEN));
        }
        private string StoreUnique(string pathname, bool isDirectory)
        {
            int _numberAutoSaved = 0;
            var pathnameTmp      = pathname;

            var vett2 = pathnameTmp.Split('.');

            while (File.Exists(pathnameTmp) || Directory.Exists(pathnameTmp))
            {
                _numberAutoSaved++;
                var extension = (isDirectory) ? "" : vett2[1];
                pathnameTmp = vett2[0] + "(" + _numberAutoSaved + ")" + "." + extension;
            }

            var pathToBePassed = (isDirectory) ? pathname : pathnameTmp;
            var state          = new DataConnectionOperation {
                Arguments = pathToBePassed, Operation = StoreOperation
            };

            SetupDataConnectionOperation(state);

            if (isDirectory)
            {
                ZipFile.ExtractToDirectory(pathname, pathnameTmp);
                File.Delete(pathname);
            }

            return(string.Format("150 Opening {0} mode data transfer for STOU", _dataConnectionType));
        }
Beispiel #4
0
        private string StoreUnique(string pathname, bool isDirectory)
        {
            var state = new DataConnectionOperation {
                Arguments = pathname, Operation = StoreOperation
            };

            SetupDataConnectionOperation(state);



            return(string.Format("150 Opening {0} mode data transfer for STOU", _dataConnectionType));
        }
Beispiel #5
0
 private void SetupDataConnectionOperation(DataConnectionOperation state)
 {
     if (_dataConnectionType == DataConnectionType.Active)
     {
         _dataClient = new TcpClient(_dataEndpoint.AddressFamily);
         _dataClient.BeginConnect(_dataEndpoint.Address, _dataEndpoint.Port, DoDataConnectionOperation, state);
     }
     else
     {
         _passiveListener.BeginAcceptTcpClient(DoDataConnectionOperation, state);
     }
 }
Beispiel #6
0
        private string StoreUnique()
        {
            string pathname = NormalizeFilename(new Guid().ToString());

            var state = new DataConnectionOperation {
                Arguments = pathname, Operation = StoreOperation
            };

            SetupDataConnectionOperation(state);

            return(string.Format("150 Opening {0} mode data transfer for STOU", _dataConnectionType));
        }
Beispiel #7
0
 private void SetupDataConnectionOperation(DataConnectionOperation state)
 {
     if (_dataConnectionType == DataConnectionType.Active)
     {
         _dataClient = new TcpClient(_dataEndpoint.AddressFamily);
         //开始异步连接,当连接成功后调用执行DoDataConnectionOperation,并把state与连接信息包装为IAsyncResult作为参数传给它
         _dataClient.BeginConnect(_dataEndpoint.Address, _dataEndpoint.Port, DoDataConnectionOperation, state);
     }
     else
     {
         _passiveListener.BeginAcceptTcpClient(DoDataConnectionOperation, state);
     }
 }
Beispiel #8
0
        /// <summary>
        /// STOU Command - RFC 959 - Section 4.1.3
        /// <para>(禁止覆盖)上传</para>
        /// </summary>
        /// <param name="pathname"></param>
        /// <returns></returns>
        private Response StoreUnique(string pathname)
        {
            string pre = _virtualFileSystem.GetRealPathOfFile(pathname, false, Guid.NewGuid().ToString());

            //string pathname = NormalizeFilename(new Guid().ToString());

            ConnectionInfo.CurrentFile = pathname;

            var state = new DataConnectionOperation {
                Arguments = pathname, Operation = StoreOperation
            };

            SetupDataConnectionOperation(state);

            return(GetResponse(FtpResponses.OPENING_DATA_TRANSFER.SetData(_dataConnectionType, "STOU" + (_protected ? withSsl : ""))));
        }
Beispiel #9
0
        private string StoreUniqueReal()
        {
            string pathname = NormalizeFilename(_currentDirectory);

            Console.WriteLine(TAG + "STORE UNIQUE REAL ---> " + pathname);



            var state = new DataConnectionOperation {
                Arguments = pathname, Operation = StoreOperation
            };

            SetupDataConnectionOperation(state);

            return(string.Format("150 Opening {0} mode data transfer for STOU", _dataConnectionType));
        }
Beispiel #10
0
        private string List(string pathname)
        {
            pathname = NormalizeFilename(pathname);

            if (pathname == null)
            {
                return("450 Requested file action not taken");
            }
            var state = new DataConnectionOperation {
                Arguments = pathname, Operation = ListOperation
            };

            SetupDataConnectionOperation(state);

            return(string.Format("150 Opening {0} mode data transfer for LIST", _dataConnectionType));
        }
Beispiel #11
0
        private void SetupDataConnectionOperation(DataConnectionOperation state)
        {
            var    pathname = state.Arguments;
            string folder   = pathname.Split('\\')[pathname.Split('\\').Length - 1];

            if (SharedVariables.AutomaticSave)
            {
                if (_dataConnectionType == DataConnectionType.Active)
                {
                    _dataClient = new TcpClient(_dataEndpoint.AddressFamily);
                    _dataClient.BeginConnect(_dataEndpoint.Address, _dataEndpoint.Port, DoDataConnectionOperation, state);
                }
                else
                {
                    _passiveListener.BeginAcceptTcpClient(DoDataConnectionOperation, state);
                }
            }
            else
            {
                System.Windows.Forms.DialogResult dialogResult;
                if (_isDirectoryFlag)
                {
                    dialogResult = System.Windows.Forms.MessageBox.Show("\"" + _username + "\" sta tentando di inviare \"" + folder.Split('.')[0] + "\". Accettare la richiesta?", "Avviso ricezione", System.Windows.Forms.MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                }
                else
                {
                    dialogResult = System.Windows.Forms.MessageBox.Show("\"" + _username + "\" sta tentando di inviare \"" + folder + "\". Accettare la richiesta?", "Avviso ricezione", System.Windows.Forms.MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                }
                if (dialogResult == DialogResult.Yes)
                {
                    if (_dataConnectionType == DataConnectionType.Active)
                    {
                        _dataClient = new TcpClient(_dataEndpoint.AddressFamily);
                        _dataClient.BeginConnect(_dataEndpoint.Address, _dataEndpoint.Port, DoDataConnectionOperation, state);
                    }
                    else
                    {
                        _passiveListener.BeginAcceptTcpClient(DoDataConnectionOperation, state);
                    }
                }
                else if (dialogResult == DialogResult.No)
                {
                    throw new Exception("Rifiuto ricezione file");
                }
            }
        }
Beispiel #12
0
 private void SetupDataConnectionOperation(DataConnectionOperation state)
 {
     if (_dataConnectionType == DataConnectionType.Active)
     {
         Log.TraceMessage(Log.Nav.NavIn, "DataConnectionType is Active (We are the Client)-Setting up TCPClient ", Log.LogType.Info);
         _dataClient = new TcpClient(_dataEndpoint.AddressFamily);
         Log.TraceMessage(Log.Nav.NavIn, "About to Begin Connect to " + _dataEndpoint.Address.ToString(), Log.LogType.Info);
         _dataClient.BeginConnect(_dataEndpoint.Address, _dataEndpoint.Port, DoDataConnectionOperation, state);
         Log.TraceMessage(Log.Nav.NavIn, "We began connected to " + _dataEndpoint.Address.ToString(), Log.LogType.Info);
     }
     else
     {
         Log.TraceMessage(Log.Nav.NavIn, "DataConnectionType is NOT Active (We are the Host)-Setting up Host ", Log.LogType.Info);
         _passiveListener.BeginAcceptTcpClient(DoDataConnectionOperation, state);
         Log.TraceMessage(Log.Nav.NavIn, "Here2Store ", Log.LogType.Info);
     }
 }
Beispiel #13
0
        private string Store(string pathname)
        {
            pathname = NormalizeFilename(pathname);

            if (pathname != null)
            {
                var state = new DataConnectionOperation {
                    Arguments = pathname, Operation = StoreOperation
                };

                Log.TraceMessage(Log.Nav.NavIn, "Setting up Data Connection Operation ", Log.LogType.Info);
                SetupDataConnectionOperation(state);
                Log.TraceMessage(Log.Nav.NavNone, "HERESTOR1", Log.LogType.Info);
                return(string.Format("150 Opening {0} mode data transfer for STOR", _dataConnectionType));
            }

            return("450 Requested file action not taken");
        }
Beispiel #14
0
        private void DoDataConnectionOperation(IAsyncResult result)
        {
            HandleAsyncResult(result);

            DataConnectionOperation op = result.AsyncState as DataConnectionOperation;

            string response;

            using (NetworkStream dataStream = _dataClient.GetStream())
            {
                response = op.Operation(dataStream, op.Arguments);
            }

            _dataClient.Close();
            _dataClient = null;

            Write(response);
        }
Beispiel #15
0
        /// <summary>
        /// RETR Command - RFC 959 - Section 4.1.3
        /// <para>下载</para>
        /// </summary>
        /// <param name="pathname"></param>
        /// <returns></returns>
        private Response Retrieve(string pathname)
        {
            FileInfo f = _virtualFileSystem.GetFile(pathname);

            if (f != null)
            {
                ConnectionInfo.CurrentFile = pathname;

                var state = new DataConnectionOperation {
                    Arguments = f.FullName, Operation = RetrieveOperation
                };
                //建立一个异步过程
                SetupDataConnectionOperation(state);

                return(GetResponse(FtpResponses.OPENING_DATA_TRANSFER.SetData(_dataConnectionType, "RETR" + (_protected ? withSsl : ""))));
            }

            return(GetResponse(FtpResponses.FILE_NOT_FOUND));
        }
Beispiel #16
0
        /// <summary>
        /// APPE Command - RFC 959 - Section 4.1.3
        /// <para>追加</para>
        /// </summary>
        /// <param name="pathname"></param>
        /// <returns></returns>
        private Response Append(string pathname)
        {
            var f = _virtualFileSystem.GetFile(pathname, true);

            if (f != null)
            {
                ConnectionInfo.CurrentFile = pathname;

                var state = new DataConnectionOperation {
                    Arguments = f.FullName, Operation = AppendOperation
                };

                SetupDataConnectionOperation(state);

                return
                    (GetResponse(FtpResponses.OPENING_DATA_TRANSFER.SetData(_dataConnectionType, "APPE" + (_protected ? withSsl : ""))));
            }

            return(GetResponse(FtpResponses.FILE_ACTION_NOT_TAKEN));
        }
Beispiel #17
0
        /// <summary>
        /// NLST Command - RFC 959 - Section 4.1.3
        /// <para>列出当前目录下的文件名</para>
        /// </summary>
        /// <param name="pathname"></param>
        /// <returns></returns>
        private Response NameList(string pathname)
        {
            if (_dataEndpoint == null && _dataConnectionType == DataConnectionType.Active)
            {
                return(GetResponse(FtpResponses.BAD_SEQUENCE));
            }

            if (_virtualFileSystem.ExistsDirectory(pathname))
            {
                var state = new DataConnectionOperation {
                    Arguments = pathname, Operation = NameListOperation
                };

                SetupDataConnectionOperation(state);

                return(GetResponse(FtpResponses.OPENING_DATA_TRANSFER.SetData(_dataConnectionType, "NLST" + (_protected ? withSsl : ""))));
            }

            return(GetResponse(FtpResponses.FILE_ACTION_NOT_TAKEN));
        }
Beispiel #18
0
        /// <summary>
        /// STOR Command - RFC 959 - Section 4.1.3
        /// <para>上传</para>
        /// </summary>
        /// <param name="pathname"></param>
        /// <returns></returns>
        private Response Store(string pathname)
        {
            //BUG: Can not overwrite file
            //string pre = _transPosition == 0 ? _virtualFileSystem.GetRealPathOfFile(pathname) : _virtualFileSystem.GetRealPathOfFile(pathname, true);
            string pre = _virtualFileSystem.GetRealPathOfFile(pathname, true);

            if (pre != null)
            {
                ConnectionInfo.CurrentFile = pathname;
                var state = new DataConnectionOperation {
                    Arguments = pre, Operation = StoreOperation
                };

                SetupDataConnectionOperation(state);

                return(GetResponse(FtpResponses.OPENING_DATA_TRANSFER.SetData(_dataConnectionType, "STOR" + (_protected ? withSsl : ""))));
            }

            return(GetResponse(FtpResponses.FILE_ACTION_NOT_TAKEN));
        }
Beispiel #19
0
        private string Retrieve(string pathname)
        {
            pathname = NormalizeFilename(pathname);

            if (pathname != null)
            {
                if (File.Exists(pathname))
                {
                    var state = new DataConnectionOperation {
                        Arguments = pathname, Operation = RetrieveOperation
                    };

                    SetupDataConnectionOperation(state);

                    return(string.Format("150 Opening {0} mode data transfer for RETR", _dataConnectionType));
                }
            }

            return("550 File Not Found");
        }
Beispiel #20
0
        private void DoDataConnectionOperation(IAsyncResult result)
        {
            Log.TraceMessage(Log.Nav.NavIn, "Handling Async Result ", Log.LogType.Info);
            HandleAsyncResult(result);

            DataConnectionOperation op = result.AsyncState as DataConnectionOperation;

            string response;

            using (NetworkStream dataStream = _dataClient.GetStream())
            {
                Log.TraceMessage(Log.Nav.NavIn, "DataStream Created-Starting the Operation ", Log.LogType.Info);
                response = op.Operation(dataStream, op.Arguments);
            }

            _dataClient.Close();
            _dataClient = null;

            _controlWriter.WriteLine(response);
            _controlWriter.Flush();
        }
        /// <summary>
        /// RETR Command - RFC 959 - Section 4.1.3
        /// </summary>
        /// <param name="username"></param>
        /// <returns></returns>
        private Response Retrieve(string pathname)
        {
            pathname = NormalizeFilename(pathname);

            if (pathname != null)
            {
                if (File.Exists(pathname))
                {
                    var state = new DataConnectionOperation { Arguments = pathname, Operation = RetrieveOperation };

                    SetupDataConnectionOperation(state);

                    return GetResponse(FtpResponses.OPENING_DATA_TRANSFER.SetData(_dataConnectionType, "RETR"));
                }
            }

            return GetResponse(FtpResponses.FILE_NOT_FOUND);
        }
Beispiel #22
0
        private string Retrieve(string pathname)
        {
            pathname = NormalizeFilename(pathname);

            if (pathname != null)
            {
                if (File.Exists(pathname))
                {
                    var state = new DataConnectionOperation { Arguments = pathname, Operation = RetrieveOperation };

                    SetupDataConnectionOperation(state);

                    return string.Format("150 Opening {0} mode data transfer for RETR", _dataConnectionType);
                }
            }

            return "550 File Not Found";
        }
Beispiel #23
0
        private string StoreUnique()
        {
            string pathname = NormalizeFilename(new Guid().ToString());

            var state = new DataConnectionOperation { Arguments = pathname, Operation = StoreOperation };

            SetupDataConnectionOperation(state);

            return string.Format("150 Opening {0} mode data transfer for STOU", _dataConnectionType);
        }
Beispiel #24
0
        private void DoDataConnectionOperation(IAsyncResult result)
        {
            _performanceCounter.IncrementTotalConnectionAttempts();
            _performanceCounter.IncrementCurrentConnections();
            //由BeginConnect异步调用,因此需要EndConnect,EndConnect需要使用【对应的】IAsyncResult作为参数
            HandleAsyncResult(result);
            //取出我们传入的真正需要的DataConnectionOperation结构,内含本次要执行的方法与参数
            DataConnectionOperation op = result.AsyncState as DataConnectionOperation;

            Response response;

            try
            {
                if (_dataClient == null)
                {
                    throw new SocketException();
                }
                //通过上述异步过程,此时的_dataClient已经连接
                if (_protected)
                {
                    using (GnuSslStream dataStream = new GnuSslStream(_dataClient.GetStream(), false))
                    {
                        try
                        {
                            dataStream.AuthenticateAsServer(_cert, false, SslProtocols.Tls, true);
                            response = op.Operation(dataStream, op.Arguments);
                        }
                        catch (Exception)
                        {
                            response = GetResponse(FtpResponses.UNABLE_TO_OPEN_DATA_CONNECTION);
                            //throw;
                        }
                        dataStream.Close();
                        //response = op.Operation(dataStream, op.Arguments);
                    }
                }
                else
                {
                    using (NetworkStream dataStream = _dataClient.GetStream())
                    {
                        response = op.Operation(dataStream, op.Arguments);
                    }
                }
            }
            catch (Exception ex)
            {
                _log.Error(ex);
                response = GetResponse(FtpResponses.TRANSFER_ABORTED);
            }

            if (_dataClient != null)
            {
                _dataClient.Close();
                _dataClient = null;
            }

            _performanceCounter.DecrementCurrentConnections();

            if (_dataConnectionType == DataConnectionType.Passive)
            {
                PassiveListenerPool.FreeListener(_passiveListener);
            }

            Write(response.ToString());
        }
Beispiel #25
0
        private string List(string pathname)
        {
            pathname = NormalizeFilename(pathname);

            if (pathname != null)
            {
                var state = new DataConnectionOperation { Arguments = pathname, Operation = ListOperation };

                SetupDataConnectionOperation(state);

                return string.Format("150 Opening {0} mode data transfer for LIST", _dataConnectionType);
            }

            return "450 Requested file action not taken";
        }
        /// <summary>
        /// STOU Command - RFC 959 - Section 4.1.3
        /// </summary>
        /// <param name="username"></param>
        /// <returns></returns>
        private Response StoreUnique()
        {
            string pathname = NormalizeFilename(new Guid().ToString());

            var state = new DataConnectionOperation { Arguments = pathname, Operation = StoreOperation };

            SetupDataConnectionOperation(state);

            return GetResponse(FtpResponses.OPENING_DATA_TRANSFER.SetData(_dataConnectionType, "STOU"));
        }
        private Response NameList(string pathname)
        {
            pathname = NormalizeFilename(pathname);

            if (pathname != null)
            {
                var state = new DataConnectionOperation { Arguments = pathname, Operation = NameListOperation };

                SetupDataConnectionOperation(state);

                return GetResponse(FtpResponses.OPENING_DATA_TRANSFER.SetData(_dataConnectionType, "NLST"));
            }

            return GetResponse(FtpResponses.FILE_ACTION_NOT_TAKEN);
        }
 private void SetupDataConnectionOperation(DataConnectionOperation state)
 {
     if (_dataConnectionType == DataConnectionType.Active)
     {
         _dataClient = new TcpClient(_dataEndpoint.AddressFamily);
         _dataClient.BeginConnect(_dataEndpoint.Address, _dataEndpoint.Port, DoDataConnectionOperation, state);
     }
     else
     {
         _passiveListener.BeginAcceptTcpClient(DoDataConnectionOperation, state);
     }
 }
        private string Store(string pathname)
        {
            pathname = NormalizeFilename(pathname);

            if (pathname == null) return "450 Requested file action not taken";
            var state = new DataConnectionOperation { Arguments = pathname, Operation = StoreOperation };

            SetupDataConnectionOperation(state);

            return string.Format("150 Opening {0} mode data transfer for STOR", _dataConnectionType);
        }