public async Task OnConnection(ConnectionFilterContext context)
        {
            await _previous.OnConnection(context);

            if (string.Equals(context.Address.Scheme, "https", StringComparison.OrdinalIgnoreCase))
            {
                var sslStream = new SslStream(context.Connection);
                await sslStream.AuthenticateAsServerAsync(_cert);
                context.Connection = sslStream;
            }
        }
Ejemplo n.º 2
0
        public void Start()
        {
            Log.ConnectionStart(_connectionId);

            // Start socket prior to applying the ConnectionFilter
            _socket.ReadStart(_allocCallback, _readCallback, this);

            // Don't initialize _frame until SocketInput and SocketOutput are set to their final values.
            if (ConnectionFilter == null)
            {
                SocketInput = _rawSocketInput;
                SocketOutput = _rawSocketOutput;

                _frame = new Frame(this);
                _frame.Start();
            }
            else
            {
                var libuvStream = new LibuvStream(_rawSocketInput, _rawSocketOutput);

                _filterContext = new ConnectionFilterContext
                {
                    Connection = libuvStream,
                    Address = ServerAddress
                };

                ConnectionFilter.OnConnection(_filterContext).ContinueWith((task, state) =>
                {
                    var connection = (Connection)state;

                    if (task.IsFaulted)
                    {
                        connection.Log.LogError("ConnectionFilter.OnConnection", task.Exception);
                        ConnectionControl.End(ProduceEndType.SocketDisconnect);
                    }
                    else if (task.IsCanceled)
                    {
                        connection.Log.LogError("ConnectionFilter.OnConnection Canceled");
                        ConnectionControl.End(ProduceEndType.SocketDisconnect);
                    }
                    else
                    {
                        connection.ApplyConnectionFilter();
                    }
                }, this);
            }
        }
Ejemplo n.º 3
0
 public Task OnConnectionAsync(ConnectionFilterContext context)
 {
     return(TaskUtilities.CompletedTask);
 }
 public Task OnConnection(ConnectionFilterContext context)
 {
     return(_empty);
 }
        public async Task OnConnectionAsync(ConnectionFilterContext context)
        {
            await _previous.OnConnectionAsync(context);

            context.Connection = new LoggingStream(context.Connection, _logger);
        }
            public async Task OnConnection(ConnectionFilterContext context)
            {
                var oldConnection = context.Connection;

                // Set Connection to null to ensure it isn't used until the returned task completes.
                context.Connection = null;
                await Task.Delay(100);

                context.Connection = new RewritingStream(oldConnection);
            }
 public Task OnConnection(ConnectionFilterContext context)
 {
     _rewritingStream = new RewritingStream(context.Connection);
     context.Connection = _rewritingStream;
     return _empty;
 }
        public async Task OnConnectionAsync(ConnectionFilterContext context)
        {
            await _previous.OnConnectionAsync(context);

            if (string.Equals(context.Address.Scheme, "https", StringComparison.OrdinalIgnoreCase))
            {
                X509Certificate2 clientCertificate = null;
                SslStream sslStream;
                if (_options.ClientCertificateMode == ClientCertificateMode.NoCertificate)
                {
                    sslStream = new SslStream(context.Connection);
                    await sslStream.AuthenticateAsServerAsync(_options.ServerCertificate, clientCertificateRequired: false,
                        enabledSslProtocols: _options.SslProtocols, checkCertificateRevocation: _options.CheckCertificateRevocation);
                }
                else
                {
                    sslStream = new SslStream(context.Connection, leaveInnerStreamOpen: false,
                        userCertificateValidationCallback: (sender, certificate, chain, sslPolicyErrors) =>
                        {
                            if (certificate == null)
                            {
                                return _options.ClientCertificateMode != ClientCertificateMode.RequireCertificate;
                            }

                            if (_options.ClientCertificateValidation == null)
                            {
                                if (sslPolicyErrors != SslPolicyErrors.None)
                                {
                                    return false;
                                }
                            }

                            X509Certificate2 certificate2 = certificate as X509Certificate2;
                            if (certificate2 == null)
                            {
#if DOTNET5_4
                                // conversion X509Certificate to X509Certificate2 not supported
                                // https://github.com/dotnet/corefx/issues/4510
                                return false;
#else
                                certificate2 = new X509Certificate2(certificate);
#endif
                            }

                            if (_options.ClientCertificateValidation != null)
                            {
                                if (!_options.ClientCertificateValidation(certificate2, chain, sslPolicyErrors))
                                {
                                    return false;
                                }
                            }

                            clientCertificate = certificate2;
                            return true;
                        });
                    await sslStream.AuthenticateAsServerAsync(_options.ServerCertificate, clientCertificateRequired: true,
                        enabledSslProtocols: _options.SslProtocols, checkCertificateRevocation: _options.CheckCertificateRevocation);
                }

                var previousPrepareRequest = context.PrepareRequest;
                context.PrepareRequest = features =>
                {
                    previousPrepareRequest?.Invoke(features);

                    if (clientCertificate != null)
                    {
                        features.Set<ITlsConnectionFeature>(new TlsConnectionFeature { ClientCertificate = clientCertificate });
                    }

                    features.Get<IHttpRequestFeature>().Scheme = "https";
                };
                context.Connection = sslStream;
            }
        }
Ejemplo n.º 9
0
        public void Start()
        {
            Log.ConnectionStart(_connectionId);

            // Start socket prior to applying the ConnectionFilter
            _socket.ReadStart(_allocCallback, _readCallback, this);

            var tcpHandle = _socket as UvTcpHandle;
            if (tcpHandle != null)
            {
                _remoteEndPoint = tcpHandle.GetPeerIPEndPoint();
                _localEndPoint = tcpHandle.GetSockIPEndPoint();
            }

            // Don't initialize _frame until SocketInput and SocketOutput are set to their final values.
            if (ConnectionFilter == null)
            {
                SocketInput = _rawSocketInput;
                SocketOutput = _rawSocketOutput;

                _frame = CreateFrame();
                _frame.Start();
            }
            else
            {
                var libuvStream = new LibuvStream(_rawSocketInput, _rawSocketOutput);

                _filterContext = new ConnectionFilterContext
                {
                    Connection = libuvStream,
                    Address = ServerAddress
                };

                try
                {
                    ConnectionFilter.OnConnectionAsync(_filterContext).ContinueWith((task, state) =>
                    {
                        var connection = (Connection)state;

                        if (task.IsFaulted)
                        {
                            connection.Log.LogError("ConnectionFilter.OnConnection", task.Exception);
                            connection.ConnectionControl.End(ProduceEndType.SocketDisconnect);
                        }
                        else if (task.IsCanceled)
                        {
                            connection.Log.LogError("ConnectionFilter.OnConnection Canceled");
                            connection.ConnectionControl.End(ProduceEndType.SocketDisconnect);
                        }
                        else
                        {
                            connection.ApplyConnectionFilter();
                        }
                    }, this);
                }
                catch (Exception ex)
                {
                    Log.LogError("ConnectionFilter.OnConnection", ex);
                    ConnectionControl.End(ProduceEndType.SocketDisconnect);
                }
            }
        }
 public Task OnConnectionAsync(ConnectionFilterContext context)
 {
     return TaskUtilities.CompletedTask;
 }
 public Task OnConnectionAsync(ConnectionFilterContext context)
 {
     throw new Exception();
 }
 public Task OnConnection(ConnectionFilterContext context)
 {
     return _empty;
 }