potential optimizations (ConfigureAwait, Logging Templates)

This commit is contained in:
Stanley Dimant
2022-08-22 10:46:37 +02:00
parent 98ee6c7f66
commit 927e1aaceb
11 changed files with 182 additions and 179 deletions

View File

@@ -113,7 +113,7 @@ namespace MareSynchronosServer.Authentication
var token = failedAuth.ResetCts.Token; var token = failedAuth.ResetCts.Token;
failedAuth.ResetTask = Task.Run(async () => failedAuth.ResetTask = Task.Run(async () =>
{ {
await Task.Delay(TimeSpan.FromMinutes(tempBanMinutes), token); await Task.Delay(TimeSpan.FromMinutes(tempBanMinutes), token).ConfigureAwait(false);
if (token.IsCancellationRequested) return; if (token.IsCancellationRequested) return;
FailedAuthorization fauth; FailedAuthorization fauth;
lock (failedAuthLock) lock (failedAuthLock)
@@ -123,7 +123,7 @@ namespace MareSynchronosServer.Authentication
fauth.Dispose(); fauth.Dispose();
}, token); }, token);
Logger.LogWarning("TempBan " + ip + " for authorization spam"); Logger.LogWarning("TempBan {ip} for authorization spam", ip);
return AuthenticateResult.Fail("Failed Authorization"); return AuthenticateResult.Fail("Failed Authorization");
} }
} }
@@ -142,7 +142,7 @@ namespace MareSynchronosServer.Authentication
lock (failedAuthLock) lock (failedAuthLock)
{ {
Logger.LogWarning("Failed authorization from " + ip); Logger.LogWarning("Failed authorization from {ip}", ip);
if (FailedAuthorizations.TryGetValue(ip, out var auth)) if (FailedAuthorizations.TryGetValue(ip, out var auth))
{ {
auth.IncreaseFailedAttempts(); auth.IncreaseFailedAttempts();
@@ -163,7 +163,7 @@ namespace MareSynchronosServer.Authentication
if (string.IsNullOrEmpty(uid)) if (string.IsNullOrEmpty(uid))
{ {
uid = (await _mareDbContext.Auth.AsNoTracking() uid = (await _mareDbContext.Auth.AsNoTracking()
.FirstOrDefaultAsync(m => m.HashedKey == hashedHeader))?.UserUID; .FirstOrDefaultAsync(m => m.HashedKey == hashedHeader).ConfigureAwait(false))?.UserUID;
if (uid == null) if (uid == null)
{ {
@@ -172,7 +172,7 @@ namespace MareSynchronosServer.Authentication
Authentications[hashedHeader] = unauthorized; Authentications[hashedHeader] = unauthorized;
} }
Logger.LogWarning("Failed authorization from " + ip); Logger.LogWarning("Failed authorization from {ip}", ip);
lock (failedAuthLock) lock (failedAuthLock)
{ {
if (FailedAuthorizations.TryGetValue(ip, out var auth)) if (FailedAuthorizations.TryGetValue(ip, out var auth))

View File

@@ -49,7 +49,7 @@ namespace MareSynchronosServer
using var scope = _services.CreateScope(); using var scope = _services.CreateScope();
using var dbContext = scope.ServiceProvider.GetService<MareDbContext>()!; using var dbContext = scope.ServiceProvider.GetService<MareDbContext>()!;
_logger.LogInformation($"Cleaning up files older than {filesOlderThanDays} days"); _logger.LogInformation("Cleaning up files older than {filesOlderThanDays} days", filesOlderThanDays);
try try
{ {
@@ -62,13 +62,13 @@ namespace MareSynchronosServer
var fi = new FileInfo(fileName); var fi = new FileInfo(fileName);
if (!fi.Exists) if (!fi.Exists)
{ {
_logger.LogInformation("File does not exist anymore: " + fileName); _logger.LogInformation("File does not exist anymore: {fileName}", fileName);
dbContext.Files.Remove(file); dbContext.Files.Remove(file);
} }
else if (fi.LastAccessTime < prevTime) else if (fi.LastAccessTime < prevTime)
{ {
MareMetrics.FilesTotalSize.Dec(fi.Length); MareMetrics.FilesTotalSize.Dec(fi.Length);
_logger.LogInformation("File outdated: " + fileName); _logger.LogInformation("File outdated: {fileName}", fileName);
dbContext.Files.Remove(file); dbContext.Files.Remove(file);
fi.Delete(); fi.Delete();
} }
@@ -144,7 +144,7 @@ namespace MareSynchronosServer
usersOlderThanDays = 14; usersOlderThanDays = 14;
} }
_logger.LogInformation($"Cleaning up users older than {usersOlderThanDays} days"); _logger.LogInformation("Cleaning up users older than {usersOlderThanDays} days", usersOlderThanDays);
var allUsers = dbContext.Users.ToList(); var allUsers = dbContext.Users.ToList();
List<User> usersToRemove = new(); List<User> usersToRemove = new();
@@ -152,7 +152,7 @@ namespace MareSynchronosServer
{ {
if (user.LastLoggedIn < (DateTime.UtcNow - TimeSpan.FromDays(usersOlderThanDays))) if (user.LastLoggedIn < (DateTime.UtcNow - TimeSpan.FromDays(usersOlderThanDays)))
{ {
_logger.LogInformation("User outdated: " + user.UID); _logger.LogInformation("User outdated: {userUID}", user.UID);
usersToRemove.Add(user); usersToRemove.Add(user);
} }
} }

View File

@@ -58,21 +58,21 @@ namespace MareSynchronosServer.Discord
private async Task DiscordClient_SlashCommandExecuted(SocketSlashCommand arg) private async Task DiscordClient_SlashCommandExecuted(SocketSlashCommand arg)
{ {
await semaphore.WaitAsync(); await semaphore.WaitAsync().ConfigureAwait(false);
try try
{ {
if (arg.Data.Name == "register") if (arg.Data.Name == "register")
{ {
if (arg.Data.Options.FirstOrDefault(f => f.Name == "overwrite_old_account") != null) if (arg.Data.Options.FirstOrDefault(f => f.Name == "overwrite_old_account") != null)
{ {
await DeletePreviousUserAccount(arg.User.Id); await DeletePreviousUserAccount(arg.User.Id).ConfigureAwait(false);
} }
var modal = new ModalBuilder(); var modal = new ModalBuilder();
modal.WithTitle("Verify with Lodestone"); modal.WithTitle("Verify with Lodestone");
modal.WithCustomId("register_modal"); modal.WithCustomId("register_modal");
modal.AddTextInput("Enter the Lodestone URL of your Character", "lodestoneurl", TextInputStyle.Short, "https://*.finalfantasyxiv.com/lodestone/character/<CHARACTERID>/", required: true); modal.AddTextInput("Enter the Lodestone URL of your Character", "lodestoneurl", TextInputStyle.Short, "https://*.finalfantasyxiv.com/lodestone/character/<CHARACTERID>/", required: true);
await arg.RespondWithModalAsync(modal.Build()); await arg.RespondWithModalAsync(modal.Build()).ConfigureAwait(false);
} }
else if (arg.Data.Name == "verify") else if (arg.Data.Name == "verify")
{ {
@@ -81,23 +81,23 @@ namespace MareSynchronosServer.Discord
{ {
eb.WithTitle("Already queued for verfication"); eb.WithTitle("Already queued for verfication");
eb.WithDescription("You are already queued for verification. Please wait."); eb.WithDescription("You are already queued for verification. Please wait.");
await arg.RespondAsync(embeds: new[] { eb.Build() }, ephemeral: true); await arg.RespondAsync(embeds: new[] { eb.Build() }, ephemeral: true).ConfigureAwait(false);
} }
else if (!DiscordLodestoneMapping.ContainsKey(arg.User.Id)) else if (!DiscordLodestoneMapping.ContainsKey(arg.User.Id))
{ {
eb.WithTitle("Cannot verify registration"); eb.WithTitle("Cannot verify registration");
eb.WithDescription("You need to **/register** first before you can **/verify**"); eb.WithDescription("You need to **/register** first before you can **/verify**");
await arg.RespondAsync(embeds: new[] { eb.Build() }, ephemeral: true); await arg.RespondAsync(embeds: new[] { eb.Build() }, ephemeral: true).ConfigureAwait(false);
} }
else else
{ {
await arg.DeferAsync(ephemeral: true); await arg.DeferAsync(ephemeral: true).ConfigureAwait(false);
verificationQueue.Enqueue(arg); verificationQueue.Enqueue(arg);
} }
} }
else else
{ {
await arg.RespondAsync("idk what you did to get here to start, just follow the instructions as provided.", ephemeral: true); await arg.RespondAsync("idk what you did to get here to start, just follow the instructions as provided.", ephemeral: true).ConfigureAwait(false);
} }
} }
finally finally
@@ -110,7 +110,7 @@ namespace MareSynchronosServer.Discord
{ {
using var scope = services.CreateScope(); using var scope = services.CreateScope();
using var db = scope.ServiceProvider.GetService<MareDbContext>(); using var db = scope.ServiceProvider.GetService<MareDbContext>();
var discordAuthedUser = await db.LodeStoneAuth.Include(u => u.User).FirstOrDefaultAsync(u => u.DiscordId == id); var discordAuthedUser = await db.LodeStoneAuth.Include(u => u.User).FirstOrDefaultAsync(u => u.DiscordId == id).ConfigureAwait(false);
if (discordAuthedUser != null) if (discordAuthedUser != null)
{ {
if (discordAuthedUser.User != null) if (discordAuthedUser.User != null)
@@ -122,7 +122,7 @@ namespace MareSynchronosServer.Discord
db.Remove(discordAuthedUser); db.Remove(discordAuthedUser);
} }
await db.SaveChangesAsync(); await db.SaveChangesAsync().ConfigureAwait(false);
} }
} }
@@ -130,8 +130,8 @@ namespace MareSynchronosServer.Discord
{ {
if (arg.Data.CustomId == "register_modal") if (arg.Data.CustomId == "register_modal")
{ {
var embed = await HandleRegisterModalAsync(arg); var embed = await HandleRegisterModalAsync(arg).ConfigureAwait(false);
await arg.RespondAsync(embeds: new Embed[] { embed }, ephemeral: true); await arg.RespondAsync(embeds: new Embed[] { embed }, ephemeral: true).ConfigureAwait(false);
} }
} }
@@ -147,10 +147,10 @@ namespace MareSynchronosServer.Discord
if (lodestoneAuth != null && DiscordLodestoneMapping.ContainsKey(id)) if (lodestoneAuth != null && DiscordLodestoneMapping.ContainsKey(id))
{ {
var randomServer = LodestoneServers[random.Next(LodestoneServers.Length)]; var randomServer = LodestoneServers[random.Next(LodestoneServers.Length)];
var response = await req.GetAsync($"https://{randomServer}.finalfantasyxiv.com/lodestone/character/{DiscordLodestoneMapping[id]}"); var response = await req.GetAsync($"https://{randomServer}.finalfantasyxiv.com/lodestone/character/{DiscordLodestoneMapping[id]}").ConfigureAwait(false);
if (response.IsSuccessStatusCode) if (response.IsSuccessStatusCode)
{ {
var content = await response.Content.ReadAsStringAsync(); var content = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
if (content.Contains(lodestoneAuth.LodestoneAuthString)) if (content.Contains(lodestoneAuth.LodestoneAuthString))
{ {
DiscordLodestoneMapping.TryRemove(id, out _); DiscordLodestoneMapping.TryRemove(id, out _);
@@ -168,7 +168,7 @@ namespace MareSynchronosServer.Discord
} }
// make the first registered user on the service to admin // make the first registered user on the service to admin
if (!await db.Users.AnyAsync()) if (!await db.Users.AnyAsync().ConfigureAwait(false))
{ {
user.IsAdmin = true; user.IsAdmin = true;
} }
@@ -187,10 +187,10 @@ namespace MareSynchronosServer.Discord
User = user, User = user,
}; };
db.Users.Add(user); await db.Users.AddAsync(user).ConfigureAwait(false);
db.Auth.Add(auth); await db.Auth.AddAsync(auth).ConfigureAwait(false);
logger.LogInformation("User registered: " + user.UID); logger.LogInformation("User registered: {userUID}", user.UID);
MareMetrics.UsersRegistered.Inc(); MareMetrics.UsersRegistered.Inc();
@@ -217,7 +217,7 @@ namespace MareSynchronosServer.Discord
} }
} }
await db.SaveChangesAsync(); await db.SaveChangesAsync().ConfigureAwait(false);
} }
else else
{ {
@@ -270,7 +270,7 @@ namespace MareSynchronosServer.Discord
} }
else else
{ {
string lodestoneAuth = await GenerateLodestoneAuth(arg.User.Id, hashedLodestoneId, db); string lodestoneAuth = await GenerateLodestoneAuth(arg.User.Id, hashedLodestoneId, db).ConfigureAwait(false);
// check if lodestone id is already in db // check if lodestone id is already in db
embed.WithTitle("Authorize your character"); embed.WithTitle("Authorize your character");
embed.WithDescription("Add following key to your character profile at https://na.finalfantasyxiv.com/lodestone/my/setting/profile/" embed.WithDescription("Add following key to your character profile at https://na.finalfantasyxiv.com/lodestone/my/setting/profile/"
@@ -303,7 +303,7 @@ namespace MareSynchronosServer.Discord
}; };
dbContext.Add(lsAuth); dbContext.Add(lsAuth);
await dbContext.SaveChangesAsync(); await dbContext.SaveChangesAsync().ConfigureAwait(false);
return auth; return auth;
} }
@@ -345,8 +345,8 @@ namespace MareSynchronosServer.Discord
try try
{ {
await discordClient.CreateGlobalApplicationCommandAsync(register.Build()); await discordClient.CreateGlobalApplicationCommandAsync(register.Build()).ConfigureAwait(false);
await discordClient.CreateGlobalApplicationCommandAsync(verify.Build()); await discordClient.CreateGlobalApplicationCommandAsync(verify.Build()).ConfigureAwait(false);
} }
catch (Exception ex) catch (Exception ex)
{ {
@@ -356,7 +356,7 @@ namespace MareSynchronosServer.Discord
private Task Log(LogMessage msg) private Task Log(LogMessage msg)
{ {
logger.LogInformation(msg.ToString()); logger.LogInformation("{msg}", msg);
return Task.CompletedTask; return Task.CompletedTask;
} }
@@ -367,8 +367,8 @@ namespace MareSynchronosServer.Discord
{ {
authToken = configuration.GetValue<string>("DiscordBotToken"); authToken = configuration.GetValue<string>("DiscordBotToken");
await discordClient.LoginAsync(TokenType.Bot, authToken); await discordClient.LoginAsync(TokenType.Bot, authToken).ConfigureAwait(false);
await discordClient.StartAsync(); await discordClient.StartAsync().ConfigureAwait(false);
discordClient.Ready += DiscordClient_Ready; discordClient.Ready += DiscordClient_Ready;
discordClient.SlashCommandExecuted += DiscordClient_SlashCommandExecuted; discordClient.SlashCommandExecuted += DiscordClient_SlashCommandExecuted;
@@ -388,18 +388,18 @@ namespace MareSynchronosServer.Discord
{ {
try try
{ {
var dataEmbed = await HandleVerifyAsync(queueitem.User.Id); var dataEmbed = await HandleVerifyAsync(queueitem.User.Id).ConfigureAwait(false);
await queueitem.FollowupAsync(embed: dataEmbed, ephemeral: true); await queueitem.FollowupAsync(embed: dataEmbed, ephemeral: true).ConfigureAwait(false);
logger.LogInformation("Sent login information to user"); logger.LogInformation("Sent login information to user");
} }
catch (Exception e) catch (Exception e)
{ {
logger.LogError(e.Message); logger.LogError(e, "Error during queue work");
} }
} }
await Task.Delay(TimeSpan.FromSeconds(2), verificationTaskCts.Token); await Task.Delay(TimeSpan.FromSeconds(2), verificationTaskCts.Token).ConfigureAwait(false);
} }
} }
@@ -408,14 +408,14 @@ namespace MareSynchronosServer.Discord
updateStatusCts = new(); updateStatusCts = new();
while (!updateStatusCts.IsCancellationRequested) while (!updateStatusCts.IsCancellationRequested)
{ {
using var scope = services.CreateScope(); await using var scope = services.CreateAsyncScope();
using var db = scope.ServiceProvider.GetService<MareDbContext>(); await using var db = scope.ServiceProvider.GetService<MareDbContext>();
var users = db.Users.Count(c => c.CharacterIdentification != null); var users = db.Users.Count(c => c.CharacterIdentification != null);
await discordClient.SetActivityAsync(new Game("Mare for " + users + " Users")); await discordClient.SetActivityAsync(new Game("Mare for " + users + " Users")).ConfigureAwait(false);
await Task.Delay(TimeSpan.FromSeconds(15)); await Task.Delay(TimeSpan.FromSeconds(15)).ConfigureAwait(false);
} }
} }
@@ -424,8 +424,8 @@ namespace MareSynchronosServer.Discord
verificationTaskCts?.Cancel(); verificationTaskCts?.Cancel();
updateStatusCts?.Cancel(); updateStatusCts?.Cancel();
await discordClient.LogoutAsync(); await discordClient.LogoutAsync().ConfigureAwait(false);
await discordClient.StopAsync(); await discordClient.StopAsync().ConfigureAwait(false);
} }
} }
} }

