Make sure BTCPay show TOR lightning node info if the site is accessed through TOR

This commit is contained in:
nicolas.dorier 2019-03-17 21:28:47 +09:00
parent e3a8892d24
commit a4051dac72
8 changed files with 23 additions and 9 deletions

View file

@ -33,7 +33,7 @@
<EmbeddedResource Include="Currencies.txt" /> <EmbeddedResource Include="Currencies.txt" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="BTCPayServer.Lightning.All" Version="1.1.0.13" /> <PackageReference Include="BTCPayServer.Lightning.All" Version="1.1.0.14" />
<PackageReference Include="BuildBundlerMinifier" Version="2.7.385" /> <PackageReference Include="BuildBundlerMinifier" Version="2.7.385" />
<PackageReference Include="DigitalRuby.ExchangeSharp" Version="0.5.3" /> <PackageReference Include="DigitalRuby.ExchangeSharp" Version="0.5.3" />
<PackageReference Include="HtmlSanitizer" Version="4.0.199" /> <PackageReference Include="HtmlSanitizer" Version="4.0.199" />

View file

@ -242,6 +242,7 @@ namespace BTCPayServer.Controllers
paymentMethod.Network = network; paymentMethod.Network = network;
paymentMethod.SetId(supportedPaymentMethod.PaymentId); paymentMethod.SetId(supportedPaymentMethod.PaymentId);
paymentMethod.Rate = rate.BidAsk.Bid; paymentMethod.Rate = rate.BidAsk.Bid;
paymentMethod.PreferOnion = this.Request.IsOnion();
var paymentDetails = await handler.CreatePaymentMethodDetails(supportedPaymentMethod, paymentMethod, store, network, preparePayment); var paymentDetails = await handler.CreatePaymentMethodDetails(supportedPaymentMethod, paymentMethod, store, network, preparePayment);
paymentMethod.SetPaymentMethodDetails(paymentDetails); paymentMethod.SetPaymentMethodDetails(paymentDetails);

View file

