From e3a8892d24b53cb49b52c02c9596c5db42b8db57 Mon Sep 17 00:00:00 2001 From: "nicolas.dorier" Date: Sun, 17 Mar 2019 21:07:24 +0900 Subject: [PATCH] Check tor services in the background --- BTCPayServer/Controllers/ServerController.cs | 4 +-- .../TorServicesHostedService.cs | 36 +++++++++++++++++++ BTCPayServer/Hosting/BTCPayServerServices.cs | 1 + BTCPayServer/Services/TorServices.cs | 31 +++++++++++----- BTCPayServer/Views/Server/Services.cshtml | 2 +- 5 files changed, 63 insertions(+), 11 deletions(-) create mode 100644 BTCPayServer/HostedServices/TorServicesHostedService.cs diff --git a/BTCPayServer/Controllers/ServerController.cs b/BTCPayServer/Controllers/ServerController.cs index 3fb17cd2e..275cc86e1 100644 --- a/BTCPayServer/Controllers/ServerController.cs +++ b/BTCPayServer/Controllers/ServerController.cs @@ -446,7 +446,7 @@ namespace BTCPayServer.Controllers } [Route("server/services")] - public async Task Services() + public IActionResult Services() { var result = new ServicesViewModel(); result.ExternalServices = _Options.ExternalServices; @@ -466,7 +466,7 @@ namespace BTCPayServer.Controllers Link = this.Url.Action(nameof(SSHService)) }); } - foreach(var torService in await _torServices.GetServices()) + foreach(var torService in _torServices.Services) { if (torService.VirtualPort == 80) { diff --git a/BTCPayServer/HostedServices/TorServicesHostedService.cs b/BTCPayServer/HostedServices/TorServicesHostedService.cs new file mode 100644 index 000000000..1f08fda2d --- /dev/null +++ b/BTCPayServer/HostedServices/TorServicesHostedService.cs @@ -0,0 +1,36 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using BTCPayServer.Configuration; +using BTCPayServer.Services; +using Microsoft.Extensions.Hosting; + +namespace BTCPayServer.HostedServices +{ + public class TorServicesHostedService : BaseAsyncService + { + private readonly BTCPayServerOptions _options; + private readonly TorServices _torServices; + + public TorServicesHostedService(BTCPayServerOptions options, TorServices torServices) + { + _options = options; + _torServices = torServices; + } + + internal override Task[] InitializeTasks() + { + // TODO: We should report auto configured services (like bitcoind, lnd or clightning) + if (string.IsNullOrEmpty(_options.TorrcFile)) + return Array.Empty(); + return new Task[] { CreateLoopTask(RefreshTorServices) }; + } + + async Task RefreshTorServices() + { + await _torServices.Refresh(); + await Task.Delay(TimeSpan.FromSeconds(120), Cancellation); + } + } +} diff --git a/BTCPayServer/Hosting/BTCPayServerServices.cs b/BTCPayServer/Hosting/BTCPayServerServices.cs index 3d8b82e2d..00768c290 100644 --- a/BTCPayServer/Hosting/BTCPayServerServices.cs +++ b/BTCPayServer/Hosting/BTCPayServerServices.cs @@ -189,6 +189,7 @@ namespace BTCPayServer.Hosting services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); + services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddTransient, BTCPayClaimsFilter>(); diff --git a/BTCPayServer/Services/TorServices.cs b/BTCPayServer/Services/TorServices.cs index ab9d9bf7e..11487d462 100644 --- a/BTCPayServer/Services/TorServices.cs +++ b/BTCPayServer/Services/TorServices.cs @@ -1,9 +1,11 @@ using System; +using Microsoft.Extensions.Logging; using System.Collections.Generic; using System.IO; using System.Linq; using System.Threading.Tasks; using BTCPayServer.Configuration; +using BTCPayServer.Logging; namespace BTCPayServer.Services { @@ -15,19 +17,31 @@ namespace BTCPayServer.Services _Options = options; } - public async Task GetServices() + public TorService[] Services { get; internal set; } = Array.Empty(); + + + internal async Task Refresh() { if (string.IsNullOrEmpty(_Options.TorrcFile) || !File.Exists(_Options.TorrcFile)) - return Array.Empty(); + { + if (!string.IsNullOrEmpty(_Options.TorrcFile)) + Logs.PayServer.LogWarning("Torrc file is not found"); + Services = Array.Empty(); + return; + } List result = new List(); try { var torrcContent = await File.ReadAllTextAsync(_Options.TorrcFile); if (!Torrc.TryParse(torrcContent, out var torrc)) - return Array.Empty(); + { + Logs.PayServer.LogWarning("Torrc file could not be parsed"); + Services = Array.Empty(); + return; + } var services = torrc.ServiceDirectories.SelectMany(d => d.ServicePorts.Select(p => (Directory: new DirectoryInfo(d.DirectoryPath), VirtualPort: p.VirtualPort))) - .Select(d => (ServiceName: d.Directory.Name, + .Select(d => (ServiceName: d.Directory.Name, ReadingLines: System.IO.File.ReadAllLinesAsync(Path.Combine(d.Directory.FullName, "hostname")), VirtualPort: d.VirtualPort)) .ToArray(); @@ -46,16 +60,17 @@ namespace BTCPayServer.Services torService.ServiceType = TorServiceType.BTCPayServer; result.Add(torService); } - catch + catch (Exception ex) { - + Logs.PayServer.LogWarning(ex, $"Error while reading hidden service {service.ServiceName} configuration"); } } } - catch + catch (Exception ex) { + Logs.PayServer.LogWarning(ex, $"Error while reading torrc file"); } - return result.ToArray(); + Services = result.ToArray(); } } diff --git a/BTCPayServer/Views/Server/Services.cshtml b/BTCPayServer/Views/Server/Services.cshtml index 1d2e3178f..d4e02a09d 100644 --- a/BTCPayServer/Views/Server/Services.cshtml +++ b/BTCPayServer/Views/Server/Services.cshtml @@ -79,7 +79,7 @@ } -@if (Model.TorServices.Length != 0) +@if (Model.TorServices.Count != 0) {