Skip to content

Commit a067da4

Browse files
Merge pull request #23 from AMoktar/master
add console commands to list [hits, notified], event BotBlockedEvent
2 parents f878c52 + cd65b97 commit a067da4

File tree

6 files changed

+160
-4
lines changed

6 files changed

+160
-4
lines changed

src/BlockBotsServiceProvider.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
namespace Potelo\LaravelBlockBots;
44

55
use Illuminate\Support\ServiceProvider;
6-
use Potelo\LaravelBlockBots\Commands\ListWhitelist;
76
use Potelo\LaravelBlockBots\Commands\ClearWhitelist;
7+
use Potelo\LaravelBlockBots\Commands\ListWhitelist;
8+
use Potelo\LaravelBlockBots\Commands\ListHits;
9+
use Potelo\LaravelBlockBots\Commands\ListNotified;
810

911
class BlockBotsServiceProvider extends ServiceProvider
1012
{
@@ -18,8 +20,10 @@ public function boot()
1820

1921
if ($this->app->runningInConsole()) {
2022
$this->commands([
21-
ListWhitelist::class,
2223
ClearWhitelist::class,
24+
ListWhitelist::class,
25+
ListHits::class,
26+
ListNotified::class,
2327
]);
2428
}
2529

src/Commands/ListHits.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
namespace Potelo\LaravelBlockBots\Commands;
4+
5+
use Illuminate\Console\Command;
6+
use Illuminate\Support\Facades\Redis;
7+
use Potelo\LaravelBlockBots\Contracts\Configuration;
8+
9+
class ListHits extends Command
10+
{
11+
/**
12+
* The name and signature of the console command.
13+
*
14+
* @var string
15+
*/
16+
protected $signature = 'block-bots:list-hits';
17+
18+
/**
19+
* The console command description.
20+
*
21+
* @var string
22+
*/
23+
protected $description = 'Show the list of IPs with hits count';
24+
25+
/**
26+
* @var \Potelo\LaravelBlockBots\Contracts\Configuration
27+
*/
28+
private $options;
29+
30+
/**
31+
* Create a new command instance.
32+
*
33+
* @return void
34+
*/
35+
public function __construct()
36+
{
37+
parent::__construct();
38+
$this->options = new Configuration();
39+
}
40+
41+
/**
42+
* Execute the console command.
43+
*
44+
* @return void
45+
*/
46+
public function handle()
47+
{
48+
$this->info("List of IPs hits:");
49+
50+
$keys = Redis::keys('block_bot:hits*');
51+
52+
foreach ($keys as $key) {
53+
$key = str($key)->afterLast(':');
54+
$this->info($key . " : " . Redis::get("block_bot:hits:{$key}"));
55+
}
56+
}
57+
}

src/Commands/ListNotified.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
namespace Potelo\LaravelBlockBots\Commands;
4+
5+
use Illuminate\Console\Command;
6+
use Illuminate\Support\Facades\Redis;
7+
use Potelo\LaravelBlockBots\Contracts\Configuration;
8+
9+
class ListNotified extends Command
10+
{
11+
/**
12+
* The name and signature of the console command.
13+
*
14+
* @var string
15+
*/
16+
protected $signature = 'block-bots:list-notified';
17+
18+
/**
19+
* The console command description.
20+
*
21+
* @var string
22+
*/
23+
protected $description = 'Show the list of notified IPs with notified count';
24+
25+
/**
26+
* @var \Potelo\LaravelBlockBots\Contracts\Configuration
27+
*/
28+
private $options;
29+
30+
/**
31+
* Create a new command instance.
32+
*
33+
* @return void
34+
*/
35+
public function __construct()
36+
{
37+
parent::__construct();
38+
$this->options = new Configuration();
39+
}
40+
41+
/**
42+
* Execute the console command.
43+
*
44+
* @return void
45+
*/
46+
public function handle()
47+
{
48+
$this->info("List of notified IPs with notified count:");
49+
50+
$keys = Redis::keys('block_bot:notified*');
51+
52+
foreach ($keys as $key) {
53+
$key = str($key)->afterLast(':');
54+
$this->info($key . " : " . Redis::get("block_bot:notified:{$key}"));
55+
}
56+
}
57+
}

src/Contracts/Client.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public function __construct($request)
2626
$this->ip = $request->getClientIp();
2727
$this->id = Auth::check() ? Auth::id() : $this->ip;
2828
$this->userAgent = $request->header('User-Agent');
29-
$this->key = "block_bot:{$this->id}";
29+
$this->key = "block_bot:hits:{$this->id}";
3030
$this->logKey = "block_bot:notified:{$this->ip}";
3131
$this->url = substr($request->fullUrl(), strlen($request->getScheme() . "://"));
3232
$this->options = new Configuration();

src/Events/BotBlockedEvent.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Potelo\LaravelBlockBots\Events;
4+
5+
use Carbon\Carbon;
6+
use Illuminate\Queue\SerializesModels;
7+
8+
class BotBlockedEvent
9+
{
10+
use SerializesModels;
11+
12+
public $ip;
13+
14+
/** @var integer */
15+
public $number_of_hits;
16+
17+
/** @var Carbon */
18+
public $block_date;
19+
20+
/**
21+
* Create a new event instance.
22+
*
23+
* @param $ip
24+
* @param integer $number_of_hits
25+
* @param Carbon $block_date
26+
*
27+
* @return void
28+
*/
29+
public function __construct($ip, $number_of_hits, $block_date)
30+
{
31+
$this->ip = $ip;
32+
$this->number_of_hits = $number_of_hits;
33+
$this->block_date = $block_date;
34+
}
35+
}

src/Middleware/BlockBots.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
use Potelo\LaravelBlockBots\Events\UserBlockedEvent;
1212
use Potelo\LaravelBlockBots\Jobs\ProcessLogWithIpInfo;
1313
use Potelo\LaravelBlockBots\Abstracts\AbstractBlockBots;
14-
14+
use Potelo\LaravelBlockBots\Events\BotBlockedEvent;
1515

1616
class BlockBots extends AbstractBlockBots
1717
{
@@ -56,6 +56,9 @@ protected function notAllowed()
5656
event(new UserBlockedEvent(Auth::user(), $this->hits, Carbon::now()));
5757
}
5858

59+
if (Auth::guest() && $this->isTheFirstOverflow()) {
60+
event(new BotBlockedEvent($this->client->ip, $this->hits, Carbon::now()));
61+
}
5962

6063
if ($this->request->expectsJson()) {
6164
return response()->json($this->options->json_response, 429);

0 commit comments

Comments
 (0)