From 3ac37497ab9b5ff2c28eaab54c7f2a12356659dd Mon Sep 17 00:00:00 2001 From: Aaron Clauson Date: Mon, 29 Oct 2018 16:11:02 +0100 Subject: [PATCH] Added configuration options for BtcPayServer https binding. (#360) --- .../Configuration/BTCPayServerOptions.cs | 10 ++++++ .../Configuration/ConfigurationExtensions.cs | 2 ++ .../Configuration/DefaultConfiguration.cs | 2 ++ BTCPayServer/Hosting/Startup.cs | 34 ++++++++++++++----- BTCPayServer/Properties/launchSettings.json | 3 +- 5 files changed, 41 insertions(+), 10 deletions(-) diff --git a/BTCPayServer/Configuration/BTCPayServerOptions.cs b/BTCPayServer/Configuration/BTCPayServerOptions.cs index 51febf179..50a889ea7 100644 --- a/BTCPayServer/Configuration/BTCPayServerOptions.cs +++ b/BTCPayServer/Configuration/BTCPayServerOptions.cs @@ -262,5 +262,15 @@ namespace BTCPayServer.Configuration builder.Path = RootPath; return builder.ToString(); } + public string HttpsCertificateFilePath // Certificate and key file (typically .pfx) to use when binding to HTTPS listener. + { + get; + set; + } + public string HttpsCertificateFilePassword // Password for certificate when binding to HTTPS listener. + { + get; + set; + } } } diff --git a/BTCPayServer/Configuration/ConfigurationExtensions.cs b/BTCPayServer/Configuration/ConfigurationExtensions.cs index 528c63d73..f7c033ce4 100644 --- a/BTCPayServer/Configuration/ConfigurationExtensions.cs +++ b/BTCPayServer/Configuration/ConfigurationExtensions.cs @@ -37,6 +37,8 @@ namespace BTCPayServer.Configuration } else if (typeof(T) == typeof(string)) return (T)(object)str; + else if (typeof(T) == typeof(IPAddress)) + return (T)(object)IPAddress.Parse(str); else if (typeof(T) == typeof(IPEndPoint)) { var separator = str.LastIndexOf(":", StringComparison.InvariantCulture); diff --git a/BTCPayServer/Configuration/DefaultConfiguration.cs b/BTCPayServer/Configuration/DefaultConfiguration.cs index 05de6d0d3..a59d513f5 100644 --- a/BTCPayServer/Configuration/DefaultConfiguration.cs +++ b/BTCPayServer/Configuration/DefaultConfiguration.cs @@ -106,6 +106,8 @@ namespace BTCPayServer.Configuration builder.AppendLine("### Server settings ###"); builder.AppendLine("#port=" + defaultSettings.DefaultPort); builder.AppendLine("#bind=127.0.0.1"); + builder.AppendLine("#httpscertificatefilepath=devtest.pfx"); + builder.AppendLine("#httpscertificatefilepassword=toto"); builder.AppendLine(); builder.AppendLine("### Database ###"); builder.AppendLine("#postgres=User ID=root;Password=myPassword;Host=localhost;Port=5432;Database=myDataBase;"); diff --git a/BTCPayServer/Hosting/Startup.cs b/BTCPayServer/Hosting/Startup.cs index 2e4d4628a..ba9ef1ab2 100644 --- a/BTCPayServer/Hosting/Startup.cs +++ b/BTCPayServer/Hosting/Startup.cs @@ -118,15 +118,33 @@ namespace BTCPayServer.Hosting b.AllowAnyMethod().AllowAnyHeader().AllowAnyOrigin(); }); }); + services.Configure(kestrel => + { + var networkType = DefaultConfiguration.GetNetworkType(Configuration); + var defaultSettings = BTCPayDefaultSettings.GetDefaultSettings(networkType); + var btcPaySettings = Configuration.Get(); + var bind = Configuration.GetOrDefault("bind", IPAddress.Loopback); + int port = Configuration.GetOrDefault("port", defaultSettings.DefaultPort); - // Needed to debug U2F for ledger support - //services.Configure(kestrel => - //{ - // kestrel.Listen(IPAddress.Loopback, 5012, l => - // { - // l.UseHttps("devtest.pfx", "toto"); - // }); - //}); + if (!String.IsNullOrEmpty(btcPaySettings.HttpsCertificateFilePath)) + { + if (!File.Exists(btcPaySettings.HttpsCertificateFilePath)) + { + // Note that by design this is a fatal error condition that will cause the process to exit. + throw new ApplicationException($"The https certificate file could not be found at {btcPaySettings.HttpsCertificateFilePath}."); + } + + Logs.Configuration.LogInformation($"Https certificate file path {btcPaySettings.HttpsCertificateFilePath}."); + kestrel.Listen(bind, port, l => + { + l.UseHttps(btcPaySettings.HttpsCertificateFilePath, btcPaySettings.HttpsCertificateFilePassword); + }); + } + else + { + kestrel.Listen(bind, port); + } + }); } public void Configure( diff --git a/BTCPayServer/Properties/launchSettings.json b/BTCPayServer/Properties/launchSettings.json index e398044a6..05afc2faa 100644 --- a/BTCPayServer/Properties/launchSettings.json +++ b/BTCPayServer/Properties/launchSettings.json @@ -15,8 +15,7 @@ "ASPNETCORE_ENVIRONMENT": "Development", "BTCPAY_CHAINS": "btc,ltc", "BTCPAY_POSTGRES": "User ID=postgres;Host=127.0.0.1;Port=39372;Database=btcpayserver" - }, - "applicationUrl": "http://127.0.0.1:14142/" + } } } }