2017-09-13 08:47:34 +02:00
|
|
|
|
using Microsoft.AspNetCore.Hosting;
|
2017-09-25 18:31:43 +02:00
|
|
|
|
using System.Reflection;
|
2017-09-13 08:47:34 +02:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using Microsoft.AspNetCore.Builder;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2018-02-12 19:27:36 +01:00
|
|
|
|
|
2017-09-13 08:47:34 +02:00
|
|
|
|
using Microsoft.AspNetCore.Mvc.ModelBinding.Validation;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using NBitpayClient;
|
|
|
|
|
using BTCPayServer.Authentication;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using BTCPayServer.Filters;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc.Infrastructure;
|
|
|
|
|
using BTCPayServer.Services;
|
|
|
|
|
using BTCPayServer.Models;
|
|
|
|
|
using Microsoft.AspNetCore.Identity;
|
|
|
|
|
using BTCPayServer.Data;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
2017-09-25 10:18:13 +02:00
|
|
|
|
using Hangfire;
|
2017-09-13 08:47:34 +02:00
|
|
|
|
using BTCPayServer.Logging;
|
2017-09-13 16:50:36 +02:00
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using BTCPayServer.Controllers;
|
2017-09-15 09:06:57 +02:00
|
|
|
|
using BTCPayServer.Services.Stores;
|
|
|
|
|
using BTCPayServer.Services.Mails;
|
2017-09-22 18:31:29 +02:00
|
|
|
|
using Microsoft.Extensions.Configuration;
|
2017-09-25 10:18:13 +02:00
|
|
|
|
using Hangfire.AspNetCore;
|
|
|
|
|
using BTCPayServer.Configuration;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using Hangfire.Dashboard;
|
|
|
|
|
using Hangfire.Annotations;
|
2017-09-25 18:31:43 +02:00
|
|
|
|
using Microsoft.Extensions.DependencyInjection.Extensions;
|
|
|
|
|
using System.Threading;
|
2017-10-12 09:33:53 +02:00
|
|
|
|
using Microsoft.Extensions.Options;
|
2017-10-13 10:46:19 +02:00
|
|
|
|
using Microsoft.AspNetCore.Mvc.Cors.Internal;
|
2018-02-12 19:27:36 +01:00
|
|
|
|
using Microsoft.AspNetCore.Server.Kestrel.Core;
|
|
|
|
|
using System.Net;
|
2018-01-30 19:34:09 +01:00
|
|
|
|
using Meziantou.AspNetCore.BundleTagHelpers;
|
2018-07-12 10:38:21 +02:00
|
|
|
|
using BTCPayServer.Security;
|
2017-09-13 08:47:34 +02:00
|
|
|
|
|
|
|
|
|
namespace BTCPayServer.Hosting
|
|
|
|
|
{
|
2017-10-27 10:53:04 +02:00
|
|
|
|
public class Startup
|
|
|
|
|
{
|
|
|
|
|
class NeedRole : IDashboardAuthorizationFilter
|
|
|
|
|
{
|
|
|
|
|
string _Role;
|
|
|
|
|
public NeedRole(string role)
|
|
|
|
|
{
|
|
|
|
|
_Role = role;
|
|
|
|
|
}
|
|
|
|
|
public bool Authorize([NotNull] DashboardContext context)
|
|
|
|
|
{
|
|
|
|
|
return context.GetHttpContext().User.IsInRole(_Role);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public Startup(IConfiguration conf, IHostingEnvironment env)
|
|
|
|
|
{
|
|
|
|
|
Configuration = conf;
|
|
|
|
|
_Env = env;
|
|
|
|
|
}
|
|
|
|
|
IHostingEnvironment _Env;
|
|
|
|
|
public IConfiguration Configuration
|
|
|
|
|
{
|
|
|
|
|
get; set;
|
|
|
|
|
}
|
2017-09-13 08:47:34 +02:00
|
|
|
|
|
2017-10-27 10:53:04 +02:00
|
|
|
|
public void ConfigureServices(IServiceCollection services)
|
|
|
|
|
{
|
|
|
|
|
services.ConfigureBTCPayServer(Configuration);
|
|
|
|
|
services.AddMemoryCache();
|
|
|
|
|
services.AddIdentity<ApplicationUser, IdentityRole>()
|
|
|
|
|
.AddEntityFrameworkStores<ApplicationDbContext>()
|
|
|
|
|
.AddDefaultTokenProviders();
|
2017-09-25 18:31:43 +02:00
|
|
|
|
|
2017-10-27 10:53:04 +02:00
|
|
|
|
services.AddBTCPayServer();
|
|
|
|
|
services.AddMvc(o =>
|
|
|
|
|
{
|
|
|
|
|
o.Filters.Add(new XFrameOptionsAttribute("DENY"));
|
2018-07-11 18:43:16 +02:00
|
|
|
|
o.Filters.Add(new XContentTypeOptionsAttribute("nosniff"));
|
2018-07-11 19:23:54 +02:00
|
|
|
|
o.Filters.Add(new XXSSProtectionAttribute());
|
2018-07-11 19:38:08 +02:00
|
|
|
|
o.Filters.Add(new ReferrerPolicyAttribute("same-origin"));
|
2018-07-12 11:19:43 +02:00
|
|
|
|
//o.Filters.Add(new ContentSecurityPolicyAttribute()
|
|
|
|
|
//{
|
|
|
|
|
// FontSrc = "'self' https://fonts.gstatic.com/",
|
|
|
|
|
// ImgSrc = "'self' data:",
|
|
|
|
|
// DefaultSrc = "'none'",
|
|
|
|
|
// StyleSrc = "'self' 'unsafe-inline'",
|
|
|
|
|
// ScriptSrc = "'self' 'unsafe-inline'"
|
|
|
|
|
//});
|
2017-10-27 10:53:04 +02:00
|
|
|
|
});
|
2018-07-12 10:38:21 +02:00
|
|
|
|
services.TryAddScoped<ContentSecurityPolicies>();
|
2018-01-04 14:56:49 +01:00
|
|
|
|
services.Configure<IdentityOptions>(options =>
|
|
|
|
|
{
|
|
|
|
|
options.Password.RequireDigit = false;
|
|
|
|
|
options.Password.RequiredLength = 7;
|
|
|
|
|
options.Password.RequireLowercase = false;
|
|
|
|
|
options.Password.RequireNonAlphanumeric = false;
|
|
|
|
|
options.Password.RequireUppercase = false;
|
2018-09-12 13:36:44 +02:00
|
|
|
|
options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(5);
|
|
|
|
|
options.Lockout.MaxFailedAccessAttempts = 5;
|
|
|
|
|
options.Lockout.AllowedForNewUsers = true;
|
2018-01-04 14:56:49 +01:00
|
|
|
|
});
|
2018-02-12 19:27:36 +01:00
|
|
|
|
|
2018-04-09 07:31:39 +02:00
|
|
|
|
services.AddHangfire((o) =>
|
2017-10-27 10:53:04 +02:00
|
|
|
|
{
|
|
|
|
|
var scope = AspNetCoreJobActivator.Current.BeginScope(null);
|
|
|
|
|
var options = (ApplicationDbContextFactory)scope.Resolve(typeof(ApplicationDbContextFactory));
|
|
|
|
|
options.ConfigureHangfireBuilder(o);
|
2018-04-09 07:31:39 +02:00
|
|
|
|
});
|
2017-10-27 10:53:04 +02:00
|
|
|
|
services.AddCors(o =>
|
|
|
|
|
{
|
|
|
|
|
o.AddPolicy("BitpayAPI", b =>
|
|
|
|
|
{
|
|
|
|
|
b.AllowAnyMethod().AllowAnyHeader().AllowAnyOrigin();
|
|
|
|
|
});
|
|
|
|
|
});
|
2018-04-09 07:31:39 +02:00
|
|
|
|
|
2018-11-01 04:07:28 +01:00
|
|
|
|
// If the HTTPS certificate path is not set this logic will NOT be used and the default Kestrel binding logic will be.
|
|
|
|
|
string httpsCertificateFilePath = Configuration.GetOrDefault<string>("HttpsCertificateFilePath", null);
|
|
|
|
|
|
|
|
|
|
if (!String.IsNullOrEmpty(httpsCertificateFilePath))
|
|
|
|
|
{
|
|
|
|
|
var bindAddress = Configuration.GetOrDefault<IPAddress>("bind", IPAddress.Any);
|
|
|
|
|
int bindPort = Configuration.GetOrDefault<int>("port", 443);
|
|
|
|
|
|
|
|
|
|
services.Configure<KestrelServerOptions>(kestrel =>
|
|
|
|
|
{
|
|
|
|
|
if (!File.Exists(httpsCertificateFilePath))
|
|
|
|
|
{
|
|
|
|
|
// Note that by design this is a fatal error condition that will cause the process to exit.
|
|
|
|
|
throw new ConfigException($"The https certificate file could not be found at {httpsCertificateFilePath}.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Logs.Configuration.LogInformation($"Https certificate file path {httpsCertificateFilePath}.");
|
|
|
|
|
kestrel.Listen(bindAddress, bindPort, l =>
|
|
|
|
|
{
|
|
|
|
|
l.UseHttps(httpsCertificateFilePath, Configuration.GetOrDefault<string>("HttpsCertificateFilePassword", null));
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
2017-10-27 10:53:04 +02:00
|
|
|
|
}
|
2017-09-22 18:31:29 +02:00
|
|
|
|
|
2017-10-27 10:53:04 +02:00
|
|
|
|
public void Configure(
|
|
|
|
|
IApplicationBuilder app,
|
|
|
|
|
IHostingEnvironment env,
|
|
|
|
|
IServiceProvider prov,
|
2018-04-05 08:50:23 +02:00
|
|
|
|
BTCPayServerOptions options,
|
2017-10-27 10:53:04 +02:00
|
|
|
|
ILoggerFactory loggerFactory)
|
2018-04-05 08:50:23 +02:00
|
|
|
|
{
|
|
|
|
|
Logs.Configure(loggerFactory);
|
|
|
|
|
Logs.Configuration.LogInformation($"Root Path: {options.RootPath}");
|
|
|
|
|
if (options.RootPath.Equals("/", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
2018-04-09 07:41:52 +02:00
|
|
|
|
ConfigureCore(app, env, prov, loggerFactory, options);
|
2018-04-05 08:50:23 +02:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
app.Map(options.RootPath, appChild =>
|
|
|
|
|
{
|
2018-04-09 07:41:52 +02:00
|
|
|
|
ConfigureCore(appChild, env, prov, loggerFactory, options);
|
2018-04-05 08:50:23 +02:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-09 07:41:52 +02:00
|
|
|
|
private static void ConfigureCore(IApplicationBuilder app, IHostingEnvironment env, IServiceProvider prov, ILoggerFactory loggerFactory, BTCPayServerOptions options)
|
2017-10-27 10:53:04 +02:00
|
|
|
|
{
|
|
|
|
|
if (env.IsDevelopment())
|
|
|
|
|
{
|
|
|
|
|
app.UseDeveloperExceptionPage();
|
|
|
|
|
}
|
2017-09-22 18:31:29 +02:00
|
|
|
|
|
2018-08-06 05:04:36 +02:00
|
|
|
|
app.UseCors();
|
2017-10-27 10:53:04 +02:00
|
|
|
|
app.UsePayServer();
|
|
|
|
|
app.UseStaticFiles();
|
|
|
|
|
app.UseAuthentication();
|
|
|
|
|
app.UseHangfireServer();
|
2018-04-09 07:41:52 +02:00
|
|
|
|
app.UseHangfireDashboard("/hangfire", new DashboardOptions()
|
|
|
|
|
{
|
|
|
|
|
AppPath = options.GetRootUri(),
|
|
|
|
|
Authorization = new[] { new NeedRole(Roles.ServerAdmin) }
|
|
|
|
|
});
|
2017-12-17 11:58:55 +01:00
|
|
|
|
app.UseWebSockets();
|
2018-08-25 13:28:46 +02:00
|
|
|
|
app.UseStatusCodePages();
|
2017-10-27 10:53:04 +02:00
|
|
|
|
app.UseMvc(routes =>
|
|
|
|
|
{
|
|
|
|
|
routes.MapRoute(
|
|
|
|
|
name: "default",
|
|
|
|
|
template: "{controller=Home}/{action=Index}/{id?}");
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-09-13 08:47:34 +02:00
|
|
|
|
}
|