From 2ce811364402e39e9431c06cb71b20d347a2e103 Mon Sep 17 00:00:00 2001 From: TheJoKlLa Date: Tue, 31 Jan 2023 14:14:33 +0100 Subject: [PATCH] Fixed: TLS Timeout --- FabAccessAPI/API.cs | 48 ++++++++++++++++++++++++++++++--------------- 1 file changed, 32 insertions(+), 16 deletions(-) diff --git a/FabAccessAPI/API.cs b/FabAccessAPI/API.cs index 3c81f40..04494cd 100644 --- a/FabAccessAPI/API.cs +++ b/FabAccessAPI/API.cs @@ -6,6 +6,7 @@ using NLog; using S22.Sasl; using System; using System.Collections.Generic; +using System.IO; using System.Linq; using System.Net.Security; using System.Security.Cryptography.X509Certificates; @@ -336,28 +337,43 @@ namespace FabAccessAPI return true; } + /// + /// Injects SSL as Midlayer in TCPRPCConnection + /// + /// + private Stream InjectSSL(Stream tcpstream) + { + SslStream sslStream = new SslStream(tcpstream, false, new RemoteCertificateValidationCallback(_RemoteCertificateValidationCallback)); + try + { + sslStream.ReadTimeout = 2000; + sslStream.AuthenticateAsClient("bffhd"); + sslStream.ReadTimeout = -1; + + return sslStream; + } + catch (System.Security.Authentication.AuthenticationException exception) + { + sslStream.Close(); + Log.Warn(exception); + throw new ConnectionException("TLS failed", exception); + } + catch(IOException exception) + { + sslStream.Close(); + Log.Warn(exception); + throw new ConnectionException("TLS failed", new Exceptions.TimeoutException("TLS timeout", exception)); + } + } + /// /// Connect async to a server with ConnectionData /// /// Based on RPC Exception private async Task _ConnectAsync(TcpRpcClient tcprpcClient, ConnectionData connectionData) { - tcprpcClient.InjectMidlayer((tcpstream) => - { - var sslStream = new SslStream(tcpstream, false, new RemoteCertificateValidationCallback(_RemoteCertificateValidationCallback)); - try - { - sslStream.AuthenticateAsClient("bffhd"); - return sslStream; - } - catch (System.Security.Authentication.AuthenticationException exception) - { - sslStream.Close(); - Log.Warn(exception); - throw new ConnectionException("TLS failed", exception); - } - }); - + tcprpcClient.InjectMidlayer(InjectSSL); + try { Task timeoutTask = Task.Delay(3000);