Make TorHttpClient work with localhost
MODIFY
MagicalCryptoWallet.Tests/RegTests.cs
MagicalCryptoWallet.Tests/RegTests.cs
@@ -4,6 +4,7 @@ using MagicalCryptoWallet.Logging;
using MagicalCryptoWallet.Models;
using MagicalCryptoWallet.Services;
using MagicalCryptoWallet.Tests.NodeBuilding;
+using MagicalCryptoWallet.TorSocks5;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using NBitcoin;
@@ -15,6 +16,7 @@ using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
+using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading;
@@ -66,10 +68,11 @@ namespace MagicalCryptoWallet.Tests
var firstHash = Global.RpcClient.GetBlockHash(0);
while (true)
{
- using (var client = new HttpClient() { BaseAddress = new Uri(Fixture.BackendEndPoint) })
- using (var request = await client.GetAsync("/api/v1/btc/Blockchain/filters/" + firstHash))
+ using (var client = new TorHttpClient(new Uri(Fixture.BackendEndPoint)))
+ using (var response = await client.SendAsync(HttpMethod.Get, "/api/v1/btc/Blockchain/filters/" + firstHash))
{
- var filters = await request.Content.ReadAsJsonAsync<List<string>>();
+ Assert.Equal(HttpStatusCode.OK, response.StatusCode);
+ var filters = await response.Content.ReadAsJsonAsync<List<string>>();
var filterCount = filters.Count();
if (filterCount >= 101)
{
@@ -86,12 +89,12 @@ namespace MagicalCryptoWallet.Tests
[Fact]
public async void GetExchangeRatesAsyncAsync()
{
- using (var client = new HttpClient() { BaseAddress = new Uri(Fixture.BackendEndPoint) })
- using (var res = await client.GetAsync("/api/v1/btc/Blockchain/exchange-rates"))
+ using (var client = new TorHttpClient(new Uri(Fixture.BackendEndPoint)))
+ using (var response = await client.SendAsync(HttpMethod.Get, "/api/v1/btc/Blockchain/exchange-rates"))
{
- Assert.True(res.IsSuccessStatusCode);
+ Assert.True(response.IsSuccessStatusCode);
- var exchangeRates = await res.Content.ReadAsJsonAsync<List<ExchangeRate>>();
+ var exchangeRates = await response.Content.ReadAsJsonAsync<List<ExchangeRate>>();
Assert.Single(exchangeRates);
var rate = exchangeRates[0];
@@ -113,12 +116,12 @@ namespace MagicalCryptoWallet.Tests
var signedTx = await Global.RpcClient.SignRawTransactionAsync(tx);
var content = new StringContent($"'{signedTx.ToHex()}'", Encoding.UTF8, "application/json");
- using (var client = new HttpClient() { BaseAddress = new Uri(Fixture.BackendEndPoint) })
- using (var res = await client.PostAsync("/api/v1/btc/Blockchain/broadcast", content))
+ using (var client = new TorHttpClient(new Uri(Fixture.BackendEndPoint)))
+ using (var response = await client.SendAsync(HttpMethod.Post, "/api/v1/btc/Blockchain/broadcast", content))
{
- Assert.False(res.IsSuccessStatusCode);
- Assert.Equal(System.Net.HttpStatusCode.BadRequest, res.StatusCode);
+ Assert.False(response.IsSuccessStatusCode);
+ Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
}
}
@@ -130,11 +133,11 @@ namespace MagicalCryptoWallet.Tests
var utxo = utxos[0];
var tx = await Global.RpcClient.GetRawTransactionAsync(utxo.OutPoint.Hash);
var content = new StringContent($"'{tx.ToHex()}'", Encoding.UTF8, "application/json");
- using (var client = new HttpClient() { BaseAddress = new Uri(Fixture.BackendEndPoint) })
- using (var res = await client.PostAsync("/api/v1/btc/Blockchain/broadcast", content))
+ using (var client = new TorHttpClient(new Uri(Fixture.BackendEndPoint)))
+ using (var response = await client.SendAsync(HttpMethod.Post, "/api/v1/btc/Blockchain/broadcast", content))
{
- Assert.True(res.IsSuccessStatusCode);
- Assert.Equal("\"Transaction is already in the blockchain.\"", await res.Content.ReadAsStringAsync());
+ Assert.True(response.IsSuccessStatusCode);
+ Assert.Equal("\"Transaction is already in the blockchain.\"", await response.Content.ReadAsStringAsync());
}
}
@@ -142,12 +145,12 @@ namespace MagicalCryptoWallet.Tests
public async void BroadcastInvalidTxAsync()
{
var content = new StringContent($"''", Encoding.UTF8, "application/json");
- using (var client = new HttpClient() { BaseAddress = new Uri(Fixture.BackendEndPoint) })
- using (var res = await client.PostAsync("/api/v1/btc/Blockchain/broadcast", content))
+ using (var client = new TorHttpClient(new Uri(Fixture.BackendEndPoint)))
+ using (var response = await client.SendAsync(HttpMethod.Post, "/api/v1/btc/Blockchain/broadcast", content))
{
- Assert.False(res.IsSuccessStatusCode);
- Assert.Equal(System.Net.HttpStatusCode.BadRequest, res.StatusCode);
- Assert.Equal("\"Invalid hex.\"", await res.Content.ReadAsStringAsync());
+ Assert.False(response.IsSuccessStatusCode);
+ Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
+ Assert.Equal("\"Invalid hex.\"", await response.Content.ReadAsStringAsync());
}
}

MODIFY
MagicalCryptoWallet/TorSocks5/TorHttpClient.cs
MagicalCryptoWallet/TorSocks5/TorHttpClient.cs
@@ -33,7 +33,14 @@ namespace MagicalCryptoWallet.TorSocks5
public TorHttpClient(Uri baseUri, IPEndPoint torSocks5EndPoint = null, bool isolateStream = false)
{
DestinationUri = Guard.NotNull(nameof(baseUri), baseUri);
+ if(DestinationUri.IsLoopback)
+ {
+ TorSocks5EndPoint = null;
+ }
+ else
+ {
TorSocks5EndPoint = torSocks5EndPoint ?? new IPEndPoint(IPAddress.Loopback, 9050);
+ }
TorSocks5Client = null;
IsolateStream = isolateStream;
}

Make TorHttpClient work with localhost