try fix tor socks5 connection

This commit is contained in:
Kukks 2020-03-14 12:21:12 +01:00
parent 56d5e6f99f
commit 1a62ee9260
8 changed files with 74 additions and 70 deletions

View File

@ -51,6 +51,7 @@ services:
- customer_lnd
- merchant_lnd
- sshd
- tor
sshd:
build:
@ -318,6 +319,19 @@ services:
- "bitcoin_datadir:/deps/.bitcoin"
links:
- bitcoind
tor:
restart: unless-stopped
image: btcpayserver/tor:0.4.1.5
container_name: tor
environment:
TOR_PASSWORD: btcpayserver
ports:
- "9050" # SOCKS
- "9051" # Tor Control
volumes:
- "tor_datadir:/home/tor/.tor"
- "torrcdir:/usr/local/etc/tor"
- "tor_servicesdir:/var/lib/tor/hidden_services"
volumes:
sshd_datadir:
@ -328,3 +342,6 @@ volumes:
lightning_charge_datadir:
customer_lnd_datadir:
merchant_lnd_datadir:
tor_datadir:
torrcdir:
tor_servicesdir:

View File

@ -35,6 +35,7 @@
<PackageReference Include="BundlerMinifier.Core" Version="3.2.435" />
<PackageReference Include="BundlerMinifier.TagHelpers" Version="3.2.435" />
<PackageReference Include="HtmlSanitizer" Version="4.0.217" />
<PackageReference Include="HttpToSocks5Proxy" Version="1.4.0" />
<PackageReference Include="LedgerWallet" Version="2.0.0.5" />
<PackageReference Include="Microsoft.Extensions.Logging.Filter" Version="1.1.2" />
<PackageReference Include="Microsoft.NetCore.Analyzers" Version="2.9.8">
@ -51,7 +52,6 @@
<PackageReference Include="Serilog" Version="2.9.0" />
<PackageReference Include="Serilog.AspNetCore" Version="3.2.0" />
<PackageReference Include="Serilog.Sinks.File" Version="4.1.0" />
<PackageReference Include="SocksWebProxy" Version="1.0.5" />
<PackageReference Include="SSH.NET" Version="2016.1.0" />
<PackageReference Include="Text.Analyzers" Version="2.6.4">
<PrivateAssets>all</PrivateAssets>

View File

