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