public ImplicitFtpsControlConnectionStreamAdapter(
     ImplicitFtpsControlConnectionStreamAdapterOptions options,
     ISslStreamWrapperFactory sslStreamWrapperFactory)
 {
     _options = options;
     _sslStreamWrapperFactory = sslStreamWrapperFactory;
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="TlsAuthenticationMechanism"/> class.
 /// </summary>
 /// <param name="connection">The required FTP connection.</param>
 /// <param name="sslStreamWrapperFactory">The SslStream wrapper factory.</param>
 public TlsAuthenticationMechanism(
     IFtpConnection connection,
     ISslStreamWrapperFactory sslStreamWrapperFactory)
     : base(connection)
 {
     _sslStreamWrapperFactory = sslStreamWrapperFactory;
 }
Beispiel #3
0
 /// <summary>
 /// Initializes a new instance of the <see cref="SecureDataConnectionWrapper"/> class.
 /// </summary>
 /// <param name="connectionAccessor">Accessor for the FTP connection.</param>
 /// <param name="sslStreamWrapperFactory">The SSL stream wrapper factory.</param>
 public SecureDataConnectionWrapper(
     [NotNull] IFtpConnectionAccessor connectionAccessor,
     [NotNull] ISslStreamWrapperFactory sslStreamWrapperFactory)
 {
     _connectionAccessor      = connectionAccessor;
     _sslStreamWrapperFactory = sslStreamWrapperFactory;
 }
Beispiel #4
0
 public SecureFtpDataConnection(
     [NotNull] IFtpDataConnection originalDataConnection,
     [NotNull] ISslStreamWrapperFactory sslStreamWrapperFactory,
     [NotNull] Stream stream)
 {
     _originalDataConnection  = originalDataConnection;
     _sslStreamWrapperFactory = sslStreamWrapperFactory;
     LocalAddress             = originalDataConnection.LocalAddress;
     RemoteAddress            = originalDataConnection.RemoteAddress;
     Stream = stream;
 }
Beispiel #5
0
 public SslStreamConnectionAdapter(
     [NotNull] IDuplexPipe socketPipe,
     [NotNull] IDuplexPipe connectionPipe,
     [NotNull] ISslStreamWrapperFactory sslStreamWrapperFactory,
     [NotNull] X509Certificate2 certificate,
     CancellationToken connectionClosed)
 {
     _socketPipe              = socketPipe;
     _connectionPipe          = connectionPipe;
     _sslStreamWrapperFactory = sslStreamWrapperFactory;
     _certificate             = certificate;
     _connectionClosed        = connectionClosed;
 }
 public SslStreamConnectionAdapter(
     IDuplexPipe socketPipe,
     IDuplexPipe connectionPipe,
     ISslStreamWrapperFactory sslStreamWrapperFactory,
     X509Certificate certificate,
     CancellationToken connectionClosed,
     ILoggerFactory?loggerFactory = null)
 {
     _socketPipe              = socketPipe;
     _connectionPipe          = connectionPipe;
     _sslStreamWrapperFactory = sslStreamWrapperFactory;
     _certificate             = certificate;
     _connectionClosed        = connectionClosed;
     _loggerFactory           = loggerFactory;
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="SecureConnectionAdapter"/> class.
 /// </summary>
 /// <param name="socketPipe">The pipe from the socket.</param>
 /// <param name="connectionPipe">The pipe to the connection object.</param>
 /// <param name="sslStreamWrapperFactory">The SSL stream wrapper factory.</param>
 /// <param name="connectionClosed">The cancellation token for a closed connection.</param>
 public SecureConnectionAdapter(
     [NotNull] IDuplexPipe socketPipe,
     [NotNull] IDuplexPipe connectionPipe,
     [NotNull] ISslStreamWrapperFactory sslStreamWrapperFactory,
     CancellationToken connectionClosed)
 {
     _socketPipe                 = socketPipe;
     _connectionPipe             = connectionPipe;
     _sslStreamWrapperFactory    = sslStreamWrapperFactory;
     _connectionClosed           = connectionClosed;
     _activeCommunicationService = new PassThroughConnectionAdapter(
         socketPipe,
         connectionPipe,
         connectionClosed);
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="SecureConnectionAdapter"/> class.
 /// </summary>
 /// <param name="socketPipe">The pipe from the socket.</param>
 /// <param name="connectionPipe">The pipe to the connection object.</param>
 /// <param name="sslStreamWrapperFactory">The SSL stream wrapper factory.</param>
 /// <param name="connectionClosed">The cancellation token for a closed connection.</param>
 /// <param name="loggerFactory">The logger factory.</param>
 public SecureConnectionAdapter(
     IDuplexPipe socketPipe,
     IDuplexPipe connectionPipe,
     ISslStreamWrapperFactory sslStreamWrapperFactory,
     CancellationToken connectionClosed,
     ILoggerFactory?loggerFactory = null)
 {
     _socketPipe                 = socketPipe;
     _connectionPipe             = connectionPipe;
     _sslStreamWrapperFactory    = sslStreamWrapperFactory;
     _connectionClosed           = connectionClosed;
     _loggerFactory              = loggerFactory;
     _activeCommunicationService = new PassThroughConnectionAdapter(
         socketPipe,
         connectionPipe,
         connectionClosed,
         loggerFactory);
 }
Beispiel #9
0
        /// <summary>
        /// Initializes a new instance of the <see cref="FtpConnection"/> class.
        /// </summary>
        /// <param name="socketAccessor">The accessor to get the socket used to communicate with the client.</param>
        /// <param name="options">The options for the FTP connection.</param>
        /// <param name="portOptions">The <c>PORT</c> command options.</param>
        /// <param name="connectionAccessor">The accessor to get the connection that is active during the <see cref="FtpCommandHandler.Process"/> method execution.</param>
        /// <param name="catalogLoader">The catalog loader for the FTP server.</param>
        /// <param name="serverCommandExecutor">The executor for server commands.</param>
        /// <param name="serviceProvider">The service provider for the connection.</param>
        /// <param name="secureDataConnectionWrapper">Wraps a data connection into an SSL stream.</param>
        /// <param name="sslStreamWrapperFactory">The SSL stream wrapper factory.</param>
        /// <param name="logger">The logger for the FTP connection.</param>
        public FtpConnection(
            TcpSocketClientAccessor socketAccessor,
            IOptions <FtpConnectionOptions> options,
            IOptions <PortCommandOptions> portOptions,
            IFtpConnectionAccessor connectionAccessor,
            IFtpCatalogLoader catalogLoader,
            IServerCommandExecutor serverCommandExecutor,
            IServiceProvider serviceProvider,
            SecureDataConnectionWrapper secureDataConnectionWrapper,
            ISslStreamWrapperFactory sslStreamWrapperFactory,
            ILogger <FtpConnection>?logger = null)
        {
            var socket = socketAccessor.TcpSocketClient ?? throw new InvalidOperationException("The socket to communicate with the client was not set");

            ConnectionServices = serviceProvider;

            ConnectionId = "FTP-" + Guid.NewGuid().ToString("N");

            _dataPort = portOptions.Value.DataPort;
            var remoteEndPoint = _remoteEndPoint = (IPEndPoint)socket.Client.RemoteEndPoint;

#pragma warning disable 618
            RemoteAddress = new Address(remoteEndPoint.Address.ToString(), remoteEndPoint.Port);
#pragma warning restore 618

            var properties = new Dictionary <string, object?>
            {
                ["RemoteAddress"] = remoteEndPoint.ToString(),
                ["RemoteIp"]      = remoteEndPoint.Address.ToString(),
                ["RemotePort"]    = remoteEndPoint.Port,
                ["ConnectionId"]  = ConnectionId,
            };

            _loggerScope = logger?.BeginScope(properties);

            _socket                      = socket;
            _connectionAccessor          = connectionAccessor;
            _serverCommandExecutor       = serverCommandExecutor;
            _secureDataConnectionWrapper = secureDataConnectionWrapper;
            _serverCommandChannel        = Channel.CreateBounded <IServerCommand>(new BoundedChannelOptions(3));

            _logger = logger;

            var parentFeatures    = new FeatureCollection();
            var connectionFeature = new ConnectionFeature(
                (IPEndPoint)socket.Client.LocalEndPoint,
                remoteEndPoint);
            var secureConnectionFeature = new SecureConnectionFeature();

            var applicationInputPipe  = new Pipe();
            var applicationOutputPipe = new Pipe();
            var socketPipe            = new DuplexPipe(_socketCommandPipe.Reader, _socketResponsePipe.Writer);
            var connectionPipe        = new DuplexPipe(applicationOutputPipe.Reader, applicationInputPipe.Writer);

            var originalStream = socketAccessor.TcpSocketStream ?? socket.GetStream();
            _streamReaderService = new ConnectionClosingNetworkStreamReader(
                originalStream,
                _socketCommandPipe.Writer,
                _cancellationTokenSource);
            _streamWriterService = new StreamPipeWriterService(
                originalStream,
                _socketResponsePipe.Reader,
                _cancellationTokenSource.Token);

            _networkStreamFeature = new NetworkStreamFeature(
                new SecureConnectionAdapter(
                    socketPipe,
                    connectionPipe,
                    sslStreamWrapperFactory,
                    _cancellationTokenSource.Token),
                applicationOutputPipe.Writer);

            parentFeatures.Set <IConnectionFeature>(connectionFeature);
            parentFeatures.Set <ISecureConnectionFeature>(secureConnectionFeature);
            parentFeatures.Set <IServerCommandFeature>(new ServerCommandFeature(_serverCommandChannel));
            parentFeatures.Set <INetworkStreamFeature>(_networkStreamFeature);

            var features = new FeatureCollection(parentFeatures);
#pragma warning disable 618
            Data = new FtpConnectionData(
                options.Value.DefaultEncoding ?? Encoding.ASCII,
                features,
                catalogLoader);
#pragma warning restore 618

            Features = features;

            _commandReader = ReadCommandsFromPipeline(
                applicationInputPipe.Reader,
                _ftpCommandChannel.Writer,
                _cancellationTokenSource.Token);
        }
Beispiel #10
0
        /// <summary>
        /// Initializes a new instance of the <see cref="FtpConnection"/> class.
        /// </summary>
        /// <param name="socket">The socket to use to communicate with the client.</param>
        /// <param name="options">The options for the FTP connection.</param>
        /// <param name="portOptions">The <c>PORT</c> command options.</param>
        /// <param name="connectionAccessor">The accessor to get the connection that is active during the <see cref="FtpCommandHandler.Process"/> method execution.</param>
        /// <param name="catalogLoader">The catalog loader for the FTP server.</param>
        /// <param name="serverCommandExecutor">The executor for server commands.</param>
        /// <param name="serviceProvider">The service provider for the connection.</param>
        /// <param name="secureDataConnectionWrapper">Wraps a data connection into an SSL stream.</param>
        /// <param name="serverMessages">The server messages.</param>
        /// <param name="sslStreamWrapperFactory">The SSL stream wrapper factory.</param>
        /// <param name="logger">The logger for the FTP connection.</param>
        public FtpConnection(
            [NotNull] TcpClient socket,
            [NotNull] IOptions <FtpConnectionOptions> options,
            [NotNull] IOptions <PortCommandOptions> portOptions,
            [NotNull] IFtpConnectionAccessor connectionAccessor,
            [NotNull] IFtpCatalogLoader catalogLoader,
            [NotNull] IServerCommandExecutor serverCommandExecutor,
            [NotNull] IServiceProvider serviceProvider,
            [NotNull] SecureDataConnectionWrapper secureDataConnectionWrapper,
            [NotNull] IFtpServerMessages serverMessages,
            [NotNull] ISslStreamWrapperFactory sslStreamWrapperFactory,
            [CanBeNull] ILogger <FtpConnection> logger = null)
        {
            ConnectionServices = serviceProvider;

            ConnectionId = "FTP-" + Guid.NewGuid().ToString("N");

            _dataPort = portOptions.Value.DataPort;
            var endpoint      = (IPEndPoint)socket.Client.RemoteEndPoint;
            var remoteAddress = _remoteAddress = new Address(endpoint.Address.ToString(), endpoint.Port);

            var properties = new Dictionary <string, object>
            {
                ["RemoteAddress"] = remoteAddress.ToString(true),
                ["RemoteIp"]      = remoteAddress.IPAddress?.ToString(),
                ["RemotePort"]    = remoteAddress.Port,
                ["ConnectionId"]  = ConnectionId,
            };

            _loggerScope = logger?.BeginScope(properties);

            _socket                      = socket;
            _connectionAccessor          = connectionAccessor;
            _serverCommandExecutor       = serverCommandExecutor;
            _secureDataConnectionWrapper = secureDataConnectionWrapper;
            _serverMessages              = serverMessages;
            _serverCommandChannel        = Channel.CreateBounded <IServerCommand>(new BoundedChannelOptions(3));

            _logger = logger;

            var parentFeatures    = new FeatureCollection();
            var connectionFeature = new ConnectionFeature(
                (IPEndPoint)socket.Client.LocalEndPoint,
                remoteAddress);
            var secureConnectionFeature = new SecureConnectionFeature(socket);

            var applicationInputPipe  = new Pipe();
            var applicationOutputPipe = new Pipe();
            var socketPipe            = new DuplexPipe(_socketCommandPipe.Reader, _socketResponsePipe.Writer);
            var connectionPipe        = new DuplexPipe(applicationOutputPipe.Reader, applicationInputPipe.Writer);

            _networkStreamFeature = new NetworkStreamFeature(
                new SecureConnectionAdapter(
                    socketPipe,
                    connectionPipe,
                    sslStreamWrapperFactory,
                    _cancellationTokenSource.Token),
                new ConnectionClosingNetworkStreamReader(
                    secureConnectionFeature.OriginalStream,
                    _socketCommandPipe.Writer,
                    _cancellationTokenSource),
                new StreamPipeWriterService(
                    secureConnectionFeature.OriginalStream,
                    _socketResponsePipe.Reader,
                    _cancellationTokenSource.Token),
                applicationOutputPipe.Writer);

            parentFeatures.Set <IConnectionFeature>(connectionFeature);
            parentFeatures.Set <ISecureConnectionFeature>(secureConnectionFeature);
            parentFeatures.Set <IServerCommandFeature>(new ServerCommandFeature(_serverCommandChannel));
            parentFeatures.Set <INetworkStreamFeature>(_networkStreamFeature);

            var features = new FeatureCollection(parentFeatures);

#pragma warning disable 618
            Data = new FtpConnectionData(
                options.Value.DefaultEncoding ?? Encoding.ASCII,
                features,
                catalogLoader);
#pragma warning restore 618

            Features = features;

            _commandReader = ReadCommandsFromPipeline(
                applicationInputPipe.Reader,
                _ftpCommandChannel.Writer,
                _cancellationTokenSource.Token);
        }