Add Server-Side Download Queue (#21)

* test add queueing to file service

* further adjustments to download queueing

* add check for whether the request is still in the queue to CheckQueue

* forcefully release slot if download didn't finish in 15s

* actually cancel the delay task

* add metrics and refactor some of the request queue service

* refactor pathing

* reuse httpclient

* add queue request dto to requestfile, enqueue users immediately if a slot is available

* change startup to include all controllers

* update server pathing

* update pathing, again

* several adjustments to auth, banning, jwt server tokens, renaming, authorization

* update api I guess

* adjust automated banning of charaident and reg

* generate jwt on servers for internal authentication

* remove mvcextensions

Co-authored-by: rootdarkarchon <root.darkarchon@outlook.com>
This commit is contained in:
rootdarkarchon
2023-01-11 12:22:22 +01:00
committed by GitHub
parent db2d0451ca
commit 42b15cb6b7
38 changed files with 1116 additions and 98 deletions

View File

@@ -4,4 +4,5 @@ public static class MareClaimTypes
{
public const string Uid = "uid";
public const string CharaIdent = "character_identification";
public const string Internal = "internal";
}

View File

@@ -0,0 +1,54 @@
using MareSynchronosShared.Services;
using Microsoft.IdentityModel.Tokens;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using System.Text;
namespace MareSynchronosShared.Utils;
public class ServerTokenGenerator
{
private readonly IConfigurationService<MareConfigurationAuthBase> _configuration;
private Dictionary<string, string> _tokenDictionary { get; set; } = new(StringComparer.Ordinal);
public string Token
{
get
{
var currentJwt = _configuration.GetValue<string>(nameof(MareConfigurationAuthBase.Jwt));
if (_tokenDictionary.TryGetValue(currentJwt, out var token))
{
return token;
}
return GenerateToken();
}
}
public ServerTokenGenerator(IConfigurationService<MareConfigurationAuthBase> configuration)
{
_configuration = configuration;
}
private string GenerateToken()
{
var signingKey = _configuration.GetValue<string>(nameof(MareConfigurationAuthBase.Jwt));
var authSigningKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(signingKey));
var token = new SecurityTokenDescriptor()
{
Subject = new ClaimsIdentity(new List<Claim>()
{
new Claim(MareClaimTypes.Uid, _configuration.GetValue<string>(nameof(MareConfigurationBase.ShardName))),
new Claim(MareClaimTypes.Internal, true.ToString())
}),
SigningCredentials = new SigningCredentials(authSigningKey, SecurityAlgorithms.HmacSha256Signature)
};
var handler = new JwtSecurityTokenHandler();
var rawData = handler.CreateJwtSecurityToken(token).RawData;
_tokenDictionary[signingKey] = rawData;
return rawData;
}
}

View File

@@ -12,6 +12,9 @@ public class StaticFilesServerConfiguration : MareConfigurationBase
public string CacheDirectory { get; set; }
public Uri? RemoteCacheSourceUri { get; set; } = null;
public Uri MainServerGrpcAddress { get; set; } = null;
public int DownloadQueueSize { get; set; } = 50;
public int DownloadTimeoutSeconds { get; set; } = 5;
public int DownloadQueueReleaseSeconds { get; set; } = 15;
public override string ToString()
{
StringBuilder sb = new();
@@ -23,6 +26,8 @@ public class StaticFilesServerConfiguration : MareConfigurationBase
sb.AppendLine($"{nameof(UnusedFileRetentionPeriodInDays)} => {UnusedFileRetentionPeriodInDays}");
sb.AppendLine($"{nameof(CacheDirectory)} => {CacheDirectory}");
sb.AppendLine($"{nameof(RemoteCacheSourceUri)} => {RemoteCacheSourceUri}");
sb.AppendLine($"{nameof(DownloadQueueSize)} => {DownloadQueueSize}");
sb.AppendLine($"{nameof(DownloadQueueReleaseSeconds)} => {DownloadQueueReleaseSeconds}");
return sb.ToString();
}
}