2020-11-27 22:52:52 -05:00
|
|
|
|
using System;
|
|
|
|
|
using FluentValidation;
|
2020-05-24 22:22:26 -04:00
|
|
|
|
using FluentValidation.AspNetCore;
|
|
|
|
|
using Microsoft.AspNetCore.Authentication.Cookies;
|
2018-02-21 20:29:23 -05:00
|
|
|
|
using Microsoft.AspNetCore.Builder;
|
|
|
|
|
using Microsoft.AspNetCore.Hosting;
|
2019-02-16 18:18:50 -05:00
|
|
|
|
using Microsoft.AspNetCore.Mvc.ApplicationParts;
|
2020-08-20 12:08:21 -04:00
|
|
|
|
using Microsoft.AspNetCore.Mvc.Razor;
|
2018-02-21 20:29:23 -05:00
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
2020-02-11 17:44:06 -05:00
|
|
|
|
using SharedLibraryCore;
|
2020-08-20 11:38:11 -04:00
|
|
|
|
using SharedLibraryCore.Configuration;
|
2020-05-24 22:22:26 -04:00
|
|
|
|
using SharedLibraryCore.Dtos;
|
2020-08-20 11:38:11 -04:00
|
|
|
|
using SharedLibraryCore.Dtos.Meta.Responses;
|
2019-12-02 16:52:36 -05:00
|
|
|
|
using SharedLibraryCore.Interfaces;
|
2020-05-24 22:22:26 -04:00
|
|
|
|
using SharedLibraryCore.Services;
|
|
|
|
|
using Stats.Dtos;
|
|
|
|
|
using Stats.Helpers;
|
2020-02-11 17:44:06 -05:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
2020-01-14 19:56:23 -05:00
|
|
|
|
using System.Net;
|
2020-02-11 17:44:06 -05:00
|
|
|
|
using System.Reflection;
|
2020-01-14 19:56:23 -05:00
|
|
|
|
using System.Threading.Tasks;
|
2021-03-22 12:09:25 -04:00
|
|
|
|
using Data.Abstractions;
|
|
|
|
|
using Data.Helpers;
|
|
|
|
|
using Stats.Client.Abstractions;
|
2021-11-23 18:26:33 -05:00
|
|
|
|
using Stats.Config;
|
2020-05-24 22:22:26 -04:00
|
|
|
|
using WebfrontCore.Controllers.API.Validation;
|
2019-02-12 21:34:29 -05:00
|
|
|
|
using WebfrontCore.Middleware;
|
2022-06-02 17:48:47 -04:00
|
|
|
|
using WebfrontCore.QueryHelpers;
|
|
|
|
|
using WebfrontCore.QueryHelpers.Models;
|
2018-02-21 20:29:23 -05:00
|
|
|
|
|
|
|
|
|
namespace WebfrontCore
|
|
|
|
|
{
|
|
|
|
|
public class Startup
|
|
|
|
|
{
|
|
|
|
|
// This method gets called by the runtime. Use this method to add services to the container.
|
|
|
|
|
public void ConfigureServices(IServiceCollection services)
|
|
|
|
|
{
|
2019-03-27 20:40:26 -04:00
|
|
|
|
// allow CORS
|
|
|
|
|
services.AddCors(_options =>
|
|
|
|
|
{
|
|
|
|
|
_options.AddPolicy("AllowAll",
|
|
|
|
|
_builder =>
|
|
|
|
|
{
|
|
|
|
|
_builder.AllowAnyOrigin()
|
|
|
|
|
.AllowAnyMethod()
|
2019-09-30 19:35:36 -04:00
|
|
|
|
.AllowAnyHeader();
|
2019-03-27 20:40:26 -04:00
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2020-02-11 17:44:06 -05:00
|
|
|
|
IEnumerable<Assembly> pluginAssemblies()
|
|
|
|
|
{
|
|
|
|
|
string pluginDir = $"{Utilities.OperatingDirectory}Plugins{Path.DirectorySeparatorChar}";
|
|
|
|
|
|
|
|
|
|
if (Directory.Exists(pluginDir))
|
|
|
|
|
{
|
|
|
|
|
var dllFileNames = Directory.GetFiles($"{Utilities.OperatingDirectory}Plugins{Path.DirectorySeparatorChar}", "*.dll");
|
|
|
|
|
return dllFileNames.Select(_file => Assembly.LoadFrom(_file));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Enumerable.Empty<Assembly>();
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-21 20:29:23 -05:00
|
|
|
|
// Add framework services.
|
2019-09-30 19:35:36 -04:00
|
|
|
|
var mvcBuilder = services.AddMvc(_options => _options.SuppressAsyncSuffixInActionNames = false)
|
2020-05-24 22:22:26 -04:00
|
|
|
|
.AddFluentValidation()
|
2020-02-11 17:44:06 -05:00
|
|
|
|
.ConfigureApplicationPartManager(_partManager =>
|
2019-02-16 18:18:50 -05:00
|
|
|
|
{
|
2020-02-11 17:44:06 -05:00
|
|
|
|
foreach (var assembly in pluginAssemblies())
|
2019-02-16 18:18:50 -05:00
|
|
|
|
{
|
|
|
|
|
if (assembly.FullName.Contains("Views"))
|
|
|
|
|
{
|
2020-02-11 17:44:06 -05:00
|
|
|
|
_partManager.ApplicationParts.Add(new CompiledRazorAssemblyPart(assembly));
|
2019-02-16 18:18:50 -05:00
|
|
|
|
}
|
2018-05-28 21:30:31 -04:00
|
|
|
|
|
2019-02-16 18:18:50 -05:00
|
|
|
|
else if (assembly.FullName.Contains("Web"))
|
|
|
|
|
{
|
2020-02-11 17:44:06 -05:00
|
|
|
|
_partManager.ApplicationParts.Add(new AssemblyPart(assembly));
|
2019-02-16 18:18:50 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
2018-05-28 21:30:31 -04:00
|
|
|
|
|
2019-02-25 20:36:10 -05:00
|
|
|
|
#if DEBUG
|
|
|
|
|
{
|
2021-03-22 12:09:25 -04:00
|
|
|
|
mvcBuilder = mvcBuilder.AddRazorRuntimeCompilation();
|
|
|
|
|
services.Configure<RazorViewEngineOptions>(_options =>
|
|
|
|
|
{
|
|
|
|
|
_options.ViewLocationFormats.Add(@"/Views/Plugins/{1}/{0}" + RazorViewEngine.ViewExtension);
|
|
|
|
|
_options.ViewLocationFormats.Add("/Views/Plugins/Stats/Advanced.cshtml");
|
|
|
|
|
});
|
|
|
|
|
}
|
2019-02-25 20:36:10 -05:00
|
|
|
|
#endif
|
|
|
|
|
|
2020-02-11 17:44:06 -05:00
|
|
|
|
foreach (var asm in pluginAssemblies())
|
2018-05-28 21:30:31 -04:00
|
|
|
|
{
|
2019-02-16 18:18:50 -05:00
|
|
|
|
mvcBuilder.AddApplicationPart(asm);
|
|
|
|
|
}
|
2018-05-28 21:30:31 -04:00
|
|
|
|
|
2019-08-04 21:38:55 -04:00
|
|
|
|
services.AddHttpContextAccessor();
|
2020-11-27 22:52:52 -05:00
|
|
|
|
|
2018-04-08 02:44:42 -04:00
|
|
|
|
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
|
|
|
|
|
.AddCookie(CookieAuthenticationDefaults.AuthenticationScheme, options =>
|
|
|
|
|
{
|
|
|
|
|
options.AccessDeniedPath = "/";
|
|
|
|
|
options.LoginPath = "/";
|
2022-09-08 16:03:38 -04:00
|
|
|
|
options.Events.OnValidatePrincipal += ClaimsPermissionRemoval.ValidateAsync;
|
|
|
|
|
options.Events.OnSignedIn += ClaimsPermissionRemoval.OnSignedIn;
|
2018-04-08 02:44:42 -04:00
|
|
|
|
});
|
2019-02-12 21:34:29 -05:00
|
|
|
|
|
2019-12-02 16:52:36 -05:00
|
|
|
|
services.AddSingleton(Program.Manager);
|
2020-08-20 11:38:11 -04:00
|
|
|
|
services.AddSingleton<IResourceQueryHelper<ChatSearchQuery, MessageResponse>, ChatResourceQueryHelper>();
|
2020-05-24 22:22:26 -04:00
|
|
|
|
services.AddTransient<IValidator<FindClientRequest>, FindClientRequestValidator>();
|
|
|
|
|
services.AddSingleton<IResourceQueryHelper<FindClientRequest, FindClientResult>, ClientService>();
|
|
|
|
|
services.AddSingleton<IResourceQueryHelper<StatsInfoRequest, StatsInfoResult>, StatsResourceQueryHelper>();
|
2021-03-22 12:09:25 -04:00
|
|
|
|
services.AddSingleton<IResourceQueryHelper<StatsInfoRequest, AdvancedStatsInfo>, AdvancedClientStatsResourceQueryHelper>();
|
2023-01-23 17:38:16 -05:00
|
|
|
|
services.AddScoped(sp =>
|
|
|
|
|
Program.ApplicationServiceProvider
|
|
|
|
|
.GetRequiredService<IResourceQueryHelper<ClientResourceRequest, ClientResourceResponse>>());
|
2021-03-22 12:09:25 -04:00
|
|
|
|
services.AddSingleton(typeof(IDataValueCache<,>), typeof(DataValueCache<,>));
|
2020-04-28 17:48:06 -04:00
|
|
|
|
// todo: this needs to be handled more gracefully
|
2021-07-12 15:57:44 -04:00
|
|
|
|
services.AddSingleton(Program.ApplicationServiceProvider.GetRequiredService<DefaultSettings>());
|
2021-06-29 16:02:01 -04:00
|
|
|
|
services.AddSingleton(Program.ApplicationServiceProvider.GetRequiredService<ILoggerFactory>());
|
2022-03-23 09:43:57 -04:00
|
|
|
|
services.AddSingleton(Program.ApplicationServiceProvider.GetRequiredService<IConfigurationHandlerFactory>());
|
|
|
|
|
services.AddSingleton(Program.ApplicationServiceProvider.GetRequiredService<IDatabaseContextFactory>());
|
|
|
|
|
services.AddSingleton(Program.ApplicationServiceProvider.GetRequiredService<IAuditInformationRepository>());
|
|
|
|
|
services.AddSingleton(Program.ApplicationServiceProvider.GetRequiredService<ITranslationLookup>());
|
|
|
|
|
services.AddSingleton(Program.ApplicationServiceProvider.GetRequiredService<IEnumerable<IManagerCommand>>());
|
2022-03-24 12:34:32 -04:00
|
|
|
|
#pragma warning disable CS0618
|
2022-03-23 09:43:57 -04:00
|
|
|
|
services.AddSingleton(Program.ApplicationServiceProvider.GetRequiredService<IMetaService>());
|
2022-03-24 12:34:32 -04:00
|
|
|
|
#pragma warning restore CS0618
|
2022-03-23 09:43:57 -04:00
|
|
|
|
services.AddSingleton(Program.ApplicationServiceProvider.GetRequiredService<IMetaServiceV2>());
|
|
|
|
|
services.AddSingleton(Program.ApplicationServiceProvider.GetRequiredService<ApplicationConfiguration>());
|
2021-01-17 22:58:18 -05:00
|
|
|
|
services.AddSingleton(Program.ApplicationServiceProvider.GetRequiredService<ClientService>());
|
2022-06-02 17:48:47 -04:00
|
|
|
|
services.AddSingleton<IResourceQueryHelper<BanInfoRequest, BanInfo>, BanInfoResourceQueryHelper>();
|
2021-03-22 12:09:25 -04:00
|
|
|
|
services.AddSingleton(
|
|
|
|
|
Program.ApplicationServiceProvider.GetRequiredService<IServerDistributionCalculator>());
|
|
|
|
|
services.AddSingleton(Program.ApplicationServiceProvider
|
|
|
|
|
.GetRequiredService<IConfigurationHandler<DefaultSettings>>());
|
2022-04-19 19:43:58 -04:00
|
|
|
|
services.AddSingleton(Program.ApplicationServiceProvider
|
|
|
|
|
.GetRequiredService<IGeoLocationService>());
|
2021-03-22 12:09:25 -04:00
|
|
|
|
services.AddSingleton(Program.ApplicationServiceProvider
|
2022-01-28 18:28:49 -05:00
|
|
|
|
.GetRequiredService<StatsConfiguration>());
|
2021-08-26 18:35:05 -04:00
|
|
|
|
services.AddSingleton(Program.ApplicationServiceProvider.GetRequiredService<IServerDataViewer>());
|
2022-09-08 16:03:38 -04:00
|
|
|
|
services.AddSingleton(Program.ApplicationServiceProvider.GetRequiredService<IInteractionRegistration>());
|
2022-10-12 22:06:18 -04:00
|
|
|
|
services.AddSingleton(Program.ApplicationServiceProvider.GetRequiredService<IRemoteCommandService>());
|
2018-02-21 20:29:23 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
2020-11-27 22:52:52 -05:00
|
|
|
|
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IServiceProvider serviceProvider)
|
2018-02-21 20:29:23 -05:00
|
|
|
|
{
|
2020-01-14 19:56:23 -05:00
|
|
|
|
app.UseStatusCodePages(_context =>
|
|
|
|
|
{
|
|
|
|
|
if (_context.HttpContext.Response.StatusCode == (int)HttpStatusCode.NotFound)
|
|
|
|
|
{
|
|
|
|
|
_context.HttpContext.Response.Redirect($"/Home/ResponseStatusCode?statusCode={_context.HttpContext.Response.StatusCode}");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
});
|
2019-10-23 14:35:20 -04:00
|
|
|
|
|
2019-09-30 19:35:36 -04:00
|
|
|
|
if (env.EnvironmentName == "Development")
|
2018-02-21 20:29:23 -05:00
|
|
|
|
{
|
|
|
|
|
app.UseDeveloperExceptionPage();
|
|
|
|
|
}
|
2019-01-26 21:33:37 -05:00
|
|
|
|
|
2018-02-21 20:29:23 -05:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
app.UseExceptionHandler("/Home/Error");
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-12 21:34:29 -05:00
|
|
|
|
if (Program.Manager.GetApplicationSettings().Configuration().EnableWebfrontConnectionWhitelist)
|
|
|
|
|
{
|
2020-11-27 22:52:52 -05:00
|
|
|
|
app.UseMiddleware<IPWhitelist>(serviceProvider.GetService<ILogger<IPWhitelist>>(), serviceProvider.GetRequiredService<ApplicationConfiguration>().WebfrontConnectionWhitelist);
|
2019-02-12 21:34:29 -05:00
|
|
|
|
}
|
|
|
|
|
|
2018-02-21 20:29:23 -05:00
|
|
|
|
app.UseStaticFiles();
|
2018-04-08 02:44:42 -04:00
|
|
|
|
app.UseAuthentication();
|
2019-03-27 20:40:26 -04:00
|
|
|
|
app.UseCors("AllowAll");
|
2018-04-04 15:38:34 -04:00
|
|
|
|
|
2019-10-07 18:35:37 -04:00
|
|
|
|
// prevents banned/demoted users from keeping their claims
|
|
|
|
|
app.UseMiddleware<ClaimsPermissionRemoval>(Program.Manager);
|
|
|
|
|
|
2019-09-30 19:35:36 -04:00
|
|
|
|
app.UseRouting();
|
2019-12-25 22:05:57 -05:00
|
|
|
|
app.UseAuthorization();
|
2019-09-30 19:35:36 -04:00
|
|
|
|
app.UseEndpoints(endpoints =>
|
2018-02-21 20:29:23 -05:00
|
|
|
|
{
|
2019-09-30 19:35:36 -04:00
|
|
|
|
endpoints.MapControllerRoute("default", "{controller=Home}/{action=Index}/{id?}");
|
2018-02-21 20:29:23 -05:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|