View File

@@ -23,14 +23,14 @@ namespace MareSynchronosServer.Hubs
public async Task ChangeModeratorStatus(string uid, bool isModerator) public async Task ChangeModeratorStatus(string uid, bool isModerator)
{ {
if (!IsAdmin) return; if (!IsAdmin) return;
var user = await _dbContext.Users.SingleOrDefaultAsync(u => u.UID == uid); var user = await _dbContext.Users.SingleOrDefaultAsync(u => u.UID == uid).ConfigureAwait(false);
if (user == null) return; if (user == null) return;
user.IsModerator = isModerator; user.IsModerator = isModerator;
_dbContext.Update(user); _dbContext.Update(user);
await _dbContext.SaveChangesAsync(); await _dbContext.SaveChangesAsync().ConfigureAwait(false);
await Clients.Users(user.UID).SendAsync(Api.OnAdminForcedReconnect); await Clients.Users(user.UID).SendAsync(Api.OnAdminForcedReconnect).ConfigureAwait(false);
} }
[Authorize(AuthenticationSchemes = SecretKeyAuthenticationHandler.AuthScheme)] [Authorize(AuthenticationSchemes = SecretKeyAuthenticationHandler.AuthScheme)]
@@ -40,15 +40,15 @@ namespace MareSynchronosServer.Hubs
if (!IsModerator || string.IsNullOrEmpty(dto.CharacterHash)) return; if (!IsModerator || string.IsNullOrEmpty(dto.CharacterHash)) return;
var existingUser = var existingUser =
await _dbContext.BannedUsers.SingleOrDefaultAsync(b => b.CharacterIdentification == dto.CharacterHash); await _dbContext.BannedUsers.SingleOrDefaultAsync(b => b.CharacterIdentification == dto.CharacterHash).ConfigureAwait(false);
if (existingUser == null) if (existingUser == null)
{ {
return; return;
} }
_dbContext.Remove(existingUser); _dbContext.Remove(existingUser);
await _dbContext.SaveChangesAsync(); await _dbContext.SaveChangesAsync().ConfigureAwait(false);
await Clients.Users(OnlineAdmins).SendAsync(Api.OnAdminDeleteBannedUser, dto); await Clients.Users(OnlineAdmins).SendAsync(Api.OnAdminDeleteBannedUser, dto).ConfigureAwait(false);
} }
[Authorize(AuthenticationSchemes = SecretKeyAuthenticationHandler.AuthScheme)] [Authorize(AuthenticationSchemes = SecretKeyAuthenticationHandler.AuthScheme)]
@@ -58,15 +58,15 @@ namespace MareSynchronosServer.Hubs
if (!IsAdmin || string.IsNullOrEmpty(dto.Hash)) return; if (!IsAdmin || string.IsNullOrEmpty(dto.Hash)) return;
var existingFile = var existingFile =
await _dbContext.ForbiddenUploadEntries.SingleOrDefaultAsync(b => b.Hash == dto.Hash); await _dbContext.ForbiddenUploadEntries.SingleOrDefaultAsync(b => b.Hash == dto.Hash).ConfigureAwait(false);
if (existingFile == null) if (existingFile == null)
{ {
return; return;
} }
_dbContext.Remove(existingFile); _dbContext.Remove(existingFile);
await _dbContext.SaveChangesAsync(); await _dbContext.SaveChangesAsync().ConfigureAwait(false);
await Clients.Users(OnlineAdmins).SendAsync(Api.OnAdminDeleteForbiddenFile, dto); await Clients.Users(OnlineAdmins).SendAsync(Api.OnAdminDeleteForbiddenFile, dto).ConfigureAwait(false);
} }
[Authorize(AuthenticationSchemes = SecretKeyAuthenticationHandler.AuthScheme)] [Authorize(AuthenticationSchemes = SecretKeyAuthenticationHandler.AuthScheme)]
@@ -79,7 +79,7 @@ namespace MareSynchronosServer.Hubs
{ {
CharacterHash = b.CharacterIdentification, CharacterHash = b.CharacterIdentification,
Reason = b.Reason Reason = b.Reason
}).ToListAsync(); }).ToListAsync().ConfigureAwait(false);
} }
[Authorize(AuthenticationSchemes = SecretKeyAuthenticationHandler.AuthScheme)] [Authorize(AuthenticationSchemes = SecretKeyAuthenticationHandler.AuthScheme)]
@@ -92,7 +92,7 @@ namespace MareSynchronosServer.Hubs
{ {
Hash = b.Hash, Hash = b.Hash,
ForbiddenBy = b.ForbiddenBy ForbiddenBy = b.ForbiddenBy
}).ToListAsync(); }).ToListAsync().ConfigureAwait(false);
} }
[Authorize(AuthenticationSchemes = SecretKeyAuthenticationHandler.AuthScheme)] [Authorize(AuthenticationSchemes = SecretKeyAuthenticationHandler.AuthScheme)]
@@ -107,7 +107,7 @@ namespace MareSynchronosServer.Hubs
UID = b.UID, UID = b.UID,
IsModerator = b.IsModerator, IsModerator = b.IsModerator,
IsAdmin = b.IsAdmin IsAdmin = b.IsAdmin
}).ToListAsync(); }).ToListAsync().ConfigureAwait(false);
} }
[Authorize(AuthenticationSchemes = SecretKeyAuthenticationHandler.AuthScheme)] [Authorize(AuthenticationSchemes = SecretKeyAuthenticationHandler.AuthScheme)]
@@ -117,7 +117,7 @@ namespace MareSynchronosServer.Hubs
if (!IsModerator || string.IsNullOrEmpty(dto.CharacterHash)) return; if (!IsModerator || string.IsNullOrEmpty(dto.CharacterHash)) return;
var existingUser = var existingUser =
await _dbContext.BannedUsers.SingleOrDefaultAsync(b => b.CharacterIdentification == dto.CharacterHash); await _dbContext.BannedUsers.SingleOrDefaultAsync(b => b.CharacterIdentification == dto.CharacterHash).ConfigureAwait(false);
if (existingUser != null) if (existingUser != null)
{ {
existingUser.Reason = dto.Reason; existingUser.Reason = dto.Reason;
@@ -129,16 +129,16 @@ namespace MareSynchronosServer.Hubs
{ {
CharacterIdentification = dto.CharacterHash, CharacterIdentification = dto.CharacterHash,
Reason = dto.Reason Reason = dto.Reason
}); }).ConfigureAwait(false);
} }
await _dbContext.SaveChangesAsync(); await _dbContext.SaveChangesAsync().ConfigureAwait(false);
await Clients.Users(OnlineAdmins).SendAsync(Api.OnAdminUpdateOrAddBannedUser, dto); await Clients.Users(OnlineAdmins).SendAsync(Api.OnAdminUpdateOrAddBannedUser, dto).ConfigureAwait(false);
var bannedUser = var bannedUser =
await _dbContext.Users.SingleOrDefaultAsync(u => u.CharacterIdentification == dto.CharacterHash); await _dbContext.Users.SingleOrDefaultAsync(u => u.CharacterIdentification == dto.CharacterHash).ConfigureAwait(false);
if (bannedUser != null) if (bannedUser != null)
{ {
await Clients.User(bannedUser.UID).SendAsync(Api.OnAdminForcedReconnect); await Clients.User(bannedUser.UID).SendAsync(Api.OnAdminForcedReconnect).ConfigureAwait(false);
} }
} }
@@ -149,7 +149,7 @@ namespace MareSynchronosServer.Hubs
if (!IsAdmin || string.IsNullOrEmpty(dto.Hash)) return; if (!IsAdmin || string.IsNullOrEmpty(dto.Hash)) return;
var existingForbiddenFile = var existingForbiddenFile =
await _dbContext.ForbiddenUploadEntries.SingleOrDefaultAsync(b => b.Hash == dto.Hash); await _dbContext.ForbiddenUploadEntries.SingleOrDefaultAsync(b => b.Hash == dto.Hash).ConfigureAwait(false);
if (existingForbiddenFile != null) if (existingForbiddenFile != null)
{ {
existingForbiddenFile.ForbiddenBy = dto.ForbiddenBy; existingForbiddenFile.ForbiddenBy = dto.ForbiddenBy;
@@ -161,12 +161,12 @@ namespace MareSynchronosServer.Hubs
{ {
Hash = dto.Hash, Hash = dto.Hash,
ForbiddenBy = dto.ForbiddenBy ForbiddenBy = dto.ForbiddenBy
}); }).ConfigureAwait(false);
} }
await _dbContext.SaveChangesAsync(); await _dbContext.SaveChangesAsync().ConfigureAwait(false);
await Clients.Users(OnlineAdmins).SendAsync(Api.OnAdminUpdateOrAddForbiddenFile, dto); await Clients.Users(OnlineAdmins).SendAsync(Api.OnAdminUpdateOrAddForbiddenFile, dto).ConfigureAwait(false);
} }
} }
} }

