Skip to content

Commit 06c1381

Browse files
committed
Fix TLS error handling and remove excessive logging
- Added try-catch around CreateOutboundCommandStream to properly catch and log TLS handshake failures - Fixed indentation issue in TrySendRemoteCommandTcpWithAck - Removed spammy HandleClientComm log that was printing every 500ms
1 parent 3f09fa9 commit 06c1381

2 files changed

Lines changed: 48 additions & 22 deletions

File tree

DesktopShell/GlobalVar.cs

Lines changed: 48 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -613,22 +613,37 @@ public static Stream CreateOutboundCommandStream(Socket connectedSocket, string
613613
{
614614
if (cert == null)
615615
{
616+
Log("### TLS validation: No certificate presented");
616617
return false;
617618
}
618619

620+
var presented = new X509Certificate2(cert);
621+
string presentedThumbprint = presented.Thumbprint?.Replace(" ", "", StringComparison.OrdinalIgnoreCase) ?? "";
622+
619623
if (!string.IsNullOrWhiteSpace(pinnedThumbprint))
620624
{
621-
var presented = new X509Certificate2(cert);
622-
string presentedThumbprint = presented.Thumbprint?.Replace(" ", "", StringComparison.OrdinalIgnoreCase) ?? "";
623625
string expected = pinnedThumbprint.Replace(" ", "", StringComparison.OrdinalIgnoreCase);
624-
return string.Equals(presentedThumbprint, expected, StringComparison.OrdinalIgnoreCase);
626+
bool matches = string.Equals(presentedThumbprint, expected, StringComparison.OrdinalIgnoreCase);
627+
Log($"^^^ TLS pinned validation: expected={expected}, presented={presentedThumbprint}, match={matches}");
628+
return matches;
625629
}
626630

631+
Log($"^^^ TLS validation: sslPolicyErrors={sslPolicyErrors}, subject={presented.Subject}, thumbprint={presentedThumbprint}");
627632
return sslPolicyErrors == SslPolicyErrors.None;
628633
};
629634

630635
var sslStream = new SslStream(networkStream, leaveInnerStreamOpen: false, validator);
631-
sslStream.AuthenticateAsClient(serverHost);
636+
try
637+
{
638+
Log($"^^^ TLS handshake starting with serverHost='{serverHost}'");
639+
sslStream.AuthenticateAsClient(serverHost);
640+
Log($"^^^ TLS handshake completed successfully");
641+
}
642+
catch (Exception e)
643+
{
644+
Log($"### TLS handshake failed: {e.GetType()}: {e.Message}");
645+
throw;
646+
}
632647
return sslStream;
633648
}
634649

@@ -815,32 +830,44 @@ public static bool TrySendRemoteCommandTcpWithAck(int port, string command, stri
815830
clientSocket.Connect(serverHost, port, TimeSpan.FromSeconds(TcpConnectionTimeoutSeconds));
816831
if (!clientSocket.Connected)
817832
{
818-
Log($"### Socket not connected when trying to send '{command}', closing connection");
833+
Log($"### Socket not connected after Connect() when trying to send '{command}', closing connection");
819834
return false;
820835
}
821836

822-
using Stream stream = CreateOutboundCommandStream(clientSocket, serverHost);
823-
WriteRemoteCommand(stream, command, includePassPhrase: true);
824-
825-
// Wait for server ACK to confirm delivery.
826-
string? responseRaw = ReadSingleLineResponse(stream);
827-
string response = TrimPassPhrasePrefix((responseRaw ?? "").Trim());
828-
Log($"^^^ TCP responseRaw='{responseRaw}', response(after trim)='{response}'");
829-
if (string.Equals(response, "ack", StringComparison.OrdinalIgnoreCase))
837+
Stream stream;
838+
try
830839
{
831-
Log($"^^^ TCP ACK received from {serverHost}:{port}");
832-
return true;
840+
stream = CreateOutboundCommandStream(clientSocket, serverHost);
833841
}
834-
835-
if (string.Equals(response, "lol", StringComparison.OrdinalIgnoreCase))
842+
catch (Exception tlsEx)
836843
{
837-
Log("### Remote returned 'lol' (likely bad passphrase)");
844+
Log($"### Failed to create outbound stream (likely TLS issue): {tlsEx.GetType()}: {tlsEx.Message}");
845+
return false;
838846
}
839-
else
847+
848+
using (stream)
840849
{
841-
Log($"### No ACK received (response='{responseRaw ?? ""}')");
842-
}
850+
WriteRemoteCommand(stream, command, includePassPhrase: true);
851+
852+
// Wait for server ACK to confirm delivery.
853+
string? responseRaw = ReadSingleLineResponse(stream);
854+
string response = TrimPassPhrasePrefix((responseRaw ?? "").Trim());
855+
Log($"^^^ TCP responseRaw='{responseRaw}', response(after trim)='{response}'");
856+
if (string.Equals(response, "ack", StringComparison.OrdinalIgnoreCase))
857+
{
858+
Log($"^^^ TCP ACK received from {serverHost}:{port}");
859+
return true;
860+
}
843861

862+
if (string.Equals(response, "lol", StringComparison.OrdinalIgnoreCase))
863+
{
864+
Log("### Remote returned 'lol' (likely bad passphrase)");
865+
}
866+
else
867+
{
868+
Log($"### No ACK received (response='{responseRaw ?? ""}')");
869+
}
870+
}
844871
try
845872
{
846873
clientSocket.Shutdown(SocketShutdown.Both);

DesktopShell/TCPServer.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,6 @@ private void HandleClientComm(object client)
214214
do
215215
{
216216
string receivedString = ReadStream(tcpClient, clientStream, token);
217-
GlobalVar.Log($"@@@ HandleClientComm()");
218217
if (receivedString.Equals(""))
219218
{
220219
Thread.Sleep(GlobalVar.WebBrowserLaunchDelayMs);

0 commit comments

Comments
 (0)