Remotely/Remotely_Server/Startup.cs
2021-07-29 07:53:42 -07:00

193 lines
7.9 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Remotely_Server.Data;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Identity.UI.Services;
using System.IO;
using Remotely_Server.Services;
using Microsoft.Extensions.FileProviders;
using Microsoft.AspNetCore.StaticFiles;
using Microsoft.AspNetCore.SignalR;
using Remotely_Library.Models;
using Microsoft.AspNetCore.Http.Connections;
using Remotely_Library.Services;
using Microsoft.AspNetCore.HttpOverrides;
using Microsoft.AspNetCore.Identity.UI;
namespace Remotely_Server
{
public class Startup
{
public Startup(IConfiguration configuration, IHostingEnvironment env)
{
Configuration = configuration;
IsDev = env.IsDevelopment();
}
public IConfiguration Configuration { get; }
private bool IsDev { get; set; }
private DataService DataService { get; set; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
var dbProvider = Configuration["ApplicationOptions:DBProvider"].ToLower();
if (dbProvider == "sqlite")
{
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlite(
Configuration.GetConnectionString("SQLite")));
}
else if (dbProvider == "sqlserver")
{
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(
Configuration.GetConnectionString("SQLServer")));
}
else if (dbProvider == "postgresql")
{
services.AddDbContext<ApplicationDbContext>(options =>
options.UseNpgsql(
Configuration.GetConnectionString("PostgreSQL")));
}
services.AddIdentity<RemotelyUser, IdentityRole>(options => options.Stores.MaxLengthForKeys = 128)
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultUI(UIFramework.Bootstrap4)
.AddDefaultTokenProviders();
services.AddMvc()
.SetCompatibilityVersion(CompatibilityVersion.Version_2_2).AddJsonOptions(options =>
{
options.SerializerSettings.ContractResolver = new Newtonsoft.Json.Serialization.DefaultContractResolver();
options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
options.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.None;
});
services.AddSignalR(options =>
{
options.EnableDetailedErrors = IsDev;
}
).AddJsonProtocol(options =>
{
options.PayloadSerializerSettings.ContractResolver = new Newtonsoft.Json.Serialization.DefaultContractResolver();
});
services.AddLogging();
services.AddScoped<IEmailSender, EmailSender>();
services.AddScoped<EmailSender>();
services.AddScoped<DataService>();
services.AddSingleton<ApplicationConfig>();
services.AddSingleton<RandomGenerator>();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, DataService dataService)
{
DataService = dataService;
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseDatabaseErrorPage();
}
else
{
app.UseExceptionHandler("/Error");
app.UseHsts();
app.UseHttpsRedirection();
}
ConfigureStaticFiles(app);
app.UseCookiePolicy();
// Uncomment to run .NET Core behind a reverse proxy.
//app.UseForwardedHeaders(new ForwardedHeadersOptions
//{
// ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
//});
app.UseAuthentication();
app.UseSignalR(routes =>
{
routes.MapHub<BrowserSocketHub>("/BrowserHub", options =>
{
options.ApplicationMaxBufferSize = 500000;
options.TransportMaxBufferSize = 500000;
});
routes.MapHub<DeviceSocketHub>("/DeviceHub", options =>
{
options.ApplicationMaxBufferSize = 500000;
options.TransportMaxBufferSize = 500000;
});
routes.MapHub<RCDeviceSocketHub>("/RCDeviceHub", options =>
{
options.ApplicationMaxBufferSize = 5000000;
options.TransportMaxBufferSize = 5000000;
});
routes.MapHub<RCBrowserSocketHub>("/RCBrowserHub", options =>
{
options.ApplicationMaxBufferSize = 5000000;
options.TransportMaxBufferSize = 5000000;
});
});
app.UseMvcWithDefaultRoute();
dataService.SetAllDevicesNotOnline();
dataService.CleanupEmptyOrganizations();
dataService.CleanupOldRecords();
}
private void ConfigureStaticFiles(IApplicationBuilder app)
{
Newtonsoft.Json.JsonConvert.DefaultSettings = () =>
{
var settings = new Newtonsoft.Json.JsonSerializerSettings();
settings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
settings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.None;
return settings;
};
var provider = new FileExtensionContentTypeProvider();
// Add new mappings
provider.Mappings[".ps1"] = "application/octet-stream";
provider.Mappings[".exe"] = "application/octet-stream";
provider.Mappings[".appimage"] = "application/octet-stream";
provider.Mappings[".zip"] = "application/octet-stream";
app.UseStaticFiles();
app.UseStaticFiles(new StaticFileOptions()
{
FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "Downloads")),
RequestPath = new PathString("/Downloads"),
ContentTypeProvider = provider,
DefaultContentType = "application/octet-stream"
});
// Needed for Let's Encrypt.
if (Directory.Exists(Path.Combine(Directory.GetCurrentDirectory(), ".well-known")))
{
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), @".well-known")),
RequestPath = new PathString("/.well-known"),
ServeUnknownFileTypes = true
});
}
}
}
}