File tree Expand file tree Collapse file tree 6 files changed +160
-4
lines changed Expand file tree Collapse file tree 6 files changed +160
-4
lines changed Original file line number Diff line number Diff line change 3
3
namespace Potelo \LaravelBlockBots ;
4
4
5
5
use Illuminate \Support \ServiceProvider ;
6
- use Potelo \LaravelBlockBots \Commands \ListWhitelist ;
7
6
use Potelo \LaravelBlockBots \Commands \ClearWhitelist ;
7
+ use Potelo \LaravelBlockBots \Commands \ListWhitelist ;
8
+ use Potelo \LaravelBlockBots \Commands \ListHits ;
9
+ use Potelo \LaravelBlockBots \Commands \ListNotified ;
8
10
9
11
class BlockBotsServiceProvider extends ServiceProvider
10
12
{
@@ -18,8 +20,10 @@ public function boot()
18
20
19
21
if ($ this ->app ->runningInConsole ()) {
20
22
$ this ->commands ([
21
- ListWhitelist::class,
22
23
ClearWhitelist::class,
24
+ ListWhitelist::class,
25
+ ListHits::class,
26
+ ListNotified::class,
23
27
]);
24
28
}
25
29
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -26,7 +26,7 @@ public function __construct($request)
26
26
$ this ->ip = $ request ->getClientIp ();
27
27
$ this ->id = Auth::check () ? Auth::id () : $ this ->ip ;
28
28
$ this ->userAgent = $ request ->header ('User-Agent ' );
29
- $ this ->key = "block_bot: {$ this ->id }" ;
29
+ $ this ->key = "block_bot:hits: {$ this ->id }" ;
30
30
$ this ->logKey = "block_bot:notified: {$ this ->ip }" ;
31
31
$ this ->url = substr ($ request ->fullUrl (), strlen ($ request ->getScheme () . ":// " ));
32
32
$ this ->options = new Configuration ();
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change 11
11
use Potelo \LaravelBlockBots \Events \UserBlockedEvent ;
12
12
use Potelo \LaravelBlockBots \Jobs \ProcessLogWithIpInfo ;
13
13
use Potelo \LaravelBlockBots \Abstracts \AbstractBlockBots ;
14
-
14
+ use Potelo \ LaravelBlockBots \ Events \ BotBlockedEvent ;
15
15
16
16
class BlockBots extends AbstractBlockBots
17
17
{
@@ -56,6 +56,9 @@ protected function notAllowed()
56
56
event (new UserBlockedEvent (Auth::user (), $ this ->hits , Carbon::now ()));
57
57
}
58
58
59
+ if (Auth::guest () && $ this ->isTheFirstOverflow ()) {
60
+ event (new BotBlockedEvent ($ this ->client ->ip , $ this ->hits , Carbon::now ()));
61
+ }
59
62
60
63
if ($ this ->request ->expectsJson ()) {
61
64
return response ()->json ($ this ->options ->json_response , 429 );
You can’t perform that action at this time.
0 commit comments