TorMonitor: Use HttpClientFactory to obtain TorHttpClient.
MODIFY
WalletWasabi.Gui/Global.cs
WalletWasabi.Gui/Global.cs
@@ -156,9 +156,10 @@ namespace WalletWasabi.Gui
await TorManager.StartAsync(ensureRunning: true).ConfigureAwait(false);
}
- var fallbackRequestTestUri = new Uri(Config.GetFallbackBackendUri(), "/api/software/versions");
-
- HostedServices.Register(new TorMonitor(period: TimeSpan.FromSeconds(3), fallbackRequestTestUri, Config.TorSocks5EndPoint, TorManager), nameof(TorMonitor));
+ var httpClient = (WalletWasabi.Tor.Http.TorHttpClient)Synchronizer.HttpClientFactory.NewHttpClient(() => Config.GetFallbackBackendUri(), isolateStream: false);
+#pragma warning disable CA2000 // Dispose objects before losing scope
+ HostedServices.Register(new TorMonitor(period: TimeSpan.FromSeconds(3), httpClient, TorManager), nameof(TorMonitor));
+#pragma warning restore CA2000 // Dispose objects before losing scope
}
Logger.LogInfo($"{nameof(TorProcessManager)} is initialized.");

MODIFY
WalletWasabi/Tor/TorMonitor.cs
WalletWasabi/Tor/TorMonitor.cs
using System;
-using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
@@ -21,16 +20,14 @@ namespace WalletWasabi.Tor
/// <summary>
/// Creates a new instance of the object.
/// </summary>
- public TorMonitor(TimeSpan period, Uri fallBackTestRequestUri, EndPoint torSocks5EndPoint, TorProcessManager torProcessManager) : base(period)
+ public TorMonitor(TimeSpan period, TorHttpClient httpClient, TorProcessManager torProcessManager) : base(period)
{
- FallBackTestRequestUri = fallBackTestRequestUri;
- TorSocks5EndPoint = torSocks5EndPoint;
+ HttpClient = httpClient;
TorProcessManager = torProcessManager;
}
public static bool RequestFallbackAddressUsage { get; private set; } = false;
- private Uri FallBackTestRequestUri { get; }
- private EndPoint TorSocks5EndPoint { get; }
+ private TorHttpClient HttpClient { get; }
private TorProcessManager TorProcessManager { get; }
/// <inheritdoc/>
@@ -46,12 +43,8 @@ namespace WalletWasabi.Tor
{
if (torEx.RepField == RepField.HostUnreachable)
{
- Uri baseUri = new Uri($"{FallBackTestRequestUri.Scheme}://{FallBackTestRequestUri.DnsSafeHost}");
- using (var client = new TorHttpClient(baseUri, TorSocks5EndPoint))
- {
- using HttpRequestMessage message = new(HttpMethod.Get, FallBackTestRequestUri);
- using HttpResponseMessage _ = await client.SendAsync(message, token).ConfigureAwait(false);
- }
+ Logger.LogInfo("Tor does not work properly. Test fallback URI.");
+ using var _ = await HttpClient.SendAsync(HttpMethod.Get, "/", content: null, token).ConfigureAwait(false);
// Check if it changed in the meantime...
if (TorHttpClient.LatestTorException is TorConnectCommandFailedException torEx2 && torEx2.RepField == RepField.HostUnreachable)
@@ -76,7 +69,7 @@ namespace WalletWasabi.Tor
}
else
{
- Logger.LogInfo($"Tor is running. Waiting for a confirmation that HTTP requests can pass through.");
+ Logger.LogInfo("Tor is running. Waiting for a confirmation that HTTP requests can pass through.");
}
}
}

TorMonitor: Use HttpClientFactory to obtain TorHttpClient.