minor refactoring
This commit is contained in:
@@ -1,236 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using MareSynchronosServer.Metrics;
|
||||
using MareSynchronosShared.Authentication;
|
||||
using MareSynchronosShared.Data;
|
||||
using MareSynchronosShared.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace MareSynchronosServer
|
||||
{
|
||||
public class CleanupService : IHostedService, IDisposable
|
||||
{
|
||||
private readonly ILogger<CleanupService> _logger;
|
||||
private readonly IServiceProvider _services;
|
||||
private readonly IConfiguration _configuration;
|
||||
private Timer _timer;
|
||||
|
||||
public CleanupService(ILogger<CleanupService> logger, IServiceProvider services, IConfiguration configuration)
|
||||
{
|
||||
_logger = logger;
|
||||
_services = services;
|
||||
_configuration = configuration;
|
||||
}
|
||||
|
||||
public Task StartAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
_logger.LogInformation("Cleanup Service started");
|
||||
|
||||
_timer = new Timer(CleanUp, null, TimeSpan.Zero, TimeSpan.FromMinutes(10));
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
private void CleanUp(object state)
|
||||
{
|
||||
if (!int.TryParse(_configuration["UnusedFileRetentionPeriodInDays"], out var filesOlderThanDays))
|
||||
{
|
||||
filesOlderThanDays = 7;
|
||||
}
|
||||
|
||||
using var scope = _services.CreateScope();
|
||||
using var dbContext = scope.ServiceProvider.GetService<MareDbContext>()!;
|
||||
|
||||
_logger.LogInformation("Cleaning up files older than {filesOlderThanDays} days", filesOlderThanDays);
|
||||
|
||||
try
|
||||
{
|
||||
var prevTime = DateTime.Now.Subtract(TimeSpan.FromDays(filesOlderThanDays));
|
||||
|
||||
var allFiles = dbContext.Files.ToList();
|
||||
foreach (var file in allFiles.Where(f => f.Uploaded))
|
||||
{
|
||||
var fileName = Path.Combine(_configuration["CacheDirectory"], file.Hash);
|
||||
var fi = new FileInfo(fileName);
|
||||
if (!fi.Exists)
|
||||
{
|
||||
_logger.LogInformation("File does not exist anymore: {fileName}", fileName);
|
||||
dbContext.Files.Remove(file);
|
||||
}
|
||||
else if (fi.LastAccessTime < prevTime)
|
||||
{
|
||||
MareMetrics.FilesTotalSize.Dec(fi.Length);
|
||||
_logger.LogInformation("File outdated: {fileName}", fileName);
|
||||
dbContext.Files.Remove(file);
|
||||
fi.Delete();
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogWarning(ex, "Error during file cleanup");
|
||||
}
|
||||
|
||||
var cacheSizeLimitInGiB = _configuration.GetValue<double>("CacheSizeHardLimitInGiB", -1);
|
||||
|
||||
try
|
||||
{
|
||||
if (cacheSizeLimitInGiB > 0)
|
||||
{
|
||||
_logger.LogInformation("Cleaning up files beyond the cache size limit");
|
||||
var allLocalFiles = Directory.EnumerateFiles(_configuration["CacheDirectory"]).Select(f => new FileInfo(f)).ToList().OrderBy(f => f.LastAccessTimeUtc).ToList();
|
||||
var totalCacheSizeInBytes = allLocalFiles.Sum(s => s.Length);
|
||||
long cacheSizeLimitInBytes = (long)(cacheSizeLimitInGiB * 1024 * 1024 * 1024);
|
||||
HashSet<string> removedHashes = new();
|
||||
while (totalCacheSizeInBytes > cacheSizeLimitInBytes && allLocalFiles.Any())
|
||||
{
|
||||
var oldestFile = allLocalFiles.First();
|
||||
removedHashes.Add(oldestFile.Name.ToLower());
|
||||
allLocalFiles.Remove(oldestFile);
|
||||
totalCacheSizeInBytes -= oldestFile.Length;
|
||||
MareMetrics.FilesTotal.Dec();
|
||||
MareMetrics.FilesTotalSize.Dec(oldestFile.Length);
|
||||
oldestFile.Delete();
|
||||
}
|
||||
|
||||
dbContext.Files.RemoveRange(dbContext.Files.Where(f => removedHashes.Contains(f.Hash.ToLower())));
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogWarning(ex, "Error during cache size limit cleanup");
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
_logger.LogInformation($"Cleaning up expired lodestone authentications");
|
||||
var lodestoneAuths = dbContext.LodeStoneAuth.Include(u => u.User).Where(a => a.StartedAt != null).ToList();
|
||||
List<LodeStoneAuth> expiredAuths = new List<LodeStoneAuth>();
|
||||
foreach (var auth in lodestoneAuths)
|
||||
{
|
||||
if (auth.StartedAt < DateTime.UtcNow - TimeSpan.FromMinutes(15))
|
||||
{
|
||||
expiredAuths.Add(auth);
|
||||
}
|
||||
}
|
||||
|
||||
dbContext.RemoveRange(expiredAuths.Select(a => a.User));
|
||||
dbContext.RemoveRange(expiredAuths);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogWarning(ex, "Error during expired auths cleanup");
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
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", usersOlderThanDays);
|
||||
|
||||
var allUsers = dbContext.Users.ToList();
|
||||
List<User> usersToRemove = new();
|
||||
foreach (var user in allUsers)
|
||||
{
|
||||
if (user.LastLoggedIn < (DateTime.UtcNow - TimeSpan.FromDays(usersOlderThanDays)))
|
||||
{
|
||||
_logger.LogInformation("User outdated: {userUID}", user.UID);
|
||||
usersToRemove.Add(user);
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var user in usersToRemove)
|
||||
{
|
||||
PurgeUser(user, dbContext, _configuration);
|
||||
}
|
||||
}
|
||||
|
||||
_logger.LogInformation("Cleaning up unauthorized users");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogWarning(ex, "Error during user purge");
|
||||
}
|
||||
|
||||
SecretKeyAuthenticationHandler.ClearUnauthorizedUsers();
|
||||
|
||||
_logger.LogInformation($"Cleanup complete");
|
||||
|
||||
dbContext.SaveChanges();
|
||||
}
|
||||
|
||||
public static void PurgeUser(User user, MareDbContext dbContext, IConfiguration _configuration)
|
||||
{
|
||||
var lodestone = dbContext.LodeStoneAuth.SingleOrDefault(a => a.User.UID == user.UID);
|
||||
|
||||
if (lodestone != null)
|
||||
{
|
||||
dbContext.Remove(lodestone);
|
||||
}
|
||||
|
||||
SecretKeyAuthenticationHandler.RemoveAuthentication(user.UID);
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
public Task StopAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
_timer?.Change(Timeout.Infinite, 0);
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_timer?.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,431 +0,0 @@
|
||||
using Discord;
|
||||
using Discord.WebSocket;
|
||||
using MareSynchronosServer.Hubs;
|
||||
using MareSynchronosServer.Metrics;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Linq;
|
||||
using System.Net.Http;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using MareSynchronosShared.Data;
|
||||
using MareSynchronosShared.Models;
|
||||
|
||||
namespace MareSynchronosServer.Discord
|
||||
{
|
||||
public class DiscordBot : IHostedService
|
||||
{
|
||||
private readonly IServiceProvider services;
|
||||
private readonly IConfiguration configuration;
|
||||
private readonly ILogger<DiscordBot> logger;
|
||||
private readonly Random random;
|
||||
private string authToken = string.Empty;
|
||||
DiscordSocketClient discordClient;
|
||||
ConcurrentDictionary<ulong, string> DiscordLodestoneMapping = new();
|
||||
private CancellationTokenSource verificationTaskCts;
|
||||
private CancellationTokenSource updateStatusCts;
|
||||
private readonly string[] LodestoneServers = new[] { "eu", "na", "jp", "fr", "de" };
|
||||
private readonly ConcurrentQueue<SocketSlashCommand> verificationQueue = new();
|
||||
|
||||
private SemaphoreSlim semaphore;
|
||||
|
||||
public DiscordBot(IServiceProvider services, IConfiguration configuration, ILogger<DiscordBot> logger)
|
||||
{
|
||||
this.services = services;
|
||||
this.configuration = configuration;
|
||||
this.logger = logger;
|
||||
this.verificationQueue = new ConcurrentQueue<SocketSlashCommand>();
|
||||
this.semaphore = new SemaphoreSlim(1);
|
||||
|
||||
random = new();
|
||||
authToken = configuration.GetValue<string>("DiscordBotToken");
|
||||
|
||||
discordClient = new(new DiscordSocketConfig()
|
||||
{
|
||||
DefaultRetryMode = RetryMode.AlwaysRetry
|
||||
});
|
||||
|
||||
discordClient.Log += Log;
|
||||
}
|
||||
|
||||
private async Task DiscordClient_SlashCommandExecuted(SocketSlashCommand arg)
|
||||
{
|
||||
await semaphore.WaitAsync().ConfigureAwait(false);
|
||||
try
|
||||
{
|
||||
if (arg.Data.Name == "register")
|
||||
{
|
||||
if (arg.Data.Options.FirstOrDefault(f => f.Name == "overwrite_old_account") != null)
|
||||
{
|
||||
await DeletePreviousUserAccount(arg.User.Id).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
var modal = new ModalBuilder();
|
||||
modal.WithTitle("Verify with Lodestone");
|
||||
modal.WithCustomId("register_modal");
|
||||
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()).ConfigureAwait(false);
|
||||
}
|
||||
else if (arg.Data.Name == "verify")
|
||||
{
|
||||
EmbedBuilder eb = new();
|
||||
if (verificationQueue.Any(u => u.User.Id == arg.User.Id))
|
||||
{
|
||||
eb.WithTitle("Already queued for verfication");
|
||||
eb.WithDescription("You are already queued for verification. Please wait.");
|
||||
await arg.RespondAsync(embeds: new[] { eb.Build() }, ephemeral: true).ConfigureAwait(false);
|
||||
}
|
||||
else if (!DiscordLodestoneMapping.ContainsKey(arg.User.Id))
|
||||
{
|
||||
eb.WithTitle("Cannot verify registration");
|
||||
eb.WithDescription("You need to **/register** first before you can **/verify**");
|
||||
await arg.RespondAsync(embeds: new[] { eb.Build() }, ephemeral: true).ConfigureAwait(false);
|
||||
}
|
||||
else
|
||||
{
|
||||
await arg.DeferAsync(ephemeral: true).ConfigureAwait(false);
|
||||
verificationQueue.Enqueue(arg);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
await arg.RespondAsync("idk what you did to get here to start, just follow the instructions as provided.", ephemeral: true).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
semaphore.Release();
|
||||
}
|
||||
}
|
||||
|
||||
private async Task DeletePreviousUserAccount(ulong id)
|
||||
{
|
||||
using var scope = services.CreateScope();
|
||||
using var db = scope.ServiceProvider.GetService<MareDbContext>();
|
||||
var discordAuthedUser = await db.LodeStoneAuth.Include(u => u.User).FirstOrDefaultAsync(u => u.DiscordId == id).ConfigureAwait(false);
|
||||
if (discordAuthedUser != null)
|
||||
{
|
||||
if (discordAuthedUser.User != null)
|
||||
{
|
||||
CleanupService.PurgeUser(discordAuthedUser.User, db, configuration);
|
||||
}
|
||||
else
|
||||
{
|
||||
db.Remove(discordAuthedUser);
|
||||
}
|
||||
|
||||
await db.SaveChangesAsync().ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task DiscordClient_ModalSubmitted(SocketModal arg)
|
||||
{
|
||||
if (arg.Data.CustomId == "register_modal")
|
||||
{
|
||||
var embed = await HandleRegisterModalAsync(arg).ConfigureAwait(false);
|
||||
await arg.RespondAsync(embeds: new Embed[] { embed }, ephemeral: true).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task<Embed> HandleVerifyAsync(ulong id)
|
||||
{
|
||||
var embedBuilder = new EmbedBuilder();
|
||||
|
||||
using var scope = services.CreateScope();
|
||||
var req = new HttpClient();
|
||||
using var db = scope.ServiceProvider.GetService<MareDbContext>();
|
||||
|
||||
var lodestoneAuth = db.LodeStoneAuth.SingleOrDefault(u => u.DiscordId == id);
|
||||
if (lodestoneAuth != null && DiscordLodestoneMapping.ContainsKey(id))
|
||||
{
|
||||
var randomServer = LodestoneServers[random.Next(LodestoneServers.Length)];
|
||||
var response = await req.GetAsync($"https://{randomServer}.finalfantasyxiv.com/lodestone/character/{DiscordLodestoneMapping[id]}").ConfigureAwait(false);
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
var content = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
|
||||
if (content.Contains(lodestoneAuth.LodestoneAuthString))
|
||||
{
|
||||
DiscordLodestoneMapping.TryRemove(id, out _);
|
||||
|
||||
using var sha256 = SHA256.Create();
|
||||
var user = new User();
|
||||
|
||||
var hasValidUid = false;
|
||||
while (!hasValidUid)
|
||||
{
|
||||
var uid = MareHub.GenerateRandomString(10);
|
||||
if (db.Users.Any(u => u.UID == uid)) continue;
|
||||
user.UID = uid;
|
||||
hasValidUid = true;
|
||||
}
|
||||
|
||||
// make the first registered user on the service to admin
|
||||
if (!await db.Users.AnyAsync().ConfigureAwait(false))
|
||||
{
|
||||
user.IsAdmin = true;
|
||||
}
|
||||
|
||||
if (configuration.GetValue<bool>("PurgeUnusedAccounts"))
|
||||
{
|
||||
var purgedDays = configuration.GetValue<int>("PurgeUnusedAccountsPeriodInDays");
|
||||
user.LastLoggedIn = DateTime.UtcNow - TimeSpan.FromDays(purgedDays) + TimeSpan.FromDays(1);
|
||||
}
|
||||
|
||||
var computedHash = BitConverter.ToString(sha256.ComputeHash(Encoding.UTF8.GetBytes(MareHub.GenerateRandomString(64)))).Replace("-", "");
|
||||
var auth = new Auth()
|
||||
{
|
||||
HashedKey = BitConverter.ToString(sha256.ComputeHash(Encoding.UTF8.GetBytes(computedHash)))
|
||||
.Replace("-", ""),
|
||||
User = user,
|
||||
};
|
||||
|
||||
await db.Users.AddAsync(user).ConfigureAwait(false);
|
||||
await db.Auth.AddAsync(auth).ConfigureAwait(false);
|
||||
|
||||
logger.LogInformation("User registered: {userUID}", user.UID);
|
||||
|
||||
MareMetrics.UsersRegistered.Inc();
|
||||
|
||||
lodestoneAuth.StartedAt = null;
|
||||
lodestoneAuth.User = user;
|
||||
lodestoneAuth.LodestoneAuthString = null;
|
||||
|
||||
embedBuilder.WithTitle("Registration successful");
|
||||
embedBuilder.WithDescription("This is your private secret key. Do not share this private secret key with anyone. **If you lose it, it is irrevocably lost.**"
|
||||
+ Environment.NewLine + Environment.NewLine
|
||||
+ $"**{computedHash}**"
|
||||
+ Environment.NewLine + Environment.NewLine
|
||||
+ "Enter this key in Mare Synchronos and hit save to connect to the service."
|
||||
+ Environment.NewLine
|
||||
+ "You should connect as soon as possible to not get caught by the automatic cleanup process."
|
||||
+ Environment.NewLine
|
||||
+ "Have fun.");
|
||||
}
|
||||
else
|
||||
{
|
||||
embedBuilder.WithTitle("Failed to verify your character");
|
||||
embedBuilder.WithDescription("Did not find requested authentication key on your profile. Make sure you have saved *twice*, then do **/verify** again.");
|
||||
lodestoneAuth.StartedAt = DateTime.UtcNow;
|
||||
}
|
||||
}
|
||||
|
||||
await db.SaveChangesAsync().ConfigureAwait(false);
|
||||
}
|
||||
else
|
||||
{
|
||||
embedBuilder.WithTitle("Your auth has expired or something else went wrong");
|
||||
embedBuilder.WithDescription("Start again with **/register**");
|
||||
DiscordLodestoneMapping.TryRemove(id, out _);
|
||||
}
|
||||
|
||||
return embedBuilder.Build();
|
||||
}
|
||||
|
||||
private async Task<Embed> HandleRegisterModalAsync(SocketModal arg)
|
||||
{
|
||||
var embed = new EmbedBuilder();
|
||||
|
||||
var lodestoneId = ParseCharacterIdFromLodestoneUrl(arg.Data.Components.Single(c => c.CustomId == "lodestoneurl").Value);
|
||||
if (lodestoneId == null)
|
||||
{
|
||||
embed.WithTitle("Invalid Lodestone URL");
|
||||
embed.WithDescription("The lodestone URL was not valid. It should have following format:" + Environment.NewLine
|
||||
+ "https://eu.finalfantasyxiv.com/lodestone/character/YOUR_LODESTONE_ID/");
|
||||
}
|
||||
else
|
||||
{
|
||||
// check if userid is already in db
|
||||
using var scope = services.CreateScope();
|
||||
using var sha256 = SHA256.Create();
|
||||
|
||||
var hashedLodestoneId = BitConverter.ToString(sha256.ComputeHash(Encoding.UTF8.GetBytes(lodestoneId.ToString()))).Replace("-", "");
|
||||
|
||||
using var db = scope.ServiceProvider.GetService<MareDbContext>();
|
||||
|
||||
// check if discord id or lodestone id is banned
|
||||
if (db.BannedRegistrations.Any(a => a.DiscordIdOrLodestoneAuth == arg.User.Id.ToString() || a.DiscordIdOrLodestoneAuth == hashedLodestoneId))
|
||||
{
|
||||
embed.WithTitle("no");
|
||||
embed.WithDescription("your account is banned");
|
||||
}
|
||||
else if (db.LodeStoneAuth.Any(a => a.DiscordId == arg.User.Id))
|
||||
{
|
||||
// user already in db
|
||||
embed.WithTitle("Registration failed");
|
||||
embed.WithDescription("You cannot register more than one lodestone character to your discord account.");
|
||||
}
|
||||
else if (db.LodeStoneAuth.Any(a => a.HashedLodestoneId == hashedLodestoneId))
|
||||
{
|
||||
// character already in db
|
||||
embed.WithTitle("Registration failed");
|
||||
embed.WithDescription("This lodestone character already exists in the Database. If you are the rightful owner for this character and lost your secret key generated with it, contact the developer.");
|
||||
}
|
||||
else
|
||||
{
|
||||
string lodestoneAuth = await GenerateLodestoneAuth(arg.User.Id, hashedLodestoneId, db).ConfigureAwait(false);
|
||||
// check if lodestone id is already in db
|
||||
embed.WithTitle("Authorize your character");
|
||||
embed.WithDescription("Add following key to your character profile at https://na.finalfantasyxiv.com/lodestone/my/setting/profile/"
|
||||
+ Environment.NewLine + Environment.NewLine
|
||||
+ $"**{lodestoneAuth}**"
|
||||
+ Environment.NewLine + Environment.NewLine
|
||||
+ $"**! THIS IS NOT THE KEY YOU HAVE TO ENTER IN MARE !**"
|
||||
+ Environment.NewLine + Environment.NewLine
|
||||
+ "Once added and saved, use command **/verify** to finish registration and receive a secret key to use for Mare Synchronos."
|
||||
+ Environment.NewLine
|
||||
+ "You can delete the entry from your profile after verification."
|
||||
+ Environment.NewLine + Environment.NewLine
|
||||
+ "The verification will expire in approximately 15 minutes. If you fail to **/verify** the registration will be invalidated and you have to **/register** again.");
|
||||
DiscordLodestoneMapping[arg.User.Id] = lodestoneId.ToString();
|
||||
}
|
||||
}
|
||||
|
||||
return embed.Build();
|
||||
}
|
||||
|
||||
private async Task<string> GenerateLodestoneAuth(ulong discordid, string hashedLodestoneId, MareDbContext dbContext)
|
||||
{
|
||||
var auth = MareHub.GenerateRandomString(32);
|
||||
LodeStoneAuth lsAuth = new LodeStoneAuth()
|
||||
{
|
||||
DiscordId = discordid,
|
||||
HashedLodestoneId = hashedLodestoneId,
|
||||
LodestoneAuthString = auth,
|
||||
StartedAt = DateTime.UtcNow
|
||||
};
|
||||
|
||||
dbContext.Add(lsAuth);
|
||||
await dbContext.SaveChangesAsync().ConfigureAwait(false);
|
||||
|
||||
return auth;
|
||||
}
|
||||
|
||||
private int? ParseCharacterIdFromLodestoneUrl(string lodestoneUrl)
|
||||
{
|
||||
var regex = new Regex(@"https:\/\/(na|eu|de|fr|jp)\.finalfantasyxiv\.com\/lodestone\/character\/\d+");
|
||||
var matches = regex.Match(lodestoneUrl);
|
||||
var isLodestoneUrl = matches.Success;
|
||||
if (!isLodestoneUrl || matches.Groups.Count < 1) return null;
|
||||
|
||||
lodestoneUrl = matches.Groups[0].ToString();
|
||||
var stringId = lodestoneUrl.Split('/', StringSplitOptions.RemoveEmptyEntries).Last();
|
||||
if (!int.TryParse(stringId, out int lodestoneId))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return lodestoneId;
|
||||
}
|
||||
|
||||
private async Task DiscordClient_Ready()
|
||||
{
|
||||
var register = new SlashCommandBuilder()
|
||||
.WithName("register")
|
||||
.WithDescription("Registration for the Mare Synchronos server of this Discord")
|
||||
.AddOption(new SlashCommandOptionBuilder()
|
||||
.WithName("new_account")
|
||||
.WithDescription("Starts the registration process for the Mare Synchronos server of this Discord")
|
||||
.WithType(ApplicationCommandOptionType.SubCommand))
|
||||
.AddOption(new SlashCommandOptionBuilder()
|
||||
.WithName("overwrite_old_account")
|
||||
.WithDescription("Will forcefully overwrite your current character on the service, if present")
|
||||
.WithType(ApplicationCommandOptionType.SubCommand));
|
||||
|
||||
var verify = new SlashCommandBuilder();
|
||||
verify.WithName("verify");
|
||||
verify.WithDescription("Finishes the registration process for the Mare Synchronos server of this Discord");
|
||||
|
||||
try
|
||||
{
|
||||
await discordClient.CreateGlobalApplicationCommandAsync(register.Build()).ConfigureAwait(false);
|
||||
await discordClient.CreateGlobalApplicationCommandAsync(verify.Build()).ConfigureAwait(false);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, "Failed to create command");
|
||||
}
|
||||
}
|
||||
|
||||
private Task Log(LogMessage msg)
|
||||
{
|
||||
logger.LogInformation("{msg}", msg);
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public async Task StartAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(authToken))
|
||||
{
|
||||
authToken = configuration.GetValue<string>("DiscordBotToken");
|
||||
|
||||
await discordClient.LoginAsync(TokenType.Bot, authToken).ConfigureAwait(false);
|
||||
await discordClient.StartAsync().ConfigureAwait(false);
|
||||
|
||||
discordClient.Ready += DiscordClient_Ready;
|
||||
discordClient.SlashCommandExecuted += DiscordClient_SlashCommandExecuted;
|
||||
discordClient.ModalSubmitted += DiscordClient_ModalSubmitted;
|
||||
|
||||
_ = ProcessQueueWork();
|
||||
_ = UpdateStatusAsync();
|
||||
}
|
||||
}
|
||||
|
||||
private async Task ProcessQueueWork()
|
||||
{
|
||||
verificationTaskCts = new CancellationTokenSource();
|
||||
while (!verificationTaskCts.IsCancellationRequested)
|
||||
{
|
||||
if (verificationQueue.TryDequeue(out var queueitem))
|
||||
{
|
||||
try
|
||||
{
|
||||
var dataEmbed = await HandleVerifyAsync(queueitem.User.Id).ConfigureAwait(false);
|
||||
await queueitem.FollowupAsync(embed: dataEmbed, ephemeral: true).ConfigureAwait(false);
|
||||
|
||||
logger.LogInformation("Sent login information to user");
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
logger.LogError(e, "Error during queue work");
|
||||
}
|
||||
|
||||
}
|
||||
await Task.Delay(TimeSpan.FromSeconds(2), verificationTaskCts.Token).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task UpdateStatusAsync()
|
||||
{
|
||||
updateStatusCts = new();
|
||||
while (!updateStatusCts.IsCancellationRequested)
|
||||
{
|
||||
await using var scope = services.CreateAsyncScope();
|
||||
await using var db = scope.ServiceProvider.GetService<MareDbContext>();
|
||||
|
||||
var users = db.Users.Count(c => c.CharacterIdentification != null);
|
||||
|
||||
await discordClient.SetActivityAsync(new Game("Mare for " + users + " Users")).ConfigureAwait(false);
|
||||
|
||||
await Task.Delay(TimeSpan.FromSeconds(15)).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task StopAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
verificationTaskCts?.Cancel();
|
||||
updateStatusCts?.Cancel();
|
||||
|
||||
await discordClient.LogoutAsync().ConfigureAwait(false);
|
||||
await discordClient.StopAsync().ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,7 @@
|
||||
using System.Security.Claims;
|
||||
using Microsoft.AspNetCore.SignalR;
|
||||
|
||||
namespace MareSynchronosServer
|
||||
namespace MareSynchronosServer.Hubs
|
||||
{
|
||||
public class IdBasedUserIdProvider : IUserIdProvider
|
||||
{
|
||||
@@ -18,7 +18,7 @@ namespace MareSynchronosServer.Hubs
|
||||
|
||||
private List<string> OnlineAdmins => _dbContext.Users.Where(u => !string.IsNullOrEmpty(u.CharacterIdentification) && (u.IsModerator || u.IsAdmin))
|
||||
.Select(u => u.UID).ToList();
|
||||
[Authorize(AuthenticationSchemes = SecretKeyAuthenticationHandler.AuthScheme)]
|
||||
[Authorize(AuthenticationSchemes = SecretKeyGrpcAuthenticationHandler.AuthScheme)]
|
||||
[HubMethodName(Api.SendAdminChangeModeratorStatus)]
|
||||
public async Task ChangeModeratorStatus(string uid, bool isModerator)
|
||||
{
|
||||
@@ -33,7 +33,7 @@ namespace MareSynchronosServer.Hubs
|
||||
await Clients.Users(user.UID).SendAsync(Api.OnAdminForcedReconnect).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
[Authorize(AuthenticationSchemes = SecretKeyAuthenticationHandler.AuthScheme)]
|
||||
[Authorize(AuthenticationSchemes = SecretKeyGrpcAuthenticationHandler.AuthScheme)]
|
||||
[HubMethodName(Api.SendAdminDeleteBannedUser)]
|
||||
public async Task DeleteBannedUser(BannedUserDto dto)
|
||||
{
|
||||
@@ -51,7 +51,7 @@ namespace MareSynchronosServer.Hubs
|
||||
await Clients.Users(OnlineAdmins).SendAsync(Api.OnAdminDeleteBannedUser, dto).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
[Authorize(AuthenticationSchemes = SecretKeyAuthenticationHandler.AuthScheme)]
|
||||
[Authorize(AuthenticationSchemes = SecretKeyGrpcAuthenticationHandler.AuthScheme)]
|
||||
[HubMethodName(Api.SendAdminDeleteForbiddenFile)]
|
||||
public async Task DeleteForbiddenFile(ForbiddenFileDto dto)
|
||||
{
|
||||
@@ -69,7 +69,7 @@ namespace MareSynchronosServer.Hubs
|
||||
await Clients.Users(OnlineAdmins).SendAsync(Api.OnAdminDeleteForbiddenFile, dto).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
[Authorize(AuthenticationSchemes = SecretKeyAuthenticationHandler.AuthScheme)]
|
||||
[Authorize(AuthenticationSchemes = SecretKeyGrpcAuthenticationHandler.AuthScheme)]
|
||||
[HubMethodName(Api.InvokeAdminGetBannedUsers)]
|
||||
public async Task<List<BannedUserDto>> GetBannedUsers()
|
||||
{
|
||||
@@ -82,7 +82,7 @@ namespace MareSynchronosServer.Hubs
|
||||
}).ToListAsync().ConfigureAwait(false);
|
||||
}
|
||||
|
||||
[Authorize(AuthenticationSchemes = SecretKeyAuthenticationHandler.AuthScheme)]
|
||||
[Authorize(AuthenticationSchemes = SecretKeyGrpcAuthenticationHandler.AuthScheme)]
|
||||
[HubMethodName(Api.InvokeAdminGetForbiddenFiles)]
|
||||
public async Task<List<ForbiddenFileDto>> GetForbiddenFiles()
|
||||
{
|
||||
@@ -95,7 +95,7 @@ namespace MareSynchronosServer.Hubs
|
||||
}).ToListAsync().ConfigureAwait(false);
|
||||
}
|
||||
|
||||
[Authorize(AuthenticationSchemes = SecretKeyAuthenticationHandler.AuthScheme)]
|
||||
[Authorize(AuthenticationSchemes = SecretKeyGrpcAuthenticationHandler.AuthScheme)]
|
||||
[HubMethodName(Api.InvokeAdminGetOnlineUsers)]
|
||||
public async Task<List<OnlineUserDto>> AdminGetOnlineUsers()
|
||||
{
|
||||
@@ -110,7 +110,7 @@ namespace MareSynchronosServer.Hubs
|
||||
}).ToListAsync().ConfigureAwait(false);
|
||||
}
|
||||
|
||||
[Authorize(AuthenticationSchemes = SecretKeyAuthenticationHandler.AuthScheme)]
|
||||
[Authorize(AuthenticationSchemes = SecretKeyGrpcAuthenticationHandler.AuthScheme)]
|
||||
[HubMethodName(Api.SendAdminUpdateOrAddBannedUser)]
|
||||
public async Task UpdateOrAddBannedUser(BannedUserDto dto)
|
||||
{
|
||||
@@ -142,7 +142,7 @@ namespace MareSynchronosServer.Hubs
|
||||
}
|
||||
}
|
||||
|
||||
[Authorize(AuthenticationSchemes = SecretKeyAuthenticationHandler.AuthScheme)]
|
||||
[Authorize(AuthenticationSchemes = SecretKeyGrpcAuthenticationHandler.AuthScheme)]
|
||||
[HubMethodName(Api.SendAdminUpdateOrAddForbiddenFile)]
|
||||
public async Task UpdateOrAddForbiddenFile(ForbiddenFileDto dto)
|
||||
{
|
||||
|
||||
@@ -7,9 +7,10 @@ using System.Security.Cryptography;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using MareSynchronos.API;
|
||||
using MareSynchronosServer.Metrics;
|
||||
using MareSynchronosShared.Authentication;
|
||||
using MareSynchronosShared.Metrics;
|
||||
using MareSynchronosShared.Models;
|
||||
using MareSynchronosShared.Protos;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.SignalR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
@@ -21,7 +22,7 @@ namespace MareSynchronosServer.Hubs
|
||||
{
|
||||
private string BasePath => _configuration["CacheDirectory"];
|
||||
|
||||
[Authorize(AuthenticationSchemes = SecretKeyAuthenticationHandler.AuthScheme)]
|
||||
[Authorize(AuthenticationSchemes = SecretKeyGrpcAuthenticationHandler.AuthScheme)]
|
||||
[HubMethodName(Api.SendFileAbortUpload)]
|
||||
public async Task AbortUpload()
|
||||
{
|
||||
@@ -32,7 +33,7 @@ namespace MareSynchronosServer.Hubs
|
||||
await _dbContext.SaveChangesAsync().ConfigureAwait(false);
|
||||
}
|
||||
|
||||
[Authorize(AuthenticationSchemes = SecretKeyAuthenticationHandler.AuthScheme)]
|
||||
[Authorize(AuthenticationSchemes = SecretKeyGrpcAuthenticationHandler.AuthScheme)]
|
||||
[HubMethodName(Api.SendFileDeleteAllFiles)]
|
||||
public async Task DeleteAllFiles()
|
||||
{
|
||||
@@ -44,8 +45,10 @@ namespace MareSynchronosServer.Hubs
|
||||
var fi = new FileInfo(Path.Combine(BasePath, file.Hash));
|
||||
if (fi.Exists)
|
||||
{
|
||||
MareMetrics.FilesTotalSize.Dec(fi.Length);
|
||||
MareMetrics.FilesTotal.Dec();
|
||||
await _metricsClient.DecGaugeAsync(new GaugeRequest()
|
||||
{GaugeName = MetricsAPI.GaugeFilesTotalSize, Value = fi.Length}).ConfigureAwait(false);
|
||||
await _metricsClient.DecGaugeAsync(new GaugeRequest()
|
||||
{ GaugeName = MetricsAPI.GaugeFilesTotal, Value = 1}).ConfigureAwait(false);
|
||||
fi.Delete();
|
||||
}
|
||||
}
|
||||
@@ -53,7 +56,7 @@ namespace MareSynchronosServer.Hubs
|
||||
await _dbContext.SaveChangesAsync().ConfigureAwait(false);
|
||||
}
|
||||
|
||||
[Authorize(AuthenticationSchemes = SecretKeyAuthenticationHandler.AuthScheme)]
|
||||
[Authorize(AuthenticationSchemes = SecretKeyGrpcAuthenticationHandler.AuthScheme)]
|
||||
[HubMethodName(Api.InvokeGetFilesSizes)]
|
||||
public async Task<List<DownloadFileDto>> GetFilesSizes(List<string> hashes)
|
||||
{
|
||||
@@ -97,7 +100,7 @@ namespace MareSynchronosServer.Hubs
|
||||
return response;
|
||||
}
|
||||
|
||||
[Authorize(AuthenticationSchemes = SecretKeyAuthenticationHandler.AuthScheme)]
|
||||
[Authorize(AuthenticationSchemes = SecretKeyGrpcAuthenticationHandler.AuthScheme)]
|
||||
[HubMethodName(Api.InvokeFileIsUploadFinished)]
|
||||
public async Task<bool> IsUploadFinished()
|
||||
{
|
||||
@@ -106,7 +109,7 @@ namespace MareSynchronosServer.Hubs
|
||||
.AnyAsync(f => f.Uploader.UID == userUid && !f.Uploaded).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
[Authorize(AuthenticationSchemes = SecretKeyAuthenticationHandler.AuthScheme)]
|
||||
[Authorize(AuthenticationSchemes = SecretKeyGrpcAuthenticationHandler.AuthScheme)]
|
||||
[HubMethodName(Api.InvokeFileSendFiles)]
|
||||
public async Task<List<UploadFileDto>> SendFiles(List<string> fileListHashes)
|
||||
{
|
||||
@@ -156,7 +159,7 @@ namespace MareSynchronosServer.Hubs
|
||||
return notCoveredFiles.Values.ToList();
|
||||
}
|
||||
|
||||
[Authorize(AuthenticationSchemes = SecretKeyAuthenticationHandler.AuthScheme)]
|
||||
[Authorize(AuthenticationSchemes = SecretKeyGrpcAuthenticationHandler.AuthScheme)]
|
||||
[HubMethodName(Api.SendFileUploadFileStreamAsync)]
|
||||
public async Task UploadFileStreamAsync(string hash, IAsyncEnumerable<byte[]> fileContent)
|
||||
{
|
||||
@@ -224,8 +227,10 @@ namespace MareSynchronosServer.Hubs
|
||||
relatedFile = _dbContext.Files.Single(f => f.Hash == hash);
|
||||
relatedFile.Uploaded = true;
|
||||
|
||||
MareMetrics.FilesTotal.Inc();
|
||||
MareMetrics.FilesTotalSize.Inc(length);
|
||||
await _metricsClient.IncGaugeAsync(new GaugeRequest()
|
||||
{ GaugeName = MetricsAPI.GaugeFilesTotalSize, Value = length }).ConfigureAwait(false);
|
||||
await _metricsClient.IncGaugeAsync(new GaugeRequest()
|
||||
{ GaugeName = MetricsAPI.GaugeFilesTotal, Value = 1 }).ConfigureAwait(false);
|
||||
|
||||
await _dbContext.SaveChangesAsync().ConfigureAwait(false);
|
||||
_logger.LogInformation("File {hash} added to DB", hash);
|
||||
|
||||
@@ -2,9 +2,10 @@
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using MareSynchronos.API;
|
||||
using MareSynchronosServer.Metrics;
|
||||
using MareSynchronosShared.Authentication;
|
||||
using MareSynchronosShared.Metrics;
|
||||
using MareSynchronosShared.Models;
|
||||
using MareSynchronosShared.Protos;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.SignalR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
@@ -14,7 +15,7 @@ namespace MareSynchronosServer.Hubs
|
||||
{
|
||||
public partial class MareHub
|
||||
{
|
||||
[Authorize(AuthenticationSchemes = SecretKeyAuthenticationHandler.AuthScheme)]
|
||||
[Authorize(AuthenticationSchemes = SecretKeyGrpcAuthenticationHandler.AuthScheme)]
|
||||
[HubMethodName(Api.SendUserDeleteAccount)]
|
||||
public async Task DeleteAccount()
|
||||
{
|
||||
@@ -37,10 +38,8 @@ namespace MareSynchronosServer.Hubs
|
||||
await Task.Delay(1000).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
SecretKeyAuthenticationHandler.RemoveAuthentication(userid);
|
||||
await _authServiceClient.RemoveAuthAsync(new RemoveAuthRequest() { Uid = userid }).ConfigureAwait(false);
|
||||
|
||||
MareMetrics.Pairs.Dec(ownPairData.Count);
|
||||
MareMetrics.PairsPaused.Dec(ownPairData.Count(c => c.IsPaused));
|
||||
|
||||
_dbContext.RemoveRange(ownPairData);
|
||||
await _dbContext.SaveChangesAsync().ConfigureAwait(false);
|
||||
@@ -56,9 +55,12 @@ namespace MareSynchronosServer.Hubs
|
||||
}, userEntry.CharacterIdentification).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
MareMetrics.Pairs.Dec(otherPairData.Count);
|
||||
MareMetrics.PairsPaused.Dec(otherPairData.Count(c => c.IsPaused));
|
||||
MareMetrics.UsersRegistered.Dec();
|
||||
await _metricsClient.DecGaugeAsync(new GaugeRequest()
|
||||
{ GaugeName = MetricsAPI.GaugePairs, Value = ownPairData.Count + otherPairData.Count }).ConfigureAwait(false);
|
||||
await _metricsClient.DecGaugeAsync(new GaugeRequest()
|
||||
{ GaugeName = MetricsAPI.GaugePairsPaused, Value = ownPairData.Count(c => c.IsPaused) }).ConfigureAwait(false);
|
||||
await _metricsClient.DecGaugeAsync(new GaugeRequest()
|
||||
{ GaugeName = MetricsAPI.GaugeUsersRegistered, Value = 1 }).ConfigureAwait(false);
|
||||
|
||||
_dbContext.RemoveRange(otherPairData);
|
||||
_dbContext.Remove(userEntry);
|
||||
@@ -66,7 +68,7 @@ namespace MareSynchronosServer.Hubs
|
||||
await _dbContext.SaveChangesAsync().ConfigureAwait(false);
|
||||
}
|
||||
|
||||
[Authorize(AuthenticationSchemes = SecretKeyAuthenticationHandler.AuthScheme)]
|
||||
[Authorize(AuthenticationSchemes = SecretKeyGrpcAuthenticationHandler.AuthScheme)]
|
||||
[HubMethodName(Api.InvokeUserGetOnlineCharacters)]
|
||||
public async Task<List<string>> GetOnlineCharacters()
|
||||
{
|
||||
@@ -88,7 +90,7 @@ namespace MareSynchronosServer.Hubs
|
||||
return otherEntries.Select(e => e.User.CharacterIdentification).Distinct().ToList();
|
||||
}
|
||||
|
||||
[Authorize(AuthenticationSchemes = SecretKeyAuthenticationHandler.AuthScheme)]
|
||||
[Authorize(AuthenticationSchemes = SecretKeyGrpcAuthenticationHandler.AuthScheme)]
|
||||
[HubMethodName(Api.InvokeUserGetPairedClients)]
|
||||
public async Task<List<ClientPairDto>> GetPairedClients()
|
||||
{
|
||||
@@ -126,7 +128,7 @@ namespace MareSynchronosServer.Hubs
|
||||
}).ToList();
|
||||
}
|
||||
|
||||
[Authorize(AuthenticationSchemes = SecretKeyAuthenticationHandler.AuthScheme)]
|
||||
[Authorize(AuthenticationSchemes = SecretKeyGrpcAuthenticationHandler.AuthScheme)]
|
||||
[HubMethodName(Api.InvokeUserPushCharacterDataToVisibleClients)]
|
||||
public async Task PushCharacterDataToVisibleClients(CharacterCacheDto characterCache, List<string> visibleCharacterIds)
|
||||
{
|
||||
@@ -158,11 +160,13 @@ namespace MareSynchronosServer.Hubs
|
||||
|
||||
await Clients.Users(otherEntries).SendAsync(Api.OnUserReceiveCharacterData, characterCache, user.CharacterIdentification).ConfigureAwait(false);
|
||||
|
||||
MareMetrics.UserPushData.Inc();
|
||||
MareMetrics.UserPushDataTo.Inc(otherEntries.Count);
|
||||
await _metricsClient.IncreaseCounterAsync(new IncreaseCounterRequest()
|
||||
{ CounterName = MetricsAPI.CounterUserPushData, Value = 1 }).ConfigureAwait(false);
|
||||
await _metricsClient.IncreaseCounterAsync(new IncreaseCounterRequest()
|
||||
{ CounterName = MetricsAPI.CounterUserPushDataTo, Value = otherEntries.Count }).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
[Authorize(AuthenticationSchemes = SecretKeyAuthenticationHandler.AuthScheme)]
|
||||
[Authorize(AuthenticationSchemes = SecretKeyGrpcAuthenticationHandler.AuthScheme)]
|
||||
[HubMethodName(Api.SendUserPairedClientAddition)]
|
||||
public async Task SendPairedClientAddition(string uid)
|
||||
{
|
||||
@@ -215,10 +219,10 @@ namespace MareSynchronosServer.Hubs
|
||||
}
|
||||
}
|
||||
|
||||
MareMetrics.Pairs.Inc();
|
||||
await _metricsClient.IncGaugeAsync(new GaugeRequest() {GaugeName = MetricsAPI.GaugePairs, Value = 1}).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
[Authorize(AuthenticationSchemes = SecretKeyAuthenticationHandler.AuthScheme)]
|
||||
[Authorize(AuthenticationSchemes = SecretKeyGrpcAuthenticationHandler.AuthScheme)]
|
||||
[HubMethodName(Api.SendUserPairedClientPauseChange)]
|
||||
public async Task SendPairedClientPauseChange(string otherUserUid, bool isPaused)
|
||||
{
|
||||
@@ -255,15 +259,15 @@ namespace MareSynchronosServer.Hubs
|
||||
|
||||
if (isPaused)
|
||||
{
|
||||
MareMetrics.PairsPaused.Inc();
|
||||
await _metricsClient.IncGaugeAsync(new GaugeRequest() { GaugeName = MetricsAPI.GaugePairsPaused, Value = 1 }).ConfigureAwait(false);
|
||||
}
|
||||
else
|
||||
{
|
||||
MareMetrics.PairsPaused.Dec();
|
||||
await _metricsClient.DecGaugeAsync(new GaugeRequest() { GaugeName = MetricsAPI.GaugePairsPaused, Value = 1 }).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
[Authorize(AuthenticationSchemes = SecretKeyAuthenticationHandler.AuthScheme)]
|
||||
[Authorize(AuthenticationSchemes = SecretKeyGrpcAuthenticationHandler.AuthScheme)]
|
||||
[HubMethodName(Api.SendUserPairedClientRemoval)]
|
||||
public async Task SendPairedClientRemoval(string uid)
|
||||
{
|
||||
@@ -303,7 +307,7 @@ namespace MareSynchronosServer.Hubs
|
||||
}
|
||||
}
|
||||
|
||||
MareMetrics.Pairs.Dec();
|
||||
await _metricsClient.DecGaugeAsync(new GaugeRequest() { GaugeName = MetricsAPI.GaugePairs, Value = 1 }).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
private ClientPair OppositeEntry(string otherUID) =>
|
||||
|
||||
@@ -4,10 +4,11 @@ using System.Security.Claims;
|
||||
using System.Security.Cryptography;
|
||||
using System.Threading.Tasks;
|
||||
using MareSynchronos.API;
|
||||
using MareSynchronosServer.Metrics;
|
||||
using MareSynchronosShared.Authentication;
|
||||
using MareSynchronosShared.Data;
|
||||
using MareSynchronosShared.Metrics;
|
||||
using MareSynchronosShared.Models;
|
||||
using MareSynchronosShared.Protos;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.SignalR;
|
||||
@@ -19,14 +20,19 @@ namespace MareSynchronosServer.Hubs
|
||||
{
|
||||
public partial class MareHub : Hub
|
||||
{
|
||||
private readonly MetricsService.MetricsServiceClient _metricsClient;
|
||||
private readonly AuthService.AuthServiceClient _authServiceClient;
|
||||
private readonly SystemInfoService _systemInfoService;
|
||||
private readonly IConfiguration _configuration;
|
||||
private readonly IHttpContextAccessor contextAccessor;
|
||||
private readonly ILogger<MareHub> _logger;
|
||||
private readonly MareDbContext _dbContext;
|
||||
|
||||
public MareHub(MareDbContext mareDbContext, ILogger<MareHub> logger, SystemInfoService systemInfoService, IConfiguration configuration, IHttpContextAccessor contextAccessor)
|
||||
public MareHub(MetricsService.MetricsServiceClient metricsClient, AuthService.AuthServiceClient authServiceClient,
|
||||
MareDbContext mareDbContext, ILogger<MareHub> logger, SystemInfoService systemInfoService, IConfiguration configuration, IHttpContextAccessor contextAccessor)
|
||||
{
|
||||
_metricsClient = metricsClient;
|
||||
_authServiceClient = authServiceClient;
|
||||
_systemInfoService = systemInfoService;
|
||||
_configuration = configuration;
|
||||
this.contextAccessor = contextAccessor;
|
||||
@@ -35,10 +41,10 @@ namespace MareSynchronosServer.Hubs
|
||||
}
|
||||
|
||||
[HubMethodName(Api.InvokeHeartbeat)]
|
||||
[Authorize(AuthenticationSchemes = SecretKeyAuthenticationHandler.AuthScheme)]
|
||||
[Authorize(AuthenticationSchemes = SecretKeyGrpcAuthenticationHandler.AuthScheme)]
|
||||
public async Task<ConnectionDto> Heartbeat(string characterIdentification)
|
||||
{
|
||||
MareMetrics.InitializedConnections.Inc();
|
||||
await _metricsClient.IncreaseCounterAsync(new() { CounterName = MetricsAPI.CounterInitializedConnections, Value = 1 }).ConfigureAwait(false);
|
||||
|
||||
var userId = Context.User!.Claims.SingleOrDefault(c => c.Type == ClaimTypes.NameIdentifier)?.Value;
|
||||
|
||||
@@ -60,7 +66,7 @@ namespace MareSynchronosServer.Hubs
|
||||
}
|
||||
else if (string.IsNullOrEmpty(user.CharacterIdentification))
|
||||
{
|
||||
MareMetrics.AuthorizedConnections.Inc();
|
||||
await _metricsClient.IncGaugeAsync(new GaugeRequest() { GaugeName = MetricsAPI.GaugeAuthorizedConnections, Value = 1 }).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
user.LastLoggedIn = DateTime.UtcNow;
|
||||
@@ -81,32 +87,34 @@ namespace MareSynchronosServer.Hubs
|
||||
};
|
||||
}
|
||||
|
||||
public override Task OnConnectedAsync()
|
||||
public override async Task OnConnectedAsync()
|
||||
{
|
||||
_logger.LogInformation("Connection from {ip}", contextAccessor.GetIpAddress());
|
||||
MareMetrics.Connections.Inc();
|
||||
return base.OnConnectedAsync();
|
||||
await _metricsClient.IncGaugeAsync(new GaugeRequest() { GaugeName = MetricsAPI.GaugeConnections, Value = 1 }).ConfigureAwait(false);
|
||||
await base.OnConnectedAsync().ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public override async Task OnDisconnectedAsync(Exception exception)
|
||||
{
|
||||
MareMetrics.Connections.Dec();
|
||||
await _metricsClient.DecGaugeAsync(new GaugeRequest() { GaugeName = MetricsAPI.GaugeConnections, Value = 1 }).ConfigureAwait(false);
|
||||
|
||||
var user = await _dbContext.Users.SingleOrDefaultAsync(u => u.UID == AuthenticatedUserId).ConfigureAwait(false);
|
||||
if (user != null && !string.IsNullOrEmpty(user.CharacterIdentification))
|
||||
{
|
||||
MareMetrics.AuthorizedConnections.Dec();
|
||||
await _metricsClient.DecGaugeAsync(new GaugeRequest() { GaugeName = MetricsAPI.GaugeAuthorizedConnections, Value = 1 }).ConfigureAwait(false);
|
||||
|
||||
_logger.LogInformation("Disconnect from {id}", AuthenticatedUserId);
|
||||
|
||||
var query =
|
||||
from userToOther in _dbContext.ClientPairs
|
||||
join otherToUser in _dbContext.ClientPairs
|
||||
on new {
|
||||
on new
|
||||
{
|
||||
user = userToOther.UserUID,
|
||||
other = userToOther.OtherUserUID
|
||||
|
||||
} equals new {
|
||||
} equals new
|
||||
{
|
||||
user = otherToUser.OtherUserUID,
|
||||
other = otherToUser.UserUID
|
||||
}
|
||||
@@ -118,7 +126,7 @@ namespace MareSynchronosServer.Hubs
|
||||
var otherEntries = await query.ToListAsync().ConfigureAwait(false);
|
||||
|
||||
await Clients.Users(otherEntries).SendAsync(Api.OnUserRemoveOnlinePairedPlayer, user.CharacterIdentification).ConfigureAwait(false);
|
||||
|
||||
|
||||
_dbContext.RemoveRange(_dbContext.Files.Where(f => !f.Uploaded && f.UploaderUID == user.UID));
|
||||
|
||||
user.CharacterIdentification = null;
|
||||
@@ -128,24 +136,6 @@ namespace MareSynchronosServer.Hubs
|
||||
await base.OnDisconnectedAsync(exception).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public static string GenerateRandomString(int length, string allowableChars = null)
|
||||
{
|
||||
if (string.IsNullOrEmpty(allowableChars))
|
||||
allowableChars = @"ABCDEFGHJKLMNPQRSTUVWXYZ0123456789";
|
||||
|
||||
// Generate random data
|
||||
var rnd = RandomNumberGenerator.GetBytes(length);
|
||||
|
||||
// Generate the output string
|
||||
var allowable = allowableChars.ToCharArray();
|
||||
var l = allowable.Length;
|
||||
var chars = new char[length];
|
||||
for (var i = 0; i < length; i++)
|
||||
chars[i] = allowable[rnd[i] % l];
|
||||
|
||||
return new string(chars);
|
||||
}
|
||||
|
||||
protected string AuthenticatedUserId => Context.User?.Claims?.SingleOrDefault(c => c.Type == ClaimTypes.NameIdentifier)?.Value ?? "Unknown";
|
||||
|
||||
protected async Task<User> GetAuthenticatedUserUntrackedAsync()
|
||||
|
||||
@@ -1,22 +1,22 @@
|
||||
using AspNetCoreRateLimit;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.SignalR;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
using System;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Security.Claims;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using AspNetCoreRateLimit;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.SignalR;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace MareSynchronosServer.Throttling;
|
||||
namespace MareSynchronosServer.Hubs;
|
||||
public class SignalRLimitFilter : IHubFilter
|
||||
{
|
||||
private readonly IRateLimitProcessor _processor;
|
||||
private readonly IHttpContextAccessor accessor;
|
||||
private readonly ILogger<SignalRLimitFilter> logger;
|
||||
private static SemaphoreSlim ConnectionLimiterSemaphore = new(20);
|
||||
private static SemaphoreSlim DisconnectLimiterSemaphore = new(20);
|
||||
private static readonly SemaphoreSlim ConnectionLimiterSemaphore = new(20);
|
||||
private static readonly SemaphoreSlim DisconnectLimiterSemaphore = new(20);
|
||||
|
||||
public SignalRLimitFilter(
|
||||
IOptions<IpRateLimitOptions> options, IProcessingStrategy processing, IIpPolicyStore policyStore, IHttpContextAccessor accessor, ILogger<SignalRLimitFilter> logger)
|
||||
@@ -10,8 +10,9 @@
|
||||
<PackageReference Include="AspNetCoreRateLimit" Version="4.0.2" />
|
||||
<PackageReference Include="Bazinga.AspNetCore.Authentication.Basic" Version="2.0.1" />
|
||||
<PackageReference Include="Ben.BlockingDetector" Version="0.0.4" />
|
||||
<PackageReference Include="Discord.Net" Version="3.7.2" />
|
||||
<PackageReference Include="EFCore.NamingConventions" Version="6.0.0" />
|
||||
<PackageReference Include="Grpc.AspNetCore" Version="2.40.0" />
|
||||
<PackageReference Include="Grpc.Net.Client" Version="2.47.0" />
|
||||
<PackageReference Include="Karambolo.Extensions.Logging.File" Version="3.3.1" />
|
||||
<PackageReference Include="lz4net" Version="1.0.15.93" />
|
||||
<PackageReference Include="Meziantou.Analyzer" Version="1.0.715">
|
||||
@@ -27,7 +28,6 @@
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.Extensions.Hosting.Systemd" Version="6.0.0" />
|
||||
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="6.0.6" />
|
||||
<PackageReference Include="prometheus-net.AspNetCore" Version="6.0.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
@@ -6,7 +6,6 @@ using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using MareSynchronosServer.Metrics;
|
||||
using System.Collections.Generic;
|
||||
using MareSynchronosShared.Data;
|
||||
|
||||
@@ -37,8 +36,6 @@ namespace MareSynchronosServer
|
||||
context.RemoveRange(unfinishedRegistrations);
|
||||
context.RemoveRange(looseFiles);
|
||||
context.SaveChanges();
|
||||
|
||||
MareMetrics.InitializeMetrics(context, services.GetRequiredService<IConfiguration>());
|
||||
}
|
||||
|
||||
if (args.Length == 0 || args[0] != "dry")
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
using System;
|
||||
using MareSynchronos.API;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
@@ -9,15 +10,12 @@ using MareSynchronosServer.Hubs;
|
||||
using Microsoft.AspNetCore.Authentication;
|
||||
using Microsoft.AspNetCore.Http.Connections;
|
||||
using Microsoft.AspNetCore.SignalR;
|
||||
using Prometheus;
|
||||
using Microsoft.Extensions.FileProviders;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using MareSynchronosServer.Discord;
|
||||
using AspNetCoreRateLimit;
|
||||
using MareSynchronosServer.Throttling;
|
||||
using Ben.Diagnostics;
|
||||
using MareSynchronosShared.Authentication;
|
||||
using MareSynchronosShared.Data;
|
||||
using MareSynchronosShared.Protos;
|
||||
|
||||
namespace MareSynchronosServer
|
||||
{
|
||||
@@ -47,6 +45,15 @@ namespace MareSynchronosServer
|
||||
services.AddSingleton<IUserIdProvider, IdBasedUserIdProvider>();
|
||||
services.AddTransient(_ => Configuration);
|
||||
|
||||
services.AddGrpcClient<AuthService.AuthServiceClient>(c =>
|
||||
{
|
||||
c.Address = new Uri(Configuration.GetValue<string>("ServiceAddress"));
|
||||
});
|
||||
services.AddGrpcClient<MetricsService.MetricsServiceClient>(c =>
|
||||
{
|
||||
c.Address = new Uri(Configuration.GetValue<string>("ServiceAddress"));
|
||||
});
|
||||
|
||||
services.AddDbContextPool<MareDbContext>(options =>
|
||||
{
|
||||
options.UseNpgsql(Configuration.GetConnectionString("DefaultConnection"), builder =>
|
||||
@@ -56,15 +63,13 @@ namespace MareSynchronosServer
|
||||
options.EnableThreadSafetyChecks(false);
|
||||
}, Configuration.GetValue("DbContextPoolSize", 1024));
|
||||
|
||||
services.AddHostedService<CleanupService>();
|
||||
services.AddHostedService(provider => provider.GetService<SystemInfoService>());
|
||||
services.AddHostedService<DiscordBot>();
|
||||
|
||||
services.AddAuthentication(options =>
|
||||
{
|
||||
options.DefaultScheme = SecretKeyAuthenticationHandler.AuthScheme;
|
||||
options.DefaultScheme = SecretKeyGrpcAuthenticationHandler.AuthScheme;
|
||||
})
|
||||
.AddScheme<AuthenticationSchemeOptions, SecretKeyAuthenticationHandler>(SecretKeyAuthenticationHandler.AuthScheme, options => { });
|
||||
.AddScheme<AuthenticationSchemeOptions, SecretKeyGrpcAuthenticationHandler>(SecretKeyGrpcAuthenticationHandler.AuthScheme, options => { });
|
||||
services.AddAuthorization(options => options.FallbackPolicy = new AuthorizationPolicyBuilder().RequireAuthenticatedUser().Build());
|
||||
|
||||
services.AddSingleton<IRateLimitConfiguration, RateLimitConfiguration>();
|
||||
@@ -98,27 +103,13 @@ namespace MareSynchronosServer
|
||||
|
||||
app.UseIpRateLimiting();
|
||||
|
||||
app.UseStaticFiles();
|
||||
app.UseHttpLogging();
|
||||
|
||||
app.UseRouting();
|
||||
|
||||
app.UseHttpMetrics();
|
||||
app.UseWebSockets();
|
||||
|
||||
app.UseAuthentication();
|
||||
app.UseAuthorization();
|
||||
|
||||
var metricServer = new KestrelMetricServer(4980);
|
||||
metricServer.Start();
|
||||
|
||||
app.UseStaticFiles(new StaticFileOptions()
|
||||
{
|
||||
FileProvider = new PhysicalFileProvider(Configuration["CacheDirectory"]),
|
||||
RequestPath = "/cache",
|
||||
ServeUnknownFileTypes = true
|
||||
});
|
||||
|
||||
app.UseEndpoints(endpoints =>
|
||||
{
|
||||
endpoints.MapHub<MareHub>(Api.Path, options =>
|
||||
|
||||
@@ -4,8 +4,9 @@ using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using MareSynchronos.API;
|
||||
using MareSynchronosServer.Hubs;
|
||||
using MareSynchronosServer.Metrics;
|
||||
using MareSynchronosShared.Data;
|
||||
using MareSynchronosShared.Metrics;
|
||||
using MareSynchronosShared.Protos;
|
||||
using Microsoft.AspNetCore.SignalR;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
@@ -41,11 +42,15 @@ public class SystemInfoService : IHostedService, IDisposable
|
||||
{
|
||||
ThreadPool.GetAvailableThreads(out int workerThreads, out int ioThreads);
|
||||
_logger.LogInformation("ThreadPool: {workerThreads} workers available, {ioThreads} IO workers available", workerThreads, ioThreads);
|
||||
MareMetrics.AvailableWorkerThreads.Set(workerThreads);
|
||||
MareMetrics.AvailableIOWorkerThreads.Set(ioThreads);
|
||||
|
||||
using var scope = _services.CreateScope();
|
||||
using var db = scope.ServiceProvider.GetService<MareDbContext>();
|
||||
using var db = scope.ServiceProvider.GetService<MareDbContext>()!;
|
||||
|
||||
var metricsServiceClient = scope.ServiceProvider.GetService<MetricsService.MetricsServiceClient>()!;
|
||||
_ = metricsServiceClient.SetGauge(new SetGaugeRequest()
|
||||
{ GaugeName = MetricsAPI.GaugeAvailableWorkerThreads, Value = workerThreads });
|
||||
_ = metricsServiceClient.SetGauge(new SetGaugeRequest()
|
||||
{ GaugeName = MetricsAPI.GaugeAvailableIOWorkerThreads, Value = ioThreads });
|
||||
|
||||
var users = db.Users.Count(c => c.CharacterIdentification != null);
|
||||
|
||||
|
||||
@@ -25,14 +25,9 @@
|
||||
},
|
||||
"DbContextPoolSize": 2000,
|
||||
"CdnFullUrl": "https://<url or ip to your server>/cache/",
|
||||
"FailedAuthForTempBan": 5,
|
||||
"TempBanDurationInMinutes": 30,
|
||||
"DiscordBotToken": "",
|
||||
"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
|
||||
"CacheSizeHardLimitInGiB": -1,
|
||||
"ServicesUrl": "http://localhost:5002",
|
||||
|
||||
"AllowedHosts": "*",
|
||||
"Kestrel": {
|
||||
"Endpoints": {
|
||||
|
||||
Reference in New Issue
Block a user