2020-06-28 21:44:35 -05:00
using System ;
2017-09-13 15:47:34 +09:00
using System.IO ;
2021-01-02 13:44:28 +01:00
using System.Linq ;
2017-09-27 23:56:43 +09:00
using System.Threading ;
2020-11-17 13:46:23 +01:00
using BTCPayServer.Abstractions.Contracts ;
using BTCPayServer.Abstractions.Extensions ;
using BTCPayServer.Abstractions.Models ;
2021-07-12 14:17:25 +02:00
using BTCPayServer.Common ;
2021-07-27 14:11:47 +02:00
using BTCPayServer.Client ;
2020-06-28 17:55:27 +09:00
using BTCPayServer.Configuration ;
using BTCPayServer.Controllers ;
2021-07-27 14:11:47 +02:00
using BTCPayServer.Controllers.GreenField ;
2020-06-28 17:55:27 +09:00
using BTCPayServer.Data ;
2021-10-18 05:37:59 +02:00
using BTCPayServer.Data.Payouts.LightningLike ;
2018-01-08 02:36:41 +09:00
using BTCPayServer.HostedServices ;
2021-01-06 15:51:13 +01:00
using BTCPayServer.Lightning ;
2020-06-28 17:55:27 +09:00
using BTCPayServer.Logging ;
2019-01-14 22:43:29 +01:00
using BTCPayServer.PaymentRequest ;
2019-05-24 06:11:38 +00:00
using BTCPayServer.Payments ;
using BTCPayServer.Payments.Bitcoin ;
2019-01-07 09:52:27 +01:00
using BTCPayServer.Payments.Lightning ;
2020-01-06 13:57:32 +01:00
using BTCPayServer.Payments.PayJoin ;
2020-10-21 14:02:20 +02:00
using BTCPayServer.Plugins ;
2018-04-30 02:33:42 +09:00
using BTCPayServer.Security ;
2019-10-12 20:35:30 +09:00
using BTCPayServer.Security.Bitpay ;
2020-03-27 12:55:21 +09:00
using BTCPayServer.Security.GreenField ;
2020-06-28 17:55:27 +09:00
using BTCPayServer.Services ;
using BTCPayServer.Services.Apps ;
using BTCPayServer.Services.Fees ;
using BTCPayServer.Services.Invoices ;
2020-04-28 09:53:34 +02:00
using BTCPayServer.Services.Labels ;
2020-06-28 17:55:27 +09:00
using BTCPayServer.Services.Mails ;
2020-06-14 23:47:11 -05:00
using BTCPayServer.Services.Notifications ;
2020-06-16 23:29:25 +09:00
using BTCPayServer.Services.Notifications.Blobs ;
2020-06-28 17:55:27 +09:00
using BTCPayServer.Services.PaymentRequests ;
using BTCPayServer.Services.Rates ;
using BTCPayServer.Services.Stores ;
using BTCPayServer.Services.Wallets ;
using BundlerMinifier.TagHelpers ;
using Microsoft.AspNetCore.Authorization ;
using Microsoft.AspNetCore.Builder ;
using Microsoft.AspNetCore.Http ;
using Microsoft.AspNetCore.Mvc ;
using Microsoft.AspNetCore.Mvc.ModelBinding ;
2020-12-28 21:59:01 +09:00
using Microsoft.AspNetCore.Mvc.ModelBinding.Validation ;
2020-06-28 17:55:27 +09:00
using Microsoft.Extensions.Configuration ;
using Microsoft.Extensions.DependencyInjection ;
using Microsoft.Extensions.DependencyInjection.Extensions ;
using Microsoft.Extensions.Hosting ;
using Microsoft.Extensions.Logging ;
using Microsoft.Extensions.Options ;
using NBitcoin ;
using NBitpayClient ;
using NBXplorer.DerivationStrategy ;
2020-06-24 10:34:09 +09:00
using Newtonsoft.Json ;
2020-06-28 17:55:27 +09:00
using NicolasDorier.RateLimits ;
using Serilog ;
2021-10-11 12:32:09 +09:00
using NBitcoin.RPC ;
2020-07-29 18:55:28 +09:00
#if ALTCOINS
2020-07-28 22:48:51 +02:00
using BTCPayServer.Services.Altcoins.Monero ;
2020-07-28 11:30:23 +02:00
using BTCPayServer.Services.Altcoins.Ethereum ;
2020-07-28 22:48:51 +02:00
#endif
2017-09-13 15:47:34 +09:00
namespace BTCPayServer.Hosting
{
2017-10-27 17:53:04 +09:00
public static class BTCPayServerServices
{
2020-06-24 10:34:09 +09:00
public static IServiceCollection RegisterJsonConverter ( this IServiceCollection services , Func < BTCPayNetwork , JsonConverter > create )
{
services . AddSingleton < IJsonConverterRegistration , JsonConverterRegistration > ( ( s ) = > new JsonConverterRegistration ( create ) ) ;
return services ;
}
2019-05-14 15:46:43 +00:00
public static IServiceCollection AddBTCPayServer ( this IServiceCollection services , IConfiguration configuration )
2017-10-27 17:53:04 +09:00
{
2020-06-28 17:55:27 +09:00
services . AddSingleton < MvcNewtonsoftJsonOptions > ( o = > o . GetRequiredService < IOptions < MvcNewtonsoftJsonOptions > > ( ) . Value ) ;
2017-10-27 17:53:04 +09:00
services . AddDbContext < ApplicationDbContext > ( ( provider , o ) = >
{
var factory = provider . GetRequiredService < ApplicationDbContextFactory > ( ) ;
factory . ConfigureBuilder ( o ) ;
} ) ;
2018-08-21 14:33:13 +09:00
services . AddHttpClient ( ) ;
2019-06-18 13:37:24 +09:00
services . AddHttpClient ( nameof ( ExplorerClientProvider ) , httpClient = >
{
httpClient . Timeout = Timeout . InfiniteTimeSpan ;
} ) ;
2020-06-24 10:34:09 +09:00
services . AddSingleton < BTCPayNetworkJsonSerializerSettings > ( ) ;
2020-01-06 13:57:32 +01:00
services . AddPayJoinServices ( ) ;
2020-07-29 18:55:28 +09:00
#if ALTCOINS
2019-10-01 15:30:27 +09:00
services . AddMoneroLike ( ) ;
2020-07-28 11:30:23 +02:00
services . AddEthereumLike ( ) ;
2020-07-28 22:48:51 +02:00
#endif
2017-10-27 17:53:04 +09:00
services . TryAddSingleton < SettingsRepository > ( ) ;
2020-10-21 14:02:20 +02:00
services . TryAddSingleton < ISettingsRepository > ( provider = > provider . GetService < SettingsRepository > ( ) ) ;
2020-04-28 09:53:34 +02:00
services . TryAddSingleton < LabelFactory > ( ) ;
2019-03-17 12:57:18 +09:00
services . TryAddSingleton < TorServices > ( ) ;
2021-04-18 11:26:06 +09:00
services . AddSingleton < IHostedService > ( provider = > provider . GetRequiredService < TorServices > ( ) ) ;
2019-03-18 00:03:02 +09:00
services . TryAddSingleton < SocketFactory > ( ) ;
2019-04-11 01:10:29 +09:00
services . TryAddSingleton < LightningClientFactoryService > ( ) ;
2017-10-27 17:53:04 +09:00
services . TryAddSingleton < InvoicePaymentNotification > ( ) ;
2019-05-14 15:46:43 +00:00
services . TryAddSingleton < BTCPayServerOptions > ( o = >
o . GetRequiredService < IOptions < BTCPayServerOptions > > ( ) . Value ) ;
2020-12-28 11:10:53 +01:00
// Don't move this StartupTask, we depend on it being right here
2019-07-08 12:12:39 +09:00
services . AddStartupTask < MigrationStartupTask > ( ) ;
2020-12-28 11:10:53 +01:00
//
2020-10-21 09:53:05 +02:00
services . AddStartupTask < BlockExplorerLinkStartupTask > ( ) ;
2021-10-05 11:10:41 +02:00
services . TryAddSingleton < InvoiceRepository > ( ) ;
services . AddSingleton < PaymentService > ( ) ;
2017-10-27 17:53:04 +09:00
services . AddSingleton < BTCPayServerEnvironment > ( ) ;
services . TryAddSingleton < TokenRepository > ( ) ;
2019-08-03 00:42:30 +09:00
services . TryAddSingleton < WalletRepository > ( ) ;
2017-12-17 14:17:42 +09:00
services . TryAddSingleton < EventAggregator > ( ) ;
2019-01-14 22:43:29 +01:00
services . TryAddSingleton < PaymentRequestService > ( ) ;
2021-03-14 12:24:32 -07:00
services . TryAddSingleton < UserService > ( ) ;
2021-01-06 15:51:13 +01:00
services . AddSingleton < ApplicationDbContextFactory > ( ) ;
services . AddOptions < BTCPayServerOptions > ( ) . Configure (
( options ) = >
{
options . LoadArgs ( configuration ) ;
} ) ;
services . AddOptions < DataDirectories > ( ) . Configure (
( options ) = >
{
options . Configure ( configuration ) ;
} ) ;
services . AddOptions < DatabaseOptions > ( ) . Configure < IOptions < DataDirectories > > (
( options , datadirs ) = >
2017-12-17 01:04:20 +09:00
{
2021-01-06 15:51:13 +01:00
var postgresConnectionString = configuration [ "postgres" ] ;
var mySQLConnectionString = configuration [ "mysql" ] ;
var sqliteFileName = configuration [ "sqlitefile" ] ;
if ( ! string . IsNullOrEmpty ( postgresConnectionString ) )
2020-12-27 22:06:00 +09:00
{
2021-01-06 15:51:13 +01:00
options . DatabaseType = DatabaseType . Postgres ;
options . ConnectionString = postgresConnectionString ;
2020-12-27 22:06:00 +09:00
}
2021-01-06 15:51:13 +01:00
else if ( ! string . IsNullOrEmpty ( mySQLConnectionString ) )
2020-12-27 22:06:00 +09:00
{
2021-01-06 15:51:13 +01:00
options . DatabaseType = DatabaseType . MySQL ;
options . ConnectionString = mySQLConnectionString ;
2020-12-27 22:06:00 +09:00
}
2021-01-06 15:51:13 +01:00
else if ( ! string . IsNullOrEmpty ( sqliteFileName ) )
2020-12-27 22:06:00 +09:00
{
2021-01-06 15:51:13 +01:00
var connStr = "Data Source=" + ( Path . IsPathRooted ( sqliteFileName )
? sqliteFileName
: Path . Combine ( datadirs . Value . DataDir , sqliteFileName ) ) ;
options . DatabaseType = DatabaseType . Sqlite ;
2021-01-17 10:02:25 +01:00
options . ConnectionString = connStr ;
2020-12-27 22:06:00 +09:00
}
2021-01-06 15:51:13 +01:00
else
{
throw new InvalidOperationException ( "No database option was configured." ) ;
}
} ) ;
2021-01-02 13:44:28 +01:00
services . AddOptions < NBXplorerOptions > ( ) . Configure < BTCPayNetworkProvider > (
( options , btcPayNetworkProvider ) = >
{
2021-01-06 15:51:13 +01:00
foreach ( BTCPayNetwork btcPayNetwork in btcPayNetworkProvider . GetAll ( ) . OfType < BTCPayNetwork > ( ) )
{
NBXplorerConnectionSetting setting =
new NBXplorerConnectionSetting
{
CryptoCode = btcPayNetwork . CryptoCode ,
ExplorerUri = configuration . GetOrDefault < Uri > (
$"{btcPayNetwork.CryptoCode}.explorer.url" ,
btcPayNetwork . NBXplorerNetwork . DefaultSettings . DefaultUrl ) ,
CookieFile = configuration . GetOrDefault < string > (
$"{btcPayNetwork.CryptoCode}.explorer.cookiefile" ,
btcPayNetwork . NBXplorerNetwork . DefaultSettings . DefaultCookieFile )
} ;
options . NBXplorerConnectionSettings . Add ( setting ) ;
}
2021-01-02 13:44:28 +01:00
} ) ;
services . AddOptions < LightningNetworkOptions > ( ) . Configure < BTCPayNetworkProvider > (
( options , btcPayNetworkProvider ) = >
{
2021-01-06 15:51:13 +01:00
foreach ( var net in btcPayNetworkProvider . GetAll ( ) . OfType < BTCPayNetwork > ( ) )
{
var lightning = configuration . GetOrDefault < string > ( $"{net.CryptoCode}.lightning" , string . Empty ) ;
if ( lightning . Length ! = 0 )
{
if ( ! LightningConnectionString . TryParse ( lightning , true , out var connectionString ,
out var error ) )
{
Logs . Configuration . LogWarning ( $"Invalid setting {net.CryptoCode}.lightning, " +
Environment . NewLine +
$"If you have a c-lightning server use: 'type=clightning;server=/root/.lightning/lightning-rpc', " +
Environment . NewLine +
$"If you have a lightning charge server: 'type=charge;server=https://charge.example.com;api-token=yourapitoken'" +
Environment . NewLine +
$"If you have a lnd server: 'type=lnd-rest;server=https://lnd:lnd@lnd.example.com;macaroon=abf239...;certthumbprint=2abdf302...'" +
Environment . NewLine +
$" lnd server: 'type=lnd-rest;server=https://lnd:lnd@lnd.example.com;macaroonfilepath=/root/.lnd/admin.macaroon;certthumbprint=2abdf302...'" +
Environment . NewLine +
$"If you have an eclair server: 'type=eclair;server=http://eclair.com:4570;password=eclairpassword;bitcoin-host=bitcoind:37393;bitcoin-auth=bitcoinrpcuser:bitcoinrpcpassword" +
Environment . NewLine +
$" eclair server: 'type=eclair;server=http://eclair.com:4570;password=eclairpassword;bitcoin-host=bitcoind:37393" +
Environment . NewLine +
$"Error: {error}" + Environment . NewLine +
"This service will not be exposed through BTCPay Server" ) ;
}
else
{
if ( connectionString . IsLegacy )
{
Logs . Configuration . LogWarning (
$"Setting {net.CryptoCode}.lightning is a deprecated format, it will work now, but please replace it for future versions with '{connectionString.ToString()}'" ) ;
}
options . InternalLightningByCryptoCode . Add ( net . CryptoCode , connectionString ) ;
}
}
}
2021-01-02 13:44:28 +01:00
} ) ;
services . AddOptions < ExternalServicesOptions > ( ) . Configure < BTCPayNetworkProvider > (
( options , btcPayNetworkProvider ) = >
{
2021-01-06 15:51:13 +01:00
foreach ( var net in btcPayNetworkProvider . GetAll ( ) . OfType < BTCPayNetwork > ( ) )
{
options . ExternalServices . Load ( net . CryptoCode , configuration ) ;
}
options . ExternalServices . LoadNonCryptoServices ( configuration ) ;
var services = configuration . GetOrDefault < string > ( "externalservices" , null ) ;
if ( services ! = null )
{
foreach ( var service in services . Split ( new [ ] { ';' , ',' } , StringSplitOptions . RemoveEmptyEntries )
. Select ( p = > ( p , SeparatorIndex : p . IndexOf ( ':' , StringComparison . OrdinalIgnoreCase ) ) )
. Where ( p = > p . SeparatorIndex ! = - 1 )
. Select ( p = > ( Name : p . p . Substring ( 0 , p . SeparatorIndex ) ,
Link : p . p . Substring ( p . SeparatorIndex + 1 ) ) ) )
{
if ( Uri . TryCreate ( service . Link , UriKind . RelativeOrAbsolute , out var uri ) )
options . OtherExternalServices . AddOrReplace ( service . Name , uri ) ;
}
}
2021-01-02 13:44:28 +01:00
} ) ;
services . TryAddSingleton ( o = > configuration . ConfigureNetworkProvider ( ) ) ;
2017-12-21 15:52:04 +09:00
2019-02-19 13:04:58 +09:00
services . TryAddSingleton < AppService > ( ) ;
2020-10-21 14:02:20 +02:00
services . AddSingleton < PluginService > ( ) ;
2021-01-07 14:49:53 +01:00
services . AddSingleton < IPluginHookService , PluginHookService > ( ) ;
2019-08-10 14:05:11 +09:00
services . TryAddTransient < Safe > ( ) ;
2019-02-17 16:53:41 +09:00
services . TryAddSingleton < Ganss . XSS . HtmlSanitizer > ( o = >
{
var htmlSanitizer = new Ganss . XSS . HtmlSanitizer ( ) ;
htmlSanitizer . RemovingAtRule + = ( sender , args ) = >
{
} ;
htmlSanitizer . RemovingTag + = ( sender , args ) = >
{
if ( args . Tag . TagName . Equals ( "img" , StringComparison . InvariantCultureIgnoreCase ) )
{
if ( ! args . Tag . ClassList . Contains ( "img-fluid" ) )
{
args . Tag . ClassList . Add ( "img-fluid" ) ;
}
args . Cancel = true ;
}
} ;
htmlSanitizer . RemovingAttribute + = ( sender , args ) = >
{
if ( args . Tag . TagName . Equals ( "img" , StringComparison . InvariantCultureIgnoreCase ) & &
args . Attribute . Name . Equals ( "src" , StringComparison . InvariantCultureIgnoreCase ) & &
args . Reason = = Ganss . XSS . RemoveReason . NotAllowedUrlValue )
{
args . Cancel = true ;
}
} ;
htmlSanitizer . RemovingStyle + = ( sender , args ) = > { args . Cancel = true ; } ;
htmlSanitizer . AllowedAttributes . Add ( "class" ) ;
htmlSanitizer . AllowedTags . Add ( "iframe" ) ;
2020-07-30 11:36:33 +09:00
htmlSanitizer . AllowedTags . Add ( "style" ) ;
2019-02-17 16:53:41 +09:00
htmlSanitizer . AllowedTags . Remove ( "img" ) ;
htmlSanitizer . AllowedAttributes . Add ( "webkitallowfullscreen" ) ;
htmlSanitizer . AllowedAttributes . Add ( "allowfullscreen" ) ;
return htmlSanitizer ;
} ) ;
2018-08-30 13:16:24 -05:00
2018-07-22 18:38:14 +09:00
services . TryAddSingleton < LightningConfigurationProvider > ( ) ;
2018-03-23 17:27:48 +09:00
services . TryAddSingleton < LanguageService > ( ) ;
2018-01-08 04:14:35 +09:00
services . TryAddSingleton < NBXplorerDashboard > ( ) ;
2020-12-02 07:19:48 +01:00
services . AddSingleton < ISyncSummaryProvider , NBXSyncSummaryProvider > ( ) ;
2017-10-27 17:53:04 +09:00
services . TryAddSingleton < StoreRepository > ( ) ;
2019-01-14 22:43:29 +01:00
services . TryAddSingleton < PaymentRequestRepository > ( ) ;
2018-01-11 14:36:12 +09:00
services . TryAddSingleton < BTCPayWalletProvider > ( ) ;
2021-03-11 13:34:52 +01:00
services . TryAddSingleton < WalletReceiveService > ( ) ;
services . AddSingleton < IHostedService > ( provider = > provider . GetService < WalletReceiveService > ( ) ) ;
2020-05-31 12:18:29 +02:00
services . TryAddSingleton < CurrencyNameTable > ( CurrencyNameTable . Instance ) ;
2018-01-08 02:36:41 +09:00
services . TryAddSingleton < IFeeProviderFactory > ( o = > new NBXplorerFeeProviderFactory ( o . GetRequiredService < ExplorerClientProvider > ( ) )
2017-10-27 17:53:04 +09:00
{
2019-11-06 16:21:33 -08:00
Fallback = new FeeRate ( 100L , 1 )
2017-10-27 17:53:04 +09:00
} ) ;
2017-12-17 01:04:20 +09:00
2020-06-28 17:55:27 +09:00
services . Configure < MvcOptions > ( ( o ) = >
{
2018-07-26 22:32:24 +09:00
o . ModelMetadataDetailsProviders . Add ( new SuppressChildValidationMetadataProvider ( typeof ( WalletId ) ) ) ;
o . ModelMetadataDetailsProviders . Add ( new SuppressChildValidationMetadataProvider ( typeof ( DerivationStrategyBase ) ) ) ;
} ) ;
2018-04-23 17:42:03 -05:00
2019-08-27 23:30:25 +09:00
services . AddSingleton < HostedServices . CheckConfigurationHostedService > ( ) ;
services . AddSingleton < IHostedService , HostedServices . CheckConfigurationHostedService > ( o = > o . GetRequiredService < CheckConfigurationHostedService > ( ) ) ;
2020-11-06 20:42:26 +09:00
services . AddSingleton < HostedServices . WebhookNotificationManager > ( ) ;
services . AddSingleton < IHostedService , WebhookNotificationManager > ( o = > o . GetRequiredService < WebhookNotificationManager > ( ) ) ;
2021-03-01 14:44:53 +01:00
services . AddHttpClient ( WebhookNotificationManager . OnionNamedClient )
. ConfigureHttpClient ( h = > h . DefaultRequestHeaders . ConnectionClose = true )
. ConfigurePrimaryHttpMessageHandler < Socks5HttpClientHandler > ( ) ;
2021-04-13 10:36:49 +02:00
services . AddSingleton < IPayoutHandler , BitcoinLikePayoutHandler > ( ) ;
2021-10-18 05:37:59 +02:00
services . AddSingleton < IPayoutHandler , LightningLikePayoutHandler > ( ) ;
services . AddHttpClient ( LightningLikePayoutHandler . LightningLikePayoutHandlerOnionNamedClient )
. ConfigureHttpClient ( h = > h . DefaultRequestHeaders . ConnectionClose = true )
. ConfigurePrimaryHttpMessageHandler < Socks5HttpClientHandler > ( ) ;
2020-06-24 10:34:09 +09:00
services . AddSingleton < HostedServices . PullPaymentHostedService > ( ) ;
services . AddSingleton < IHostedService , HostedServices . PullPaymentHostedService > ( o = > o . GetRequiredService < PullPaymentHostedService > ( ) ) ;
2019-05-24 06:11:38 +00:00
services . AddSingleton < BitcoinLikePaymentHandler > ( ) ;
services . AddSingleton < IPaymentMethodHandler > ( provider = > provider . GetService < BitcoinLikePaymentHandler > ( ) ) ;
services . AddSingleton < IHostedService , NBXplorerListener > ( ) ;
2018-08-12 21:38:45 +09:00
2019-01-07 09:52:27 +01:00
services . AddSingleton < LightningLikePaymentHandler > ( ) ;
2019-05-24 06:11:38 +00:00
services . AddSingleton < IPaymentMethodHandler > ( provider = > provider . GetService < LightningLikePaymentHandler > ( ) ) ;
services . AddSingleton < IHostedService , LightningListener > ( ) ;
2019-05-29 14:33:31 +00:00
services . AddSingleton < PaymentMethodHandlerDictionary > ( ) ;
2020-06-12 00:01:45 -05:00
services . AddSingleton < NotificationManager > ( ) ;
2020-06-11 23:52:46 -05:00
services . AddScoped < NotificationSender > ( ) ;
2020-05-27 18:41:00 -05:00
2018-01-08 02:36:41 +09:00
services . AddSingleton < IHostedService , NBXplorerWaiters > ( ) ;
2020-11-06 22:23:10 +09:00
services . AddSingleton < IHostedService , InvoiceEventSaverService > ( ) ;
2018-01-08 02:36:41 +09:00
services . AddSingleton < IHostedService , InvoiceNotificationManager > ( ) ;
services . AddSingleton < IHostedService , InvoiceWatcher > ( ) ;
2018-04-14 22:35:52 +09:00
services . AddSingleton < IHostedService , RatesHostedService > ( ) ;
2019-01-16 19:14:45 +09:00
services . AddSingleton < IHostedService , BackgroundJobSchedulerHostedService > ( ) ;
2019-02-19 13:18:30 +09:00
services . AddSingleton < IHostedService , AppHubStreamer > ( ) ;
2019-09-02 15:37:52 +02:00
services . AddSingleton < IHostedService , AppInventoryUpdaterHostedService > ( ) ;
2020-04-28 08:06:28 +02:00
services . AddSingleton < IHostedService , TransactionLabelMarkerHostedService > ( ) ;
2020-03-13 11:47:22 +01:00
services . AddSingleton < IHostedService , UserEventHostedService > ( ) ;
2019-07-24 17:59:30 +09:00
services . AddSingleton < IHostedService , DynamicDnsHostedService > ( ) ;
2019-01-14 22:43:29 +01:00
services . AddSingleton < IHostedService , PaymentRequestStreamer > ( ) ;
2019-01-16 19:14:45 +09:00
services . AddSingleton < IBackgroundJobClient , BackgroundJobClient > ( ) ;
2019-10-12 20:35:30 +09:00
services . AddScoped < IAuthorizationHandler , CookieAuthorizationHandler > ( ) ;
services . AddScoped < IAuthorizationHandler , BitpayAuthorizationHandler > ( ) ;
2020-07-30 19:12:14 -05:00
2020-07-30 20:43:44 -05:00
services . AddSingleton < IVersionFetcher , GithubVersionFetcher > ( ) ;
2020-07-30 19:12:14 -05:00
services . AddSingleton < IHostedService , NewVersionCheckerHostedService > ( ) ;
2020-06-16 23:29:25 +09:00
services . AddSingleton < INotificationHandler , NewVersionNotification . Handler > ( ) ;
2020-07-30 19:12:14 -05:00
2020-06-22 09:32:51 +02:00
services . AddSingleton < INotificationHandler , InvoiceEventNotification . Handler > ( ) ;
2020-06-24 10:34:09 +09:00
services . AddSingleton < INotificationHandler , PayoutNotification . Handler > ( ) ;
2021-07-16 09:57:37 +02:00
services . AddSingleton < INotificationHandler , ExternalPayoutTransactionNotification . Handler > ( ) ;
2020-12-28 11:10:53 +01:00
services . AddSingleton < IHostedService , DbMigrationsHostedService > ( ) ;
2020-08-19 13:31:28 +02:00
#if DEBUG
services . AddSingleton < INotificationHandler , JunkNotification . Handler > ( ) ;
#endif
2018-01-08 02:36:41 +09:00
services . TryAddSingleton < ExplorerClientProvider > ( ) ;
2021-07-12 14:17:25 +02:00
services . AddSingleton < IExplorerClientProvider , ExplorerClientProvider > ( x = >
x . GetRequiredService < ExplorerClientProvider > ( ) ) ;
2017-10-27 17:53:04 +09:00
services . TryAddSingleton < Bitpay > ( o = >
{
2021-01-27 14:39:38 +09:00
if ( o . GetRequiredService < BTCPayServerOptions > ( ) . NetworkType = = ChainName . Mainnet )
2017-10-27 17:53:04 +09:00
return new Bitpay ( new Key ( ) , new Uri ( "https://bitpay.com/" ) ) ;
else
return new Bitpay ( new Key ( ) , new Uri ( "https://test.bitpay.com/" ) ) ;
} ) ;
2018-08-22 16:53:40 +09:00
services . TryAddSingleton < RateProviderFactory > ( ) ;
services . TryAddSingleton < RateFetcher > ( ) ;
2017-12-17 01:04:20 +09:00
2017-10-27 17:53:04 +09:00
services . TryAddScoped < IHttpContextAccessor , HttpContextAccessor > ( ) ;
services . AddTransient < AccessTokenController > ( ) ;
services . AddTransient < InvoiceController > ( ) ;
2018-12-28 12:07:15 +01:00
services . AddTransient < AppsPublicController > ( ) ;
2019-01-14 22:43:29 +01:00
services . AddTransient < PaymentRequestController > ( ) ;
2017-10-27 17:53:04 +09:00
// Add application services.
2019-01-06 15:53:37 +01:00
services . AddSingleton < EmailSenderFactory > ( ) ;
2021-07-27 14:11:47 +02:00
//create a simple client which hooks up to the http scope
services . AddScoped < BTCPayServerClient , LocalBTCPayServerClient > ( ) ;
//also provide a factory that can impersonate user/store id
services . AddSingleton < IBTCPayServerClientFactory , BTCPayServerClientFactory > ( ) ;
2020-06-28 17:55:27 +09:00
2020-02-24 14:36:15 +01:00
services . AddAPIKeyAuthentication ( ) ;
services . AddBtcPayServerAuthenticationSchemes ( ) ;
2019-10-12 20:35:30 +09:00
services . AddAuthorization ( o = > o . AddBTCPayPolicies ( ) ) ;
2020-02-24 14:36:15 +01:00
// bundling
2019-03-23 23:24:29 +09:00
services . AddSingleton < IBundleProvider , ResourceBundleProvider > ( ) ;
2018-02-21 00:48:25 -06:00
services . AddTransient < BundleOptions > ( provider = >
2018-02-21 00:05:08 -06:00
{
2018-02-21 00:48:25 -06:00
var opts = provider . GetRequiredService < BTCPayServerOptions > ( ) ;
var bundle = new BundleOptions ( ) ;
2018-08-25 23:08:46 +09:00
bundle . UseBundles = opts . BundleJsCss ;
2018-02-21 00:48:25 -06:00
bundle . AppendVersion = true ;
return bundle ;
2018-02-21 00:05:08 -06:00
} ) ;
2017-09-15 19:25:02 +09:00
2019-05-14 15:46:43 +00:00
services . AddCors ( options = >
2018-08-06 12:04:36 +09:00
{
2019-05-14 15:46:43 +00:00
options . AddPolicy ( CorsPolicies . All , p = > p . AllowAnyHeader ( ) . AllowAnyMethod ( ) . AllowAnyOrigin ( ) ) ;
2018-08-06 12:04:36 +09:00
} ) ;
2020-02-19 09:35:23 +01:00
services . AddSingleton ( provider = >
{
var btcPayEnv = provider . GetService < BTCPayServerEnvironment > ( ) ;
var rateLimits = new RateLimitService ( ) ;
2020-07-30 20:43:44 -05:00
if ( btcPayEnv . IsDeveloping )
2020-02-19 09:35:23 +01:00
{
rateLimits . SetZone ( $"zone={ZoneLimits.Login} rate=1000r/min burst=100 nodelay" ) ;
2020-03-18 23:10:15 +09:00
rateLimits . SetZone ( $"zone={ZoneLimits.Register} rate=1000r/min burst=100 nodelay" ) ;
2020-01-06 13:57:32 +01:00
rateLimits . SetZone ( $"zone={ZoneLimits.PayJoin} rate=1000r/min burst=100 nodelay" ) ;
2020-09-21 12:40:45 +02:00
rateLimits . SetZone ( $"zone={ZoneLimits.Shopify} rate=1000r/min burst=100 nodelay" ) ;
2021-03-28 20:56:46 +09:00
rateLimits . SetZone ( $"zone={ZoneLimits.ForgotPassword} rate=5r/d burst=3 nodelay" ) ;
2020-02-19 09:35:23 +01:00
}
else
{
rateLimits . SetZone ( $"zone={ZoneLimits.Login} rate=5r/min burst=3 nodelay" ) ;
2020-03-18 23:10:15 +09:00
rateLimits . SetZone ( $"zone={ZoneLimits.Register} rate=2r/min burst=2 nodelay" ) ;
2020-03-13 16:52:50 +01:00
rateLimits . SetZone ( $"zone={ZoneLimits.PayJoin} rate=5r/min burst=3 nodelay" ) ;
2020-09-21 12:40:45 +02:00
rateLimits . SetZone ( $"zone={ZoneLimits.Shopify} rate=20r/min burst=3 nodelay" ) ;
2021-03-28 20:56:46 +09:00
rateLimits . SetZone ( $"zone={ZoneLimits.ForgotPassword} rate=5r/d burst=5 nodelay" ) ;
2020-02-19 09:35:23 +01:00
}
return rateLimits ;
} ) ;
2019-11-16 14:06:37 +09:00
services . AddLogging ( logBuilder = >
{
var debugLogFile = BTCPayServerOptions . GetDebugLog ( configuration ) ;
if ( ! string . IsNullOrEmpty ( debugLogFile ) )
{
Serilog . Log . Logger = new LoggerConfiguration ( )
. Enrich . FromLogContext ( )
. MinimumLevel . Is ( BTCPayServerOptions . GetDebugLogLevel ( configuration ) )
. WriteTo . File ( debugLogFile , rollingInterval : RollingInterval . Day , fileSizeLimitBytes : MAX_DEBUG_LOG_FILE_SIZE , rollOnFileSizeLimit : true , retainedFileCountLimit : 1 )
. CreateLogger ( ) ;
2020-01-16 14:00:31 +09:00
logBuilder . AddProvider ( new Serilog . Extensions . Logging . SerilogLoggerProvider ( Log . Logger ) ) ;
2019-11-16 14:06:37 +09:00
}
} ) ;
2020-12-28 21:59:01 +09:00
services . AddSingleton < IObjectModelValidator , SkippableObjectValidatorProvider > ( ) ;
services . SkipModelValidation < RootedKeyPath > ( ) ;
2021-10-11 12:32:09 +09:00
if ( configuration . GetOrDefault < bool > ( "cheatmode" , false ) )
{
services . AddSingleton < Cheater > ( ) ;
2021-10-11 17:49:04 +09:00
services . AddSingleton < IHostedService , Cheater > ( o = > o . GetRequiredService < Cheater > ( ) ) ;
2021-10-11 12:32:09 +09:00
}
2017-10-27 17:53:04 +09:00
return services ;
}
2020-12-28 21:59:01 +09:00
public static void SkipModelValidation < T > ( this IServiceCollection services )
{
services . AddSingleton < SkippableObjectValidatorProvider . ISkipValidation , SkippableObjectValidatorProvider . SkipValidationType < T > > ( ) ;
}
2019-11-16 14:06:37 +09:00
private const long MAX_DEBUG_LOG_FILE_SIZE = 2000000 ; // If debug log is in use roll it every N MB.
2020-02-24 14:36:15 +01:00
private static void AddBtcPayServerAuthenticationSchemes ( this IServiceCollection services )
2019-05-14 15:46:43 +00:00
{
services . AddAuthentication ( )
2020-02-24 14:36:15 +01:00
. AddBitpayAuthentication ( )
. AddAPIKeyAuthentication ( ) ;
2019-05-14 15:46:43 +00:00
}
2017-09-13 15:47:34 +09:00
2017-10-27 17:53:04 +09:00
public static IApplicationBuilder UsePayServer ( this IApplicationBuilder app )
{
app . UseMiddleware < BTCPayMiddleware > ( ) ;
2020-06-28 17:55:27 +09:00
return app ;
2017-10-27 17:53:04 +09:00
}
2019-10-09 22:26:54 +09:00
public static IApplicationBuilder UseHeadersOverride ( this IApplicationBuilder app )
{
app . UseMiddleware < HeadersOverrideMiddleware > ( ) ;
return app ;
}
2017-10-27 17:53:04 +09:00
}
2017-09-13 15:47:34 +09:00
}