Restructure solution into folders and fix persistence issues
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
37987a394a
commit
7c39cb5562
62 files changed
+219
-151
No files matched your search
@@ -0,0 +1,157 @@
|
||||
using System.Globalization;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace Flypaper.Core.Settings;
|
||||
|
||||
public static partial class RuntimeSettingsParser
|
||||
{
|
||||
private const int MaxListEntries = 500;
|
||||
private const int MaxPathLength = 256;
|
||||
private const int MaxHostLength = 253;
|
||||
private const int MaxLimit = 100_000;
|
||||
|
||||
private static readonly TimeSpan MinInterval = TimeSpan.FromMilliseconds(100);
|
||||
private static readonly TimeSpan MaxInterval = TimeSpan.FromHours(1);
|
||||
private static readonly TimeSpan MinHold = TimeSpan.FromSeconds(1);
|
||||
private static readonly TimeSpan MaxHold = TimeSpan.FromDays(7);
|
||||
|
||||
[GeneratedRegex(@"^(\*\.)?[A-Za-z0-9]([A-Za-z0-9.\-]*[A-Za-z0-9])?$")]
|
||||
private static partial Regex HostPattern();
|
||||
|
||||
public static RuntimeSettingsParseResult Parse(RuntimeSettingsInput input)
|
||||
{
|
||||
var errors = new Dictionary<string, string>();
|
||||
|
||||
var paths = ParseList(input.TrapPaths, nameof(input.TrapPaths), ValidatePath, errors);
|
||||
var hosts = ParseList(input.ExtraKnownHosts, nameof(input.ExtraKnownHosts), ValidateHost, errors);
|
||||
var interval = ParseTimeSpan(input.DripInterval, nameof(input.DripInterval), MinInterval, MaxInterval, errors);
|
||||
var hold = ParseTimeSpan(input.MaxHoldDuration, nameof(input.MaxHoldDuration), MinHold, MaxHold, errors);
|
||||
var maxConnections = ParseInt(input.MaxConnections, nameof(input.MaxConnections), errors);
|
||||
var perIp = ParseInt(input.MaxConnectionsPerIp, nameof(input.MaxConnectionsPerIp), 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(paths!, interval!.Value, maxConnections!.Value, perIp!.Value, hold!.Value, hosts!);
|
||||
return new RuntimeSettingsParseResult(settings, errors);
|
||||
}
|
||||
|
||||
private static string[]? ParseList(
|
||||
string? raw,
|
||||
string field,
|
||||
Func<string, string?> validate,
|
||||
Dictionary<string, string> errors)
|
||||
{
|
||||
var lines = (raw ?? string.Empty)
|
||||
.Split(['\r', '\n'], StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries);
|
||||
|
||||
if (lines.Length > MaxListEntries)
|
||||
{
|
||||
errors[field] = $"Höchstens {MaxListEntries} Einträge.";
|
||||
return null;
|
||||
}
|
||||
|
||||
var problems = new List<string>();
|
||||
for (var i = 0; i < lines.Length; i++)
|
||||
{
|
||||
if (validate(lines[i]) is { } problem)
|
||||
{
|
||||
problems.Add($"„{lines[i]}“: {problem}");
|
||||
}
|
||||
}
|
||||
|
||||
if (problems.Count > 0)
|
||||
{
|
||||
errors[field] = string.Join(" ", problems.Take(3)) + (problems.Count > 3 ? $" (+{problems.Count - 3} weitere)" : "");
|
||||
return null;
|
||||
}
|
||||
|
||||
return lines.Distinct(StringComparer.Ordinal).ToArray();
|
||||
}
|
||||
|
||||
private static string? ValidatePath(string path)
|
||||
{
|
||||
if (path[0] != '/')
|
||||
{
|
||||
return "muss mit / beginnen.";
|
||||
}
|
||||
|
||||
if (path.Length > MaxPathLength)
|
||||
{
|
||||
return $"höchstens {MaxPathLength} Zeichen.";
|
||||
}
|
||||
|
||||
if (path.Any(char.IsWhiteSpace))
|
||||
{
|
||||
return "darf keine Leerzeichen enthalten.";
|
||||
}
|
||||
|
||||
var star = path.IndexOf('*');
|
||||
return star >= 0 && star != path.Length - 1 ? "* ist nur am Ende erlaubt." : null;
|
||||
}
|
||||
|
||||
private static string? ValidateHost(string host)
|
||||
{
|
||||
if (host.Length > MaxHostLength)
|
||||
{
|
||||
return $"höchstens {MaxHostLength} Zeichen.";
|
||||
}
|
||||
|
||||
return HostPattern().IsMatch(host) ? null : "ungültiger Hostname (nur Name, ohne Schema, Port oder Pfad; Wildcard als *.beispiel.de).";
|
||||
}
|
||||
|
||||
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, 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 is < 1 or > MaxLimit)
|
||||
{
|
||||
errors[field] = $"Ganze Zahl zwischen 1 und {MaxLimit}.";
|
||||
return null;
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user