135 lines
4.6 KiB
C#
135 lines
4.6 KiB
C#
using System.Globalization;
|
|
using Flypaper.Core.Strategies;
|
|
|
|
namespace Flypaper.Core.Settings;
|
|
|
|
public static class RuntimeSettingsParser
|
|
{
|
|
private const int MaxLimit = 100_000;
|
|
private const int MaxRetentionDays = 3650;
|
|
|
|
private static readonly TimeSpan MinHold = TimeSpan.FromSeconds(1);
|
|
private static readonly TimeSpan MaxHold = TimeSpan.FromDays(7);
|
|
private const int MinDelayMs = 10;
|
|
private const int MaxDelayMs = 30_000;
|
|
private const int MaxChunkSize = 4096;
|
|
|
|
public static RuntimeSettingsParseResult Parse(RuntimeSettingsInput input)
|
|
{
|
|
var errors = new Dictionary<string, string>();
|
|
|
|
var maxConnections = ParseInt(input.MaxConnections, nameof(input.MaxConnections), 1, MaxLimit, errors);
|
|
var perIp = ParseInt(input.MaxConnectionsPerIp, nameof(input.MaxConnectionsPerIp), 1, MaxLimit, errors);
|
|
var retention = ParseInt(input.RetentionDays, nameof(input.RetentionDays), 0, MaxRetentionDays, errors);
|
|
|
|
if (maxConnections is { } total && perIp is { } single && single > total
|
|
&& !errors.ContainsKey(nameof(input.MaxConnectionsPerIp)))
|
|
{
|
|
errors[nameof(input.MaxConnectionsPerIp)] = "Darf nicht größer sein als die Gesamtzahl der Verbindungen.";
|
|
}
|
|
|
|
if (errors.Count > 0)
|
|
{
|
|
return new RuntimeSettingsParseResult(null, errors);
|
|
}
|
|
|
|
var settings = new RuntimeSettings(
|
|
maxConnections!.Value,
|
|
perIp!.Value,
|
|
retention!.Value,
|
|
input.IgnorePrivateNetworks);
|
|
return new RuntimeSettingsParseResult(settings, errors);
|
|
}
|
|
|
|
public static HoldMethodParseResult ParseHoldMethod(HoldMethodInput input)
|
|
{
|
|
var errors = new Dictionary<string, string>();
|
|
|
|
DripContent? content = Enum.TryParse<DripContent>(input.Content, ignoreCase: true, out var parsed) && Enum.IsDefined(parsed)
|
|
? parsed
|
|
: null;
|
|
if (content is null)
|
|
{
|
|
errors[nameof(input.Content)] = "Bitte eine Methode wählen.";
|
|
}
|
|
|
|
var minDelay = ParseInt(input.MinDelayMs, nameof(input.MinDelayMs), MinDelayMs, MaxDelayMs, errors);
|
|
var maxDelay = ParseInt(input.MaxDelayMs, nameof(input.MaxDelayMs), MinDelayMs, MaxDelayMs, errors);
|
|
var minChunk = ParseInt(input.MinChunkSize, nameof(input.MinChunkSize), 1, MaxChunkSize, errors);
|
|
var maxChunk = ParseInt(input.MaxChunkSize, nameof(input.MaxChunkSize), 1, MaxChunkSize, errors);
|
|
var hold = ParseTimeSpan(input.MaxHoldDuration, nameof(input.MaxHoldDuration), MinHold, MaxHold, errors);
|
|
|
|
if (minDelay > maxDelay)
|
|
{
|
|
errors[nameof(input.MaxDelayMs)] = "Darf nicht kleiner sein als das Minimum.";
|
|
}
|
|
|
|
if (minChunk > maxChunk)
|
|
{
|
|
errors[nameof(input.MaxChunkSize)] = "Darf nicht kleiner sein als das Minimum.";
|
|
}
|
|
|
|
if (errors.Count > 0)
|
|
{
|
|
return new HoldMethodParseResult(null, errors);
|
|
}
|
|
|
|
return new HoldMethodParseResult(
|
|
new HoldMethodSettings(
|
|
content!.Value,
|
|
TimeSpan.FromMilliseconds(minDelay!.Value),
|
|
TimeSpan.FromMilliseconds(maxDelay!.Value),
|
|
minChunk!.Value,
|
|
maxChunk!.Value,
|
|
hold!.Value),
|
|
errors);
|
|
}
|
|
|
|
private static TimeSpan? ParseTimeSpan(
|
|
string? raw,
|
|
string field,
|
|
TimeSpan min,
|
|
TimeSpan max,
|
|
Dictionary<string, string> errors)
|
|
{
|
|
var text = raw?.Trim();
|
|
if (string.IsNullOrEmpty(text))
|
|
{
|
|
errors[field] = "Pflichtfeld.";
|
|
return null;
|
|
}
|
|
|
|
if (!text.Contains(':') || !TimeSpan.TryParse(text, CultureInfo.InvariantCulture, out var value))
|
|
{
|
|
errors[field] = "Format hh:mm:ss, zum Beispiel 00:00:10.";
|
|
return null;
|
|
}
|
|
|
|
if (value < min || value > max)
|
|
{
|
|
errors[field] = $"Zwischen {min.ToString("c", CultureInfo.InvariantCulture)} und {max.ToString("c", CultureInfo.InvariantCulture)}.";
|
|
return null;
|
|
}
|
|
|
|
return value;
|
|
}
|
|
|
|
private static int? ParseInt(string? raw, string field, int min, int max, Dictionary<string, string> errors)
|
|
{
|
|
var text = raw?.Trim();
|
|
if (string.IsNullOrEmpty(text))
|
|
{
|
|
errors[field] = "Pflichtfeld.";
|
|
return null;
|
|
}
|
|
|
|
if (!int.TryParse(text, NumberStyles.None, CultureInfo.InvariantCulture, out var value) || value < min || value > max)
|
|
{
|
|
errors[field] = $"Ganze Zahl zwischen {min} und {max}.";
|
|
return null;
|
|
}
|
|
|
|
return value;
|
|
}
|
|
}
|