"; const ARIA2RPC_PROTOCOL = "https"; // or http - not accommodating websocket (ws/wss) const ARIA2RPC_ENDPOINT = ARIA2RPC_PROTOCOL . "://" . ARIA2RPC_HOST . ":" . ARIA2RPC_PORT . "/jsonrpc"; const DISALLOWED_CLIENT_IDS = [ 'XL', 'SD', ]; // Get all active downloads $aria2_tasks = json_decode(file_post_contents(ARIA2RPC_ENDPOINT, [ 'jsonrpc' => '2.0', 'id' => base64_encode("php-bt-fail2ban_" . microtime() . "_" . rand(100000, 999999)), 'method' => 'aria2.tellActive', 'params' => [ "token:" . ARIA2RPC_TOKEN, [ 'gid', 'status', 'connections', ], ], ])); foreach ($aria2_tasks->result as $task) { // Get task details $aria2_task_details = json_decode(file_post_contents(ARIA2RPC_ENDPOINT, [ 'jsonrpc' => '2.0', 'id' => base64_encode("php-bt-fail2ban_" . microtime() . "_" . rand(100000, 999999)), 'method' => 'aria2.getPeers', 'params' => [ "token:" . ARIA2RPC_TOKEN, $task->gid, ], ])); foreach ($aria2_task_details->result as $peer) { // Decode peer id $peer_id_decoded = urldecode($peer->peerId); // Matching peer id with ban-list if (preg_match("/^(-(" . implode("|", DISALLOWED_CLIENT_IDS) . ")\d{2,4}-)/", $peer_id_decoded, $peer_client)) { // Write log entry for fail2ban file_put_contents( LOG_FILE_PATH, time() . " aria2-rpc-block: gid[" . $task->gid . "] from [" . $peer->ip . "] Client:(" . $peer_client[1] . "+random)\n", FILE_APPEND ); } } } // HTTP POST and JSON-related functions from https://stackoverflow.com/a/11319621/11296357 function file_post_contents($url, $data, $username = null, $password = null) { $post_data = json_encode($data); if ($post_data === false) return false; $opts = [ 'http' => [ 'method' => 'POST', 'header' => 'Content-type: application/json', 'content' => $post_data, ], 'ssl' => [ "verify_peer" => false, "verify_peer_name" => false, ], ]; if ($username && $password) $opts['http']['header'] .= ("Authorization: Basic " . base64_encode("$username:$password")); return file_get_contents($url, false, stream_context_create($opts)); }