Add data management, randomized HTML drip and public README
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
3671019e60
commit
f5d8ca7484
46 files changed
+1601
-172
No files matched your search
@@ -4,5 +4,11 @@ public sealed class DripOptions
|
||||
{
|
||||
public const string SectionName = "Flypaper:Drip";
|
||||
|
||||
public TimeSpan Interval { get; set; } = TimeSpan.FromSeconds(10);
|
||||
public TimeSpan MinDelay { get; set; } = TimeSpan.FromMilliseconds(50);
|
||||
|
||||
public TimeSpan MaxDelay { get; set; } = TimeSpan.FromSeconds(1);
|
||||
|
||||
public int MinChunkSize { get; set; } = 1;
|
||||
|
||||
public int MaxChunkSize { get; set; } = 64;
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
using System.Text;
|
||||
using Flypaper.Core.Abstractions;
|
||||
|
||||
namespace Flypaper.Core.Strategies;
|
||||
|
||||
public static class DripSchedule
|
||||
{
|
||||
public static IEnumerable<DripStep> Create(IEnumerable<string> content, IRandomSource random, DripOptions options)
|
||||
{
|
||||
var minChunk = Math.Max(1, options.MinChunkSize);
|
||||
var maxChunk = Math.Max(minChunk, options.MaxChunkSize);
|
||||
var minDelay = (int)Math.Max(0, options.MinDelay.TotalMilliseconds);
|
||||
var maxDelay = Math.Max(minDelay, (int)options.MaxDelay.TotalMilliseconds);
|
||||
|
||||
using var fragments = content.GetEnumerator();
|
||||
var pending = Array.Empty<byte>();
|
||||
var offset = 0;
|
||||
|
||||
while (true)
|
||||
{
|
||||
var chunk = new byte[random.Next(minChunk, maxChunk + 1)];
|
||||
var filled = 0;
|
||||
|
||||
while (filled < chunk.Length)
|
||||
{
|
||||
if (offset == pending.Length)
|
||||
{
|
||||
if (!fragments.MoveNext())
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
pending = Encoding.UTF8.GetBytes(fragments.Current);
|
||||
offset = 0;
|
||||
continue;
|
||||
}
|
||||
|
||||
var count = Math.Min(chunk.Length - filled, pending.Length - offset);
|
||||
pending.AsSpan(offset, count).CopyTo(chunk.AsSpan(filled));
|
||||
offset += count;
|
||||
filled += count;
|
||||
}
|
||||
|
||||
if (filled == 0)
|
||||
{
|
||||
yield break;
|
||||
}
|
||||
|
||||
yield return new DripStep(chunk.AsMemory(0, filled), TimeSpan.FromMilliseconds(random.Next(minDelay, maxDelay + 1)));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
namespace Flypaper.Core.Strategies;
|
||||
|
||||
public readonly record struct DripStep(ReadOnlyMemory<byte> Chunk, TimeSpan Delay);
|
||||
@@ -1,26 +0,0 @@
|
||||
using Flypaper.Core.Abstractions;
|
||||
using Flypaper.Core.Models;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace Flypaper.Core.Strategies;
|
||||
|
||||
public sealed class DripStrategy(IOptionsMonitor<DripOptions> options, TimeProvider timeProvider) : ITarpitStrategy
|
||||
{
|
||||
private static readonly byte[] Alphabet = "abcdefghijklmnopqrstuvwxyz0123456789"u8.ToArray();
|
||||
|
||||
public string Name => "drip";
|
||||
|
||||
public bool Supports(TrapProtocol protocol) => protocol == TrapProtocol.Http;
|
||||
|
||||
public async Task RunAsync(ITrapChannel channel, CancellationToken ct)
|
||||
{
|
||||
var buffer = new byte[1];
|
||||
while (true)
|
||||
{
|
||||
ct.ThrowIfCancellationRequested();
|
||||
buffer[0] = Alphabet[Random.Shared.Next(Alphabet.Length)];
|
||||
await channel.WriteAsync(buffer, ct);
|
||||
await Task.Delay(options.CurrentValue.Interval, timeProvider, ct);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,170 @@
|
||||
using System.Text;
|
||||
using Flypaper.Core.Abstractions;
|
||||
|
||||
namespace Flypaper.Core.Strategies;
|
||||
|
||||
public static class HtmlContent
|
||||
{
|
||||
private const string Css = """
|
||||
body { margin: 0 auto; max-width: 52rem; padding: 1.5rem; font: 16px/1.6 system-ui, sans-serif; color: #222; }
|
||||
header { border-bottom: 1px solid #ddd; margin-bottom: 1.5rem; }
|
||||
nav a { margin-right: 1rem; color: #0b5cad; text-decoration: none; }
|
||||
h1, h2, h3 { line-height: 1.25; }
|
||||
article { margin-bottom: 2rem; }
|
||||
a { color: #0b5cad; }
|
||||
""";
|
||||
|
||||
private static readonly string[] Vocabulary =
|
||||
[
|
||||
"lorem", "ipsum", "dolor", "sit", "amet", "consectetur", "adipiscing", "elit", "sed", "do", "eiusmod",
|
||||
"tempor", "incididunt", "ut", "labore", "et", "dolore", "magna", "aliqua", "enim", "ad", "minim",
|
||||
"veniam", "quis", "nostrud", "exercitation", "ullamco", "laboris", "nisi", "aliquip", "ex", "ea",
|
||||
"commodo", "consequat", "duis", "aute", "irure", "in", "reprehenderit", "voluptate", "velit", "esse",
|
||||
"cillum", "fugiat", "nulla", "pariatur", "excepteur", "sint", "occaecat", "cupidatat", "non", "proident",
|
||||
"sunt", "culpa", "qui", "officia", "deserunt", "mollit", "anim", "id", "est", "laborum", "praesent",
|
||||
"luctus", "vitae", "semper", "facilisis", "integer", "nec", "odio", "mauris", "massa", "vestibulum",
|
||||
"lacinia", "arcu", "eget", "nunc", "tristique", "fermentum", "pellentesque", "habitant", "morbi",
|
||||
"senectus", "netus", "malesuada", "fames", "turpis", "egestas", "porttitor", "rhoncus", "dapibus",
|
||||
"ornare", "sapien", "volutpat", "blandit", "aliquam", "etiam", "erat", "velit", "scelerisque",
|
||||
"viverra", "orci", "sagittis", "eu", "tellus", "cras", "pulvinar", "mattis", "pharetra", "convallis",
|
||||
"posuere", "morbi", "leo", "urna", "molestie", "at", "elementum", "sodales", "neque", "gravida",
|
||||
"dictum", "fusce", "ultrices", "placerat", "euismod", "quam", "suspendisse", "potenti", "nullam",
|
||||
"accumsan", "tincidunt", "varius", "sollicitudin", "imperdiet", "hendrerit", "bibendum", "cursus"
|
||||
];
|
||||
|
||||
private static readonly string[] Sections =
|
||||
["docs", "blog", "wiki", "support", "products", "archive", "news", "help", "files", "guides", "kb", "releases"];
|
||||
|
||||
private static readonly string[] Titles =
|
||||
["Welcome", "Documentation", "Knowledge Base", "Support Center", "Release Notes", "Resources", "Archive", "Developer Portal"];
|
||||
|
||||
public static IEnumerable<string> Generate(IRandomSource random)
|
||||
{
|
||||
var title = $"{Pick(random, Titles)} · {Title(random, 2, 3)}";
|
||||
yield return $"""
|
||||
<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<title>{title}</title>
|
||||
<style>
|
||||
{Css}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<header>
|
||||
<h1>{title}</h1>
|
||||
|
||||
""";
|
||||
yield return Navigation(random);
|
||||
yield return "</header>\n<main>\n";
|
||||
|
||||
while (true)
|
||||
{
|
||||
yield return random.Next(0, 10) switch
|
||||
{
|
||||
< 5 => Article(random),
|
||||
< 7 => LinkList(random),
|
||||
< 9 => Paragraph(random),
|
||||
_ => $"<h3>{Title(random, 2, 6)}</h3>\n{Paragraph(random)}"
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
private static string Navigation(IRandomSource random)
|
||||
{
|
||||
var builder = new StringBuilder("<nav>\n");
|
||||
var count = random.Next(3, 7);
|
||||
for (var i = 0; i < count; i++)
|
||||
{
|
||||
builder.Append($"<a href=\"/{Pick(random, Sections)}/\">{Capitalize(Pick(random, Vocabulary))}</a>\n");
|
||||
}
|
||||
|
||||
return builder.Append("</nav>\n").ToString();
|
||||
}
|
||||
|
||||
private static string Article(IRandomSource random)
|
||||
{
|
||||
var builder = new StringBuilder($"<article>\n<h2>{Title(random, 3, 9)}</h2>\n");
|
||||
var paragraphs = random.Next(1, 6);
|
||||
for (var i = 0; i < paragraphs; i++)
|
||||
{
|
||||
builder.Append(Paragraph(random));
|
||||
}
|
||||
|
||||
return builder.Append("</article>\n").ToString();
|
||||
}
|
||||
|
||||
private static string LinkList(IRandomSource random)
|
||||
{
|
||||
var ordered = random.Next(0, 2) == 0;
|
||||
var builder = new StringBuilder(ordered ? "<ol>\n" : "<ul>\n");
|
||||
var items = random.Next(3, 10);
|
||||
for (var i = 0; i < items; i++)
|
||||
{
|
||||
builder.Append($"<li>{Link(random, random.Next(2, 6))}</li>\n");
|
||||
}
|
||||
|
||||
return builder.Append(ordered ? "</ol>\n" : "</ul>\n").ToString();
|
||||
}
|
||||
|
||||
private static string Paragraph(IRandomSource random)
|
||||
{
|
||||
var builder = new StringBuilder("<p>");
|
||||
var sentences = random.Next(2, 9);
|
||||
for (var i = 0; i < sentences; i++)
|
||||
{
|
||||
if (i > 0)
|
||||
{
|
||||
builder.Append(' ');
|
||||
}
|
||||
|
||||
builder.Append(Sentence(random));
|
||||
}
|
||||
|
||||
return builder.Append("</p>\n").ToString();
|
||||
}
|
||||
|
||||
private static string Sentence(IRandomSource random)
|
||||
{
|
||||
var builder = new StringBuilder();
|
||||
var words = random.Next(4, 21);
|
||||
var linkAt = random.Next(0, 8) == 0 ? random.Next(1, words) : -1;
|
||||
|
||||
for (var i = 0; i < words; i++)
|
||||
{
|
||||
if (i > 0)
|
||||
{
|
||||
builder.Append(i > 2 && random.Next(0, 9) == 0 ? ", " : " ");
|
||||
}
|
||||
|
||||
if (i == linkAt)
|
||||
{
|
||||
builder.Append(Link(random, random.Next(1, 4)));
|
||||
continue;
|
||||
}
|
||||
|
||||
var word = Pick(random, Vocabulary);
|
||||
builder.Append(i == 0 ? Capitalize(word) : word);
|
||||
}
|
||||
|
||||
return builder.Append(random.Next(0, 10) == 0 ? '?' : '.').ToString();
|
||||
}
|
||||
|
||||
private static string Link(IRandomSource random, int words)
|
||||
{
|
||||
var text = Phrase(random, words);
|
||||
var slug = text.Replace(' ', '-');
|
||||
return $"<a href=\"/{Pick(random, Sections)}/{slug}\">{text}</a>";
|
||||
}
|
||||
|
||||
private static string Title(IRandomSource random, int min, int max) => Capitalize(Phrase(random, random.Next(min, max + 1)));
|
||||
|
||||
private static string Phrase(IRandomSource random, int count) =>
|
||||
string.Join(' ', Enumerable.Range(0, count).Select(_ => Pick(random, Vocabulary)));
|
||||
|
||||
private static string Pick(IRandomSource random, string[] values) => values[random.Next(0, values.Length)];
|
||||
|
||||
private static string Capitalize(string value) => value.Length == 0 ? value : char.ToUpperInvariant(value[0]) + value[1..];
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
using Flypaper.Core.Abstractions;
|
||||
using Flypaper.Core.Models;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace Flypaper.Core.Strategies;
|
||||
|
||||
public sealed class HtmlDripStrategy(
|
||||
IOptionsMonitor<DripOptions> options,
|
||||
IRandomSource random,
|
||||
TimeProvider timeProvider) : ITarpitStrategy
|
||||
{
|
||||
public string Name => "html-drip";
|
||||
|
||||
public bool Supports(TrapProtocol protocol) => protocol == TrapProtocol.Http;
|
||||
|
||||
public async Task RunAsync(ITrapChannel channel, CancellationToken ct)
|
||||
{
|
||||
foreach (var step in DripSchedule.Create(HtmlContent.Generate(random), random, options.CurrentValue))
|
||||
{
|
||||
ct.ThrowIfCancellationRequested();
|
||||
await channel.WriteAsync(step.Chunk, ct);
|
||||
await Task.Delay(step.Delay, timeProvider, ct);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user