Adding update related options and using them in HostedService

This commit is contained in:
rockstardev
2020-08-01 09:10:05 -05:00
parent adefaf2fa8
commit c18167889d
4 changed files with 16 additions and 5 deletions

View File

@@ -176,6 +176,8 @@ namespace BTCPayServer.Configuration
SocksEndpoint = endpoint; SocksEndpoint = endpoint;
} }
UpdateCheck = conf.GetOrDefault<bool>("updatecheck", false);
UpdateUrl = conf.GetOrDefault<Uri>("updateurl", new Uri("https://api.github.com/repos/btcpayserver/btcpayserver/releases/latest"));
var sshSettings = ParseSSHConfiguration(conf); var sshSettings = ParseSSHConfiguration(conf);
if ((!string.IsNullOrEmpty(sshSettings.Password) || !string.IsNullOrEmpty(sshSettings.KeyFile)) && !string.IsNullOrEmpty(sshSettings.Server)) if ((!string.IsNullOrEmpty(sshSettings.Password) || !string.IsNullOrEmpty(sshSettings.KeyFile)) && !string.IsNullOrEmpty(sshSettings.Server))
@@ -301,5 +303,7 @@ namespace BTCPayServer.Configuration
set; set;
} }
public string TorrcFile { get; set; } public string TorrcFile { get; set; }
public bool UpdateCheck { get; set; }
public Uri UpdateUrl { get; set; }
} }
} }

View File

@@ -38,6 +38,8 @@ namespace BTCPayServer.Configuration
app.Option("--sshtrustedfingerprints", "SSH Host public key fingerprint or sha256 (default: empty, it will allow untrusted connections)", CommandOptionType.SingleValue); app.Option("--sshtrustedfingerprints", "SSH Host public key fingerprint or sha256 (default: empty, it will allow untrusted connections)", CommandOptionType.SingleValue);
app.Option("--torrcfile", "Path to torrc file containing hidden services directories (default: empty)", CommandOptionType.SingleValue); app.Option("--torrcfile", "Path to torrc file containing hidden services directories (default: empty)", CommandOptionType.SingleValue);
app.Option("--socksendpoint", "Socks endpoint to connect to onion urls (default: empty)", CommandOptionType.SingleValue); app.Option("--socksendpoint", "Socks endpoint to connect to onion urls (default: empty)", CommandOptionType.SingleValue);
app.Option("--updatecheck", $"Enabling once a day check for new releases (default: false)", CommandOptionType.SingleValue);
app.Option("--updateurl", $"Url location used for updatecheck (default: https://api.github.com/repos/btcpayserver/btcpayserver/releases/latest)", CommandOptionType.SingleValue);
app.Option("--debuglog", "A rolling log file for debug messages.", CommandOptionType.SingleValue); app.Option("--debuglog", "A rolling log file for debug messages.", CommandOptionType.SingleValue);
app.Option("--debugloglevel", "The severity you log (default:information)", CommandOptionType.SingleValue); app.Option("--debugloglevel", "The severity you log (default:information)", CommandOptionType.SingleValue);
app.Option("--disable-registration", "Disables new user registrations (default:true)", CommandOptionType.SingleValue); app.Option("--disable-registration", "Disables new user registrations (default:true)", CommandOptionType.SingleValue);

View File

@@ -3,6 +3,7 @@ using System.Net.Http;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using BTCPayServer.Configuration;
using BTCPayServer.Logging; using BTCPayServer.Logging;
using BTCPayServer.Services; using BTCPayServer.Services;
using BTCPayServer.Services.Notifications; using BTCPayServer.Services.Notifications;
@@ -80,18 +81,20 @@ namespace BTCPayServer.HostedServices
public class GithubVersionFetcher : IVersionFetcher public class GithubVersionFetcher : IVersionFetcher
{ {
private readonly HttpClient _httpClient; private readonly HttpClient _httpClient;
public GithubVersionFetcher(IHttpClientFactory httpClientFactory) private readonly Uri _updateurl;
public GithubVersionFetcher(IHttpClientFactory httpClientFactory, BTCPayServerOptions options)
{ {
_httpClient = httpClientFactory.CreateClient(nameof(GithubVersionFetcher)); _httpClient = httpClientFactory.CreateClient(nameof(GithubVersionFetcher));
_httpClient.DefaultRequestHeaders.Add("Accept", "application/vnd.github.v3+json"); _httpClient.DefaultRequestHeaders.Add("Accept", "application/vnd.github.v3+json");
_httpClient.DefaultRequestHeaders.Add("User-Agent", "BTCPayServer/NewVersionChecker"); _httpClient.DefaultRequestHeaders.Add("User-Agent", "BTCPayServer/NewVersionChecker");
_updateurl = options.UpdateUrl;
} }
private static readonly Regex _releaseVersionTag = new Regex("^(v[1-9]+(\\.[0-9]+)*(-[0-9]+)?)$"); private static readonly Regex _releaseVersionTag = new Regex("^(v[1-9]+(\\.[0-9]+)*(-[0-9]+)?)$");
public async Task<string> Fetch(CancellationToken cancellation) public async Task<string> Fetch(CancellationToken cancellation)
{ {
const string url = "https://api.github.com/repos/btcpayserver/btcpayserver/releases/latest"; using (var resp = await _httpClient.GetAsync(_updateurl, cancellation))
using (var resp = await _httpClient.GetAsync(url, cancellation))
{ {
var strResp = await resp.Content.ReadAsStringAsync(); var strResp = await resp.Content.ReadAsStringAsync();
if (resp.IsSuccessStatusCode) if (resp.IsSuccessStatusCode)
@@ -105,7 +108,7 @@ namespace BTCPayServer.HostedServices
else else
{ {
Logs.Events.LogWarning($"Unsuccessful status code returned during new version check. " + Logs.Events.LogWarning($"Unsuccessful status code returned during new version check. " +
$"Url: {url}, HTTP Code: {resp.StatusCode}, Response Body: {strResp}"); $"Url: {_updateurl}, HTTP Code: {resp.StatusCode}, Response Body: {strResp}");
} }
} }

View File

@@ -19,7 +19,9 @@
"BTCPAY_POSTGRES": "User ID=postgres;Host=127.0.0.1;Port=39372;Database=btcpayserver", "BTCPAY_POSTGRES": "User ID=postgres;Host=127.0.0.1;Port=39372;Database=btcpayserver",
"BTCPAY_DEBUGLOG": "debug.log", "BTCPAY_DEBUGLOG": "debug.log",
"BTCPAY_TORRCFILE": "../BTCPayServer.Tests/TestData/Tor/torrc", "BTCPAY_TORRCFILE": "../BTCPayServer.Tests/TestData/Tor/torrc",
"BTCPAY_SOCKSENDPOINT": "localhost:9050" "BTCPAY_SOCKSENDPOINT": "localhost:9050",
"BTCPAY_UPDATECHECK": "false",
"BTCPAY_UPDATEURL": ""
}, },
"applicationUrl": "http://127.0.0.1:14142/" "applicationUrl": "http://127.0.0.1:14142/"
}, },