53 lines
1.6 KiB
C#
53 lines
1.6 KiB
C#
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<string> 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<string> Parse(string? raw) =>
|
|
Split(raw).Select(Normalize).OfType<string>().Distinct().Take(MaxCount).ToList();
|
|
|
|
public static bool Matches(IReadOnlyList<string> 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));
|
|
}
|
|
}
|