update blabla

This commit is contained in:
Stanley Dimant
2022-07-31 17:32:15 +02:00
parent 641ad52313
commit 8939585c3c
7 changed files with 80 additions and 35 deletions

Submodule MareAPI updated: a649b36495...714d990c0b

View File

@@ -1,10 +1,13 @@
using System; using System;
using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using MareSynchronosServer.Data; using MareSynchronosServer.Data;
using MareSynchronosServer.Metrics; using MareSynchronosServer.Metrics;
using MareSynchronosServer.Models;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
@@ -60,7 +63,8 @@ namespace MareSynchronosServer
{ {
_logger.LogInformation("File does not exist anymore: " + fileName); _logger.LogInformation("File does not exist anymore: " + 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);
@@ -69,6 +73,67 @@ namespace MareSynchronosServer
} }
} }
if (!bool.TryParse(_configuration["PurgeUnusedAccounts"], out var purgeUnusedAccounts))
{
purgeUnusedAccounts = false;
}
if (purgeUnusedAccounts)
{
if (!int.TryParse(_configuration["PurgeUnusedAccountsPeriodInDays"], out var usersOlderThanDays))
{
usersOlderThanDays = 14;
}
_logger.LogInformation($"Cleaning up users older than {usersOlderThanDays} days");
var allUsers = dbContext.Users.ToList();
List<User> usersToRemove = new();
foreach (var user in allUsers)
{
if (user.LastLoggedIn < (DateTime.Now - TimeSpan.FromDays(usersOlderThanDays)))
{
_logger.LogInformation("User outdated: " + user.UID);
usersToRemove.Add(user);
}
}
foreach (var user in usersToRemove)
{
var auth = dbContext.Auth.Single(a => a.UserUID == user.UID);
var userFiles = dbContext.Files.Where(f => f.Uploaded && f.Uploader.UID == user.UID).ToList();
foreach (var file in userFiles)
{
var fi = new FileInfo(Path.Combine(_configuration["CacheDirectory"], file.Hash));
if (fi.Exists)
{
MareMetrics.FilesTotalSize.Dec(fi.Length);
MareMetrics.FilesTotal.Dec();
fi.Delete();
}
}
dbContext.Files.RemoveRange(userFiles);
var ownPairData = dbContext.ClientPairs.Where(u => u.User.UID == user.UID).ToList();
dbContext.RemoveRange(ownPairData);
var otherPairData = dbContext.ClientPairs.Include(u => u.User)
.Where(u => u.OtherUser.UID == user.UID).ToList();
MareMetrics.Pairs.Dec(ownPairData.Count);
MareMetrics.PairsPaused.Dec(ownPairData.Count(c => c.IsPaused));
MareMetrics.Pairs.Dec(otherPairData.Count);
MareMetrics.PairsPaused.Dec(otherPairData.Count(c => c.IsPaused));
MareMetrics.UsersRegistered.Dec();
dbContext.RemoveRange(otherPairData);
dbContext.Remove(auth);
dbContext.Remove(user);
}
}
_logger.LogInformation($"Cleanup complete"); _logger.LogInformation($"Cleanup complete");
dbContext.SaveChanges(); dbContext.SaveChanges();

View File

@@ -53,32 +53,6 @@ namespace MareSynchronosServer.Hubs
await _dbContext.SaveChangesAsync(); await _dbContext.SaveChangesAsync();
} }
[Authorize(AuthenticationSchemes = SecretKeyAuthenticationHandler.AuthScheme)]
[HubMethodName(Api.StreamFileDownloadFileAsync)]
public async IAsyncEnumerable<byte[]> DownloadFileAsync(string hash, [EnumeratorCancellation] CancellationToken ct)
{
_logger.LogInformation("User " + AuthenticatedUserId + " downloading file: " + hash);
var file = _dbContext.Files.AsNoTracking()
.SingleOrDefault(f => f.Hash == hash);
if (file == null) yield break;
var chunkSize = 1024 * 512; // 512kb
int readByteCount;
var buffer = new byte[chunkSize];
var path = Path.Combine(BasePath, hash);
await using var fs = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.Read);
while ((readByteCount = fs.Read(buffer, 0, chunkSize)) > 0)
{
yield return readByteCount == chunkSize ? buffer.ToArray() : buffer.Take(readByteCount).ToArray();
}
_logger.LogInformation("User " + AuthenticatedUserId + " finished downloading file: " + hash);
MareMetrics.UserDownloadedFiles.Inc();
MareMetrics.UserDownloadedFilesSize.Inc(new FileInfo(path).Length);
}
[Authorize(AuthenticationSchemes = SecretKeyAuthenticationHandler.AuthScheme)] [Authorize(AuthenticationSchemes = SecretKeyAuthenticationHandler.AuthScheme)]
[HubMethodName(Api.InvokeFileGetFileSize)] [HubMethodName(Api.InvokeFileGetFileSize)]
public async Task<DownloadFileDto> GetFileSize(string hash) public async Task<DownloadFileDto> GetFileSize(string hash)

