Пример #1
0
        protected override void Dispose(bool disposing)
        {
            try
            {
                if (!_disposed)
                {
                    _disposed = true;

                    if (_currentUser != null)
                    {
                        if (_currentUser.IsAnonymous)
                        {
                            _performanceCounter.DecrementAnonymousUsers();
                        }
                        else
                        {
                            _performanceCounter.DecrementNonAnonymousUsers();
                        }
                    }

                    if (_connected)
                    {
                        _performanceCounter.DecrementCurrentConnections();
                    }

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

                        if (_sslStream != null)
                        {
                            _sslStream.Dispose();
                            _sslStream = null;
                        }
                    }
                }
            }
            finally
            {
                FtpLogEntry logEntry = new FtpLogEntry()
                {
                    Date = DateTime.Now,
                    Info = LogInfo.ConnectionTerminated.ToString()
                };
                OnLog(logEntry);

                var serverConnInfos = ((FtpServer)CurrentServer).ConnectionInfos;
                if (serverConnInfos.Contains(ConnectionInfo))
                {
                    serverConnInfos.Remove(ConnectionInfo);
                }
                serverConnInfos.RemoveAll(t => t.ID == this.ID);

                base.Dispose(disposing);
            }
        }
Пример #2
0
        protected override void OnCommandComplete(Command cmd)
        {
            if (cmd.Code == "AUTH")
            {
                try
                {
                    //Write(GetResponse(FtpResponses.ENABLING_TLS));
                    //MARK:建立TLS连接
                    _cert = ((FtpServer)CurrentServer).ServerCertificate;

                    _sslStream = new GnuSslStream(ControlStream);

                    _sslStream.ReadTimeout  = 5000;
                    _sslStream.WriteTimeout = 5000;

                    _sslStream.AuthenticateAsServer(_cert, false, SslProtocols.Tls, true);

                    _sslEnabled = true;
                }
                catch (AuthenticationException exception)
                {
                    _sslEnabled = false;
                    //throw;
                }
                catch (ArgumentNullException exception)
                {
                    _sslEnabled = false;
                }
            }

            _performanceCounter.IncrementCommandsExecuted();
        }
Пример #3
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());
        }