@ -43,7 +43,7 @@ namespace BTCPayServer.Controllers
var paymentMethodDetails = GetExistingLightningSupportedPaymentMethod(cryptoCode, store); var paymentMethodDetails = GetExistingLightningSupportedPaymentMethod(cryptoCode, store);
var network = _BtcPayNetworkProvider.GetNetwork(cryptoCode); var network = _BtcPayNetworkProvider.GetNetwork(cryptoCode);
var nodeInfo = var nodeInfo =
await _LightningLikePaymentHandler.GetNodeInfo(paymentMethodDetails, await _LightningLikePaymentHandler.GetNodeInfo(this.Request.IsOnion(), paymentMethodDetails,
network); network);
return View(new ShowLightningNodeInfoViewModel() return View(new ShowLightningNodeInfoViewModel()

View file

@ -155,7 +155,7 @@ namespace BTCPayServer.Controllers
var handler = (LightningLikePaymentHandler)_ServiceProvider.GetRequiredService<IPaymentMethodHandler<Payments.Lightning.LightningSupportedPaymentMethod>>(); var handler = (LightningLikePaymentHandler)_ServiceProvider.GetRequiredService<IPaymentMethodHandler<Payments.Lightning.LightningSupportedPaymentMethod>>();
try try
{ {
var info = await handler.GetNodeInfo(paymentMethod, network); var info = await handler.GetNodeInfo(this.Request.IsOnion(), paymentMethod, network);
if (!vm.SkipPortTest) if (!vm.SkipPortTest)
{ {
using (CancellationTokenSource cts = new CancellationTokenSource(TimeSpan.FromSeconds(20))) using (CancellationTokenSource cts = new CancellationTokenSource(TimeSpan.FromSeconds(20)))

View file

@ -165,6 +165,11 @@ namespace BTCPayServer
(derivationStrategyBase is DirectDerivationStrategy direct) && direct.Segwit; (derivationStrategyBase is DirectDerivationStrategy direct) && direct.Segwit;
} }
public static bool IsOnion(this HttpRequest request)
{
return request.Host.Host.EndsWith(".onion", StringComparison.OrdinalIgnoreCase);
}
public static string GetAbsoluteRoot(this HttpRequest request) public static string GetAbsoluteRoot(this HttpRequest request)
{ {
return string.Concat( return string.Concat(

View file

@ -25,7 +25,7 @@ namespace BTCPayServer.Payments.Lightning
public override async Task<IPaymentMethodDetails> CreatePaymentMethodDetails(LightningSupportedPaymentMethod supportedPaymentMethod, PaymentMethod paymentMethod, StoreData store, BTCPayNetwork network, object preparePaymentObject) public override async Task<IPaymentMethodDetails> CreatePaymentMethodDetails(LightningSupportedPaymentMethod supportedPaymentMethod, PaymentMethod paymentMethod, StoreData store, BTCPayNetwork network, object preparePaymentObject)
{ {
var storeBlob = store.GetStoreBlob(); var storeBlob = store.GetStoreBlob();
var test = GetNodeInfo(supportedPaymentMethod, network); var test = GetNodeInfo(paymentMethod.PreferOnion, supportedPaymentMethod, network);
var invoice = paymentMethod.ParentEntity; var invoice = paymentMethod.ParentEntity;
var due = Extensions.RoundUp(invoice.ProductInformation.Price / paymentMethod.Rate, 8); var due = Extensions.RoundUp(invoice.ProductInformation.Price / paymentMethod.Rate, 8);
var client = supportedPaymentMethod.CreateClient(network); var client = supportedPaymentMethod.CreateClient(network);
@ -63,7 +63,7 @@ namespace BTCPayServer.Payments.Lightning
}; };
} }
public async Task<NodeInfo> GetNodeInfo(LightningSupportedPaymentMethod supportedPaymentMethod, BTCPayNetwork network) public async Task<NodeInfo> GetNodeInfo(bool preferOnion, LightningSupportedPaymentMethod supportedPaymentMethod, BTCPayNetwork network)
{ {
if (!_Dashboard.IsFullySynched(network.CryptoCode, out var summary)) if (!_Dashboard.IsFullySynched(network.CryptoCode, out var summary))
throw new PaymentMethodUnavailableException($"Full node not available"); throw new PaymentMethodUnavailableException($"Full node not available");
@ -84,8 +84,8 @@ namespace BTCPayServer.Payments.Lightning
{ {
throw new PaymentMethodUnavailableException($"Error while connecting to the API ({ex.Message})"); throw new PaymentMethodUnavailableException($"Error while connecting to the API ({ex.Message})");
} }
var nodeInfo = info.NodeInfoList.FirstOrDefault(i => i.IsTor == preferOnion) ?? info.NodeInfoList.FirstOrDefault();
if (info.NodeInfo == null) if (nodeInfo == null)
{ {
throw new PaymentMethodUnavailableException($"No lightning node public address has been configured"); throw new PaymentMethodUnavailableException($"No lightning node public address has been configured");
} }
@ -96,7 +96,7 @@ namespace BTCPayServer.Payments.Lightning
throw new PaymentMethodUnavailableException($"The lightning node is not synched ({blocksGap} blocks left)"); throw new PaymentMethodUnavailableException($"The lightning node is not synched ({blocksGap} blocks left)");
} }
return info.NodeInfo; return nodeInfo;
} }
} }

View file

@ -717,6 +717,12 @@ namespace BTCPayServer.Services.Invoices
[Obsolete("Use GetId().PaymentType instead")] [Obsolete("Use GetId().PaymentType instead")]
public string PaymentType { get; set; } public string PaymentType { get; set; }
/// <summary>
/// We only use this to pass a singleton asking to the payment handler to prefer payments through TOR, we don't really
/// need to save this information
/// </summary>
[JsonIgnore]
public bool PreferOnion { get; set; }
public PaymentMethodId GetId() public PaymentMethodId GetId()
{ {

View file

@ -100,7 +100,9 @@
{ {
<tr> <tr>
<td>@s.Name</td> <td>@s.Name</td>
<td style="text-align:right">
<a href="@s.Link" target="_blank">See information</a> <a href="@s.Link" target="_blank">See information</a>
</td>
</tr> </tr>
} }
</tbody> </tbody>