using System.Globalization; using Flypaper.Core.Abstractions; namespace Flypaper.Core.Strategies; public sealed record FileProfile( string Name, string MediaType, Func Matches, Func> Generate); public static class FileContent { private static readonly string[] Sections = ["docs", "blog", "wiki", "support", "products", "archive", "news", "help", "files"]; public static readonly IReadOnlyList Profiles = [ new("git-drip", "text/plain; charset=utf-8", path => path.Contains("/.git/", StringComparison.Ordinal), GitConfig), new("robots-drip", "text/plain; charset=utf-8", path => LastSegment(path) == "robots.txt", Robots), new("wordpress-drip", "text/html; charset=UTF-8", path => LastSegment(path) == "wp-login.php", WordPressLogin), new("hikvision-drip", "application/xml", path => path.StartsWith("/sdk/", StringComparison.Ordinal) || path.StartsWith("/isapi/", StringComparison.Ordinal), Hikvision), new("env-drip", "text/plain; charset=utf-8", path => IsEnv(LastSegment(path)), Env), new("json-drip", "application/json", path => path.EndsWith(".json", StringComparison.Ordinal), Json), new("xml-drip", "application/xml", path => path.EndsWith(".xml", StringComparison.Ordinal), Xml), new("yaml-drip", "text/plain; charset=utf-8", path => path.EndsWith(".yml", StringComparison.Ordinal) || path.EndsWith(".yaml", StringComparison.Ordinal), Yaml), new("sql-drip", "text/plain; charset=utf-8", path => path.EndsWith(".sql", StringComparison.Ordinal), Sql) ]; public static FileProfile? Match(string? path) { if (string.IsNullOrEmpty(path)) { return null; } var normalized = path.ToLowerInvariant(); return Profiles.FirstOrDefault(profile => profile.Matches(normalized)); } private static string LastSegment(string path) => path[(path.LastIndexOf('/') + 1)..]; private static bool IsEnv(string name) => name.StartsWith(".env", StringComparison.Ordinal) || name.EndsWith(".env", StringComparison.Ordinal) || name == "env"; private static IEnumerable Env(IRandomSource random) { yield return $"APP_NAME=Portal\nAPP_ENV=production\nAPP_DEBUG=false\nAPP_URL=https://example.com\nAPP_KEY=base64:{Hex(random, 32)}\n\n" + $"DB_CONNECTION=mysql\nDB_HOST=127.0.0.1\nDB_PORT=3306\nDB_DATABASE=app\nDB_USERNAME=app\nDB_PASSWORD={Hex(random, 16)}\n\n"; while (true) { var key = $"{Upper(random)}_{Upper(random)}"; yield return random.Next(0, 4) switch { 0 => $"{key}=true\n", 1 => $"{key}={random.Next(1, 65536)}\n", 2 => $"{key}=https://{LoremText.Word(random)}.internal/\n", _ => $"{key}={Hex(random, 24)}\n" }; } } private static IEnumerable WordPressLogin(IRandomSource random) { yield return """ Log In ‹ Portal — WordPress

Powered by WordPress

← Go to Portal

"""; while (true) { yield return $"\n"; } } private static IEnumerable Hikvision(IRandomSource random) { yield return "\n\n"; var id = 1; while (true) { yield return $" \n {id++}\n {LoremText.Word(random)}\n {(random.Next(0, 2) == 0 ? "true" : "false")}\n \n"; } } private static IEnumerable Json(IRandomSource random) { yield return "{\n \"name\": \"portal\",\n \"version\": \"1.4.2\",\n \"private\": true,\n \"entries\": [\n"; var id = 1; while (true) { yield return $" {{\"id\": {id++}, \"name\": \"{LoremText.Word(random)}\", \"token\": \"{Hex(random, 16)}\", \"enabled\": {(random.Next(0, 2) == 0 ? "true" : "false")}}},\n"; } } private static IEnumerable Xml(IRandomSource random) { yield return "\n\n"; while (true) { var date = string.Create(CultureInfo.InvariantCulture, $"2026-{random.Next(1, 13):00}-{random.Next(1, 29):00}"); yield return $" \n https://example.com/{Pick(random, Sections)}/{LoremText.Phrase(random, 2).Replace(' ', '-')}\n {date}\n \n"; } } private static IEnumerable Yaml(IRandomSource random) { yield return "version: \"3.8\"\nservices:\n"; while (true) { yield return $" {LoremText.Word(random)}:\n image: {LoremText.Word(random)}:{random.Next(1, 10)}.{random.Next(0, 20)}\n restart: unless-stopped\n environment:\n - SECRET={Hex(random, 12)}\n"; } } private static IEnumerable Sql(IRandomSource random) { yield return "-- MySQL dump 10.13 Distrib 8.0.36, for Linux (x86_64)\n--\n-- Host: localhost Database: app\n-- ------------------------------------------------------\n\n" + "DROP TABLE IF EXISTS `users`;\nCREATE TABLE `users` (\n `id` int NOT NULL AUTO_INCREMENT,\n `name` varchar(64) NOT NULL,\n `token` varchar(64) NOT NULL,\n PRIMARY KEY (`id`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;\n\n" + "LOCK TABLES `users` WRITE;\n"; var id = 1; while (true) { yield return $"INSERT INTO `users` VALUES ({id++},'{LoremText.Word(random)}','{Hex(random, 32)}');\n"; } } private static IEnumerable GitConfig(IRandomSource random) { yield return "[core]\n\trepositoryformatversion = 0\n\tfilemode = true\n\tbare = false\n\tlogallrefupdates = true\n" + "[remote \"origin\"]\n\turl = https://example.com/portal.git\n\tfetch = +refs/heads/*:refs/remotes/origin/*\n"; while (true) { var branch = $"{LoremText.Word(random)}-{LoremText.Word(random)}"; yield return $"[branch \"{branch}\"]\n\tremote = origin\n\tmerge = refs/heads/{branch}\n"; } } private static IEnumerable Robots(IRandomSource random) { yield return "User-agent: *\nCrawl-delay: 2\nSitemap: https://example.com/sitemap.xml\n"; while (true) { yield return $"Disallow: /{Pick(random, Sections)}/{LoremText.Word(random)}/\n"; } } private static string Hex(IRandomSource random, int length) { var chars = new char[length]; for (var i = 0; i < length; i++) { chars[i] = "0123456789abcdef"[random.Next(0, 16)]; } return new string(chars); } private static string Upper(IRandomSource random) => LoremText.Word(random).ToUpperInvariant(); private static string Pick(IRandomSource random, string[] values) => values[random.Next(0, values.Length)]; }