This commit is contained in:
Kukks
2020-03-13 16:52:50 +01:00
parent f1821636db
commit 56d5e6f99f
4 changed files with 32 additions and 23 deletions

View File

@@ -9,6 +9,8 @@ using System.Threading.Tasks;
using BTCPayServer.Configuration;
using com.LandonKey.SocksWebProxy;
using com.LandonKey.SocksWebProxy.Proxy;
using Microsoft.Extensions.Logging;
using NBitcoin.Logging;
using NBitcoin.Protocol.Connectors;
using NBitcoin.Protocol;
@@ -73,38 +75,45 @@ namespace BTCPayServer.Services
{
return Task.Run(() =>
{
var proxyConfig = new ProxyConfig() {Version = ProxyConfig.SocksVersion.Five};
switch (_options.SocksEndpoint)
try
{
case null:
return null;
case IPEndPoint ipEndPoint:
proxyConfig.SocksPort = ipEndPoint.Port;
proxyConfig.SocksAddress = ipEndPoint.Address;
break;
case DnsEndPoint dnsEndPoint:
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;
}
catch (Exception e)
{
return null;
}
default:
return null;
}
return new HttpClient(new HttpClientHandler {Proxy = new SocksWebProxy(proxyConfig), UseProxy = true});
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;
}
});
}
}