View File

@@ -25,20 +25,20 @@ namespace MareSynchronosServer.Hubs
[HubMethodName(Api.SendFileAbortUpload)] [HubMethodName(Api.SendFileAbortUpload)]
public async Task AbortUpload() public async Task AbortUpload()
{ {
_logger.LogInformation("User " + AuthenticatedUserId + " aborted upload"); _logger.LogInformation("User {AuthenticatedUserId} aborted upload", AuthenticatedUserId);
var userId = AuthenticatedUserId; var userId = AuthenticatedUserId;
var notUploadedFiles = _dbContext.Files.Where(f => !f.Uploaded && f.Uploader.UID == userId).ToList(); var notUploadedFiles = _dbContext.Files.Where(f => !f.Uploaded && f.Uploader.UID == userId).ToList();
_dbContext.RemoveRange(notUploadedFiles); _dbContext.RemoveRange(notUploadedFiles);
await _dbContext.SaveChangesAsync(); await _dbContext.SaveChangesAsync().ConfigureAwait(false);
} }
[Authorize(AuthenticationSchemes = SecretKeyAuthenticationHandler.AuthScheme)] [Authorize(AuthenticationSchemes = SecretKeyAuthenticationHandler.AuthScheme)]
[HubMethodName(Api.SendFileDeleteAllFiles)] [HubMethodName(Api.SendFileDeleteAllFiles)]
public async Task DeleteAllFiles() public async Task DeleteAllFiles()
{ {
_logger.LogInformation("User " + AuthenticatedUserId + " deleted all their files"); _logger.LogInformation("User {AuthenticatedUserId} deleted all their files", AuthenticatedUserId);
var ownFiles = await _dbContext.Files.Where(f => f.Uploaded && f.Uploader.UID == AuthenticatedUserId).ToListAsync(); var ownFiles = await _dbContext.Files.Where(f => f.Uploaded && f.Uploader.UID == AuthenticatedUserId).ToListAsync().ConfigureAwait(false);
foreach (var file in ownFiles) foreach (var file in ownFiles)
{ {
var fi = new FileInfo(Path.Combine(BasePath, file.Hash)); var fi = new FileInfo(Path.Combine(BasePath, file.Hash));
@@ -50,16 +50,16 @@ namespace MareSynchronosServer.Hubs
} }
} }
_dbContext.Files.RemoveRange(ownFiles); _dbContext.Files.RemoveRange(ownFiles);
await _dbContext.SaveChangesAsync(); await _dbContext.SaveChangesAsync().ConfigureAwait(false);
} }
[Authorize(AuthenticationSchemes = SecretKeyAuthenticationHandler.AuthScheme)] [Authorize(AuthenticationSchemes = SecretKeyAuthenticationHandler.AuthScheme)]
[HubMethodName(Api.InvokeGetFilesSizes)] [HubMethodName(Api.InvokeGetFilesSizes)]
public async Task<List<DownloadFileDto>> GetFilesSizes(List<string> hashes) public async Task<List<DownloadFileDto>> GetFilesSizes(List<string> hashes)
{ {
var allFiles = await _dbContext.Files.Where(f => hashes.Contains(f.Hash)).ToListAsync(); var allFiles = await _dbContext.Files.Where(f => hashes.Contains(f.Hash)).ToListAsync().ConfigureAwait(false);
var forbiddenFiles = await _dbContext.ForbiddenUploadEntries. var forbiddenFiles = await _dbContext.ForbiddenUploadEntries.
Where(f => hashes.Contains(f.Hash)).ToListAsync(); Where(f => hashes.Contains(f.Hash)).ToListAsync().ConfigureAwait(false);
List<DownloadFileDto> response = new(); List<DownloadFileDto> response = new();
foreach (var hash in hashes) foreach (var hash in hashes)
{ {
@@ -90,7 +90,7 @@ namespace MareSynchronosServer.Hubs
if (!fileInfo.Exists && downloadFile != null) if (!fileInfo.Exists && downloadFile != null)
{ {
_dbContext.Files.Remove(downloadFile); _dbContext.Files.Remove(downloadFile);
await _dbContext.SaveChangesAsync(); await _dbContext.SaveChangesAsync().ConfigureAwait(false);
} }
} }
@@ -103,7 +103,7 @@ namespace MareSynchronosServer.Hubs
{ {
var userUid = AuthenticatedUserId; var userUid = AuthenticatedUserId;
return await _dbContext.Files.AsNoTracking() return await _dbContext.Files.AsNoTracking()
.AnyAsync(f => f.Uploader.UID == userUid && !f.Uploaded); .AnyAsync(f => f.Uploader.UID == userUid && !f.Uploaded).ConfigureAwait(false);
} }
[Authorize(AuthenticationSchemes = SecretKeyAuthenticationHandler.AuthScheme)] [Authorize(AuthenticationSchemes = SecretKeyAuthenticationHandler.AuthScheme)]
@@ -111,12 +111,11 @@ namespace MareSynchronosServer.Hubs
public async Task<List<UploadFileDto>> SendFiles(List<string> fileListHashes) public async Task<List<UploadFileDto>> SendFiles(List<string> fileListHashes)
{ {
var userSentHashes = new HashSet<string>(fileListHashes.Distinct()); var userSentHashes = new HashSet<string>(fileListHashes.Distinct());
_logger.LogInformation($"User {AuthenticatedUserId} sending files: {userSentHashes.Count}"); _logger.LogInformation("User {AuthenticatedUserId} sending files: {count}", AuthenticatedUserId, userSentHashes.Count);
var notCoveredFiles = new Dictionary<string, UploadFileDto>(); var notCoveredFiles = new Dictionary<string, UploadFileDto>();
// Todo: Check if a select can directly transform to hashset var forbiddenFiles = await _dbContext.ForbiddenUploadEntries.AsNoTracking().Where(f => userSentHashes.Contains(f.Hash)).ToDictionaryAsync(f => f.Hash, f => f).ConfigureAwait(false);
var forbiddenFiles = await _dbContext.ForbiddenUploadEntries.AsNoTracking().Where(f => userSentHashes.Contains(f.Hash)).ToDictionaryAsync(f => f.Hash, f => f); var existingFiles = await _dbContext.Files.AsNoTracking().Where(f => userSentHashes.Contains(f.Hash)).ToDictionaryAsync(f => f.Hash, f => f).ConfigureAwait(false);
var existingFiles = await _dbContext.Files.AsNoTracking().Where(f => userSentHashes.Contains(f.Hash)).ToDictionaryAsync(f => f.Hash, f => f); var uploader = await _dbContext.Users.SingleAsync(u => u.UID == AuthenticatedUserId).ConfigureAwait(false);
var uploader = await _dbContext.Users.SingleAsync(u => u.UID == AuthenticatedUserId);
List<FileCache> fileCachesToUpload = new(); List<FileCache> fileCachesToUpload = new();
foreach (var file in userSentHashes) foreach (var file in userSentHashes)
@@ -137,7 +136,7 @@ namespace MareSynchronosServer.Hubs
} }
if (existingFiles.ContainsKey(file)) { continue; } if (existingFiles.ContainsKey(file)) { continue; }
_logger.LogInformation("User " + AuthenticatedUserId + " needs upload: " + file); _logger.LogInformation("User {AuthenticatedUserId} needs upload: {file}", AuthenticatedUserId, file);
var userId = AuthenticatedUserId; var userId = AuthenticatedUserId;
fileCachesToUpload.Add(new FileCache() fileCachesToUpload.Add(new FileCache()
{ {
@@ -152,8 +151,8 @@ namespace MareSynchronosServer.Hubs
}; };
} }
//Save bulk //Save bulk
await _dbContext.Files.AddRangeAsync(fileCachesToUpload); await _dbContext.Files.AddRangeAsync(fileCachesToUpload).ConfigureAwait(false);
await _dbContext.SaveChangesAsync(); await _dbContext.SaveChangesAsync().ConfigureAwait(false);
return notCoveredFiles.Values.ToList(); return notCoveredFiles.Values.ToList();
} }
@@ -161,7 +160,7 @@ namespace MareSynchronosServer.Hubs
[HubMethodName(Api.SendFileUploadFileStreamAsync)] [HubMethodName(Api.SendFileUploadFileStreamAsync)]
public async Task UploadFileStreamAsync(string hash, IAsyncEnumerable<byte[]> fileContent) public async Task UploadFileStreamAsync(string hash, IAsyncEnumerable<byte[]> fileContent)
{ {
_logger.LogInformation("User " + AuthenticatedUserId + " uploading file: " + hash); _logger.LogInformation("User {AuthenticatedUserId} uploading file: {hash}", AuthenticatedUserId, hash);
var relatedFile = _dbContext.Files.SingleOrDefault(f => f.Hash == hash && f.Uploader.UID == AuthenticatedUserId && f.Uploaded == false); var relatedFile = _dbContext.Files.SingleOrDefault(f => f.Hash == hash && f.Uploader.UID == AuthenticatedUserId && f.Uploaded == false);
if (relatedFile == null) return; if (relatedFile == null) return;
@@ -173,23 +172,23 @@ namespace MareSynchronosServer.Hubs
long length = 0; long length = 0;
try try
{ {
await foreach (var chunk in fileContent) await foreach (var chunk in fileContent.ConfigureAwait(false))
{ {
length += chunk.Length; length += chunk.Length;
await fileStream.WriteAsync(chunk); await fileStream.WriteAsync(chunk).ConfigureAwait(false);
} }
await fileStream.FlushAsync(); await fileStream.FlushAsync().ConfigureAwait(false);
await fileStream.DisposeAsync(); await fileStream.DisposeAsync().ConfigureAwait(false);
} }
catch catch
{ {
try try
{ {
await fileStream.FlushAsync(); await fileStream.FlushAsync().ConfigureAwait(false);
await fileStream.DisposeAsync(); await fileStream.DisposeAsync().ConfigureAwait(false);
_dbContext.Files.Remove(relatedFile); _dbContext.Files.Remove(relatedFile);
await _dbContext.SaveChangesAsync(); await _dbContext.SaveChangesAsync().ConfigureAwait(false);
} }
catch catch
{ {
@@ -203,20 +202,20 @@ namespace MareSynchronosServer.Hubs
return; return;
} }
_logger.LogInformation("User " + AuthenticatedUserId + " upload finished: " + hash + ", size: " + length); _logger.LogInformation("User {AuthenticatedUserId} upload finished: {hash}, size: {length}", AuthenticatedUserId, hash, length);
try try
{ {
var decodedFile = LZ4.LZ4Codec.Unwrap(await File.ReadAllBytesAsync(tempFileName)); var decodedFile = LZ4.LZ4Codec.Unwrap(await File.ReadAllBytesAsync(tempFileName).ConfigureAwait(false));
using var sha1 = SHA1.Create(); using var sha1 = SHA1.Create();
using var ms = new MemoryStream(decodedFile); using var ms = new MemoryStream(decodedFile);
var computedHash = await sha1.ComputeHashAsync(ms); var computedHash = await sha1.ComputeHashAsync(ms).ConfigureAwait(false);
var computedHashString = BitConverter.ToString(computedHash).Replace("-", ""); var computedHashString = BitConverter.ToString(computedHash).Replace("-", "");
if (hash != computedHashString) if (hash != computedHashString)
{ {
_logger.LogWarning($"Computed file hash was not expected file hash. Computed: {computedHashString}, Expected {hash}"); _logger.LogWarning("Computed file hash was not expected file hash. Computed: {computedHashString}, Expected {hash}", computedHashString, hash);
_dbContext.Remove(relatedFile); _dbContext.Remove(relatedFile);
await _dbContext.SaveChangesAsync(); await _dbContext.SaveChangesAsync().ConfigureAwait(false);
return; return;
} }
@@ -228,14 +227,14 @@ namespace MareSynchronosServer.Hubs
MareMetrics.FilesTotal.Inc(); MareMetrics.FilesTotal.Inc();
MareMetrics.FilesTotalSize.Inc(length); MareMetrics.FilesTotalSize.Inc(length);
await _dbContext.SaveChangesAsync(); await _dbContext.SaveChangesAsync().ConfigureAwait(false);
_logger.LogInformation("File " + hash + " added to DB"); _logger.LogInformation("File {hash} added to DB", hash);
} }
catch (Exception ex) catch (Exception ex)
{ {
_logger.LogWarning(ex, "Upload failed"); _logger.LogWarning(ex, "Upload failed");
_dbContext.Remove(relatedFile); _dbContext.Remove(relatedFile);
await _dbContext.SaveChangesAsync(); await _dbContext.SaveChangesAsync().ConfigureAwait(false);
} }
} }
} }

