Add link labyrinth, static robots.txt and robots bait paths
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
b7027e2a72
commit
a25072e6fb
29 files changed
+774
-34
No files matched your search
@@ -0,0 +1,52 @@
|
||||
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));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user