fix tor client creator

This commit is contained in:
Kukks
2020-03-13 15:10:26 +01:00
parent 2e3a0706ee
commit f1821636db
3 changed files with 47 additions and 16 deletions

View File

@@ -163,13 +163,13 @@ namespace BTCPayServer.Controllers
{ {
TempData.Remove("bpu"); TempData.Remove("bpu");
HttpClient httpClient; HttpClient httpClient;
if (endpoint.IsOnion() && _socketFactory.SocksClient!= null) if (endpoint.IsOnion() )
{ {
if ( _socketFactory.SocksClient == null) httpClient = await _socketFactory.SocksClient;
if (httpClient == null)
{ {
return null; return null;
} }
httpClient = _socketFactory.SocksClient;
} }
else else
{ {

View File

@@ -51,7 +51,8 @@
"BTCPAY_SSHCONNECTION": "root@127.0.0.1:21622", "BTCPAY_SSHCONNECTION": "root@127.0.0.1:21622",
"BTCPAY_SSHPASSWORD": "opD3i2282D", "BTCPAY_SSHPASSWORD": "opD3i2282D",
"BTCPAY_DEBUGLOG": "debug.log", "BTCPAY_DEBUGLOG": "debug.log",
"BTCPAY_TORRCFILE": "../BTCPayServer.Tests/TestData/Tor/torrc" "BTCPAY_TORRCFILE": "../BTCPayServer.Tests/TestData/Tor/torrc",
"BTCPAY_SOCKSENDPOINT": "tor:9050"
}, },
"applicationUrl": "https://localhost:14142/" "applicationUrl": "https://localhost:14142/"
} }

View File

@@ -1,4 +1,6 @@
using NBitcoin; using System;
using System.Linq;
using NBitcoin;
using System.Net; using System.Net;
using System.Net.Http; using System.Net.Http;
using System.Net.Sockets; using System.Net.Sockets;
@@ -15,12 +17,14 @@ namespace BTCPayServer.Services
public class SocketFactory public class SocketFactory
{ {
private readonly BTCPayServerOptions _options; private readonly BTCPayServerOptions _options;
public readonly HttpClient SocksClient; public readonly Task<HttpClient> SocksClient;
public SocketFactory(BTCPayServerOptions options) public SocketFactory(BTCPayServerOptions options)
{ {
_options = options; _options = options;
SocksClient = CreateHttpClientUsingSocks(); SocksClient = CreateHttpClientUsingSocks();
} }
public async Task<Socket> ConnectAsync(EndPoint endPoint, CancellationToken cancellationToken) public async Task<Socket> ConnectAsync(EndPoint endPoint, CancellationToken cancellationToken)
{ {
DefaultEndpointConnector connector = new DefaultEndpointConnector(); DefaultEndpointConnector connector = new DefaultEndpointConnector();
@@ -32,6 +36,7 @@ namespace BTCPayServer.Services
SocksEndpoint = _options.SocksEndpoint SocksEndpoint = _options.SocksEndpoint
}); });
} }
var socket = new Socket(SocketType.Stream, ProtocolType.Tcp); var socket = new Socket(SocketType.Stream, ProtocolType.Tcp);
try try
{ {
@@ -41,6 +46,7 @@ namespace BTCPayServer.Services
{ {
SafeCloseSocket(socket); SafeCloseSocket(socket);
} }
return socket; return socket;
} }
@@ -53,6 +59,7 @@ namespace BTCPayServer.Services
catch catch
{ {
} }
try try
{ {
socket.Dispose(); socket.Dispose();
@@ -62,19 +69,42 @@ namespace BTCPayServer.Services
} }
} }
private HttpClient CreateHttpClientUsingSocks() private Task<HttpClient> CreateHttpClientUsingSocks()
{ {
if (_options.SocksEndpoint == null) return Task.Run(() =>
{
var proxyConfig = new ProxyConfig() {Version = ProxyConfig.SocksVersion.Five};
switch (_options.SocksEndpoint)
{
case null:
return null; return null;
return new HttpClient(new HttpClientHandler case IPEndPoint ipEndPoint:
proxyConfig.SocksPort = ipEndPoint.Port;
proxyConfig.SocksAddress = ipEndPoint.Address;
break;
case DnsEndPoint dnsEndPoint:
try
{ {
Proxy = new SocksWebProxy(new ProxyConfig() proxyConfig.SocksPort = dnsEndPoint.Port;
var ip = Dns.GetHostEntry(dnsEndPoint.Host).AddressList
.SingleOrDefault(address => address.AddressFamily == AddressFamily.InterNetwork);
if (ip == null)
{ {
Version = ProxyConfig.SocksVersion.Five, return null;
SocksAddress = _options.SocksEndpoint.AsOnionCatIPEndpoint().Address, }
SocksPort = _options.SocksEndpoint.AsOnionCatIPEndpoint().Port,
}), proxyConfig.SocksAddress = ip;
UseProxy = true break;
}
catch (Exception e)
{
return null;
}
default:
return null;
}
return new HttpClient(new HttpClientHandler {Proxy = new SocksWebProxy(proxyConfig), UseProxy = true});
}); });
} }
} }