2017-09-13 15:47:34 +09:00
|
|
|
|
using BTCPayServer.Configuration;
|
2017-12-17 01:04:20 +09:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2017-09-13 15:47:34 +09:00
|
|
|
|
using Microsoft.AspNetCore.Hosting;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection.Extensions;
|
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
using NBitpayClient;
|
|
|
|
|
using NBitcoin;
|
|
|
|
|
using BTCPayServer.Data;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using NBXplorer;
|
|
|
|
|
using Microsoft.AspNetCore.Builder;
|
|
|
|
|
using Microsoft.Extensions.Hosting;
|
|
|
|
|
using BTCPayServer.Services;
|
2017-10-20 14:06:37 -05:00
|
|
|
|
using BTCPayServer.Services.Invoices;
|
2017-09-15 16:06:57 +09:00
|
|
|
|
using BTCPayServer.Services.Rates;
|
|
|
|
|
using BTCPayServer.Services.Stores;
|
|
|
|
|
using BTCPayServer.Services.Fees;
|
2017-09-15 19:25:02 +09:00
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using Microsoft.AspNetCore.Rewrite;
|
2017-09-23 01:31:29 +09:00
|
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
|
using Microsoft.Extensions.Options;
|
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
|
using BTCPayServer.Controllers;
|
|
|
|
|
using BTCPayServer.Services.Mails;
|
|
|
|
|
using Microsoft.AspNetCore.Identity;
|
|
|
|
|
using BTCPayServer.Models;
|
|
|
|
|
using System.Threading.Tasks;
|
2017-09-27 23:56:43 +09:00
|
|
|
|
using System.Threading;
|
2017-10-06 10:37:38 +09:00
|
|
|
|
using BTCPayServer.Services.Wallets;
|
2017-10-11 12:20:44 +09:00
|
|
|
|
using BTCPayServer.Authentication;
|
2017-10-27 11:39:11 +09:00
|
|
|
|
using Microsoft.Extensions.Caching.Memory;
|
2017-12-17 01:04:20 +09:00
|
|
|
|
using BTCPayServer.Logging;
|
2018-01-08 02:36:41 +09:00
|
|
|
|
using BTCPayServer.HostedServices;
|
2018-02-21 00:05:08 -06:00
|
|
|
|
using Meziantou.AspNetCore.BundleTagHelpers;
|
2018-04-30 02:33:42 +09:00
|
|
|
|
using System.Security.Claims;
|
2018-10-24 07:52:19 +02:00
|
|
|
|
using BTCPayServer.Payments.Changelly;
|
2018-04-30 02:33:42 +09:00
|
|
|
|
using BTCPayServer.Security;
|
2018-07-26 22:32:24 +09:00
|
|
|
|
using Microsoft.AspNetCore.Mvc.ModelBinding;
|
|
|
|
|
using NBXplorer.DerivationStrategy;
|
2018-08-25 20:28:46 +09:00
|
|
|
|
using NicolasDorier.RateLimits;
|
2018-10-28 21:19:18 +09:00
|
|
|
|
using Npgsql;
|
2017-09-13 15:47:34 +09:00
|
|
|
|
|
|
|
|
|
namespace BTCPayServer.Hosting
|
|
|
|
|
{
|
2017-10-27 17:53:04 +09:00
|
|
|
|
public static class BTCPayServerServices
|
|
|
|
|
{
|
|
|
|
|
public static IServiceCollection AddBTCPayServer(this IServiceCollection services)
|
|
|
|
|
{
|
|
|
|
|
services.AddDbContext<ApplicationDbContext>((provider, o) =>
|
|
|
|
|
{
|
|
|
|
|
var factory = provider.GetRequiredService<ApplicationDbContextFactory>();
|
|
|
|
|
factory.ConfigureBuilder(o);
|
|
|
|
|
});
|
2018-08-21 14:33:13 +09:00
|
|
|
|
services.AddHttpClient();
|
2017-10-27 17:53:04 +09:00
|
|
|
|
services.TryAddSingleton<SettingsRepository>();
|
|
|
|
|
services.TryAddSingleton<InvoicePaymentNotification>();
|
|
|
|
|
services.TryAddSingleton<BTCPayServerOptions>(o => o.GetRequiredService<IOptions<BTCPayServerOptions>>().Value);
|
2017-12-17 01:04:20 +09:00
|
|
|
|
services.TryAddSingleton<InvoiceRepository>(o =>
|
|
|
|
|
{
|
|
|
|
|
var opts = o.GetRequiredService<BTCPayServerOptions>();
|
|
|
|
|
var dbContext = o.GetRequiredService<ApplicationDbContextFactory>();
|
|
|
|
|
var dbpath = Path.Combine(opts.DataDir, "InvoiceDB");
|
|
|
|
|
if (!Directory.Exists(dbpath))
|
|
|
|
|
Directory.CreateDirectory(dbpath);
|
2018-01-11 22:52:28 +09:00
|
|
|
|
return new InvoiceRepository(dbContext, dbpath);
|
2017-10-27 17:53:04 +09:00
|
|
|
|
});
|
|
|
|
|
services.AddSingleton<BTCPayServerEnvironment>();
|
|
|
|
|
services.TryAddSingleton<TokenRepository>();
|
2017-12-17 14:17:42 +09:00
|
|
|
|
services.TryAddSingleton<EventAggregator>();
|
2018-04-18 16:07:16 +09:00
|
|
|
|
services.TryAddSingleton<CoinAverageSettings>();
|
2017-12-17 01:04:20 +09:00
|
|
|
|
services.TryAddSingleton<ApplicationDbContextFactory>(o =>
|
|
|
|
|
{
|
|
|
|
|
var opts = o.GetRequiredService<BTCPayServerOptions>();
|
|
|
|
|
ApplicationDbContextFactory dbContext = null;
|
2018-10-27 16:15:21 +02:00
|
|
|
|
if (!String.IsNullOrEmpty(opts.PostgresConnectionString))
|
2017-12-17 01:04:20 +09:00
|
|
|
|
{
|
2018-10-27 16:15:21 +02:00
|
|
|
|
Logs.Configuration.LogInformation($"Postgres DB used ({opts.PostgresConnectionString})");
|
|
|
|
|
dbContext = new ApplicationDbContextFactory(DatabaseType.Postgres, opts.PostgresConnectionString);
|
|
|
|
|
}
|
|
|
|
|
else if(!String.IsNullOrEmpty(opts.MySQLConnectionString))
|
|
|
|
|
{
|
|
|
|
|
Logs.Configuration.LogInformation($"MySQL DB used ({opts.MySQLConnectionString})");
|
|
|
|
|
dbContext = new ApplicationDbContextFactory(DatabaseType.MySQL, opts.MySQLConnectionString);
|
2017-12-17 01:04:20 +09:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2018-10-27 16:15:21 +02:00
|
|
|
|
var connStr = "Data Source=" + Path.Combine(opts.DataDir, "sqllite.db");
|
|
|
|
|
Logs.Configuration.LogInformation($"SQLite DB used ({connStr})");
|
|
|
|
|
dbContext = new ApplicationDbContextFactory(DatabaseType.Sqlite, connStr);
|
2017-12-17 01:04:20 +09:00
|
|
|
|
}
|
2018-10-27 16:15:21 +02:00
|
|
|
|
|
2017-12-17 01:04:20 +09:00
|
|
|
|
return dbContext;
|
|
|
|
|
});
|
2017-12-21 15:52:04 +09:00
|
|
|
|
|
|
|
|
|
services.TryAddSingleton<BTCPayNetworkProvider>(o =>
|
|
|
|
|
{
|
|
|
|
|
var opts = o.GetRequiredService<BTCPayServerOptions>();
|
2018-02-26 00:48:12 +09:00
|
|
|
|
return opts.NetworkProvider;
|
2017-12-21 15:52:04 +09:00
|
|
|
|
});
|
|
|
|
|
|
2018-08-30 13:16:24 -05:00
|
|
|
|
services.TryAddSingleton<AppsHelper>();
|
|
|
|
|
|
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>();
|
2017-10-27 17:53:04 +09:00
|
|
|
|
services.TryAddSingleton<StoreRepository>();
|
2018-01-11 14:36:12 +09:00
|
|
|
|
services.TryAddSingleton<BTCPayWalletProvider>();
|
2017-10-27 17:53:04 +09:00
|
|
|
|
services.TryAddSingleton<CurrencyNameTable>();
|
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
|
|
|
|
{
|
|
|
|
|
Fallback = new FeeRate(100, 1),
|
2017-12-21 15:52:04 +09:00
|
|
|
|
BlockTarget = 20
|
2017-10-27 17:53:04 +09:00
|
|
|
|
});
|
2017-12-17 01:04:20 +09:00
|
|
|
|
|
2018-04-23 17:42:03 -05:00
|
|
|
|
services.AddSingleton<CssThemeManager>();
|
2018-07-26 22:32:24 +09:00
|
|
|
|
services.Configure<MvcOptions>((o) => {
|
|
|
|
|
o.Filters.Add(new ContentSecurityPolicyCssThemeManager());
|
|
|
|
|
o.ModelMetadataDetailsProviders.Add(new SuppressChildValidationMetadataProvider(typeof(WalletId)));
|
|
|
|
|
o.ModelMetadataDetailsProviders.Add(new SuppressChildValidationMetadataProvider(typeof(DerivationStrategyBase)));
|
|
|
|
|
});
|
2018-04-23 17:42:03 -05:00
|
|
|
|
services.AddSingleton<IHostedService, CssThemeManagerHostedService>();
|
2018-07-19 19:31:17 +09:00
|
|
|
|
services.AddSingleton<IHostedService, MigratorHostedService>();
|
2018-04-23 17:42:03 -05:00
|
|
|
|
|
2018-02-20 12:45:04 +09:00
|
|
|
|
services.AddSingleton<Payments.IPaymentMethodHandler<DerivationStrategy>, Payments.Bitcoin.BitcoinLikePaymentHandler>();
|
2018-02-26 00:48:12 +09:00
|
|
|
|
services.AddSingleton<IHostedService, Payments.Bitcoin.NBXplorerListener>();
|
|
|
|
|
|
2018-08-12 21:38:45 +09:00
|
|
|
|
services.AddSingleton<IHostedService, HostedServices.CheckConfigurationHostedService>();
|
|
|
|
|
|
2018-02-26 00:48:12 +09:00
|
|
|
|
services.AddSingleton<Payments.IPaymentMethodHandler<Payments.Lightning.LightningSupportedPaymentMethod>, Payments.Lightning.LightningLikePaymentHandler>();
|
2018-03-20 11:59:43 +09:00
|
|
|
|
services.AddSingleton<IHostedService, Payments.Lightning.LightningListener>();
|
2018-10-24 07:52:19 +02:00
|
|
|
|
|
|
|
|
|
services.AddSingleton<ChangellyClientProvider>();
|
2018-02-20 12:45:04 +09:00
|
|
|
|
|
2018-01-08 02:36:41 +09:00
|
|
|
|
services.AddSingleton<IHostedService, NBXplorerWaiters>();
|
|
|
|
|
services.AddSingleton<IHostedService, InvoiceNotificationManager>();
|
|
|
|
|
services.AddSingleton<IHostedService, InvoiceWatcher>();
|
2018-04-14 22:35:52 +09:00
|
|
|
|
services.AddSingleton<IHostedService, RatesHostedService>();
|
2018-04-30 02:33:42 +09:00
|
|
|
|
services.AddTransient<IConfigureOptions<MvcOptions>, BTCPayClaimsFilter>();
|
2018-01-08 02:36:41 +09:00
|
|
|
|
|
|
|
|
|
services.TryAddSingleton<ExplorerClientProvider>();
|
2017-10-27 17:53:04 +09:00
|
|
|
|
services.TryAddSingleton<Bitpay>(o =>
|
|
|
|
|
{
|
2018-04-19 16:54:25 +09:00
|
|
|
|
if (o.GetRequiredService<BTCPayServerOptions>().NetworkType == NetworkType.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>();
|
|
|
|
|
// Add application services.
|
|
|
|
|
services.AddTransient<IEmailSender, EmailSender>();
|
2018-02-21 00:05:08 -06:00
|
|
|
|
// bundling
|
2018-02-21 00:48:25 -06:00
|
|
|
|
|
2018-04-30 02:33:42 +09:00
|
|
|
|
services.AddAuthorization(o => Policies.AddBTCPayPolicies(o));
|
2018-06-04 12:00:03 +09:00
|
|
|
|
BitpayAuthentication.AddAuthentication(services);
|
2018-04-30 02:33:42 +09:00
|
|
|
|
|
2018-02-21 00:48:25 -06:00
|
|
|
|
services.AddBundles();
|
|
|
|
|
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
|
|
|
|
|
2018-08-06 12:04:36 +09:00
|
|
|
|
services.AddCors(options=>
|
|
|
|
|
{
|
|
|
|
|
options.AddPolicy(CorsPolicies.All, p=>p.AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin());
|
|
|
|
|
});
|
2018-08-25 20:28:46 +09:00
|
|
|
|
|
|
|
|
|
var rateLimits = new RateLimitService();
|
|
|
|
|
rateLimits.SetZone($"zone={ZoneLimits.Login} rate=5r/min burst=3 nodelay");
|
|
|
|
|
services.AddSingleton(rateLimits);
|
2017-10-27 17:53:04 +09:00
|
|
|
|
return services;
|
|
|
|
|
}
|
2017-09-13 15:47:34 +09:00
|
|
|
|
|
2017-10-27 17:53:04 +09:00
|
|
|
|
public static IApplicationBuilder UsePayServer(this IApplicationBuilder app)
|
|
|
|
|
{
|
|
|
|
|
using (var scope = app.ApplicationServices.GetService<IServiceScopeFactory>().CreateScope())
|
|
|
|
|
{
|
|
|
|
|
//Wait the DB is ready
|
|
|
|
|
Retry(() =>
|
|
|
|
|
{
|
|
|
|
|
scope.ServiceProvider.GetRequiredService<ApplicationDbContext>().Database.Migrate();
|
|
|
|
|
});
|
|
|
|
|
}
|
2017-12-17 14:17:42 +09:00
|
|
|
|
|
2017-10-27 17:53:04 +09:00
|
|
|
|
app.UseMiddleware<BTCPayMiddleware>();
|
2018-02-23 15:21:42 +09:00
|
|
|
|
return app;
|
2017-10-27 17:53:04 +09:00
|
|
|
|
}
|
2017-09-27 23:56:43 +09:00
|
|
|
|
|
2017-10-27 17:53:04 +09:00
|
|
|
|
static void Retry(Action act)
|
|
|
|
|
{
|
2018-10-28 21:19:18 +09:00
|
|
|
|
CancellationTokenSource cts = new CancellationTokenSource(1000);
|
2017-10-27 17:53:04 +09:00
|
|
|
|
while (true)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
act();
|
|
|
|
|
return;
|
|
|
|
|
}
|
2018-10-28 21:19:18 +09:00
|
|
|
|
// Starting up
|
|
|
|
|
catch (PostgresException ex) when (ex.SqlState == "57P03") { Thread.Sleep(1000); }
|
|
|
|
|
catch when (!cts.IsCancellationRequested)
|
2017-10-27 17:53:04 +09:00
|
|
|
|
{
|
2018-01-10 02:07:42 +09:00
|
|
|
|
Thread.Sleep(100);
|
2017-10-27 17:53:04 +09:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-09-13 15:47:34 +09:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|