]> git.mxchange.org Git - friendica-addons.git/commitdiff
Blockbot: Logging of AP actors
authorMichael <heluecht@pirati.ca>
Wed, 5 Jun 2024 03:35:00 +0000 (03:35 +0000)
committerMichael <heluecht@pirati.ca>
Fri, 7 Jun 2024 04:19:53 +0000 (04:19 +0000)
blockbot/blockbot.php

index e5de1ef37cb8f51c11c3a7852d4941f360265050..eb634b1f5d5b6f21f3826a5cee75f8df93ad6c71 100644 (file)
@@ -15,6 +15,7 @@ use Friendica\Core\Logger;
 use Friendica\Core\Renderer;
 use Friendica\Core\System;
 use Friendica\Network\HTTPException\ForbiddenException;
+use Friendica\Util\HTTPSignature;
 use Friendica\Util\Network;
 
 require_once __DIR__ . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';
@@ -76,6 +77,8 @@ function blockbot_init_1()
                return;
        }
 
+       blockbot_log_activitypub($_SERVER['REQUEST_URI'], $_SERVER['HTTP_USER_AGENT']);
+
        if (blockbot_is_crawler($parts)) {
                Logger::debug('Crawler found - reject', $logdata);
                blockbot_reject();
@@ -169,7 +172,7 @@ function blockbot_init_1()
 
 function blockbot_save($database, $userAgent)
 {
-       if (!DI::config()->get('blockbot', 'training') || !function_exists('dba_open')) {
+       if (!DI::config()->get('blockbot', 'logging') || !function_exists('dba_open')) {
                return;
        }
 
@@ -181,6 +184,36 @@ function blockbot_save($database, $userAgent)
        dba_close($resource);
 }
 
+function blockbot_log_activitypub(string $url, string $agent)
+{
+       if (!DI::config()->get('blockbot', 'logging')) {
+               return;
+       }
+
+       $bot = ['/.well-known/nodeinfo', '/nodeinfo/2.0', '/nodeinfo/1.0'];
+       if (in_array($url, $bot)) {
+               blockbot_save('activitypub-stats', $agent);
+       }
+
+       $bot = ['/api/v1/instance', '/api/v2/instance', '/api/v1/instance/extended_description',
+               '/api/v1/instance/peers'];
+       if (in_array($url, $bot)) {
+               blockbot_save('activitypub-api-stats', $agent);
+       }
+
+       if (substr($url, 0, 6) == '/api/v') {
+               blockbot_save('activitypub-api', $agent);
+       }
+
+       if (($_SERVER['REQUEST_METHOD'] == 'POST') && in_array('inbox', explode('/', parse_url($url, PHP_URL_PATH)))) {
+               blockbot_save('activitypub-inbox-agents', $agent);
+       }
+
+       if (!empty($_SERVER['HTTP_SIGNATURE']) && !empty(HTTPSignature::getSigner('', $_SERVER))) {
+               blockbot_save('activitypub-signature-agents', $agent);
+       }
+}
+
 function blockbot_check_login_attempt(string $url, array $logdata)
 {
        if (in_array(trim(parse_url($url, PHP_URL_PATH), '/'), ['login', 'lostpass', 'register'])) {