Files
EonaCat.Connections/EonaCat.Connections/Helpers/SslHandshakeDiagnostics.cs
T
2026-11-04 07:53:40 +01:00

228 lines
6.4 KiB
C#

using System.Diagnostics;
using System.Net.Security;
namespace EonaCat.Connections.Helpers
{
public class SslHandshakeDiagnostics
{
private readonly SslMetrics _metrics = new();
private readonly Stopwatch _totalStopwatch = new();
private Stopwatch? _stageStopwatch;
public SslMetrics Metrics => _metrics;
public void StartHandshake(string remoteEndPoint, bool mutualAuthRequired)
{
_totalStopwatch.Restart();
_metrics.StartTime = DateTime.UtcNow;
_metrics.RemoteEndPoint = remoteEndPoint;
_metrics.MutualAuthenticationRequired = mutualAuthRequired;
_metrics.AttemptCount++;
_metrics.IsSuccessful = false;
_metrics.FailureReason = null;
}
public void StartStage(string stageName)
{
_stageStopwatch = Stopwatch.StartNew();
}
public void EndStage(string stageName)
{
if (_stageStopwatch == null)
{
return;
}
_stageStopwatch.Stop();
var duration = _stageStopwatch.Elapsed;
switch (stageName.ToLower())
{
case "serverauth":
case "authenticateasserver":
_metrics.ServerAuthenticationDuration = duration;
break;
case "clientauth":
case "authenticateasclient":
_metrics.ClientAuthenticationDuration = duration;
break;
case "retrydelay":
if (_metrics.RetryDelayDuration == null)
{
_metrics.RetryDelayDuration = duration;
}
else
{
_metrics.RetryDelayDuration += duration;
}
break;
}
_stageStopwatch = null;
}
public void RecordSuccess(SslStream sslStream)
{
_totalStopwatch.Stop();
_metrics.IsSuccessful = true;
_metrics.EndTime = DateTime.UtcNow;
_metrics.FailureReason = null;
if (sslStream != null)
{
try
{
_metrics.SslProtocolVersion = sslStream.SslProtocol.ToString();
_metrics.CipherAlgorithm = sslStream.CipherAlgorithm.ToString();
_metrics.HashAlgorithm = sslStream.HashAlgorithm.ToString();
_metrics.KeyExchangeAlgorithm = sslStream.KeyExchangeAlgorithm.ToString();
}
catch
{
}
}
_metrics.CumulativeDuration = _totalStopwatch.Elapsed;
}
public void RecordFailure(Exception exception, bool isRecoverable)
{
_totalStopwatch.Stop();
_metrics.IsSuccessful = false;
_metrics.EndTime = DateTime.UtcNow;
_metrics.FailureReason = $"{exception?.GetType().Name ?? "Unknown"}: {exception?.Message ?? "No details"}";
_metrics.IsRecoverableFailure = isRecoverable;
_metrics.CumulativeDuration = _totalStopwatch.Elapsed;
}
public static bool IsRecoverableFailure(Exception exception)
{
if (exception == null)
{
return false;
}
var message = exception.Message?.ToLower() ?? "";
var typeName = exception.GetType().Name.ToLower();
if (typeName.Contains("authenticationexception"))
{
return false;
}
if (message.Contains("certificate rejected"))
{
return false;
}
if (message.Contains("certificate validation failed"))
{
return false;
}
if (message.Contains("certificate not trusted"))
{
return false;
}
if (message.Contains("certificate chain"))
{
return false;
}
if (message.Contains("sslhandshakefailure"))
{
return false;
}
if (message.Contains("the request was aborted"))
{
return false;
}
if (message.Contains("handshake_failure"))
{
return false;
}
if (message.Contains("protocol_version"))
{
return false;
}
if (message.Contains("unsupported_certificate_type"))
{
return false;
}
if (message.Contains("0 bytes from the transport stream"))
{
return true;
}
if (message.Contains("unexpected eof"))
{
return true;
}
if (message.Contains("connection reset"))
{
return true;
}
if (message.Contains("connection aborted"))
{
return true;
}
if (message.Contains("timed out"))
{
return true;
}
if (message.Contains("timeout"))
{
return true;
}
if (message.Contains("network unreachable"))
{
return true;
}
if (message.Contains("connection refused"))
{
return true;
}
if (typeName.Contains("ioexception"))
{
return true;
}
if (typeName.Contains("socketexception"))
{
return true;
}
if (typeName.Contains("operationcanceledexception"))
{
return true;
}
if (exception.InnerException != null)
{
return IsRecoverableFailure(exception.InnerException);
}
return typeName.Contains("exception");
}
public override string ToString() => _metrics.ToString();
}
}