Added urlabuse.com check for abuse monitoring

This commit is contained in:
Pinga 2024-09-24 11:47:01 +03:00
parent 6f3ffb0588
commit 6b9bc5e7cd
2 changed files with 98 additions and 3 deletions

View file

@ -8,6 +8,11 @@ $log = setupLogger($logFilePath, 'Abuse_Monitor');
$log->info('job started.');
use Swoole\Coroutine;
use League\Flysystem\Local\LocalFilesystemAdapter;
use League\Flysystem\Filesystem;
use MatthiasMullie\Scrapbook\Adapters\Flysystem as ScrapbookFlysystem;
use MatthiasMullie\Scrapbook\Psr6\Pool;
use GuzzleHttp\Client;
// Initialize the PDO connection pool
$pool = new Swoole\Database\PDOPool(
@ -23,17 +28,55 @@ $pool = new Swoole\Database\PDOPool(
Swoole\Runtime::enableCoroutine();
// Filesystem Cache setup for URLAbuse
$cachePath = __DIR__ . '/../cache';
$adapter = new LocalFilesystemAdapter($cachePath, null, LOCK_EX);
$filesystem = new Filesystem($adapter);
$cache = new Pool(new ScrapbookFlysystem($filesystem));
// Cache key and file URL for URLAbuse data
$cacheKey = 'urlabuse_blacklist';
$fileUrl = 'https://urlabuse.com/public/data/data.txt';
// Creating first coroutine
Coroutine::create(function () use ($pool, $log) {
Coroutine::create(function () use ($pool, $log, $cache, $cacheKey, $fileUrl) {
try {
$pdo = $pool->get();
$stmt = $pdo->query('SELECT name, clid FROM domain');
// Retrieve URLAbuse data with caching
$urlAbuseData = getUrlAbuseData($cache, $cacheKey, $fileUrl);
// Get URLhaus data
$urlhausData = getUrlhausData();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$domain = $row['name'];
// Check URLAbuse
if (checkUrlAbuse($domain, $urlAbuseData)) {
$userStmt = $pdo->prepare('SELECT user_id FROM registrar_users WHERE registrar_id = ?');
$userStmt->execute([$row['clid']]);
$userData = $userStmt->fetch(PDO::FETCH_ASSOC);
if ($userData) {
// Prepare INSERT statement to add a ticket
$insertStmt = $pdo->prepare('INSERT INTO support_tickets (id, user_id, category_id, subject, message, status, priority, reported_domain, nature_of_abuse, evidence, relevant_urls, date_of_incident, date_created, last_updated) VALUES (NULL, ?, 8, ?, ?, "Open", "High", ?, "Abuse", ?, ?, ?, CURRENT_TIMESTAMP(3), CURRENT_TIMESTAMP(3))');
// Execute the prepared statement with appropriate values
$insertStmt->execute([
$userData['user_id'], // user_id
"Abuse Report for $domain", // subject
"Abuse detected for domain $domain.", // message
$domain, // reported_domain
"Link to URLAbuse", // evidence
"https://urlabuse.com/public/data/data.txt", // relevant_urls
date('Y-m-d H:i:s') // date_of_incident
]);
}
}
// Check URLhaus
$urlhausResult = checkUrlhaus($domain, $urlhausData);
if ($urlhausResult) {
@ -58,6 +101,7 @@ Coroutine::create(function () use ($pool, $log) {
}
}
// Check Spamhaus
if (checkSpamhaus($domain)) {
$userStmt = $pdo->prepare('SELECT user_id FROM registrar_users WHERE registrar_id = ?');
$userStmt->execute([$row['clid']]);

View file

@ -204,4 +204,55 @@ function generateAuthInfo(): string {
}
return $retVal;
}
}
// Function to fetch and cache URLAbuse data
function getUrlAbuseData($cache, $cacheKey, $fileUrl) {
// Check if data is cached
$cachedFile = $cache->getItem($cacheKey);
if (!$cachedFile->isHit()) {
// Data is not cached, download it
$httpClient = new Client();
$response = $httpClient->get($fileUrl);
$fileContent = $response->getBody()->getContents();
// Cache the file content
$cachedFile->set($fileContent);
$cachedFile->expiresAfter(300); // Cache for 5 minutes
$cache->save($cachedFile);
return processUrlAbuseData($fileContent);
} else {
// Retrieve data from cache
$fileContent = $cachedFile->get();
return processUrlAbuseData($fileContent);
}
}
// Function to process URLAbuse data
function processUrlAbuseData($fileContent) {
$lines = explode("\n", $fileContent);
$map = new \Ds\Map();
foreach ($lines as $line) {
// Skip comments
if (strpos(trim($line), '#') === 0) {
continue;
}
// Parse JSON data from each line
$entry = json_decode($line, true);
if ($entry && isset($entry['url'])) {
$domain = parse_url($entry['url'], PHP_URL_HOST); // Extract domain from URL
$map->put($domain, $entry); // Store data against domain
}
}
return $map;
}
// Function to check if a domain is listed in URLAbuse
function checkUrlAbuse($domain, Map $urlAbuseData) {
return $urlAbuseData->get($domain, false);
}