Audit module: audit log and address bans
This commit is contained in:
1 parent
b75403e5ea
commit
d2ea1bacac
41 files changed
+2137
-5
No files matched your search
@@ -0,0 +1,215 @@
|
||||
<?php
|
||||
|
||||
namespace IriksIT\Manager\Support;
|
||||
|
||||
/**
|
||||
* What the statistics want to know from a browser's User-Agent line: is it a
|
||||
* person, and roughly which device, browser and system. Rough on purpose; the
|
||||
* line itself is never stored.
|
||||
*/
|
||||
final class UserAgent
|
||||
{
|
||||
private const BOTS = '/bot|crawl|spider|slurp|preview|headless|lighthouse|pagespeed|pingdom|uptime|monitor|scan'
|
||||
.'|fetch|curl|wget|python|java\/|go-http|okhttp|axios|node|php|ruby|perl|libwww|httpclient|postman|insomnia'
|
||||
.'|facebookexternalhit|whatsapp|telegram|slack|discord|embedly|feed|validator|archive|phantom|selenium|puppeteer|playwright'
|
||||
// Programs that say none of the words above: AI assistants, Google's other fetchers, scanners, monitors, feed readers.
|
||||
.'|chatgpt-user|claude-user|claude-web|anthropic-ai|perplexity-user|cohere|mistralai|externalagent|externalfetcher'
|
||||
.'|googleother|google-extended|google-cloudvertex|inspectiontool|mediapartners|apis-google|sogou|qwantify|screaming frog'
|
||||
.'|censys|shodan|expanse|zgrab|masscan|nmap|nuclei|nikto|sqlmap|wpscan|buster|internetmeasurement|l9explore|l9tcpid|leakix'
|
||||
.'|gtmetrix|site24x7|statuscake|better uptime|betterstack|zabbix|nagios|check_http|datadog|newrelic'
|
||||
.'|inoreader|newsblur|miniflux|freshrss|tiny tiny rss|mastodon|pleroma|akkoma|misskey|bluesky|iframely'
|
||||
.'|guzzle|scrapy|aiohttp|httpx|undici|faraday|apache-httpclient/i';
|
||||
|
||||
public function __construct(private readonly string $line) {}
|
||||
|
||||
/** No browser line at all, or one of a program: not a visitor. */
|
||||
public function isBot(): bool
|
||||
{
|
||||
return trim($this->line) === '' || preg_match(self::BOTS, $this->line) === 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* The programs the statistics know by name, as pattern => [name, kind]. The
|
||||
* first one that fits wins, so a longer name stands before the one it holds
|
||||
* ("Applebot-Extended" before "Applebot").
|
||||
*
|
||||
* Kinds: search (search engine), ai (collects for or answers through an AI),
|
||||
* seo (marketing tools), social (the preview of a shared link), monitor, feed,
|
||||
* archive, scanner (looks for weak spots), script (a program or library).
|
||||
*/
|
||||
private const KNOWN_BOTS = [
|
||||
'/GPTBot/i' => ['GPTBot', 'ai'],
|
||||
'/ChatGPT-User/i' => ['ChatGPT', 'ai'],
|
||||
'/OAI-SearchBot/i' => ['OpenAI search', 'ai'],
|
||||
'/Claude-SearchBot/i' => ['Claude search', 'ai'],
|
||||
'/Claude-User/i' => ['Claude', 'ai'],
|
||||
'/ClaudeBot|anthropic-ai|claude-web/i' => ['ClaudeBot', 'ai'],
|
||||
'/Perplexity-User/i' => ['Perplexity', 'ai'],
|
||||
'/PerplexityBot/i' => ['PerplexityBot', 'ai'],
|
||||
'/Google-Extended|GoogleOther|Google-CloudVertexBot/i' => ['Google (AI and other)', 'ai'],
|
||||
'/Applebot-Extended/i' => ['Applebot-Extended', 'ai'],
|
||||
'/meta-externalagent|meta-externalfetcher/i' => ['Meta AI', 'ai'],
|
||||
'/Bytespider/i' => ['Bytespider', 'ai'],
|
||||
'/Amazonbot/i' => ['Amazonbot', 'ai'],
|
||||
'/CCBot/i' => ['Common Crawl', 'ai'],
|
||||
'/cohere-ai|cohere-training/i' => ['Cohere', 'ai'],
|
||||
'/MistralAI/i' => ['Mistral', 'ai'],
|
||||
'/DuckAssistBot/i' => ['DuckAssist', 'ai'],
|
||||
'/YouBot/i' => ['YouBot', 'ai'],
|
||||
'/Diffbot/i' => ['Diffbot', 'ai'],
|
||||
'/ImagesiftBot/i' => ['ImagesiftBot', 'ai'],
|
||||
'/Timpibot/i' => ['Timpibot', 'ai'],
|
||||
|
||||
'/Googlebot|Google-InspectionTool|Storebot-Google|AdsBot-Google|Mediapartners-Google|APIs-Google/i' => ['Googlebot', 'search'],
|
||||
'/bingbot|BingPreview|adidxbot|msnbot/i' => ['Bingbot', 'search'],
|
||||
'/DuckDuckBot|DuckDuckGo/i' => ['DuckDuckBot', 'search'],
|
||||
'/YandexBot|YandexImages|Yandex/i' => ['YandexBot', 'search'],
|
||||
'/Baiduspider/i' => ['Baiduspider', 'search'],
|
||||
'/Applebot/i' => ['Applebot', 'search'],
|
||||
'/Slurp/i' => ['Yahoo', 'search'],
|
||||
'/Qwantbot|Qwantify/i' => ['Qwant', 'search'],
|
||||
'/SeznamBot/i' => ['SeznamBot', 'search'],
|
||||
'/PetalBot/i' => ['PetalBot', 'search'],
|
||||
'/Sogou/i' => ['Sogou', 'search'],
|
||||
'/MojeekBot/i' => ['Mojeek', 'search'],
|
||||
'/Bravebot/i' => ['Brave', 'search'],
|
||||
'/Kagibot/i' => ['Kagi', 'search'],
|
||||
|
||||
'/AhrefsBot|AhrefsSiteAudit/i' => ['AhrefsBot', 'seo'],
|
||||
'/SemrushBot|SiteAuditBot|SplitSignalBot/i' => ['SemrushBot', 'seo'],
|
||||
'/MJ12bot/i' => ['Majestic', 'seo'],
|
||||
'/DotBot|rogerbot/i' => ['Moz', 'seo'],
|
||||
'/DataForSeoBot/i' => ['DataForSEO', 'seo'],
|
||||
'/BLEXBot/i' => ['BLEXBot', 'seo'],
|
||||
'/SerpstatBot/i' => ['Serpstat', 'seo'],
|
||||
'/Barkrowler/i' => ['Barkrowler', 'seo'],
|
||||
'/Screaming Frog/i' => ['Screaming Frog', 'seo'],
|
||||
'/SEOkicks|seobility|Sistrix|linkdexbot/i' => ['Other SEO tool', 'seo'],
|
||||
|
||||
'/facebookexternalhit|Facebot|facebookcatalog/i' => ['Facebook', 'social'],
|
||||
'/LinkedInBot/i' => ['LinkedIn', 'social'],
|
||||
'/Twitterbot/i' => ['X', 'social'],
|
||||
'/WhatsApp/i' => ['WhatsApp', 'social'],
|
||||
'/TelegramBot/i' => ['Telegram', 'social'],
|
||||
'/Slackbot|Slack-ImgProxy/i' => ['Slack', 'social'],
|
||||
'/Discordbot/i' => ['Discord', 'social'],
|
||||
'/Pinterestbot|Pinterest\\/0/i' => ['Pinterest', 'social'],
|
||||
'/redditbot/i' => ['Reddit', 'social'],
|
||||
'/Mastodon|Pleroma|Akkoma|Misskey/i' => ['Mastodon and the fediverse', 'social'],
|
||||
'/Bluesky/i' => ['Bluesky', 'social'],
|
||||
'/SkypeUriPreview|MicrosoftPreview/i' => ['Microsoft link preview', 'social'],
|
||||
'/Embedly|Iframely/i' => ['Embed service', 'social'],
|
||||
|
||||
'/UptimeRobot/i' => ['UptimeRobot', 'monitor'],
|
||||
'/Uptime-Kuma/i' => ['Uptime Kuma', 'monitor'],
|
||||
'/Pingdom/i' => ['Pingdom', 'monitor'],
|
||||
'/StatusCake/i' => ['StatusCake', 'monitor'],
|
||||
'/Better Uptime|BetterStack/i' => ['Better Stack', 'monitor'],
|
||||
'/Site24x7/i' => ['Site24x7', 'monitor'],
|
||||
'/Lighthouse|PageSpeed|GTmetrix/i' => ['Speed test', 'monitor'],
|
||||
'/Cloudflare/i' => ['Cloudflare', 'monitor'],
|
||||
'/Zabbix|Nagios|check_http|Datadog|NewRelic/i' => ['Other monitoring', 'monitor'],
|
||||
|
||||
'/Feedly|Feedbin|Inoreader|NewsBlur|Miniflux|FreshRSS|Tiny Tiny RSS|FeedFetcher/i' => ['Feed reader', 'feed'],
|
||||
'/archive\.org_bot|ia_archiver|Wayback/i' => ['Internet Archive', 'archive'],
|
||||
|
||||
'/CensysInspect/i' => ['Censys', 'scanner'],
|
||||
'/Shodan/i' => ['Shodan', 'scanner'],
|
||||
'/Expanse|Palo Alto/i' => ['Palo Alto Expanse', 'scanner'],
|
||||
'/InternetMeasurement/i' => ['InternetMeasurement', 'scanner'],
|
||||
'/l9explore|l9tcpid|LeakIX/i' => ['LeakIX', 'scanner'],
|
||||
'/zgrab|masscan|Nmap|Nuclei|Nikto|sqlmap|WPScan|dirbuster|gobuster/i' => ['Scanning tool', 'scanner'],
|
||||
|
||||
'/HeadlessChrome|Puppeteer|Playwright|Selenium|PhantomJS/i' => ['Headless browser', 'script'],
|
||||
'/curl/i' => ['curl', 'script'],
|
||||
'/Wget/i' => ['Wget', 'script'],
|
||||
'/python|aiohttp|httpx|scrapy/i' => ['Python', 'script'],
|
||||
'/Go-http-client/i' => ['Go', 'script'],
|
||||
'/okhttp|Java\/|Apache-HttpClient/i' => ['Java', 'script'],
|
||||
'/axios|node-fetch|undici|node/i' => ['Node.js', 'script'],
|
||||
'/Guzzle|PHP/i' => ['PHP', 'script'],
|
||||
'/Ruby|Faraday/i' => ['Ruby', 'script'],
|
||||
'/libwww-perl|Perl/i' => ['Perl', 'script'],
|
||||
'/Postman|Insomnia/i' => ['API client', 'script'],
|
||||
];
|
||||
|
||||
/** For a program that is not in the list: the word in its line that names it. */
|
||||
private const BOT_WORD = '/[A-Za-z0-9][A-Za-z0-9._-]*(?:bot|crawler|crawl|spider|scan|fetch|monitor|preview|archive|validator)[A-Za-z0-9._-]*/i';
|
||||
|
||||
/**
|
||||
* Which program this is, or null for a person. A program that is not known
|
||||
* by name is called by the one word of its line that names it ("FooBot"),
|
||||
* never by the line; without such a word it is `null` as a name, for the
|
||||
* caller to file under "other".
|
||||
*
|
||||
* @return array{name: string|null, kind: string}|null
|
||||
*/
|
||||
public function bot(): ?array
|
||||
{
|
||||
if (! $this->isBot()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
foreach (self::KNOWN_BOTS as $pattern => [$name, $kind]) {
|
||||
if (preg_match($pattern, $this->line) === 1) {
|
||||
return ['name' => $name, 'kind' => $kind];
|
||||
}
|
||||
}
|
||||
|
||||
$word = preg_match(self::BOT_WORD, $this->line, $match) === 1 ? substr($match[0], 0, 40) : null;
|
||||
|
||||
return ['name' => $word, 'kind' => 'other'];
|
||||
}
|
||||
|
||||
public function device(): string
|
||||
{
|
||||
if (preg_match('/iPad|Tablet|PlayBook|Silk/i', $this->line) === 1
|
||||
|| (str_contains($this->line, 'Android') && ! str_contains($this->line, 'Mobile'))) {
|
||||
return 'tablet';
|
||||
}
|
||||
|
||||
return preg_match('/Mobi|iPhone|iPod|Android/i', $this->line) === 1 ? 'mobile' : 'desktop';
|
||||
}
|
||||
|
||||
public function browser(): string
|
||||
{
|
||||
// Order matters: nearly every browser also says "Chrome" and "Safari".
|
||||
$browsers = [
|
||||
'Edge' => '/Edg(e|A|iOS)?\//',
|
||||
'Opera' => '/OPR\/|Opera/',
|
||||
'Samsung Internet' => '/SamsungBrowser\//',
|
||||
'Vivaldi' => '/Vivaldi\//',
|
||||
'Firefox' => '/Firefox\/|FxiOS\//',
|
||||
'Chrome' => '/Chrome\/|CriOS\//',
|
||||
'Safari' => '/Safari\//',
|
||||
];
|
||||
|
||||
foreach ($browsers as $name => $pattern) {
|
||||
if (preg_match($pattern, $this->line) === 1) {
|
||||
return $name;
|
||||
}
|
||||
}
|
||||
|
||||
return 'Other';
|
||||
}
|
||||
|
||||
public function os(): string
|
||||
{
|
||||
$systems = [
|
||||
'iOS' => '/iPhone|iPad|iPod/',
|
||||
'Android' => '/Android/',
|
||||
'Windows' => '/Windows/',
|
||||
'macOS' => '/Mac OS X|Macintosh/',
|
||||
'ChromeOS' => '/CrOS/',
|
||||
'Linux' => '/Linux|X11/',
|
||||
];
|
||||
|
||||
foreach ($systems as $name => $pattern) {
|
||||
if (preg_match($pattern, $this->line) === 1) {
|
||||
return $name;
|
||||
}
|
||||
}
|
||||
|
||||
return 'Other';
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user