View File

@@ -18,14 +18,14 @@ namespace MareSynchronosServer.Hubs
[HubMethodName(Api.SendUserDeleteAccount)] [HubMethodName(Api.SendUserDeleteAccount)]
public async Task DeleteAccount() public async Task DeleteAccount()
{ {
_logger.LogInformation("User " + AuthenticatedUserId + " deleted their account"); _logger.LogInformation("User {AuthenticatedUserId} deleted their account", AuthenticatedUserId);
string userid = AuthenticatedUserId; string userid = AuthenticatedUserId;
var userEntry = await _dbContext.Users.SingleAsync(u => u.UID == userid); var userEntry = await _dbContext.Users.SingleAsync(u => u.UID == userid).ConfigureAwait(false);
var ownPairData = await _dbContext.ClientPairs.Where(u => u.User.UID == userid).ToListAsync(); var ownPairData = await _dbContext.ClientPairs.Where(u => u.User.UID == userid).ToListAsync().ConfigureAwait(false);
var auth = await _dbContext.Auth.SingleAsync(u => u.UserUID == userid); var auth = await _dbContext.Auth.SingleAsync(u => u.UserUID == userid).ConfigureAwait(false);
var lodestone = await _dbContext.LodeStoneAuth.SingleOrDefaultAsync(a => a.User.UID == userid); var lodestone = await _dbContext.LodeStoneAuth.SingleOrDefaultAsync(a => a.User.UID == userid).ConfigureAwait(false);
if (lodestone != null) if (lodestone != null)
{ {
@@ -34,7 +34,7 @@ namespace MareSynchronosServer.Hubs
while (_dbContext.Files.Any(f => f.Uploader == userEntry)) while (_dbContext.Files.Any(f => f.Uploader == userEntry))
{ {
await Task.Delay(1000); await Task.Delay(1000).ConfigureAwait(false);
} }
SecretKeyAuthenticationHandler.RemoveAuthentication(userid); SecretKeyAuthenticationHandler.RemoveAuthentication(userid);
@@ -43,9 +43,9 @@ namespace MareSynchronosServer.Hubs
MareMetrics.PairsPaused.Dec(ownPairData.Count(c => c.IsPaused)); MareMetrics.PairsPaused.Dec(ownPairData.Count(c => c.IsPaused));
_dbContext.RemoveRange(ownPairData); _dbContext.RemoveRange(ownPairData);
await _dbContext.SaveChangesAsync(); await _dbContext.SaveChangesAsync().ConfigureAwait(false);
var otherPairData = await _dbContext.ClientPairs.Include(u => u.User) var otherPairData = await _dbContext.ClientPairs.Include(u => u.User)
.Where(u => u.OtherUser.UID == userid).ToListAsync(); .Where(u => u.OtherUser.UID == userid).ToListAsync().ConfigureAwait(false);
foreach (var pair in otherPairData) foreach (var pair in otherPairData)
{ {
await Clients.User(pair.User.UID) await Clients.User(pair.User.UID)
@@ -53,38 +53,38 @@ namespace MareSynchronosServer.Hubs
{ {
OtherUID = userid, OtherUID = userid,
IsRemoved = true IsRemoved = true
}, userEntry.CharacterIdentification); }, userEntry.CharacterIdentification).ConfigureAwait(false);
} }
MareMetrics.Pairs.Dec(otherPairData.Count()); MareMetrics.Pairs.Dec(otherPairData.Count);
MareMetrics.PairsPaused.Dec(otherPairData.Count(c => c.IsPaused)); MareMetrics.PairsPaused.Dec(otherPairData.Count(c => c.IsPaused));
MareMetrics.UsersRegistered.Dec(); MareMetrics.UsersRegistered.Dec();
_dbContext.RemoveRange(otherPairData); _dbContext.RemoveRange(otherPairData);
_dbContext.Remove(userEntry); _dbContext.Remove(userEntry);
_dbContext.Remove(auth); _dbContext.Remove(auth);
await _dbContext.SaveChangesAsync(); await _dbContext.SaveChangesAsync().ConfigureAwait(false);
} }
[Authorize(AuthenticationSchemes = SecretKeyAuthenticationHandler.AuthScheme)] [Authorize(AuthenticationSchemes = SecretKeyAuthenticationHandler.AuthScheme)]
[HubMethodName(Api.InvokeUserGetOnlineCharacters)] [HubMethodName(Api.InvokeUserGetOnlineCharacters)]
public async Task<List<string>> GetOnlineCharacters() public async Task<List<string>> GetOnlineCharacters()
{ {
_logger.LogInformation("User " + AuthenticatedUserId + " requested online characters"); _logger.LogInformation("User {AuthenticatedUserId} requested online characters", AuthenticatedUserId);
var ownUser = await GetAuthenticatedUserUntrackedAsync(); var ownUser = await GetAuthenticatedUserUntrackedAsync().ConfigureAwait(false);
var otherUsers = await _dbContext.ClientPairs.AsNoTracking() var otherUsers = await _dbContext.ClientPairs.AsNoTracking()
.Include(u => u.User) .Include(u => u.User)
.Include(u => u.OtherUser) .Include(u => u.OtherUser)
.Where(w => w.User.UID == ownUser.UID && !w.IsPaused) .Where(w => w.User.UID == ownUser.UID && !w.IsPaused)
.Where(w => !string.IsNullOrEmpty(w.OtherUser.CharacterIdentification)) .Where(w => !string.IsNullOrEmpty(w.OtherUser.CharacterIdentification))
.Select(e => e.OtherUser).ToListAsync(); .Select(e => e.OtherUser).ToListAsync().ConfigureAwait(false);
var otherEntries = await _dbContext.ClientPairs.AsNoTracking() var otherEntries = await _dbContext.ClientPairs.AsNoTracking()
.Include(u => u.User) .Include(u => u.User)
.Where(u => otherUsers.Any(e => e == u.User) && u.OtherUser == ownUser && !u.IsPaused).ToListAsync(); .Where(u => otherUsers.Any(e => e == u.User) && u.OtherUser == ownUser && !u.IsPaused).ToListAsync().ConfigureAwait(false);
await Clients.Users(otherEntries.Select(e => e.User.UID)).SendAsync(Api.OnUserAddOnlinePairedPlayer, ownUser.CharacterIdentification); await Clients.Users(otherEntries.Select(e => e.User.UID)).SendAsync(Api.OnUserAddOnlinePairedPlayer, ownUser.CharacterIdentification).ConfigureAwait(false);
return otherEntries.Select(e => e.User.CharacterIdentification).Distinct().ToList(); return otherEntries.Select(e => e.User.CharacterIdentification).Distinct().ToList();
} }
@@ -117,7 +117,7 @@ namespace MareSynchronosServer.Hubs
IsSynced = otherEntry != null IsSynced = otherEntry != null
}; };
return (await query.ToListAsync()).Select(f => new ClientPairDto() return (await query.ToListAsync().ConfigureAwait(false)).Select(f => new ClientPairDto()
{ {
IsPaused = f.IsPaused, IsPaused = f.IsPaused,
OtherUID = f.OtherUserUID, OtherUID = f.OtherUserUID,
@@ -130,9 +130,9 @@ namespace MareSynchronosServer.Hubs
[HubMethodName(Api.InvokeUserPushCharacterDataToVisibleClients)] [HubMethodName(Api.InvokeUserPushCharacterDataToVisibleClients)]
public async Task PushCharacterDataToVisibleClients(CharacterCacheDto characterCache, List<string> visibleCharacterIds) public async Task PushCharacterDataToVisibleClients(CharacterCacheDto characterCache, List<string> visibleCharacterIds)
{ {
_logger.LogInformation("User " + AuthenticatedUserId + " pushing character data to " + visibleCharacterIds.Count + " visible clients"); _logger.LogInformation("User {AuthenticatedUserId} pushing character data to {visibleCharacterIds} visible clients", AuthenticatedUserId, visibleCharacterIds.Count);
var user = await GetAuthenticatedUserUntrackedAsync(); var user = await GetAuthenticatedUserUntrackedAsync().ConfigureAwait(false);
var query = var query =
from userToOther in _dbContext.ClientPairs from userToOther in _dbContext.ClientPairs
@@ -154,9 +154,9 @@ namespace MareSynchronosServer.Hubs
&& visibleCharacterIds.Contains(userToOther.OtherUser.CharacterIdentification) && visibleCharacterIds.Contains(userToOther.OtherUser.CharacterIdentification)
select otherToUser.UserUID; select otherToUser.UserUID;
var otherEntries = await query.ToListAsync(); var otherEntries = await query.ToListAsync().ConfigureAwait(false);
await Clients.Users(otherEntries).SendAsync(Api.OnUserReceiveCharacterData, characterCache, user.CharacterIdentification); await Clients.Users(otherEntries).SendAsync(Api.OnUserReceiveCharacterData, characterCache, user.CharacterIdentification).ConfigureAwait(false);
MareMetrics.UserPushData.Inc(); MareMetrics.UserPushData.Inc();
MareMetrics.UserPushDataTo.Inc(otherEntries.Count); MareMetrics.UserPushDataTo.Inc(otherEntries.Count);
@@ -168,24 +168,24 @@ namespace MareSynchronosServer.Hubs
{ {
if (uid == AuthenticatedUserId) return; if (uid == AuthenticatedUserId) return;
uid = uid.Trim(); uid = uid.Trim();
var user = await _dbContext.Users.SingleAsync(u => u.UID == AuthenticatedUserId); var user = await _dbContext.Users.SingleAsync(u => u.UID == AuthenticatedUserId).ConfigureAwait(false);
var otherUser = await _dbContext.Users var otherUser = await _dbContext.Users
.SingleOrDefaultAsync(u => u.UID == uid); .SingleOrDefaultAsync(u => u.UID == uid).ConfigureAwait(false);
var existingEntry = var existingEntry =
await _dbContext.ClientPairs.AsNoTracking() await _dbContext.ClientPairs.AsNoTracking()
.FirstOrDefaultAsync(p => .FirstOrDefaultAsync(p =>
p.User.UID == AuthenticatedUserId && p.OtherUser.UID == uid); p.User.UID == AuthenticatedUserId && p.OtherUser.UID == uid).ConfigureAwait(false);
if (otherUser == null || existingEntry != null) return; if (otherUser == null || existingEntry != null) return;
_logger.LogInformation("User " + AuthenticatedUserId + " adding " + uid + " to whitelist"); _logger.LogInformation("User {AuthenticatedUserId} adding {uid} to whitelist", AuthenticatedUserId, uid);
ClientPair wl = new ClientPair() ClientPair wl = new ClientPair()
{ {
IsPaused = false, IsPaused = false,
OtherUser = otherUser, OtherUser = otherUser,
User = user User = user
}; };
await _dbContext.ClientPairs.AddAsync(wl); await _dbContext.ClientPairs.AddAsync(wl).ConfigureAwait(false);
await _dbContext.SaveChangesAsync(); await _dbContext.SaveChangesAsync().ConfigureAwait(false);
var otherEntry = OppositeEntry(uid); var otherEntry = OppositeEntry(uid);
await Clients.User(user.UID) await Clients.User(user.UID)
.SendAsync(Api.OnUserUpdateClientPairs, new ClientPairDto() .SendAsync(Api.OnUserUpdateClientPairs, new ClientPairDto()
@@ -194,7 +194,7 @@ namespace MareSynchronosServer.Hubs
IsPaused = false, IsPaused = false,
IsPausedFromOthers = otherEntry?.IsPaused ?? false, IsPausedFromOthers = otherEntry?.IsPaused ?? false,
IsSynced = otherEntry != null IsSynced = otherEntry != null
}, string.Empty); }, string.Empty).ConfigureAwait(false);
if (otherEntry != null) if (otherEntry != null)
{ {
await Clients.User(uid).SendAsync(Api.OnUserUpdateClientPairs, await Clients.User(uid).SendAsync(Api.OnUserUpdateClientPairs,
@@ -204,14 +204,14 @@ namespace MareSynchronosServer.Hubs
IsPaused = otherEntry.IsPaused, IsPaused = otherEntry.IsPaused,
IsPausedFromOthers = false, IsPausedFromOthers = false,
IsSynced = true IsSynced = true
}, user.CharacterIdentification); }, user.CharacterIdentification).ConfigureAwait(false);
if (!string.IsNullOrEmpty(otherUser.CharacterIdentification)) if (!string.IsNullOrEmpty(otherUser.CharacterIdentification))
{ {
await Clients.User(user.UID) await Clients.User(user.UID)
.SendAsync(Api.OnUserAddOnlinePairedPlayer, otherUser.CharacterIdentification); .SendAsync(Api.OnUserAddOnlinePairedPlayer, otherUser.CharacterIdentification).ConfigureAwait(false);
await Clients.User(otherUser.UID) await Clients.User(otherUser.UID)
.SendAsync(Api.OnUserAddOnlinePairedPlayer, user.CharacterIdentification); .SendAsync(Api.OnUserAddOnlinePairedPlayer, user.CharacterIdentification).ConfigureAwait(false);
} }
} }
@@ -223,15 +223,15 @@ namespace MareSynchronosServer.Hubs
public async Task SendPairedClientPauseChange(string otherUserUid, bool isPaused) public async Task SendPairedClientPauseChange(string otherUserUid, bool isPaused)
{ {
if (otherUserUid == AuthenticatedUserId) return; if (otherUserUid == AuthenticatedUserId) return;
ClientPair pair = await _dbContext.ClientPairs.SingleOrDefaultAsync(w => w.UserUID == AuthenticatedUserId && w.OtherUserUID == otherUserUid); ClientPair pair = await _dbContext.ClientPairs.SingleOrDefaultAsync(w => w.UserUID == AuthenticatedUserId && w.OtherUserUID == otherUserUid).ConfigureAwait(false);
if (pair == null) return; if (pair == null) return;
_logger.LogInformation("User " + AuthenticatedUserId + " changed pause status with " + otherUserUid + " to " + isPaused); _logger.LogInformation("User {AuthenticatedUserId} changed pause status with {otherUserUid} to {isPaused}", AuthenticatedUserId, otherUserUid, isPaused);
pair.IsPaused = isPaused; pair.IsPaused = isPaused;
_dbContext.Update(pair); _dbContext.Update(pair);
await _dbContext.SaveChangesAsync(); await _dbContext.SaveChangesAsync().ConfigureAwait(false);
var selfCharaIdent = (await _dbContext.Users.SingleAsync(u => u.UID == AuthenticatedUserId)).CharacterIdentification; var selfCharaIdent = (await _dbContext.Users.SingleAsync(u => u.UID == AuthenticatedUserId).ConfigureAwait(false)).CharacterIdentification;
var otherCharaIdent = (await _dbContext.Users.SingleAsync(u => u.UID == otherUserUid)).CharacterIdentification; var otherCharaIdent = (await _dbContext.Users.SingleAsync(u => u.UID == otherUserUid).ConfigureAwait(false)).CharacterIdentification;
var otherEntry = OppositeEntry(otherUserUid); var otherEntry = OppositeEntry(otherUserUid);
await Clients.User(AuthenticatedUserId) await Clients.User(AuthenticatedUserId)
@@ -241,7 +241,7 @@ namespace MareSynchronosServer.Hubs
IsPaused = isPaused, IsPaused = isPaused,
IsPausedFromOthers = otherEntry?.IsPaused ?? false, IsPausedFromOthers = otherEntry?.IsPaused ?? false,
IsSynced = otherEntry != null IsSynced = otherEntry != null
}, otherCharaIdent); }, otherCharaIdent).ConfigureAwait(false);
if (otherEntry != null) if (otherEntry != null)
{ {
await Clients.User(otherUserUid).SendAsync(Api.OnUserUpdateClientPairs, new ClientPairDto() await Clients.User(otherUserUid).SendAsync(Api.OnUserUpdateClientPairs, new ClientPairDto()
@@ -250,7 +250,7 @@ namespace MareSynchronosServer.Hubs
IsPaused = otherEntry.IsPaused, IsPaused = otherEntry.IsPaused,
IsPausedFromOthers = isPaused, IsPausedFromOthers = isPaused,
IsSynced = true IsSynced = true
}, selfCharaIdent); }, selfCharaIdent).ConfigureAwait(false);
} }
if (isPaused) if (isPaused)
@@ -269,37 +269,37 @@ namespace MareSynchronosServer.Hubs
{ {
if (uid == AuthenticatedUserId) return; if (uid == AuthenticatedUserId) return;
var sender = await _dbContext.Users.SingleAsync(u => u.UID == AuthenticatedUserId); var sender = await _dbContext.Users.SingleAsync(u => u.UID == AuthenticatedUserId).ConfigureAwait(false);
var otherUser = await _dbContext.Users.SingleOrDefaultAsync(u => u.UID == uid); var otherUser = await _dbContext.Users.SingleOrDefaultAsync(u => u.UID == uid).ConfigureAwait(false);
if (otherUser == null) return; if (otherUser == null) return;
_logger.LogInformation("User " + AuthenticatedUserId + " removed " + uid + " from whitelist"); _logger.LogInformation("User {AuthenticatedUserId} removed {uid} from whitelist", AuthenticatedUserId, uid);
ClientPair wl = ClientPair wl =
await _dbContext.ClientPairs.SingleOrDefaultAsync(w => w.User == sender && w.OtherUser == otherUser); await _dbContext.ClientPairs.SingleOrDefaultAsync(w => w.User == sender && w.OtherUser == otherUser).ConfigureAwait(false);
if (wl == null) return; if (wl == null) return;
_dbContext.ClientPairs.Remove(wl); _dbContext.ClientPairs.Remove(wl);
await _dbContext.SaveChangesAsync(); await _dbContext.SaveChangesAsync().ConfigureAwait(false);
var otherEntry = OppositeEntry(uid); var otherEntry = OppositeEntry(uid);
await Clients.User(sender.UID) await Clients.User(sender.UID)
.SendAsync(Api.OnUserUpdateClientPairs, new ClientPairDto() .SendAsync(Api.OnUserUpdateClientPairs, new ClientPairDto()
{ {
OtherUID = otherUser.UID, OtherUID = otherUser.UID,
IsRemoved = true IsRemoved = true
}, otherUser.CharacterIdentification); }, otherUser.CharacterIdentification).ConfigureAwait(false);
if (otherEntry != null) if (otherEntry != null)
{ {
if (!string.IsNullOrEmpty(otherUser.CharacterIdentification)) if (!string.IsNullOrEmpty(otherUser.CharacterIdentification))
{ {
await Clients.User(sender.UID) await Clients.User(sender.UID)
.SendAsync(Api.OnUserRemoveOnlinePairedPlayer, otherUser.CharacterIdentification); .SendAsync(Api.OnUserRemoveOnlinePairedPlayer, otherUser.CharacterIdentification).ConfigureAwait(false);
await Clients.User(otherUser.UID) await Clients.User(otherUser.UID)
.SendAsync(Api.OnUserRemoveOnlinePairedPlayer, sender.CharacterIdentification); .SendAsync(Api.OnUserRemoveOnlinePairedPlayer, sender.CharacterIdentification).ConfigureAwait(false);
await Clients.User(otherUser.UID).SendAsync(Api.OnUserUpdateClientPairs, new ClientPairDto() await Clients.User(otherUser.UID).SendAsync(Api.OnUserUpdateClientPairs, new ClientPairDto()
{ {
OtherUID = sender.UID, OtherUID = sender.UID,
IsPaused = otherEntry.IsPaused, IsPaused = otherEntry.IsPaused,
IsPausedFromOthers = false, IsPausedFromOthers = false,
IsSynced = false IsSynced = false
}, sender.CharacterIdentification); }, sender.CharacterIdentification).ConfigureAwait(false);
} }
} }

View File

@@ -41,15 +41,15 @@ namespace MareSynchronosServer.Hubs
var userId = Context.User!.Claims.SingleOrDefault(c => c.Type == ClaimTypes.NameIdentifier)?.Value; var userId = Context.User!.Claims.SingleOrDefault(c => c.Type == ClaimTypes.NameIdentifier)?.Value;
_logger.LogInformation("Connection from " + userId + ", CI: " + characterIdentification); _logger.LogInformation("Connection from {userId}, CI: {characterIdentification}", userId, characterIdentification);
await Clients.Caller.SendAsync(Api.OnUpdateSystemInfo, _systemInfoService.SystemInfoDto); await Clients.Caller.SendAsync(Api.OnUpdateSystemInfo, _systemInfoService.SystemInfoDto).ConfigureAwait(false);
var isBanned = await _dbContext.BannedUsers.AsNoTracking().AnyAsync(u => u.CharacterIdentification == characterIdentification); var isBanned = await _dbContext.BannedUsers.AsNoTracking().AnyAsync(u => u.CharacterIdentification == characterIdentification).ConfigureAwait(false);
if (!string.IsNullOrEmpty(userId) && !isBanned && !string.IsNullOrEmpty(characterIdentification)) if (!string.IsNullOrEmpty(userId) && !isBanned && !string.IsNullOrEmpty(characterIdentification))
{ {
var user = (await _dbContext.Users.SingleAsync(u => u.UID == userId)); var user = (await _dbContext.Users.SingleAsync(u => u.UID == userId).ConfigureAwait(false));
if (!string.IsNullOrEmpty(user.CharacterIdentification) && characterIdentification != user.CharacterIdentification) if (!string.IsNullOrEmpty(user.CharacterIdentification) && characterIdentification != user.CharacterIdentification)
{ {
return new ConnectionDto() return new ConnectionDto()
@@ -64,7 +64,7 @@ namespace MareSynchronosServer.Hubs
user.LastLoggedIn = DateTime.UtcNow; user.LastLoggedIn = DateTime.UtcNow;
user.CharacterIdentification = characterIdentification; user.CharacterIdentification = characterIdentification;
await _dbContext.SaveChangesAsync(); await _dbContext.SaveChangesAsync().ConfigureAwait(false);
return new ConnectionDto return new ConnectionDto
{ {
ServerVersion = Api.Version, ServerVersion = Api.Version,
@@ -82,7 +82,7 @@ namespace MareSynchronosServer.Hubs
public override Task OnConnectedAsync() public override Task OnConnectedAsync()
{ {
_logger.LogInformation("Connection from " + contextAccessor.GetIpAddress()); _logger.LogInformation("Connection from {ip}", contextAccessor.GetIpAddress());
MareMetrics.Connections.Inc(); MareMetrics.Connections.Inc();
return base.OnConnectedAsync(); return base.OnConnectedAsync();
} }
@@ -91,12 +91,12 @@ namespace MareSynchronosServer.Hubs
{ {
MareMetrics.Connections.Dec(); MareMetrics.Connections.Dec();
var user = await _dbContext.Users.SingleOrDefaultAsync(u => u.UID == AuthenticatedUserId); var user = await _dbContext.Users.SingleOrDefaultAsync(u => u.UID == AuthenticatedUserId).ConfigureAwait(false);
if (user != null && !string.IsNullOrEmpty(user.CharacterIdentification)) if (user != null && !string.IsNullOrEmpty(user.CharacterIdentification))
{ {
MareMetrics.AuthorizedConnections.Dec(); MareMetrics.AuthorizedConnections.Dec();
_logger.LogInformation("Disconnect from " + AuthenticatedUserId); _logger.LogInformation("Disconnect from {id}", AuthenticatedUserId);
var query = var query =
from userToOther in _dbContext.ClientPairs from userToOther in _dbContext.ClientPairs
@@ -114,17 +114,17 @@ namespace MareSynchronosServer.Hubs
&& !userToOther.IsPaused && !userToOther.IsPaused
&& !otherToUser.IsPaused && !otherToUser.IsPaused
select otherToUser.UserUID; select otherToUser.UserUID;
var otherEntries = await query.ToListAsync(); var otherEntries = await query.ToListAsync().ConfigureAwait(false);
await Clients.Users(otherEntries).SendAsync(Api.OnUserRemoveOnlinePairedPlayer, user.CharacterIdentification); await Clients.Users(otherEntries).SendAsync(Api.OnUserRemoveOnlinePairedPlayer, user.CharacterIdentification).ConfigureAwait(false);
_dbContext.RemoveRange(_dbContext.Files.Where(f => !f.Uploaded && f.UploaderUID == user.UID)); _dbContext.RemoveRange(_dbContext.Files.Where(f => !f.Uploaded && f.UploaderUID == user.UID));
user.CharacterIdentification = null; user.CharacterIdentification = null;
await _dbContext.SaveChangesAsync(); await _dbContext.SaveChangesAsync().ConfigureAwait(false);
} }
await base.OnDisconnectedAsync(exception); await base.OnDisconnectedAsync(exception).ConfigureAwait(false);
} }
public static string GenerateRandomString(int length, string allowableChars = null) public static string GenerateRandomString(int length, string allowableChars = null)
@@ -149,7 +149,7 @@ namespace MareSynchronosServer.Hubs
protected async Task<Models.User> GetAuthenticatedUserUntrackedAsync() protected async Task<Models.User> GetAuthenticatedUserUntrackedAsync()
{ {
return await _dbContext.Users.AsNoTrackingWithIdentityResolution().SingleAsync(u => u.UID == AuthenticatedUserId); return await _dbContext.Users.AsNoTrackingWithIdentityResolution().SingleAsync(u => u.UID == AuthenticatedUserId).ConfigureAwait(false);
} }
} }
} }

View File

@@ -14,6 +14,10 @@
<PackageReference Include="EFCore.NamingConventions" Version="6.0.0" /> <PackageReference Include="EFCore.NamingConventions" Version="6.0.0" />
<PackageReference Include="Karambolo.Extensions.Logging.File" Version="3.3.1" /> <PackageReference Include="Karambolo.Extensions.Logging.File" Version="3.3.1" />
<PackageReference Include="lz4net" Version="1.0.15.93" /> <PackageReference Include="lz4net" Version="1.0.15.93" />
<PackageReference Include="Meziantou.Analyzer" Version="1.0.715">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="6.0.8" /> <PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="6.0.8" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="6.0.8" /> <PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="6.0.8" />
<PackageReference Include="Microsoft.AspNetCore.Identity.UI" Version="6.0.8" /> <PackageReference Include="Microsoft.AspNetCore.Identity.UI" Version="6.0.8" />

View File

@@ -40,7 +40,7 @@ public class SystemInfoService : IHostedService, IDisposable
private void PushSystemInfo(object state) private void PushSystemInfo(object state)
{ {
ThreadPool.GetAvailableThreads(out int workerThreads, out int ioThreads); ThreadPool.GetAvailableThreads(out int workerThreads, out int ioThreads);
_logger.LogInformation($"ThreadPool: {workerThreads} workers available, {ioThreads} IO workers available"); _logger.LogInformation("ThreadPool: {workerThreads} workers available, {ioThreads} IO workers available", workerThreads, ioThreads);
MareMetrics.AvailableWorkerThreads.Set(workerThreads); MareMetrics.AvailableWorkerThreads.Set(workerThreads);
MareMetrics.AvailableIOWorkerThreads.Set(ioThreads); MareMetrics.AvailableIOWorkerThreads.Set(ioThreads);

View File

@@ -37,25 +37,25 @@ public class SignalRLimitFilter : IHubFilter
HttpVerb = "ws", HttpVerb = "ws",
ClientId = invocationContext.Context.UserIdentifier ClientId = invocationContext.Context.UserIdentifier
}; };
foreach (var rule in await _processor.GetMatchingRulesAsync(client)) foreach (var rule in await _processor.GetMatchingRulesAsync(client).ConfigureAwait(false))
{ {
var counter = await _processor.ProcessRequestAsync(client, rule); var counter = await _processor.ProcessRequestAsync(client, rule).ConfigureAwait(false);
if (counter.Count > rule.Limit) if (counter.Count > rule.Limit)
{ {
var authUserId = invocationContext.Context.User.Claims?.SingleOrDefault(c => c.Type == ClaimTypes.NameIdentifier)?.Value ?? "Unknown"; var authUserId = invocationContext.Context.User.Claims?.SingleOrDefault(c => c.Type == ClaimTypes.NameIdentifier)?.Value ?? "Unknown";
var retry = counter.Timestamp.RetryAfterFrom(rule); var retry = counter.Timestamp.RetryAfterFrom(rule);
logger.LogWarning($"Method rate limit triggered from {ip}/{authUserId}: {invocationContext.HubMethodName}"); logger.LogWarning("Method rate limit triggered from {ip}/{authUserId}: {method}", ip, authUserId, invocationContext.HubMethodName);
throw new HubException($"call limit {retry}"); throw new HubException($"call limit {retry}");
} }
} }
return await next(invocationContext); return await next(invocationContext).ConfigureAwait(false);
} }
// Optional method // Optional method
public async Task OnConnectedAsync(HubLifetimeContext context, Func<HubLifetimeContext, Task> next) public async Task OnConnectedAsync(HubLifetimeContext context, Func<HubLifetimeContext, Task> next)
{ {
await ConnectionLimiterSemaphore.WaitAsync(); await ConnectionLimiterSemaphore.WaitAsync().ConfigureAwait(false);
var ip = accessor.GetIpAddress(); var ip = accessor.GetIpAddress();
var client = new ClientRequestIdentity var client = new ClientRequestIdentity
{ {
@@ -63,13 +63,13 @@ public class SignalRLimitFilter : IHubFilter
Path = "Connect", Path = "Connect",
HttpVerb = "ws", HttpVerb = "ws",
}; };
foreach (var rule in await _processor.GetMatchingRulesAsync(client)) foreach (var rule in await _processor.GetMatchingRulesAsync(client).ConfigureAwait(false))
{ {
var counter = await _processor.ProcessRequestAsync(client, rule); var counter = await _processor.ProcessRequestAsync(client, rule).ConfigureAwait(false);
if (counter.Count > rule.Limit) if (counter.Count > rule.Limit)
{ {
var retry = counter.Timestamp.RetryAfterFrom(rule); var retry = counter.Timestamp.RetryAfterFrom(rule);
logger.LogWarning($"Connection rate limit triggered from {ip}"); logger.LogWarning("Connection rate limit triggered from {ip}", ip);
ConnectionLimiterSemaphore.Release(); ConnectionLimiterSemaphore.Release();
throw new HubException($"Connection rate limit {retry}"); throw new HubException($"Connection rate limit {retry}");
} }
@@ -77,8 +77,8 @@ public class SignalRLimitFilter : IHubFilter
try try
{ {
await Task.Delay(250); await Task.Delay(250).ConfigureAwait(false);
await next(context); await next(context).ConfigureAwait(false);
} }
catch (Exception ex) catch (Exception ex)
{ {
@@ -93,7 +93,7 @@ public class SignalRLimitFilter : IHubFilter
public async Task OnDisconnectedAsync( public async Task OnDisconnectedAsync(
HubLifetimeContext context, Exception exception, Func<HubLifetimeContext, Exception, Task> next) HubLifetimeContext context, Exception exception, Func<HubLifetimeContext, Exception, Task> next)
{ {
await DisconnectLimiterSemaphore.WaitAsync(); await DisconnectLimiterSemaphore.WaitAsync().ConfigureAwait(false);
if (exception != null) if (exception != null)
{ {
logger.LogWarning(exception, "InitialException on OnDisconnectedAsync"); logger.LogWarning(exception, "InitialException on OnDisconnectedAsync");
@@ -101,8 +101,8 @@ public class SignalRLimitFilter : IHubFilter
try try
{ {
await next(context, exception); await next(context, exception).ConfigureAwait(false);
await Task.Delay(250); await Task.Delay(250).ConfigureAwait(false);
} }
catch (Exception e) catch (Exception e)
{ {

View File

@@ -73,7 +73,7 @@ namespace MareSynchronosServerTest.Hubs {
hub.Clients = clientsMock.Object; hub.Clients = clientsMock.Object;
hub.Context = clientContextMock.Object; hub.Context = clientContextMock.Object;
await hub.OnDisconnectedAsync(new Exception("Test Exception")); await hub.OnDisconnectedAsync(new Exception("Test Exception")).ConfigureAwait(false);
clientsMock.Verify(x => x.Users(It.Is<IReadOnlyList<string>>(x => x.Count() == 2 && x[0] == "User2" && x[1] == "User3")), Times.Once); clientsMock.Verify(x => x.Users(It.Is<IReadOnlyList<string>>(x => x.Count() == 2 && x[0] == "User2" && x[1] == "User3")), Times.Once);
clientProxyMock.Verify(x => x.SendCoreAsync(It.IsAny<string>(), It.Is<object[]>(o => (string)o[0] == "Ident1"), It.IsAny<CancellationToken>()), Times.Once); clientProxyMock.Verify(x => x.SendCoreAsync(It.IsAny<string>(), It.Is<object[]>(o => (string)o[0] == "Ident1"), It.IsAny<CancellationToken>()), Times.Once);