@ -156,24 +156,22 @@ namespace BTCPayServer.Controllers
return result.PSBT;
}
private async Task<PSBT> TryGetBPProposedTX(PSBT psbt, DerivationSchemeSettings derivationSchemeSettings, BTCPayNetwork btcPayNetwork)
{
if (TempData.TryGetValue( "bpu", out var bpu) && !string.IsNullOrEmpty(bpu?.ToString()) && Uri.TryCreate(bpu.ToString(), UriKind.Absolute, out var endpoint))
{
TempData.Remove("bpu");
HttpClient httpClient;
if (endpoint.IsOnion() )
{
httpClient = await _socketFactory.SocksClient;
if (httpClient == null)
var httpClient = _socks5HttpClientFactory.CreateClient("payjoin");
if (endpoint.IsOnion() && httpClient == null)
{
return null;
}
}
else
{
httpClient = _httpClientFactory.CreateClient("bpu");
httpClient = _httpClientFactory.CreateClient("payjoin");
}
var cloned = psbt.Clone();

View File

@ -50,7 +50,7 @@ namespace BTCPayServer.Controllers
private readonly WalletReceiveStateService _WalletReceiveStateService;
private readonly EventAggregator _EventAggregator;
private readonly SettingsRepository _settingsRepository;
private readonly SocketFactory _socketFactory;
private readonly Socks5HttpClientFactory _socks5HttpClientFactory;
private readonly IHttpClientFactory _httpClientFactory;
public RateFetcher RateFetcher { get; }
@ -70,7 +70,7 @@ namespace BTCPayServer.Controllers
WalletReceiveStateService walletReceiveStateService,
EventAggregator eventAggregator,
SettingsRepository settingsRepository,
SocketFactory socketFactory,
Socks5HttpClientFactory socks5HttpClientFactory,
IHttpClientFactory httpClientFactory)
{
_currencyTable = currencyTable;
@ -88,7 +88,7 @@ namespace BTCPayServer.Controllers
_WalletReceiveStateService = walletReceiveStateService;
_EventAggregator = eventAggregator;
_settingsRepository = settingsRepository;
_socketFactory = socketFactory;
_socks5HttpClientFactory = socks5HttpClientFactory;
_httpClientFactory = httpClientFactory;
}

View File

@ -68,6 +68,7 @@ namespace BTCPayServer.Hosting
services.TryAddSingleton<SettingsRepository>();
services.TryAddSingleton<TorServices>();
services.TryAddSingleton<SocketFactory>();
services.TryAddSingleton<Socks5HttpClientFactory>();
services.TryAddSingleton<LightningClientFactoryService>();
services.TryAddSingleton<InvoicePaymentNotification>();
services.TryAddSingleton<BTCPayServerOptions>(o =>

View File

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

View File

@ -1,30 +1,64 @@
using System;
using System.Linq;
using NBitcoin;
using System.Collections.Concurrent;
using System.Net;
using System.Net.Http;
using System.Net.Sockets;
using System.Threading;
using System.Threading.Tasks;
using BTCPayServer.Configuration;
using com.LandonKey.SocksWebProxy;
using com.LandonKey.SocksWebProxy.Proxy;
using Microsoft.Extensions.Logging;
using NBitcoin.Logging;
using MihaZupan;
using NBitcoin.Protocol.Connectors;
using NBitcoin.Protocol;
namespace BTCPayServer.Services
{
public class Socks5HttpClientFactory : IHttpClientFactory
{
private readonly BTCPayServerOptions _options;
public Socks5HttpClientFactory(BTCPayServerOptions options)
{
_options = options;
}
private static (string Host, int Port)? ToParts(EndPoint endpoint)
{
switch (endpoint)
{
case DnsEndPoint dns:
return (dns.Host, dns.Port);
case IPEndPoint ipEndPoint:
return (ipEndPoint.Address.ToString(), ipEndPoint.Port);
}
return null;
}
private ConcurrentDictionary<string, HttpClient> cachedClients = new ConcurrentDictionary<string, HttpClient>();
public HttpClient CreateClient(string name)
{
return cachedClients.GetOrAdd(name, s =>
{
var parts = ToParts(_options.SocksEndpoint);
if (!parts.HasValue)
{
return null;
}
var proxy = new HttpToSocks5Proxy(parts.Value.Host, parts.Value.Port);
return new HttpClient(
new HttpClientHandler {Proxy = proxy, },
true);
});
}
}
public class SocketFactory
{
private readonly BTCPayServerOptions _options;
public readonly Task<HttpClient> SocksClient;
public SocketFactory(BTCPayServerOptions options)
{
_options = options;
SocksClient = CreateHttpClientUsingSocks();
}
public async Task<Socket> ConnectAsync(EndPoint endPoint, CancellationToken cancellationToken)
@ -70,51 +104,5 @@ namespace BTCPayServer.Services
{
}
}
private Task<HttpClient> CreateHttpClientUsingSocks()
{
return Task.Run(() =>
{
try
{
var proxyConfig = new ProxyConfig() {Version = ProxyConfig.SocksVersion.Five};
switch (_options.SocksEndpoint)
{
case null:
return null;
case IPEndPoint ipEndPoint:
proxyConfig.SocksPort = ipEndPoint.Port;
proxyConfig.SocksAddress = ipEndPoint.Address;
break;
case DnsEndPoint dnsEndPoint:
proxyConfig.SocksPort = dnsEndPoint.Port;
var ip = Dns.GetHostEntry(dnsEndPoint.Host).AddressList
.SingleOrDefault(address => address.AddressFamily == AddressFamily.InterNetwork);
if (ip == null)
{
Logs.Utils.LogWarning( $"Could not find ip for {dnsEndPoint.Host}");
return null;
}
proxyConfig.SocksAddress = ip;
break;
default:
return null;
}
Logs.Utils.LogWarning( $"Created socks proxied http client!");
return new HttpClient(new HttpClientHandler
{
Proxy = new SocksWebProxy(proxyConfig), UseProxy = true
});
}
catch (Exception e)
{
Logs.Utils.LogError(e, "Could not create Tor client");
return null;
}
});
}
}
}

View File

@ -52,7 +52,7 @@
<tr class="@(payment.Replaced ? "linethrough" : "")" >
<td>@payment.Crypto</td>
<td>@payment.DepositAddress</td>
<td>@payment.CryptoPaymentData.GetValue() @(payment.CryptoPaymentData.PayJoinSelfContributedAmount == 0? string.Empty : $"<br/>(+ Payjoin {payment.CryptoPaymentData.PayJoinSelfContributedAmount })")</td>
<td>@payment.CryptoPaymentData.GetValue() @Safe.Raw(payment.CryptoPaymentData.PayJoinSelfContributedAmount == 0? string.Empty : $"<br/>(Payjoin {payment.CryptoPaymentData.PayJoinSelfContributedAmount})")</td>
<td>
<div class="wraptextAuto">
<a href="@payment.TransactionLink" target="_blank">