Split Clearnet HTTP client implementation from TorHttpClient.
ADD
WalletWasabi/Tor/Http/ClearnetHttpClient.cs
+using System.Net;
+using System.Net.Http;
+using System.Threading;
+using System.Threading.Tasks;
+
+namespace WalletWasabi.Tor.Http
+{
+ /// <summary>
+ /// HTTP client implementation based on .NET's <see cref="HttpClient"/> which provides least privacy for Wasabi users,
+ /// as HTTP requests are being sent over clearnet.
+ /// </summary>
+ public class ClearnetHttpClient : IHttpClient
+ {
+ /// <summary>This field is temporary and should be ultimately removed.</summary>
+ public static ClearnetHttpClient Instance = new ClearnetHttpClient();
+
+ private ClearnetHttpClient()
+ {
+ HttpClient = new HttpClient(new HttpClientHandler()
+ {
+ AutomaticDecompression = DecompressionMethods.GZip,
+ SslProtocols = IHttpClient.SupportedSslProtocols
+ });
+ }
+
+ /// <summary>Predefined HTTP client that handles HTTP requests when Tor is disabled.</summary>
+ private HttpClient HttpClient { get; }
+
+ /// <inheritdoc cref="HttpClient.SendAsync(HttpRequestMessage, CancellationToken)"/>
+ public async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken token = default)
+ {
+ return await HttpClient.SendAsync(request, token).ConfigureAwait(false);
+ }
+ }
+}

ADD
WalletWasabi/Tor/Http/IHttpClient.cs
+using System.Net.Http;
+using System.Security.Authentication;
+using System.Threading;
+using System.Threading.Tasks;
+
+namespace WalletWasabi.Tor.Http
+{
+ /// <summary>
+ /// Interface defining HTTP client capable of sending HTTP requests.
+ /// </summary>
+ public interface IHttpClient
+ {
+ /// <summary>TLS protocols we support for both clearnet and Tor proxy.</summary>
+ static readonly SslProtocols SupportedSslProtocols = SslProtocols.Tls | SslProtocols.Tls11 | SslProtocols.Tls12;
+
+ /// <inheritdoc cref="HttpClient.SendAsync(HttpRequestMessage, CancellationToken)"/>
+ Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken token = default);
+ }
+}

MODIFY
WalletWasabi/Tor/Http/Interfaces/ITorHttpClient.cs
WalletWasabi/Tor/Http/Interfaces/ITorHttpClient.cs
@@ -6,7 +6,7 @@ using System.Threading.Tasks;
namespace WalletWasabi.Tor.Http.Interfaces
{
- public interface ITorHttpClient : IDisposable
+ public interface ITorHttpClient : IHttpClient, IDisposable
{
Uri DestinationUri { get; }
Func<Uri> DestinationUriAction { get; }

MODIFY
WalletWasabi/Tor/Http/TorHttpClient.cs
WalletWasabi/Tor/Http/TorHttpClient.cs
@@ -23,23 +23,10 @@ using WalletWasabi.Tor.Socks5.Models.Fields.OctetFields;
namespace WalletWasabi.Tor.Http
{
- /// <summary>
- /// TODO: What about request timeout?
- /// </summary>
public class TorHttpClient : ITorHttpClient, IDisposable
{
private static DateTimeOffset? TorDoesntWorkSinceBacking = null;
- /// <summary>TLS protocols we support for both clearnet and Tor proxy.</summary>
- private static readonly SslProtocols SupportedSslProtocols = SslProtocols.Tls | SslProtocols.Tls11 | SslProtocols.Tls12;
-
- /// <summary>Predefined HTTP client that handles HTTP requests when Tor is disabled.</summary>
- private static readonly HttpClient ClearnetHttpClient = new HttpClient(new HttpClientHandler()
- {
- AutomaticDecompression = DecompressionMethods.GZip,
- SslProtocols = SupportedSslProtocols
- });
-
private volatile bool _disposedValue = false; // To detect redundant calls
public TorHttpClient(Uri baseUri, EndPoint? torSocks5EndPoint, bool isolateStream = false):
@@ -89,13 +76,7 @@ namespace WalletWasabi.Tor.Http
private static async Task<HttpResponseMessage> ClearnetRequestAsync(HttpRequestMessage request)
{
- request.Version = HttpProtocol.HTTP11.Version;
- request.Headers.AcceptCharset.Add(new StringWithQualityHeaderValue("utf-8"));
-
- //request.Headers.TransferEncodingChunked = true;
- var response = await ClearnetHttpClient.SendAsync(request).ConfigureAwait(false);
-
- return response;
+ return await ClearnetHttpClient.Instance.SendAsync(request).ConfigureAwait(false);
}
/// <remarks>
@@ -237,7 +218,7 @@ namespace WalletWasabi.Tor.Http
.AuthenticateAsClientAsync(
host,
new X509CertificateCollection(),
- SupportedSslProtocols,
+ IHttpClient.SupportedSslProtocols,
checkCertificateRevocation: true).ConfigureAwait(false);
stream = sslStream;
}
@@ -271,7 +252,7 @@ namespace WalletWasabi.Tor.Http
}
}
- var requestString = await request.ToHttpStringAsync().ConfigureAwait(false);
+ string requestString = await request.ToHttpStringAsync().ConfigureAwait(false);
var bytes = Encoding.UTF8.GetBytes(requestString);

Split Clearnet HTTP client implementation from TorHttpClient.