add speedtest stuff or so

This commit is contained in:
Stanley Dimant
2024-11-11 12:45:52 +01:00
parent 0efd94d781
commit 2f4aa1b396
5 changed files with 84 additions and 4 deletions

View File

@@ -9,7 +9,6 @@ using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using StackExchange.Redis.Extensions.Core.Abstractions; using StackExchange.Redis.Extensions.Core.Abstractions;
using System.Security.Cryptography;
namespace MareSynchronosAuthService.Controllers; namespace MareSynchronosAuthService.Controllers;

View File

@@ -20,6 +20,7 @@ public class StaticFilesServerConfiguration : MareConfigurationBase
public bool UseColdStorage { get; set; } = false; public bool UseColdStorage { get; set; } = false;
public string ColdStorageDirectory { get; set; } = null; public string ColdStorageDirectory { get; set; } = null;
public double ColdStorageSizeHardLimitInGiB { get; set; } = -1; public double ColdStorageSizeHardLimitInGiB { get; set; } = -1;
public int SpeedTestHoursRateLimit { get; set; } = 6;
public int ColdStorageUnusedFileRetentionPeriodInDays { get; set; } = 30; public int ColdStorageUnusedFileRetentionPeriodInDays { get; set; } = 30;
[RemoteConfiguration] [RemoteConfiguration]
public Uri CdnFullUrl { get; set; } = null; public Uri CdnFullUrl { get; set; } = null;

View File

@@ -130,6 +130,20 @@ public class ServerFilesController : ControllerBase
return Ok(JsonSerializer.Serialize(response)); return Ok(JsonSerializer.Serialize(response));
} }
[HttpGet(MareFiles.ServerFiles_DownloadServers)]
public async Task<IActionResult> GetDownloadServers()
{
var allFileShards = new List<CdnShardConfiguration>(_configuration.GetValueOrDefault(nameof(StaticFilesServerConfiguration.CdnShardConfiguration), new List<CdnShardConfiguration>()))
.DistinctBy(f => f.CdnFullUrl).ToList();
if (!allFileShards.Any())
{
return Ok(JsonSerializer.Serialize(new List<string> { _configuration.GetValue<Uri>(nameof(StaticFilesServerConfiguration.CdnFullUrl)).ToString() }));
}
var selectedShards = allFileShards.Where(c => c.Continents.Contains(Continent, StringComparer.OrdinalIgnoreCase)).ToList();
if (!selectedShards.Any()) selectedShards = allFileShards.Where(c => c.Continents.Contains("*", StringComparer.Ordinal)).ToList();
return Ok(JsonSerializer.Serialize(selectedShards.Select(t => t.CdnFullUrl.ToString())));
}
[HttpPost(MareFiles.ServerFiles_FilesSend)] [HttpPost(MareFiles.ServerFiles_FilesSend)]
public async Task<IActionResult> FilesSend([FromBody] FilesSendDto filesSendDto) public async Task<IActionResult> FilesSend([FromBody] FilesSendDto filesSendDto)
{ {

View File

@@ -0,0 +1,64 @@
using MareSynchronos.API.Routes;
using MareSynchronosShared;
using MareSynchronosShared.Services;
using MareSynchronosShared.Utils.Configuration;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;
namespace MareSynchronosStaticFilesServer.Controllers;
[Route(MareFiles.Speedtest)]
public class SpeedTestController : ControllerBase
{
private readonly IMemoryCache _memoryCache;
private readonly IHttpContextAccessor _httpContextAccessor;
private readonly IConfigurationService<StaticFilesServerConfiguration> _configurationService;
private const string RandomByteDataName = "SpeedTestRandomByteData";
private static readonly SemaphoreSlim _speedtestSemaphore = new(10, 10);
public SpeedTestController(ILogger<SpeedTestController> logger,
IMemoryCache memoryCache, IHttpContextAccessor httpContextAccessor,
IConfigurationService<StaticFilesServerConfiguration> configurationService) : base(logger)
{
_memoryCache = memoryCache;
_httpContextAccessor = httpContextAccessor;
_configurationService = configurationService;
}
[HttpGet(MareFiles.Speedtest_Run)]
public async Task<IActionResult> DownloadTest(CancellationToken cancellationToken)
{
var ip = _httpContextAccessor.GetIpAddress();
var speedtestLimit = _configurationService.GetValueOrDefault(nameof(StaticFilesServerConfiguration.SpeedTestHoursRateLimit), 6);
if (_memoryCache.TryGetValue<DateTime>(ip, out var value))
{
var hoursRemaining = value.Subtract(DateTime.UtcNow).TotalHours;
return StatusCode(429, $"Can perform speedtest every {speedtestLimit} hours. {hoursRemaining:F2} hours remain.");
}
await _speedtestSemaphore.WaitAsync(cancellationToken);
try
{
var expiry = DateTime.UtcNow.Add(TimeSpan.FromHours(speedtestLimit));
_memoryCache.Set(ip, expiry, TimeSpan.FromHours(speedtestLimit));
var randomByteData = _memoryCache.GetOrCreate(RandomByteDataName, (entry) =>
{
byte[] data = new byte[10 * 1024 * 1024];
new Random().NextBytes(data);
return data;
});
return File(randomByteData, "application/octet-stream", "speedtest.dat");
}
catch (OperationCanceledException)
{
return StatusCode(499, "Cancelled");
}
finally
{
_speedtestSemaphore.Release();
}
}
}

View File

@@ -186,6 +186,8 @@ public class Startup
services.AddHostedService(p => (MareConfigurationServiceClient<StaticFilesServerConfiguration>)p.GetService<IConfigurationService<StaticFilesServerConfiguration>>()); services.AddHostedService(p => (MareConfigurationServiceClient<StaticFilesServerConfiguration>)p.GetService<IConfigurationService<StaticFilesServerConfiguration>>());
} }
services.AddMemoryCache();
// controller setup // controller setup
services.AddControllers().ConfigureApplicationPartManager(a => services.AddControllers().ConfigureApplicationPartManager(a =>
{ {
@@ -194,15 +196,15 @@ public class Startup
{ {
a.FeatureProviders.Add(new AllowedControllersFeatureProvider(typeof(MareStaticFilesServerConfigurationController), a.FeatureProviders.Add(new AllowedControllersFeatureProvider(typeof(MareStaticFilesServerConfigurationController),
typeof(CacheController), typeof(RequestController), typeof(ServerFilesController), typeof(CacheController), typeof(RequestController), typeof(ServerFilesController),
typeof(DistributionController), typeof(MainController))); typeof(DistributionController), typeof(MainController), typeof(SpeedTestController)));
} }
else if (_isDistributionNode) else if (_isDistributionNode)
{ {
a.FeatureProviders.Add(new AllowedControllersFeatureProvider(typeof(CacheController), typeof(RequestController), typeof(DistributionController))); a.FeatureProviders.Add(new AllowedControllersFeatureProvider(typeof(CacheController), typeof(RequestController), typeof(DistributionController), typeof(SpeedTestController)));
} }
else else
{ {
a.FeatureProviders.Add(new AllowedControllersFeatureProvider(typeof(CacheController), typeof(RequestController))); a.FeatureProviders.Add(new AllowedControllersFeatureProvider(typeof(CacheController), typeof(RequestController), typeof(SpeedTestController)));
} }
}); });