Extract TcpClientConnector
MODIFY
WalletWasabi/Tor/Control/TorControlClientFactory.cs
WalletWasabi/Tor/Control/TorControlClientFactory.cs
@@ -11,6 +11,7 @@ using WalletWasabi.Crypto.Randomness;
using WalletWasabi.Logging;
using WalletWasabi.Tor.Control.Exceptions;
using WalletWasabi.Tor.Control.Messages;
+using WalletWasabi.Tor.Control.Utils;
namespace WalletWasabi.Tor.Control;
@@ -39,7 +40,16 @@ public class TorControlClientFactory
/// <exception cref="TorControlException">If TCP connection cannot be established OR if authentication fails for some reason.</exception>
public async Task<TorControlClient> ConnectAndAuthenticateAsync(EndPoint endPoint, string cookieString, CancellationToken cancellationToken = default)
{
- TcpClient tcpClient = Connect(endPoint);
+ TcpClient tcpClient;
+ try
+ {
+ tcpClient = TcpClientConnector.Connect(endPoint);
+ }
+ catch (Exception e)
+ {
+ Logger.LogError($"Failed to connect to the Tor control: '{endPoint}'.", e);
+ throw new TorControlException($"Failed to connect to the Tor control: '{endPoint}'.", e);
+ }
TorControlClient? clientToDispose = null;
try
@@ -115,32 +125,4 @@ public class TorControlClientFactory
return controlClient;
}
-
- /// <summary>
- /// Connects to Tor control using a TCP client.
- /// </summary>
- private TcpClient Connect(EndPoint endPoint)
- {
- try
- {
- TcpClient tcpClient = new();
- switch (endPoint)
- {
- case DnsEndPoint dnsEndPoint:
- tcpClient.Connect(dnsEndPoint.Host, dnsEndPoint.Port);
- break;
- case IPEndPoint ipEndPoint:
- tcpClient.Connect(ipEndPoint);
- break;
- default:
- throw new ArgumentOutOfRangeException(nameof(endPoint));
- }
- return tcpClient;
- }
- catch (Exception e)
- {
- Logger.LogError($"Failed to connect to the Tor control: '{endPoint}'.", e);
- throw new TorControlException($"Failed to connect to the Tor control: '{endPoint}'.", e);
- }
- }
}

ADD
WalletWasabi/Tor/Control/Utils/TcpClientConnector.cs
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Net;
+using System.Net.Sockets;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace WalletWasabi.Tor.Control.Utils;
+
+public static class TcpClientConnector
+{
+ /// <summary>
+ /// Connects to Tor control using a TCP client.
+ /// </summary>
+ public static TcpClient Connect(EndPoint endPoint)
+ {
+ TcpClient tcpClient = new();
+ switch (endPoint)
+ {
+ case DnsEndPoint dnsEndPoint:
+ tcpClient.Connect(dnsEndPoint.Host, dnsEndPoint.Port);
+ break;
+
+ case IPEndPoint ipEndPoint:
+ tcpClient.Connect(ipEndPoint);
+ break;
+
+ default:
+ throw new ArgumentOutOfRangeException(nameof(endPoint));
+ }
+ return tcpClient;
+ }
+}

Extract TcpClientConnector