From 8e654f753ad4e49397fb0abc036529b9f6ab57f7 Mon Sep 17 00:00:00 2001 From: AMoktar Date: Sun, 24 Sep 2023 16:11:32 +0300 Subject: [PATCH 1/2] add console commands to list [hits, notified] --- src/BlockBotsServiceProvider.php | 8 +++-- src/Commands/ListHits.php | 57 ++++++++++++++++++++++++++++++++ src/Commands/ListNotified.php | 57 ++++++++++++++++++++++++++++++++ src/Contracts/Client.php | 2 +- 4 files changed, 121 insertions(+), 3 deletions(-) create mode 100644 src/Commands/ListHits.php create mode 100644 src/Commands/ListNotified.php diff --git a/src/BlockBotsServiceProvider.php b/src/BlockBotsServiceProvider.php index bda4bff..17545f3 100755 --- a/src/BlockBotsServiceProvider.php +++ b/src/BlockBotsServiceProvider.php @@ -3,8 +3,10 @@ namespace Potelo\LaravelBlockBots; use Illuminate\Support\ServiceProvider; -use Potelo\LaravelBlockBots\Commands\ListWhitelist; use Potelo\LaravelBlockBots\Commands\ClearWhitelist; +use Potelo\LaravelBlockBots\Commands\ListWhitelist; +use Potelo\LaravelBlockBots\Commands\ListHits; +use Potelo\LaravelBlockBots\Commands\ListNotified; class BlockBotsServiceProvider extends ServiceProvider { @@ -18,8 +20,10 @@ public function boot() if ($this->app->runningInConsole()) { $this->commands([ - ListWhitelist::class, ClearWhitelist::class, + ListWhitelist::class, + ListHits::class, + ListNotified::class, ]); } diff --git a/src/Commands/ListHits.php b/src/Commands/ListHits.php new file mode 100644 index 0000000..1685963 --- /dev/null +++ b/src/Commands/ListHits.php @@ -0,0 +1,57 @@ +options = new Configuration(); + } + + /** + * Execute the console command. + * + * @return void + */ + public function handle() + { + $this->info("List of IPs hits:"); + + $keys = Redis::keys('block_bot:hits*'); + + foreach ($keys as $key) { + $key = str($key)->afterLast(':'); + $this->info($key . " : " . Redis::get("block_bot:hits:{$key}")); + } + } +} diff --git a/src/Commands/ListNotified.php b/src/Commands/ListNotified.php new file mode 100644 index 0000000..5f18a58 --- /dev/null +++ b/src/Commands/ListNotified.php @@ -0,0 +1,57 @@ +options = new Configuration(); + } + + /** + * Execute the console command. + * + * @return void + */ + public function handle() + { + $this->info("List of notified IPs with notified count:"); + + $keys = Redis::keys('block_bot:notified*'); + + foreach ($keys as $key) { + $key = str($key)->afterLast(':'); + $this->info($key . " : " . Redis::get("block_bot:notified:{$key}")); + } + } +} diff --git a/src/Contracts/Client.php b/src/Contracts/Client.php index 5b36ba9..e9e7a78 100644 --- a/src/Contracts/Client.php +++ b/src/Contracts/Client.php @@ -26,7 +26,7 @@ public function __construct($request) $this->ip = $request->getClientIp(); $this->id = Auth::check() ? Auth::id() : $this->ip; $this->userAgent = $request->header('User-Agent'); - $this->key = "block_bot:{$this->id}"; + $this->key = "block_bot:hits:{$this->id}"; $this->logKey = "block_bot:notified:{$this->ip}"; $this->url = substr($request->fullUrl(), strlen($request->getScheme() . "://")); $this->options = new Configuration(); From cd65b977d572731671b05b45114691044b837947 Mon Sep 17 00:00:00 2001 From: AMoktar Date: Sun, 24 Sep 2023 16:20:01 +0300 Subject: [PATCH 2/2] adding BotBlockedEvent --- src/Events/BotBlockedEvent.php | 35 ++++++++++++++++++++++++++++++++++ src/Middleware/BlockBots.php | 5 ++++- 2 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 src/Events/BotBlockedEvent.php diff --git a/src/Events/BotBlockedEvent.php b/src/Events/BotBlockedEvent.php new file mode 100644 index 0000000..2a9a7f1 --- /dev/null +++ b/src/Events/BotBlockedEvent.php @@ -0,0 +1,35 @@ +ip = $ip; + $this->number_of_hits = $number_of_hits; + $this->block_date = $block_date; + } +} diff --git a/src/Middleware/BlockBots.php b/src/Middleware/BlockBots.php index 2191c3f..2fe0715 100644 --- a/src/Middleware/BlockBots.php +++ b/src/Middleware/BlockBots.php @@ -10,7 +10,7 @@ use Potelo\LaravelBlockBots\Events\UserBlockedEvent; use Potelo\LaravelBlockBots\Jobs\ProcessLogWithIpInfo; use Potelo\LaravelBlockBots\Abstracts\AbstractBlockBots; - +use Potelo\LaravelBlockBots\Events\BotBlockedEvent; class BlockBots extends AbstractBlockBots { @@ -55,6 +55,9 @@ protected function notAllowed() event(new UserBlockedEvent(Auth::user(), $this->hits, Carbon::now())); } + if (Auth::guest() && $this->isTheFirstOverflow()) { + event(new BotBlockedEvent($this->client->ip, $this->hits, Carbon::now())); + } if ($this->request->expectsJson()) { return response()->json($this->options->json_response, 429);