using System.Text.RegularExpressions; namespace Flypaper.Core.Strategies; public static partial class BaitPaths { public const int MaxCount = 8; public const string Default = "/backup-2024/,/admin-old/,/internal/db-export/,/old-site/"; [GeneratedRegex("^/[A-Za-z0-9._~-]+(/[A-Za-z0-9._~-]+)*/?$")] private static partial Regex Valid(); public static IReadOnlyList Split(string? raw) => (raw ?? "").Split([',', ';', ' ', '\n', '\r', '\t'], StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries); public static string? Normalize(string entry) { if (!Valid().IsMatch(entry) || entry.Length > 100) { return null; } var normalized = entry.ToLowerInvariant(); if (!normalized.EndsWith('/')) { normalized += "/"; } var first = normalized.Split('/', StringSplitOptions.RemoveEmptyEntries)[0]; return normalized == RobotsStaticStrategy.Path + "/" || Array.IndexOf(MazeContent.Roots, first) >= 0 ? null : normalized; } public static IReadOnlyList Parse(string? raw) => Split(raw).Select(Normalize).OfType().Distinct().Take(MaxCount).ToList(); public static bool Matches(IReadOnlyList baits, string? path) { if (string.IsNullOrEmpty(path) || baits.Count == 0) { return false; } var normalized = path.ToLowerInvariant(); if (!normalized.EndsWith('/')) { normalized += "/"; } return baits.Any(bait => normalized.StartsWith(bait, StringComparison.Ordinal)); } }