btcpayserver/BTCPayServer/ExplorerClientProvider.cs

119 lines
4.4 KiB
C#
Raw Normal View History

2020-06-29 04:44:35 +02:00
using System;
using System.Collections.Generic;
using System.Linq;
2020-06-28 10:55:27 +02:00
using System.Net.Http;
using BTCPayServer.Configuration;
2020-06-28 10:55:27 +02:00
using BTCPayServer.HostedServices;
2018-01-12 08:00:31 +01:00
using BTCPayServer.Logging;
2020-06-28 10:55:27 +02:00
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using NBXplorer;
namespace BTCPayServer
{
public class ExplorerClientProvider
{
readonly BTCPayNetworkProvider _NetworkProviders;
public BTCPayNetworkProvider NetworkProviders => _NetworkProviders;
readonly NBXplorerDashboard _Dashboard;
public ExplorerClientProvider(
IHttpClientFactory httpClientFactory,
BTCPayNetworkProvider networkProviders,
IOptions<NBXplorerOptions> nbXplorerOptions,
NBXplorerDashboard dashboard)
{
_Dashboard = dashboard;
_NetworkProviders = networkProviders;
2018-01-12 08:00:31 +01:00
foreach (var setting in nbXplorerOptions.Value.NBXplorerConnectionSettings)
2018-01-12 08:00:31 +01:00
{
var cookieFile = setting.CookieFile;
if (cookieFile.Trim() == "0" || string.IsNullOrEmpty(cookieFile.Trim()))
cookieFile = null;
Logs.Configuration.LogInformation($"{setting.CryptoCode}: Explorer url is {(setting.ExplorerUri.AbsoluteUri)}");
2018-01-12 08:00:31 +01:00
Logs.Configuration.LogInformation($"{setting.CryptoCode}: Cookie file is {(setting.CookieFile ?? "not set")}");
if (setting.ExplorerUri != null)
{
_Clients.TryAdd(setting.CryptoCode.ToUpperInvariant(),
CreateExplorerClient(httpClientFactory.CreateClient(nameof(ExplorerClientProvider)),
_NetworkProviders.GetNetwork<BTCPayNetwork>(setting.CryptoCode), setting.ExplorerUri,
setting.CookieFile));
2018-01-12 08:00:31 +01:00
}
}
}
private static ExplorerClient CreateExplorerClient(HttpClient httpClient, BTCPayNetwork n, Uri uri,
string cookieFile)
2018-01-12 08:00:31 +01:00
{
2019-12-24 08:20:44 +01:00
var explorer = n.NBXplorerNetwork.CreateExplorerClient(uri);
explorer.SetClient(httpClient);
if (cookieFile == null)
2018-01-12 08:00:31 +01:00
{
2019-12-24 08:20:44 +01:00
Logs.Configuration.LogWarning($"{explorer.CryptoCode}: Not using cookie authentication");
2018-01-12 08:00:31 +01:00
explorer.SetNoAuth();
}
2020-06-28 10:55:27 +02:00
if (!explorer.SetCookieAuth(cookieFile))
{
Logs.Configuration.LogWarning(
$"{explorer.CryptoCode}: Using cookie auth against NBXplorer, but {cookieFile} is not found");
}
2018-01-12 08:00:31 +01:00
return explorer;
}
readonly Dictionary<string, ExplorerClient> _Clients = new Dictionary<string, ExplorerClient>();
2018-01-12 08:00:31 +01:00
public ExplorerClient GetExplorerClient(string cryptoCode)
{
var network = _NetworkProviders.GetNetwork<BTCPayNetwork>(cryptoCode);
if (network == null)
return null;
2019-12-24 08:20:44 +01:00
_Clients.TryGetValue(network.NBXplorerNetwork.CryptoCode, out ExplorerClient client);
2018-01-12 08:00:31 +01:00
return client;
}
public ExplorerClient GetExplorerClient(BTCPayNetworkBase network)
{
2018-01-10 10:30:45 +01:00
if (network == null)
throw new ArgumentNullException(nameof(network));
return GetExplorerClient(network.CryptoCode);
}
public bool IsAvailable(BTCPayNetworkBase network)
{
return IsAvailable(network.CryptoCode);
}
public bool IsAvailable(string cryptoCode)
{
2019-12-24 08:20:44 +01:00
cryptoCode = cryptoCode.ToUpperInvariant();
return _Clients.ContainsKey(cryptoCode) && _Dashboard.IsFullySynched(cryptoCode, out var unused);
}
public BTCPayNetwork GetNetwork(string cryptoCode)
{
var network = _NetworkProviders.GetNetwork<BTCPayNetwork>(cryptoCode);
if (network == null)
return null;
2019-12-24 08:20:44 +01:00
if (_Clients.ContainsKey(network.NBXplorerNetwork.CryptoCode))
return network;
return null;
}
public IEnumerable<(BTCPayNetwork, ExplorerClient)> GetAll()
{
foreach (var net in _NetworkProviders.GetAll().OfType<BTCPayNetwork>())
{
2019-12-24 08:20:44 +01:00
if (_Clients.TryGetValue(net.NBXplorerNetwork.CryptoCode, out ExplorerClient explorer))
{
2018-01-12 08:00:31 +01:00
yield return (net, explorer);
}
}
}
}
}