-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Startup.cs
591 lines (518 loc) · 24 KB
/
Startup.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.EntityFrameworkCore;
using System.Text.Json.Serialization;
using BeatLeader_Server.Services;
using AspNetCoreRateLimit;
using Microsoft.AspNetCore.DataProtection;
using Microsoft.AspNetCore.HttpOverrides;
using Prometheus.Client.DependencyInjection;
using Prometheus.Client.HttpRequestDurations;
using Prometheus.Client.AspNetCore;
using System.Net;
using BeatLeader_Server.Bot;
using static OpenIddict.Abstractions.OpenIddictConstants;
using BeatLeader_Server.Models;
using Microsoft.OpenApi.Models;
using System.Reflection;
using ReplayDecoder;
using System.Security.Cryptography.X509Certificates;
using Prometheus.Client;
using System.Diagnostics;
using BeatLeader_Server.Utils;
using Swashbuckle.AspNetCore.Annotations;
using Prometheus.Client.Collectors.ProcessStats;
using System.Security.Claims;
using BeatLeader_Server.Enums;
using BeatLeader_Server.Extensions;
namespace BeatLeader_Server {
public class ErrorLoggingMiddleware
{
private readonly RequestDelegate _next;
private readonly ILogger<ErrorLoggingMiddleware> _logger;
private readonly Process process;
public ErrorLoggingMiddleware(RequestDelegate next, ILogger<ErrorLoggingMiddleware> logger)
{
_next = next;
_logger = logger;
process = Process.GetCurrentProcess();
}
public async Task Invoke(HttpContext context)
{
try
{
var guid = Guid.NewGuid();
float before = (float)(Process.GetCurrentProcess().WorkingSet64 / 1000l) / 1000000.0f;
if (context.Request.Path != "/servername") {
_logger.LogWarning(null, $"STARTED {guid} {before} GB {context.Request.Path}{context.Request.QueryString}");
}
await _next(context);
if (context.Request.Path != "/servername") {
_logger.LogWarning(null, $"FINISHED {guid} {before} {(float)(Process.GetCurrentProcess().WorkingSet64 / 1000l) / 1000000.0f} GB {context.Request.Path}{context.Request.QueryString}");
}
}
catch (Exception e)
{
_logger.LogWarning(null, "LOL500 " + context.Request.Path + context.Request.QueryString);
throw;
}
}
}
public class LocalstatsMiddleware
{
private readonly RequestDelegate _next;
private readonly ILogger<LocalstatsMiddleware> _logger;
public LocalstatsMiddleware(RequestDelegate next, ILogger<LocalstatsMiddleware> logger)
{
_next = next;
_logger = logger;
}
public async Task Invoke(HttpContext context)
{
if (context.Request.Path == "/metrics")
{
var remoteIp = context.Connection.RemoteIpAddress;
if (context.Request.Headers.ContainsKey("X-Forwarded-For") || !IPAddress.IsLoopback(remoteIp))
{
context.Response.StatusCode = (int)HttpStatusCode.Forbidden;
return;
}
}
await _next(context);
}
}
public class CustomCookieManager : ICookieManager
{
public string GetRequestCookie(HttpContext context, string key)
{
return context.Request.Cookies[key];
}
public void AppendResponseCookie(HttpContext context, string key, string value, CookieOptions options)
{
if (options.Domain == null)
{
options.Domain = context.Request.Host.Value.Replace("api", "");
}
context.Response.Cookies.Append(key, value, options);
}
public void DeleteCookie(HttpContext context, string key, CookieOptions options)
{
if (options.Domain == null)
{
options.Domain = context.Request.Host.Value.Replace("api", "");
}
context.Response.Cookies.Delete(key, options);
}
}
public class ReplayRecalculatorStub : IReplayRecalculator
{
public async Task<(int?, Replay)> RecalculateReplay(Replay replay)
{
return (0, replay);
}
}
public class Startup {
static string MyAllowSpecificOrigins = "_myAllowSpecificOrigins";
public Startup (IConfiguration configuration, IWebHostEnvironment env)
{
Configuration = configuration;
Environment = env;
}
public IConfiguration Configuration { get; }
public IWebHostEnvironment Environment { get; }
public void ConfigureServices (IServiceCollection services)
{
string steamKey = Configuration.GetValue<string>("SteamKey");
string steamApi = Configuration.GetValue<string>("SteamApi");
string beatSaverId = Configuration.GetValue<string>("BeatSaverId");
string beatSaverSecret = Configuration.GetValue<string>("BeatSaverSecret");
services.AddDataProtection()
.PersistKeysToFileSystem(new DirectoryInfo(@"../keys/"))
.SetApplicationName("/home/site/wwwroot/");
services.Configure<ForwardedHeadersOptions>(options =>
{
options.ForwardedHeaders =
ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;
});
var replayRecalculatorType = Assembly.GetExecutingAssembly()
.GetTypes()
.FirstOrDefault(t => typeof(IReplayRecalculator).IsAssignableFrom(t) && !t.FullName.Contains("Stub") && !t.IsInterface && !t.IsAbstract);
if (replayRecalculatorType != null)
{
services.AddScoped(typeof(IReplayRecalculator), replayRecalculatorType);
} else {
services.AddScoped(typeof(IReplayRecalculator), typeof(ReplayRecalculatorStub));
}
var authBuilder = services.AddAuthentication (options => {
options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
})
.AddCookie (options => {
if (!Environment.IsDevelopment()) {
options.CookieManager = new CustomCookieManager();
}
options.Events.OnRedirectToAccessDenied =
options.Events.OnRedirectToLogin = c => {
c.Response.StatusCode = StatusCodes.Status401Unauthorized;
return Task.FromResult<object> (null);
};
options.Events.OnSigningIn = async context =>
{
var claims = new List<Claim>
{
new Claim(CustomAuthClaims.Issued, Time.UnixNow().ToString())
};
var appIdentity = new ClaimsIdentity(claims);
context.Principal?.AddIdentity(appIdentity);
await Task.CompletedTask;
};
options.Cookie.SameSite = SameSiteMode.Lax;
options.Cookie.HttpOnly = false;
options.ExpireTimeSpan = TimeSpan.FromDays(30);
options.Cookie.MaxAge = options.ExpireTimeSpan;
options.SlidingExpiration = true;
})
.AddCookie("BLPatreon")
.AddCookie("BLTwitch")
.AddCookie("BLTwitter")
.AddCookie("BLGoogle")
.AddCookie("BLDiscord")
.AddCookie("BLBeatSaver")
.AddCookie("BLGitHub")
.AddSteamTicket(options =>
{
options.Key = steamKey;
options.ApplicationID = "620980";
options.ApiUrl = steamApi;
})
.AddOculusToken (options =>
{
})
.AddOculus(options => {})
.AddSteam (options => {
options.ApplicationKey = steamKey;
options.UserInformationEndpoint = $"{steamApi}/ISteamUser/GetPlayerSummaries/v0002/";
options.Events.OnAuthenticated = ctx => {
/* ... */
return Task.CompletedTask;
};
})
.AddBeatSaver(options => {
options.SignInScheme = "BLBeatSaver";
options.SaveTokens = true;
options.ClientId = beatSaverId;
options.ClientSecret = beatSaverSecret;
});
services.AddMemoryCache();
services.Configure<IpRateLimitOptions>(options =>
{
options.EnableEndpointRateLimiting = true;
options.StackBlockedRequests = false;
options.HttpStatusCode = 429;
options.RealIpHeader = "cf-connecting-ip";
options.ClientIdHeader = "X-ClientId";
options.GeneralRules = new List<RateLimitRule>
{
new RateLimitRule
{
Endpoint = "GET:/*",
Period = "10s",
Limit = 50,
},
new RateLimitRule
{
Endpoint = "GET:/players",
Period = "10s",
Limit = 10,
},
new RateLimitRule
{
Endpoint = "GET:/user/friendScores",
Period = "1s",
Limit = 1,
}
};
});
services.AddSingleton<IIpPolicyStore, MemoryCacheIpPolicyStore>();
services.AddSingleton<IRateLimitCounterStore, MemoryCacheRateLimitCounterStore>();
services.AddSingleton<IRateLimitConfiguration, RateLimitConfiguration>();
services.AddSingleton<IProcessingStrategy, AsyncKeyLockProcessingStrategy>();
services.AddInMemoryRateLimiting();
if (!Environment.IsDevelopment()) {
string patreonId = Configuration.GetValue<string>("PatreonId");
string patreonSecret = Configuration.GetValue<string>("PatreonSecret");
authBuilder.AddPatreon(options => {
options.SignInScheme = "BLPatreon";
options.SaveTokens = true;
options.ClientId = patreonId;
options.ClientSecret = patreonSecret;
});
string twitchId = Configuration.GetValue<string>("TwitchId");
string twitchSecret = Configuration.GetValue<string>("TwitchSecret");
authBuilder.AddTwitch(options =>
{
options.SaveTokens = true;
options.ClientId = twitchId;
options.ClientSecret = twitchSecret;
options.SignInScheme = "BLTwitch";
});
string twitterId = Configuration.GetValue<string>("TwitterId");
string twitterSecret = Configuration.GetValue<string>("TwitterSecret");
authBuilder.AddTwitter(options =>
{
options.SaveTokens = true;
options.ClientId = twitterId;
options.ClientSecret = twitterSecret;
options.SignInScheme = "BLTwitter";
});
string discordId = Configuration.GetValue<string>("DiscordId");
string discordSecret = Configuration.GetValue<string>("DiscordSecret");
authBuilder.AddDiscord(options =>
{
options.SaveTokens = true;
options.ClientId = discordId;
options.ClientSecret = discordSecret;
options.SignInScheme = "BLDiscord";
});
string googleId = Configuration.GetValue<string>("GoogleId");
string googleSecret = Configuration.GetValue<string>("GoogleSecret");
authBuilder.AddGoogle(options =>
{
options.SaveTokens = true;
options.ClientId = googleId;
options.ClientSecret = googleSecret;
options.SignInScheme = "BLGoogle";
options.Scope.Add("https://www.googleapis.com/auth/youtube.readonly");
});
string githubId = Configuration.GetValue<string>("GitHubId");
string githubSecret = Configuration.GetValue<string>("GitHubSecret");
authBuilder.AddGitHub(options =>
{
options.SaveTokens = true;
options.ClientId = githubId;
options.ClientSecret = githubSecret;
options.SignInScheme = "BLGitHub";
});
} else {
authBuilder.AddCookie("BLBeatLeader")
.AddBeatLeader(options => {
options.SignInScheme = "BLBeatLeader";
options.SaveTokens = true;
options.ClientId = OauthService.TestClientId;
options.ClientSecret = OauthService.TestClientSecret;
});
}
services.AddServerTiming();
services.AddMetricFactory();
services.AddAntiforgery(options =>
{
options.Cookie.Name = "X-CSRF-TOKEN";
options.HeaderName = "X-CSRF-TOKEN";
options.SuppressXFrameOptionsHeader = false;
});
services.AddDbContextFactory<AppContext>(options => {
options.UseSqlServer(Configuration.GetValue<string>("DefaultConnection"));
options.UseOpenIddict();
});
services.AddDbContextFactory<StorageContext>(options => {
options.UseSqlServer(Configuration.GetValue<string>("StorageConnection"));
options.UseOpenIddict();
});
services.AddOpenIddict()
// Register the OpenIddict core components.
.AddCore(options =>
{
// Configure OpenIddict to use the Entity Framework Core stores and models.
// Note: call ReplaceDefaultEntities() to replace the default entities.
options.UseEntityFrameworkCore()
.UseDbContext<AppContext>();
})
// Register the OpenIddict server components.
.AddServer(options =>
{
// Enable the token endpoint.
options.SetAuthorizationEndpointUris("oauth2/authorize")
.SetLogoutEndpointUris("signout")
.SetTokenEndpointUris("oauth2/token")
.SetUserinfoEndpointUris("oauth2/identity");
// Enable the client credentials flow.
options.RegisterScopes(
Scopes.Profile,
CustomScopes.Clan);
// Note: the sample uses the code and refresh token flows but you can enable
// the other flows if you need to support implicit, password or client credentials.
options.AllowAuthorizationCodeFlow()
.AllowRefreshTokenFlow();
options.SetRefreshTokenReuseLeeway(TimeSpan.FromMinutes(10));
if (Environment.IsDevelopment()) {
// Register the signing and encryption credentials.
options.AddDevelopmentEncryptionCertificate()
.AddDevelopmentSigningCertificate();
} else {
var signingCertificate2 = new X509Certificate2("../keys/openiddict_certificate2.pfx", Configuration.GetValue<string>("OpeniddictPassword2"));
options.AddSigningCertificate(signingCertificate2);
options.AddEncryptionCertificate(signingCertificate2);
var signingCertificate = new X509Certificate2("../keys/openiddict_certificate.pfx", Configuration.GetValue<string>("OpeniddictPassword"));
options.AddSigningCertificate(signingCertificate);
options.AddEncryptionCertificate(signingCertificate);
}
// Register the ASP.NET Core host and configure the ASP.NET Core options.
options.UseAspNetCore()
.EnableAuthorizationEndpointPassthrough()
.EnableLogoutEndpointPassthrough()
.EnableStatusCodePagesIntegration()
.EnableUserinfoEndpointPassthrough();
})
// Register the OpenIddict validation components.
.AddValidation(options =>
{
// Import the configuration from the local OpenIddict server instance.
options.UseLocalServer();
// Register the ASP.NET Core host.
options.UseAspNetCore();
});
if (!Environment.IsDevelopment()) {
if (Configuration.GetValue<string>("ServicesHost") == "YES")
{
services.AddHostedService<HourlyRefresh>();
services.AddHostedService<DailyRefresh>();
services.AddHostedService<HistoryService>();
services.AddHostedService<BotService>();
services.AddHostedService<RankingService>();
services.AddHostedService<SongService>();
}
}
services.AddHostedService<ConstantsService>();
services.AddHostedService<RefreshTaskService>();
services.AddHostedService<MinuteRefresh>();
services.AddHostedService<ClanTaskService>();
services.AddHostedService<ClanMessageService>();
services.AddHostedService<ClanCallbackService>();
services.AddHostedService<SearchService>();
services.AddHostedService<OauthService>();
services.AddSingleton<RTNominationsForum>();
services.AddSingleton<SpeedrunService>();
services.AddMvc ().AddControllersAsServices ().AddJsonOptions (options => {
options.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.IgnoreCycles;
});
services.AddCors (options => {
options.AddPolicy (name: MyAllowSpecificOrigins,
builder => {
builder.WithOrigins(Configuration.GetSection("CORS").Get<string[]>())
.AllowAnyHeader()
.AllowAnyMethod()
.AllowCredentials();
});
});
services.AddHttpClient();
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("blapi", new OpenApiInfo { Title = "API.BL", Version = "v1" });
c.SwaggerDoc("blapifull", new OpenApiInfo { Title = "API.BL.FULL", Version = "v1" });
c.CustomOperationIds(apiDesc =>
{
var controllerName = apiDesc.ActionDescriptor.RouteValues["controller"];
var actionName = apiDesc.ActionDescriptor.RouteValues["action"];
return $"{controllerName}_{actionName}";
});
c.EnableAnnotations();
c.SupportNonNullableReferenceTypes();
c.UseAllOfToExtendReferenceSchemas();
var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
var xmlPath = Path.Combine(System.AppContext.BaseDirectory, xmlFile);
c.IncludeXmlComments(xmlPath);
c.IncludeXmlComments(Path.Combine(System.AppContext.BaseDirectory, "Models.xml"));
c.IncludeXmlComments(Path.Combine(System.AppContext.BaseDirectory, "Parser.xml"));
c.DocInclusionPredicate((documentName, apiDescription) =>
{
if (documentName == "blapifull")
{
return true;
}
else
{
var swaggerOperationAttribute = apiDescription.ActionDescriptor.EndpointMetadata
.OfType<SwaggerOperationAttribute>()
.FirstOrDefault();
return swaggerOperationAttribute?.Summary != null;
}
});
c.SchemaFilter<EnumSchemaFilter>();
});
services.AddResponseCompression(options =>
{
options.EnableForHttps = true;
});
RequestDecompressionServiceCollectionExtensions.AddRequestDecompression(services);
}
public class GrafanaTimingMiddleware
{
private readonly RequestDelegate _next;
private readonly ILogger<LocalstatsMiddleware> _logger;
private readonly IMetricFamily<ISummary> _durationHistogram;
public GrafanaTimingMiddleware(
RequestDelegate next,
ILogger<LocalstatsMiddleware> logger,
IMetricFactory metricFactory)
{
_next = next;
_logger = logger;
_durationHistogram = metricFactory.CreateSummary(
"bl_requests_duration_seconds",
"Histogram of request durations",
new[] {"method", "endpoint"}
);
}
public async Task Invoke(HttpContext context)
{
var start = DateTime.Now;
await _next(context);
var duration = DateTime.Now - start;
var routeData = context.GetRouteData();
string path = routeData?.Values["controller"]?.ToString() + "/" + routeData?.Values["action"]?.ToString();
_durationHistogram.WithLabels(context.Request.Method, path).Observe(duration.TotalSeconds);
}
}
public void Configure (IApplicationBuilder app)
{
app.UseMiddleware<ErrorLoggingMiddleware>();
app.UseMiddleware<LocalstatsMiddleware>();
app.UsePrometheusServer();
app.UsePrometheusRequestDurations();
app.UseMiddleware<GrafanaTimingMiddleware>();
app.UseStaticFiles();
app.UseForwardedHeaders();
app.UseServerTiming();
app.UseWebSockets(new WebSocketOptions {
KeepAliveInterval = TimeSpan.FromMinutes(2)
});
app.UseIpRateLimiting();
app.UseRouting ();
app.UseCookiePolicy(new CookiePolicyOptions {
MinimumSameSitePolicy = SameSiteMode.None,
Secure = CookieSecurePolicy.Always
});
app.UseAuthentication ();
app.UseAuthorization ();
app.UseCors (MyAllowSpecificOrigins);
app.UseResponseCompression();
RequestDecompressionApplicationBuilderExtensions.UseRequestDecompression(app);
app.UseEndpoints (endpoints => {
endpoints.MapDefaultControllerRoute ();
});
app.UseSwagger(c => {
c.PreSerializeFilters.Add((swagger, httpReq) =>
{
swagger.Info.Title = "BeatLeader API. Get various Beat Saber information.";
swagger.Info.Description = "Retrieves players, scores, rankings, maps, leaderboards and much more for Beat Saber.";
swagger.Servers = new List<OpenApiServer> { new OpenApiServer { Url = $"{httpReq.Scheme}://{httpReq.Host.Value}" } };
});
});
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/blapi/swagger.json", "BeatLeader API v1");
c.SwaggerEndpoint("/swagger/blapifull/swagger.json", "Full(Undocumented) BL API v1");
c.RoutePrefix = "swagger";
});
app.ApplicationServices.GetService<SpeedrunService>()?.Start();
}
}
}