View File

@@ -26,6 +26,7 @@ namespace MareSynchronosServer.Hubs
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);
var ownPairData = await _dbContext.ClientPairs.Where(u => u.User.UID == userid).ToListAsync(); var ownPairData = await _dbContext.ClientPairs.Where(u => u.User.UID == userid).ToListAsync();
var auth = await _dbContext.Auth.SingleAsync(u => u.UserUID == userid);
MareMetrics.Pairs.Dec(ownPairData.Count); MareMetrics.Pairs.Dec(ownPairData.Count);
MareMetrics.PairsPaused.Dec(ownPairData.Count(c => c.IsPaused)); MareMetrics.PairsPaused.Dec(ownPairData.Count(c => c.IsPaused));
@@ -50,6 +51,7 @@ namespace MareSynchronosServer.Hubs
_dbContext.RemoveRange(otherPairData); _dbContext.RemoveRange(otherPairData);
_dbContext.Remove(userEntry); _dbContext.Remove(userEntry);
_dbContext.Remove(auth);
await _dbContext.SaveChangesAsync(); await _dbContext.SaveChangesAsync();
} }

View File

@@ -41,7 +41,7 @@ namespace MareSynchronosServer.Hubs
var isBanned = await _dbContext.BannedUsers.AsNoTracking().AnyAsync(u => u.CharacterIdentification == characterIdentification); var isBanned = await _dbContext.BannedUsers.AsNoTracking().AnyAsync(u => u.CharacterIdentification == characterIdentification);
if (userId != null && !isBanned && !string.IsNullOrEmpty(characterIdentification)) if (!string.IsNullOrEmpty(userId) && !isBanned && !string.IsNullOrEmpty(characterIdentification))
{ {
_logger.LogInformation("Connection from " + userId); _logger.LogInformation("Connection from " + userId);
var user = (await _dbContext.Users.SingleAsync(u => u.UID == userId)); var user = (await _dbContext.Users.SingleAsync(u => u.UID == userId));
@@ -52,8 +52,11 @@ namespace MareSynchronosServer.Hubs
ServerVersion = Api.Version ServerVersion = Api.Version
}; };
} }
else if (string.IsNullOrEmpty(user.CharacterIdentification))
{
MareMetrics.AuthorizedConnections.Inc();
}
MareMetrics.AuthorizedConnections.Inc();
user.LastLoggedIn = DateTime.UtcNow; user.LastLoggedIn = DateTime.UtcNow;
user.CharacterIdentification = characterIdentification; user.CharacterIdentification = characterIdentification;
await _dbContext.SaveChangesAsync(); await _dbContext.SaveChangesAsync();
@@ -109,9 +112,6 @@ namespace MareSynchronosServer.Hubs
(await _dbContext.Users.SingleAsync(u => u.UID == AuthenticatedUserId)).CharacterIdentification = null; (await _dbContext.Users.SingleAsync(u => u.UID == AuthenticatedUserId)).CharacterIdentification = null;
await _dbContext.SaveChangesAsync(); await _dbContext.SaveChangesAsync();
await Clients.All.SendAsync("UsersOnline",
await _dbContext.Users.CountAsync(u => !string.IsNullOrEmpty(u.CharacterIdentification)));
} }
await base.OnDisconnectedAsync(exception); await base.OnDisconnectedAsync(exception);

View File

@@ -8,6 +8,8 @@ using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using MareSynchronosServer.Metrics; using MareSynchronosServer.Metrics;
using MareSynchronosServer.Models;
using System.Collections.Generic;
namespace MareSynchronosServer namespace MareSynchronosServer
{ {
@@ -29,10 +31,10 @@ namespace MareSynchronosServer
context.SaveChanges(); context.SaveChanges();
// clean up residuals // clean up residuals
var users = context.Users.Where(u => u.CharacterIdentification != null); var users = context.Users;
foreach (var user in users) foreach (var user in users)
{ {
user.CharacterIdentification = string.Empty; user.CharacterIdentification = null;
} }
var looseFiles = context.Files.Where(f => f.Uploaded == false); var looseFiles = context.Files.Where(f => f.Uploaded == false);
context.RemoveRange(looseFiles); context.RemoveRange(looseFiles);

View File

@@ -24,6 +24,8 @@
} }
}, },
"UnusedFileRetentionPeriodInDays": 7, "UnusedFileRetentionPeriodInDays": 7,
"PurgeUnusedAccounts": true,
"PurgeUnusedAccountsPeriodInDays": 14,
"CacheDirectory": "G:\\ServerTest", // do not delete this key and set it to the path where the files will be stored "CacheDirectory": "G:\\ServerTest", // do not delete this key and set it to the path where the files will be stored
"AllowedHosts": "*", "AllowedHosts": "*",
"Kestrel": { "Kestrel": {