Skip to content

Commit a592ab9

Browse files
author
Bruno Cabral
committed
Configurable log stack
1 parent a024155 commit a592ab9

File tree

3 files changed

+30
-8
lines changed

3 files changed

+30
-8
lines changed

src/Jobs/CheckIfBotIsReal.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public function handle()
7171
Redis::sadd($key_whitelist, $ip);
7272

7373
if ($log_blocked_requests){
74-
Log::info("[Block-Bots] IP: {$ip} with User agent: {$user_agent} was detected as a GOOD crawler");
74+
Log::stack($this->config['channels_info'])->info("[Block-Bots] IP: {$ip} with User agent: {$user_agent} was detected as a GOOD crawler");
7575
}
7676

7777
}
@@ -80,7 +80,7 @@ public function handle()
8080
Redis::sadd($key_fake_bot, $ip);
8181

8282
if ($log_blocked_requests){
83-
Log::info("[Block-Bots] IP: {$ip} with User agent: {$user_agent} was detected as a BAD crawler");
83+
Log::stack($this->config['channels_info'])->info("[Block-Bots] IP: {$ip} with User agent: {$user_agent} was detected as a BAD crawler");
8484
}
8585
}
8686

src/Middleware/BlockBots.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function __construct()
3131
*/
3232
public function handle($request, Closure $next, $dailyLimit)
3333
{
34-
$enabled = $this->config['fake_mode'];
34+
$enabled = $this->config['enabled'];
3535
if (!$enabled){
3636
return $next($request);
3737
}
@@ -40,7 +40,7 @@ public function handle($request, Closure $next, $dailyLimit)
4040
$blocked = $this->blocked($request, $dailyLimit);
4141

4242
} catch (Exception $e) {
43-
Log::error("[Block-Bots] Error at handling request: {$e->getMessage()}");
43+
Log::stack($this->config['channels_info'])->error("[Block-Bots] Error at handling request: {$e->getMessage()}");
4444
$blocked = false;
4545
}
4646

@@ -62,6 +62,7 @@ public function handle($request, Closure $next, $dailyLimit)
6262
public function blocked($request, $dailyLimit)
6363
{
6464

65+
$dailyLimit = (int) $dailyLimit;
6566
$ip = $request->getClientIp();
6667
$user_agent = $request->header('User-Agent');
6768
$fake_mode = $this->config['fake_mode'];
@@ -94,12 +95,19 @@ public function blocked($request, $dailyLimit)
9495
return false;
9596
}
9697
else{
97-
if($fake_mode)
98-
{
9998
if ($log_blocked_requests){
100-
Log::error("[Block-Bots] IP: {$ip} with User agent: {$user_agent} would be blocked after {$number_of_hits} hits while accessing URL {$full_url}");
99+
$key_notified = "block_bot:notified:{$ip}";
100+
if(!Redis::exists($key_notified))
101+
{
102+
$end_of_day_unix_timestamp = Carbon::tomorrow()->startOfDay()->timestamp;
103+
Redis::set($key_notified, 1);
104+
Redis::expireat($key_notified, $end_of_day_unix_timestamp);
105+
Log::stack($this->config['channels_blocks'])->error("[Block-Bots] IP: {$ip} with User agent: {$user_agent} would be blocked after {$number_of_hits} hits while accessing URL {$full_url}");
106+
}
101107
}
102108

109+
if($fake_mode)
110+
{
103111
return false;
104112
}
105113
else{
@@ -142,7 +150,7 @@ public function isWhitelisted($ip, $user_agent)
142150
//Add this to the redis list as it is faster
143151
Redis::sadd($key_whitelist, $ip);
144152
if ($this->config['log_blocked_requests']){
145-
Log::info("[Block-Bots] WHITELISTED IP: {$ip} with User agent: {$user_agent} because was on our config");
153+
Log::stack($this->config['channels_info'])->info("[Block-Bots] WHITELISTED IP: {$ip} with User agent: {$user_agent} because was on our config");
146154
}
147155
}
148156

src/config/block-bots.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,20 @@
2222
'allow_logged_user' => env('BLOCK_BOTS_ALLOW_LOGGED_USER', true),
2323
'fake_mode' => env('BLOCK_BOTS_FAKE_MODE', true), // minutes - disabled by default
2424

25+
/*
26+
* Log channel
27+
*/
28+
'channels_info' => [
29+
'single'
30+
],
31+
32+
/*
33+
* This Log channel will receive when a bad crawler is detected or someone is banned
34+
*/
35+
'channels_blocks' => [
36+
'single'
37+
],
38+
2539
/*
2640
* Send suspicious events to log?
2741
*

0 commit comments

Comments
 (0)