Example #1
0
        private async Task Read()
        {
            while (!_tokenSource.IsCancellationRequested)
            {
                try
                {
                    var client = await _listener.AcceptTcpClientAsync()
                                 .ConfigureAwait(false);

                    var connection = await MllpHost.Create(
                        client,
                        _messageLog,
                        _middleware,
                        _parser,
                        _encoding,
                        _serverCertificate,
                        _userCertificateValidationCallback)
                                     .ConfigureAwait(false);

                    lock (_connections)
                    {
                        _connections.Add(connection);
                    }
                }
                catch (ObjectDisposedException)
                {
                    // Ignore
                }
            }
        }
Example #2
0
        public static async Task <MllpHost> Create(
            TcpClient tcpClient,
            IMessageLog messageLog,
            IHl7MessageMiddleware middleware,
            PipeParser?parser = null,
            Encoding?encoding = null,
            X509Certificate?serverCertificate = null,
            RemoteCertificateValidationCallback?
            userCertificateValidationCallback = null)
        {
            var host = new MllpHost(
                tcpClient,
                messageLog,
                parser ?? new PipeParser(),
                encoding ?? Encoding.ASCII,
                middleware);
            Stream stream = tcpClient.GetStream();

            if (serverCertificate != null)
            {
                var ssl = new SslStream(
                    stream,
                    false,
                    userCertificateValidationCallback);
                await ssl.AuthenticateAsServerAsync(
                    serverCertificate,
                    true,
                    SslProtocols.Tls11 | SslProtocols.Tls12,
                    false)
                .ConfigureAwait(false);

                host._stream = ssl;
            }
            else
            {
                host._stream = stream;
            }

            host._readThread = host.ReadStream(host._tokenSource.Token);
            return(host);
        }
Example #3
0
        public static async Task <MllpHost> Create(TcpClient tcpClient, IHl7MessageMiddleware middleware, Encoding encoding = null, ServerSecurityDetails securityDetails = null, CancellationToken cancellationToken = default(CancellationToken))
        {
            Stream        stream;
            NetworkStream networkStream = tcpClient.GetStream();

            if (securityDetails != null)
            {
                var sslStream = new SslStream(networkStream, true, securityDetails.ClientCertificateValidationCallback, null);

                try
                {
                    bool askForClientCertificate = securityDetails.ForceClientAuthentciation;
                    await sslStream.AuthenticateAsServerAsync(securityDetails.ServerCertificate, askForClientCertificate, securityDetails.SupportedSslProtocols, false);

                    if (askForClientCertificate && !sslStream.IsMutuallyAuthenticated)
                    {
                        throw new AuthenticationException("mutual authentication failed.");
                    }
                }
                catch (Exception)
                {
                    sslStream.Dispose();
                    throw;
                }

                stream = sslStream;
            }
            else
            {
                stream = networkStream;
            }

            var host = new MllpHost(tcpClient, encoding ?? Encoding.ASCII, middleware, stream, cancellationToken);

            host.ReadStream(host._token);
            return(host);
        }
Example #4
0
        private async Task HandleClientRequestAsync(TcpClient client)
        {
            var clientEndpoint = (IPEndPoint)client.Client.RemoteEndPoint;

            MllpHost connection;

            try
            {
                connection = await MllpHost.Create(client, _middleware, _connectionDetails.Encoding, _connectionDetails.SecurityDetails, _token);
            }
            catch (AuthenticationException)
            {
                // don't bring down server just because a client wasn't table to authenticate.
                this.TriggerClientAuthenticationFailedEvent(clientEndpoint);
                return;
            }

            lock (_connections)
            {
                _connections.Add(connection);
            }

            this.TriggerClientConnectedEvent(clientEndpoint);
        }