2021-09-27 12:42:46 +00:00
|
|
|
|
using System.Diagnostics;
|
2020-05-09 03:56:06 +00:00
|
|
|
|
using System.IO;
|
2021-11-27 18:31:35 +00:00
|
|
|
|
using System.Net.Http;
|
2022-01-17 17:02:52 +00:00
|
|
|
|
using System.Runtime.InteropServices;
|
2022-06-20 23:21:04 +00:00
|
|
|
|
using System.Text.Json;
|
|
|
|
|
using System.Text.Json.Serialization;
|
2020-05-09 03:56:06 +00:00
|
|
|
|
using System.Threading.Tasks;
|
2022-06-20 23:21:04 +00:00
|
|
|
|
using cesi.DTOs;
|
|
|
|
|
using CouchDB.Driver;
|
|
|
|
|
using CouchDB.Driver.Options;
|
2020-05-09 03:56:06 +00:00
|
|
|
|
using Microsoft.AspNetCore.Builder;
|
|
|
|
|
using Microsoft.AspNetCore.Hosting;
|
|
|
|
|
using Microsoft.AspNetCore.Http.Features;
|
2021-02-17 05:46:05 +00:00
|
|
|
|
using Microsoft.AspNetCore.ResponseCompression;
|
2020-05-09 03:56:06 +00:00
|
|
|
|
using Microsoft.AspNetCore.StaticFiles;
|
|
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
using Microsoft.Extensions.FileProviders;
|
|
|
|
|
using Microsoft.Extensions.Hosting;
|
2021-12-20 16:31:10 +00:00
|
|
|
|
using Nettle;
|
|
|
|
|
using Nettle.Compiler;
|
2020-05-09 03:56:06 +00:00
|
|
|
|
using Newtonsoft.Json;
|
2021-11-27 20:03:14 +00:00
|
|
|
|
using Octokit;
|
2020-05-09 03:56:06 +00:00
|
|
|
|
using Wabbajack.BuildServer;
|
2022-06-08 03:48:13 +00:00
|
|
|
|
using Wabbajack.Downloaders;
|
2022-01-17 17:02:52 +00:00
|
|
|
|
using Wabbajack.DTOs;
|
2021-11-27 18:31:35 +00:00
|
|
|
|
using Wabbajack.DTOs.JsonConverters;
|
2022-01-17 17:02:52 +00:00
|
|
|
|
using Wabbajack.DTOs.Logins;
|
2021-11-27 20:03:14 +00:00
|
|
|
|
using Wabbajack.Networking.GitHub;
|
2022-06-08 03:48:13 +00:00
|
|
|
|
using Wabbajack.Networking.Http;
|
2022-01-17 17:02:52 +00:00
|
|
|
|
using Wabbajack.Networking.Http.Interfaces;
|
|
|
|
|
using Wabbajack.Networking.NexusApi;
|
|
|
|
|
using Wabbajack.Paths;
|
|
|
|
|
using Wabbajack.RateLimiter;
|
2021-11-27 18:31:35 +00:00
|
|
|
|
using Wabbajack.Server.DataModels;
|
2021-12-20 16:31:10 +00:00
|
|
|
|
using Wabbajack.Server.Extensions;
|
2020-05-09 03:56:06 +00:00
|
|
|
|
using Wabbajack.Server.Services;
|
2022-01-17 17:02:52 +00:00
|
|
|
|
using Wabbajack.Services.OSIntegrated.TokenProviders;
|
2022-04-02 16:01:38 +00:00
|
|
|
|
using Wabbajack.Networking.WabbajackClientApi;
|
2022-06-08 03:48:13 +00:00
|
|
|
|
using Wabbajack.Paths.IO;
|
2022-06-22 01:38:42 +00:00
|
|
|
|
using Wabbajack.Server.DTOs;
|
2022-06-08 03:48:13 +00:00
|
|
|
|
using Wabbajack.VFS;
|
2022-06-20 23:21:04 +00:00
|
|
|
|
using YamlDotNet.Serialization.NamingConventions;
|
2022-04-02 16:01:38 +00:00
|
|
|
|
using Client = Wabbajack.Networking.GitHub.Client;
|
2020-05-09 03:56:06 +00:00
|
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
|
namespace Wabbajack.Server;
|
|
|
|
|
|
|
|
|
|
public class TestStartup : Startup
|
2020-05-09 03:56:06 +00:00
|
|
|
|
{
|
2021-10-23 16:51:17 +00:00
|
|
|
|
public TestStartup(IConfiguration configuration) : base(configuration)
|
2020-05-09 03:56:06 +00:00
|
|
|
|
{
|
|
|
|
|
}
|
2021-10-23 16:51:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class Startup
|
|
|
|
|
{
|
|
|
|
|
public Startup(IConfiguration configuration)
|
2020-05-09 03:56:06 +00:00
|
|
|
|
{
|
2021-10-23 16:51:17 +00:00
|
|
|
|
Configuration = configuration;
|
|
|
|
|
}
|
2020-05-09 03:56:06 +00:00
|
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
|
public IConfiguration Configuration { get; }
|
2020-05-09 03:56:06 +00:00
|
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
|
// This method gets called by the runtime. Use this method to add services to the container.
|
|
|
|
|
public void ConfigureServices(IServiceCollection services)
|
|
|
|
|
{
|
|
|
|
|
services.AddAuthentication(options =>
|
2020-05-09 03:56:06 +00:00
|
|
|
|
{
|
2021-10-23 16:51:17 +00:00
|
|
|
|
options.DefaultAuthenticateScheme = ApiKeyAuthenticationOptions.DefaultScheme;
|
|
|
|
|
options.DefaultChallengeScheme = ApiKeyAuthenticationOptions.DefaultScheme;
|
|
|
|
|
})
|
|
|
|
|
.AddApiKeySupport(options => { });
|
|
|
|
|
|
|
|
|
|
services.Configure<FormOptions>(x =>
|
2020-05-09 03:56:06 +00:00
|
|
|
|
{
|
2021-10-23 16:51:17 +00:00
|
|
|
|
x.ValueLengthLimit = int.MaxValue;
|
|
|
|
|
x.MultipartBodyLengthLimit = int.MaxValue;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
services.AddSingleton<AppSettings>();
|
|
|
|
|
services.AddSingleton<QuickSync>();
|
|
|
|
|
services.AddSingleton<GlobalInformation>();
|
|
|
|
|
services.AddSingleton<DiscordWebHook>();
|
2021-11-27 18:31:35 +00:00
|
|
|
|
services.AddSingleton<Metrics>();
|
|
|
|
|
services.AddSingleton<HttpClient>();
|
|
|
|
|
services.AddSingleton<AuthorFiles>();
|
|
|
|
|
services.AddSingleton<AuthorKeys>();
|
2021-11-27 20:03:14 +00:00
|
|
|
|
services.AddSingleton<Client>();
|
2022-01-17 17:02:52 +00:00
|
|
|
|
services.AddSingleton<NexusCacheManager>();
|
|
|
|
|
services.AddSingleton<NexusApi>();
|
2022-01-18 22:58:54 +00:00
|
|
|
|
services.AddSingleton<DiscordBackend>();
|
2022-02-09 18:30:55 +00:00
|
|
|
|
services.AddSingleton<TarLog>();
|
2022-06-08 03:48:13 +00:00
|
|
|
|
services.AddAllSingleton<IHttpDownloader, SingleThreadedDownloader>();
|
|
|
|
|
services.AddDownloadDispatcher(useLoginDownloaders:false, useProxyCache:false);
|
|
|
|
|
services.AddTransient(s =>
|
2022-06-08 04:00:16 +00:00
|
|
|
|
{
|
|
|
|
|
var settings = s.GetRequiredService<AppSettings>();
|
|
|
|
|
return new TemporaryFileManager(settings.TempPath.Combine(Environment.ProcessId + "_" + Guid.NewGuid()));
|
|
|
|
|
});
|
|
|
|
|
|
2022-06-08 03:48:13 +00:00
|
|
|
|
services.AddAllSingleton<ITokenProvider<WabbajackApiState>, WabbajackApiTokenProvider>();
|
|
|
|
|
services.AddAllSingleton<IResource, IResource<DownloadDispatcher>>(s => new Resource<DownloadDispatcher>("Downloads", 12));
|
|
|
|
|
services.AddAllSingleton<IResource, IResource<FileHashCache>>(s => new Resource<FileHashCache>("File Hashing", 12));
|
2022-06-22 21:11:26 +00:00
|
|
|
|
services.AddAllSingleton<IResource, IResource<Wabbajack.Networking.WabbajackClientApi.Client>>(s =>
|
|
|
|
|
new Resource<Wabbajack.Networking.WabbajackClientApi.Client>("Wabbajack Client", 4));
|
|
|
|
|
|
2022-06-08 03:48:13 +00:00
|
|
|
|
services.AddSingleton(s =>
|
|
|
|
|
new FileHashCache(KnownFolders.AppDataLocal.Combine("Wabbajack", "GlobalHashCache.sqlite"),
|
|
|
|
|
s.GetService<IResource<FileHashCache>>()!));
|
2022-06-22 21:11:26 +00:00
|
|
|
|
|
2022-01-17 17:02:52 +00:00
|
|
|
|
services.AddAllSingleton<ITokenProvider<NexusApiState>, NexusApiTokenProvider>();
|
|
|
|
|
services.AddAllSingleton<IResource, IResource<HttpClient>>(s => new Resource<HttpClient>("Web Requests", 12));
|
|
|
|
|
// Application Info
|
|
|
|
|
|
|
|
|
|
var version =
|
|
|
|
|
$"{ThisAssembly.Git.SemVer.Major}.{ThisAssembly.Git.SemVer.Major}.{ThisAssembly.Git.SemVer.Patch}{ThisAssembly.Git.SemVer.DashLabel}";
|
|
|
|
|
services.AddSingleton(s => new ApplicationInfo
|
|
|
|
|
{
|
|
|
|
|
ApplicationSlug = "Wabbajack",
|
|
|
|
|
ApplicationName = Environment.ProcessPath?.ToAbsolutePath().FileName.ToString() ?? "Wabbajack",
|
|
|
|
|
ApplicationSha = ThisAssembly.Git.Sha,
|
|
|
|
|
Platform = RuntimeInformation.ProcessArchitecture.ToString(),
|
|
|
|
|
OperatingSystemDescription = RuntimeInformation.OSDescription,
|
|
|
|
|
RuntimeIdentifier = RuntimeInformation.RuntimeIdentifier,
|
|
|
|
|
OSVersion = Environment.OSVersion.VersionString,
|
|
|
|
|
Version = version
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
2021-12-01 06:06:00 +00:00
|
|
|
|
services.AddResponseCaching();
|
2021-11-27 20:15:07 +00:00
|
|
|
|
services.AddSingleton(s =>
|
|
|
|
|
{
|
|
|
|
|
var settings = s.GetService<AppSettings>()!;
|
|
|
|
|
if (string.IsNullOrWhiteSpace(settings.GitHubKey))
|
|
|
|
|
return new GitHubClient(new ProductHeaderValue("wabbajack"));
|
|
|
|
|
|
|
|
|
|
var creds = new Credentials(settings.GitHubKey);
|
|
|
|
|
return new GitHubClient(new ProductHeaderValue("wabbajack")) {Credentials = creds};
|
|
|
|
|
});
|
2021-11-27 18:31:35 +00:00
|
|
|
|
services.AddDTOSerializer();
|
|
|
|
|
services.AddDTOConverters();
|
2021-10-23 16:51:17 +00:00
|
|
|
|
services.AddResponseCompression(options =>
|
|
|
|
|
{
|
|
|
|
|
options.Providers.Add<BrotliCompressionProvider>();
|
|
|
|
|
options.Providers.Add<GzipCompressionProvider>();
|
|
|
|
|
options.MimeTypes = new[] {"application/json"};
|
|
|
|
|
});
|
2022-06-20 23:21:04 +00:00
|
|
|
|
|
|
|
|
|
// CouchDB
|
|
|
|
|
services.AddSingleton(s =>
|
|
|
|
|
{
|
2022-06-22 21:11:26 +00:00
|
|
|
|
var settings = s.GetRequiredService<AppSettings>().CesiDB;
|
|
|
|
|
var client = new CouchClient(settings.Endpoint, b =>
|
2022-06-20 23:21:04 +00:00
|
|
|
|
{
|
2022-06-22 21:11:26 +00:00
|
|
|
|
b.UseBasicAuthentication(settings.Username, settings.Password);
|
2022-06-20 23:21:04 +00:00
|
|
|
|
b.SetPropertyCase(PropertyCaseType.None);
|
|
|
|
|
b.SetJsonNullValueHandling(NullValueHandling.Ignore);
|
|
|
|
|
});
|
2022-06-22 21:11:26 +00:00
|
|
|
|
return client.GetDatabase<Analyzed>(settings.Database);
|
2022-06-20 23:21:04 +00:00
|
|
|
|
});
|
2022-06-22 01:38:42 +00:00
|
|
|
|
|
|
|
|
|
services.AddSingleton(s =>
|
|
|
|
|
{
|
2022-06-22 21:11:26 +00:00
|
|
|
|
var settings = s.GetRequiredService<AppSettings>().MetricsDB;
|
|
|
|
|
var client = new CouchClient(settings.Endpoint, b =>
|
2022-06-22 01:38:42 +00:00
|
|
|
|
{
|
2022-06-22 21:11:26 +00:00
|
|
|
|
b.UseBasicAuthentication(settings.Username, settings.Password);
|
2022-06-22 01:38:42 +00:00
|
|
|
|
b.SetPropertyCase(PropertyCaseType.None);
|
|
|
|
|
b.SetJsonNullValueHandling(NullValueHandling.Ignore);
|
|
|
|
|
});
|
2022-06-22 21:11:26 +00:00
|
|
|
|
return client.GetDatabase<Metric>(settings.Database);
|
2022-06-22 01:38:42 +00:00
|
|
|
|
});
|
2021-10-23 16:51:17 +00:00
|
|
|
|
|
|
|
|
|
services.AddMvc();
|
2022-06-20 23:21:04 +00:00
|
|
|
|
services
|
|
|
|
|
.AddControllers()
|
|
|
|
|
.AddJsonOptions(j =>
|
|
|
|
|
{
|
|
|
|
|
j.JsonSerializerOptions.PropertyNamingPolicy = null;
|
|
|
|
|
j.JsonSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingDefault;
|
|
|
|
|
});
|
2021-12-20 16:31:10 +00:00
|
|
|
|
|
|
|
|
|
NettleEngine.GetCompiler().RegisterWJFunctions();
|
2021-10-23 16:51:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
|
|
|
|
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
|
|
|
|
|
{
|
|
|
|
|
if (env.IsDevelopment()) app.UseDeveloperExceptionPage();
|
|
|
|
|
|
|
|
|
|
app.UseDeveloperExceptionPage();
|
|
|
|
|
|
|
|
|
|
var provider = new FileExtensionContentTypeProvider();
|
|
|
|
|
provider.Mappings[".rar"] = "application/x-rar-compressed";
|
|
|
|
|
provider.Mappings[".7z"] = "application/x-7z-compressed";
|
|
|
|
|
provider.Mappings[".zip"] = "application/zip";
|
|
|
|
|
provider.Mappings[".wabbajack"] = "application/zip";
|
|
|
|
|
app.UseStaticFiles();
|
|
|
|
|
|
|
|
|
|
app.UseRouting();
|
|
|
|
|
|
|
|
|
|
app.UseAuthentication();
|
|
|
|
|
app.UseAuthorization();
|
|
|
|
|
app.UseResponseCompression();
|
|
|
|
|
|
|
|
|
|
app.UseService<DiscordWebHook>();
|
|
|
|
|
|
2021-12-01 06:06:00 +00:00
|
|
|
|
app.UseResponseCaching();
|
|
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
|
app.Use(next =>
|
|
|
|
|
{
|
|
|
|
|
return async context =>
|
2020-05-09 03:56:06 +00:00
|
|
|
|
{
|
2021-10-23 16:51:17 +00:00
|
|
|
|
var stopWatch = new Stopwatch();
|
|
|
|
|
stopWatch.Start();
|
|
|
|
|
context.Response.OnStarting(() =>
|
2020-05-09 03:56:06 +00:00
|
|
|
|
{
|
2021-10-23 16:51:17 +00:00
|
|
|
|
stopWatch.Stop();
|
|
|
|
|
var headers = context.Response.Headers;
|
|
|
|
|
headers.Add("Access-Control-Allow-Origin", "*");
|
|
|
|
|
headers.Add("Access-Control-Allow-Methods", "POST, GET");
|
|
|
|
|
headers.Add("Access-Control-Allow-Headers", "Accept, Origin, Content-type");
|
|
|
|
|
headers.Add("X-ResponseTime-Ms", stopWatch.ElapsedMilliseconds.ToString());
|
|
|
|
|
if (!headers.ContainsKey("Cache-Control"))
|
|
|
|
|
headers.Add("Cache-Control", "no-cache");
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
});
|
|
|
|
|
await next(context);
|
|
|
|
|
};
|
|
|
|
|
});
|
2020-05-09 03:56:06 +00:00
|
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
|
app.UseFileServer(new FileServerOptions
|
|
|
|
|
{
|
|
|
|
|
FileProvider = new PhysicalFileProvider(
|
|
|
|
|
Path.Combine(Directory.GetCurrentDirectory(), "public")),
|
|
|
|
|
StaticFileOptions = {ServeUnknownFileTypes = true}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
app.UseEndpoints(endpoints => { endpoints.MapControllers(); });
|
2022-01-17 17:02:52 +00:00
|
|
|
|
|
|
|
|
|
// Trigger the internal update code
|
2022-01-18 22:58:54 +00:00
|
|
|
|
app.ApplicationServices.GetRequiredService<NexusCacheManager>();
|
|
|
|
|
app.ApplicationServices.GetRequiredService<DiscordBackend>();
|
2020-05-09 03:56:06 +00:00
|
|
|
|
}
|
2021-10-23 16:51:17 +00:00
|
|
|
|
}
|