adjustments to config and cleanup nuget, remove implicit usings
This commit is contained in:
@@ -1,4 +1,8 @@
|
||||
namespace MareSynchronosServices.Authentication;
|
||||
using System;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MareSynchronosServices.Authentication;
|
||||
|
||||
public class FailedAuthorization : IDisposable
|
||||
{
|
||||
|
||||
@@ -1,10 +1,17 @@
|
||||
using System.Security.Cryptography;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using MareSynchronosServices.Metrics;
|
||||
using MareSynchronosShared.Data;
|
||||
using MareSynchronosShared.Metrics;
|
||||
using MareSynchronosShared.Protos;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace MareSynchronosServices.Authentication;
|
||||
|
||||
@@ -17,8 +24,8 @@ public class SecretKeyAuthenticationHandler
|
||||
private readonly Dictionary<string, FailedAuthorization?> failedAuthorizations = new();
|
||||
private readonly object authDictLock = new();
|
||||
private readonly object failedAuthLock = new();
|
||||
private readonly int failedAttemptsForTempBan;
|
||||
private readonly int tempBanMinutes;
|
||||
private readonly int _failedAttemptsForTempBan;
|
||||
private readonly int _tempBanMinutes;
|
||||
|
||||
public void ClearUnauthorizedUsers()
|
||||
{
|
||||
@@ -58,7 +65,7 @@ public class SecretKeyAuthenticationHandler
|
||||
|
||||
lock (failedAuthLock)
|
||||
{
|
||||
if (failedAuthorizations.TryGetValue(ip, out var existingFailedAuthorization) && existingFailedAuthorization.FailedAttempts > failedAttemptsForTempBan)
|
||||
if (failedAuthorizations.TryGetValue(ip, out var existingFailedAuthorization) && existingFailedAuthorization.FailedAttempts > _failedAttemptsForTempBan)
|
||||
{
|
||||
metrics.IncCounter(MetricsAPI.CounterAuthenticationFailures);
|
||||
|
||||
@@ -68,7 +75,7 @@ public class SecretKeyAuthenticationHandler
|
||||
var token = existingFailedAuthorization.ResetCts.Token;
|
||||
existingFailedAuthorization.ResetTask = Task.Run(async () =>
|
||||
{
|
||||
await Task.Delay(TimeSpan.FromMinutes(tempBanMinutes), token).ConfigureAwait(false);
|
||||
await Task.Delay(TimeSpan.FromMinutes(_tempBanMinutes), token).ConfigureAwait(false);
|
||||
if (token.IsCancellationRequested) return;
|
||||
FailedAuthorization? failedAuthorization;
|
||||
lock (failedAuthLock)
|
||||
@@ -159,7 +166,8 @@ public class SecretKeyAuthenticationHandler
|
||||
{
|
||||
this.logger = logger;
|
||||
this.metrics = metrics;
|
||||
failedAttemptsForTempBan = configuration.GetValue<int>("FailedAuthForTempBan", 5);
|
||||
tempBanMinutes = configuration.GetValue<int>("TempBanDurationInMinutes", 30);
|
||||
var config = configuration.GetRequiredSection("MareSynchronos");
|
||||
_failedAttemptsForTempBan = config.GetValue<int>("FailedAuthForTempBan", 5);
|
||||
_tempBanMinutes = config.GetValue<int>("TempBanDurationInMinutes", 30);
|
||||
}
|
||||
}
|
||||
@@ -3,9 +3,17 @@ using MareSynchronosServices.Metrics;
|
||||
using MareSynchronosShared.Data;
|
||||
using MareSynchronosShared.Metrics;
|
||||
using MareSynchronosShared.Models;
|
||||
using MareSynchronosShared.Protos;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using MetricsService = MareSynchronosShared.Protos.MetricsService;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MareSynchronosServices
|
||||
{
|
||||
@@ -16,7 +24,7 @@ namespace MareSynchronosServices
|
||||
private readonly ILogger<CleanupService> _logger;
|
||||
private readonly IServiceProvider _services;
|
||||
private readonly IConfiguration _configuration;
|
||||
private Timer _timer;
|
||||
private Timer? _timer;
|
||||
|
||||
public CleanupService(MareMetrics metrics, SecretKeyAuthenticationHandler authService, ILogger<CleanupService> logger, IServiceProvider services, IConfiguration configuration)
|
||||
{
|
||||
@@ -24,7 +32,7 @@ namespace MareSynchronosServices
|
||||
_authService = authService;
|
||||
_logger = logger;
|
||||
_services = services;
|
||||
_configuration = configuration;
|
||||
_configuration = configuration.GetRequiredSection("MareSynchronos");
|
||||
}
|
||||
|
||||
public Task StartAsync(CancellationToken cancellationToken)
|
||||
@@ -158,7 +166,7 @@ namespace MareSynchronosServices
|
||||
|
||||
foreach (var user in usersToRemove)
|
||||
{
|
||||
PurgeUser(user, dbContext, _configuration);
|
||||
PurgeUser(user, dbContext);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -176,7 +184,7 @@ namespace MareSynchronosServices
|
||||
dbContext.SaveChanges();
|
||||
}
|
||||
|
||||
public void PurgeUser(User user, MareDbContext dbContext, IConfiguration _configuration)
|
||||
public void PurgeUser(User user, MareDbContext dbContext)
|
||||
{
|
||||
var lodestone = dbContext.LodeStoneAuth.SingleOrDefault(a => a.User.UID == user.UID);
|
||||
|
||||
@@ -190,17 +198,6 @@ namespace MareSynchronosServices
|
||||
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)
|
||||
{
|
||||
metrics.DecGaugeBy(MetricsAPI.GaugeFilesTotalSize, fi.Length);
|
||||
metrics.DecGaugeBy(MetricsAPI.GaugeFilesTotal, 1);
|
||||
fi.Delete();
|
||||
}
|
||||
}
|
||||
|
||||
dbContext.Files.RemoveRange(userFiles);
|
||||
|
||||
var ownPairData = dbContext.ClientPairs.Where(u => u.User.UID == user.UID).ToList();
|
||||
|
||||
@@ -1,7 +1,12 @@
|
||||
using System.Collections.Concurrent;
|
||||
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 Discord;
|
||||
using Discord.WebSocket;
|
||||
using MareSynchronosServices.Metrics;
|
||||
@@ -9,6 +14,10 @@ using MareSynchronosShared.Data;
|
||||
using MareSynchronosShared.Metrics;
|
||||
using MareSynchronosShared.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace MareSynchronosServices.Discord;
|
||||
|
||||
@@ -17,14 +26,14 @@ public class DiscordBot : IHostedService
|
||||
private readonly CleanupService cleanupService;
|
||||
private readonly MareMetrics metrics;
|
||||
private readonly IServiceProvider services;
|
||||
private readonly IConfiguration configuration;
|
||||
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 CancellationTokenSource? verificationTaskCts;
|
||||
private CancellationTokenSource? updateStatusCts;
|
||||
private readonly string[] LodestoneServers = new[] { "eu", "na", "jp", "fr", "de" };
|
||||
private readonly ConcurrentQueue<SocketSlashCommand> verificationQueue = new();
|
||||
|
||||
@@ -35,13 +44,13 @@ public class DiscordBot : IHostedService
|
||||
this.cleanupService = cleanupService;
|
||||
this.metrics = metrics;
|
||||
this.services = services;
|
||||
this.configuration = configuration;
|
||||
_configuration = configuration.GetRequiredSection("MareSynchronos");
|
||||
this.logger = logger;
|
||||
this.verificationQueue = new ConcurrentQueue<SocketSlashCommand>();
|
||||
this.semaphore = new SemaphoreSlim(1);
|
||||
|
||||
random = new();
|
||||
authToken = configuration.GetValue<string>("DiscordBotToken");
|
||||
authToken = _configuration.GetValue<string>("DiscordBotToken");
|
||||
|
||||
discordClient = new(new DiscordSocketConfig()
|
||||
{
|
||||
@@ -110,7 +119,7 @@ public class DiscordBot : IHostedService
|
||||
{
|
||||
if (discordAuthedUser.User != null)
|
||||
{
|
||||
cleanupService.PurgeUser(discordAuthedUser.User, db, configuration);
|
||||
cleanupService.PurgeUser(discordAuthedUser.User, db);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -168,9 +177,9 @@ public class DiscordBot : IHostedService
|
||||
user.IsAdmin = true;
|
||||
}
|
||||
|
||||
if (configuration.GetValue<bool>("PurgeUnusedAccounts"))
|
||||
if (_configuration.GetValue<bool>("PurgeUnusedAccounts"))
|
||||
{
|
||||
var purgedDays = configuration.GetValue<int>("PurgeUnusedAccountsPeriodInDays");
|
||||
var purgedDays = _configuration.GetValue<int>("PurgeUnusedAccountsPeriodInDays");
|
||||
user.LastLoggedIn = DateTime.UtcNow - TimeSpan.FromDays(purgedDays) + TimeSpan.FromDays(1);
|
||||
}
|
||||
|
||||
@@ -360,7 +369,7 @@ public class DiscordBot : IHostedService
|
||||
{
|
||||
if (!string.IsNullOrEmpty(authToken))
|
||||
{
|
||||
authToken = configuration.GetValue<string>("DiscordBotToken");
|
||||
authToken = _configuration.GetValue<string>("DiscordBotToken");
|
||||
|
||||
await discordClient.LoginAsync(TokenType.Bot, authToken).ConfigureAwait(false);
|
||||
await discordClient.StartAsync().ConfigureAwait(false);
|
||||
@@ -423,7 +432,7 @@ public class DiscordBot : IHostedService
|
||||
await discordClient.StopAsync().ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public static string GenerateRandomString(int length, string allowableChars = null)
|
||||
public static string GenerateRandomString(int length, string? allowableChars = null)
|
||||
{
|
||||
if (string.IsNullOrEmpty(allowableChars))
|
||||
allowableChars = @"ABCDEFGHJKLMNPQRSTUVWXYZ0123456789";
|
||||
|
||||
@@ -2,21 +2,13 @@
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Discord.Net" Version="3.7.2" />
|
||||
<PackageReference Include="EFCore.NamingConventions" Version="6.0.0" />
|
||||
<PackageReference Include="Grpc.AspNetCore" Version="2.47.0" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.8" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.8">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</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>
|
||||
|
||||
|
||||
@@ -1,7 +1,11 @@
|
||||
using MareSynchronosShared.Data;
|
||||
using MareSynchronosShared.Metrics;
|
||||
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Prometheus;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace MareSynchronosServices.Metrics;
|
||||
|
||||
@@ -16,7 +20,6 @@ public class MareMetrics
|
||||
gauges[MetricsAPI.GaugePairs].IncTo(dbContext.ClientPairs.Count());
|
||||
gauges[MetricsAPI.GaugePairsPaused].IncTo(dbContext.ClientPairs.Count(p => p.IsPaused));
|
||||
gauges[MetricsAPI.GaugeFilesTotal].IncTo(dbContext.Files.Count());
|
||||
gauges[MetricsAPI.GaugeFilesTotalSize].IncTo(Directory.EnumerateFiles(configuration["CacheDirectory"]).Sum(f => new FileInfo(f).Length));
|
||||
}
|
||||
|
||||
private readonly Dictionary<string, Counter> counters = new()
|
||||
|
||||
@@ -1,4 +1,7 @@
|
||||
using MareSynchronosServices;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using System;
|
||||
|
||||
public class Program
|
||||
{
|
||||
|
||||
@@ -2,6 +2,8 @@ using Grpc.Core;
|
||||
using MareSynchronosServices.Authentication;
|
||||
using MareSynchronosShared.Data;
|
||||
using MareSynchronosShared.Protos;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MareSynchronosServices.Services
|
||||
{
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
using Grpc.Core;
|
||||
using MareSynchronosServices.Metrics;
|
||||
using MareSynchronosShared.Protos;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MareSynchronosServices.Services;
|
||||
|
||||
|
||||
@@ -5,7 +5,11 @@ using MareSynchronosServices.Metrics;
|
||||
using MareSynchronosServices.Services;
|
||||
using MareSynchronosShared.Authentication;
|
||||
using MareSynchronosShared.Data;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Prometheus;
|
||||
|
||||
namespace MareSynchronosServices;
|
||||
|
||||
@@ -16,12 +16,13 @@
|
||||
}
|
||||
}
|
||||
},
|
||||
"DbContextPoolSize": 1024,
|
||||
"DiscordBotToken": "",
|
||||
"PurgeUnusedAccounts": true,
|
||||
"PurgeUnusedAccountsPeriodInDays": 14,
|
||||
"CacheDirectory": "G:\\ServerTest", // do not delete this key and set it to the path where the files will be stored
|
||||
"FailedAuthForTempBan": 5,
|
||||
"TempBanDurationInMinutes": 30,
|
||||
"MareSynchronos": {
|
||||
"DbContextPoolSize": 1024,
|
||||
"DiscordBotToken": "",
|
||||
"PurgeUnusedAccounts": true,
|
||||
"PurgeUnusedAccountsPeriodInDays": 14,
|
||||
"FailedAuthForTempBan": 5,
|
||||
"TempBanDurationInMinutes": 30
|
||||
},
|
||||
"AllowedHosts": "*"
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user