Skip to content

Commit 2824da2

Browse files
committed
Fix logs and authorization of real bots
1 parent 1fd08ed commit 2824da2

File tree

3 files changed

+22
-15
lines changed

3 files changed

+22
-15
lines changed

src/Jobs/CheckIfBotIsReal.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class CheckIfBotIsReal implements ShouldQueue
1616

1717
protected $client;
1818
protected $options;
19+
protected $allowedBots;
1920

2021

2122
/**
@@ -52,7 +53,7 @@ public function handle()
5253
}
5354

5455
// Lets remove from the pending list
55-
Redis::srem($this->options->pending_bots_key, $this->client->ip);
56+
Redis::srem($this->options->pending_bot_list_key, $this->client->ip);
5657
if ($this->isValid($found_bot_key)) {
5758
Redis::sadd($this->options->whitelist_key, $this->client->ip);
5859

src/Jobs/ProcessLogWithIpInfo.php

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,22 @@ class ProcessLogWithIpInfo implements ShouldQueue
1818
protected $action;
1919
protected $client;
2020
protected $options;
21-
21+
protected $accessLimit;
2222

2323
/**
2424
* Checks whether the given IP address really belongs to a valid host or not
2525
*
2626
* @param $ip the IP address to check
2727
* @return bool true if the given IP address belongs to any of the valid hosts, otherwise false
2828
*/
29-
public function __construct($client, $action, $options = null)
29+
public function __construct($client, $action, $options = null, $accessLimit = null)
3030
{
3131
$this->action = $action;
3232
$this->client = $client;
3333
$this->options = $options;
34+
if (!is_null($accessLimit)) {
35+
$this->accessLimit = $accessLimit;
36+
}
3437
}
3538

3639
/**
@@ -43,7 +46,11 @@ public function handle()
4346
$hits = Redis::get($this->client->key);
4447
$host = strtolower(gethostbyaddr($this->client->ip));
4548

46-
$messsage = "[Block-Bots] IP: {$this->client->ip}; After {$hits} requests, Host: {$host} \n with User agent: {$this->client->userAgent}; was {$this->action}";
49+
if (!empty($this->accessLimit)) {
50+
$message = "[Block-Bots] IP: {$this->client->ip}; After {$hits}/{$this->accessLimit} requests, Host: {$host} \n with User agent: {$this->client->userAgent}; was {$this->action}";
51+
} else {
52+
$message = "[Block-Bots] IP: {$this->client->ip}; After {$hits} requests, Host: {$host} \n with User agent: {$this->client->userAgent}; was {$this->action}";
53+
}
4754

4855
if ($this->options->ip_info_key) {
4956
$http = new HTTP();
@@ -67,18 +74,18 @@ public function handle()
6774
$region = $json_response["region"];
6875
$country = $json_response["country"];
6976

70-
$messsage .= "Org: {$org} | city: {$city} | region: {$region} | country: {$country} ";
77+
$message .= "Org: {$org} | city: {$city} | region: {$region} | country: {$country} ";
7178
}
7279
}
7380

7481
if ($this->client->url) {
75-
$messsage .= " when accessing the URL: {$this->client->url} ";
82+
$message .= " when accessing the URL: {$this->client->url} ";
7683
}
7784

7885
if (($this->action === 'WHITELISTED') || ($this->action === 'GOOD_CRAWLER')) {
79-
Log::stack($this->options->channels_info)->info($messsage);
86+
Log::stack($this->options->channels_info)->info($message);
8087
} else {
81-
Log::stack($this->options->channels_info)->error($messsage);
88+
Log::stack($this->options->channels_info)->error($message);
8289
}
8390
}
8491
}

src/Middleware/BlockBots.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function handle($request, Closure $next, $limit = 100, $frequency = 'dail
3838
$this->setUp($request, $limit, $frequency);
3939
$this->countHits();
4040

41-
return $this->isAllowed() ? $next($this->request) : $this->notAllowed();
41+
return $this->isAllowed() ? $next($request) : $this->notAllowed();
4242
}
4343

4444
/**
@@ -77,8 +77,8 @@ protected function isAllowed()
7777
return false;
7878
} elseif (Auth::check()) {
7979
return $this->passesAuthRules() && !$this->isLimitExceeded();
80-
} elseif (Auth::guest()) {
81-
return $this->passesGuestRules() && !$this->isLimitExceeded();
80+
} elseif (Auth::guest() && $this->passesGuestRules() && !$this->isLimitExceeded()) {
81+
return true;
8282
}
8383

8484
return $this->passesBotRules();
@@ -94,6 +94,7 @@ protected function countHits()
9494
if (!Redis::exists($this->client->key)) {
9595
Redis::set($this->client->key, 1);
9696
Redis::expireat($this->client->key, $this->timeOutAt);
97+
return $this->hits = 1;
9798
}
9899

99100
return $this->hits = Redis::incr($this->client->key);
@@ -104,8 +105,7 @@ private function logDisallowance()
104105
if (!Redis::exists($this->client->logKey)) {
105106
Redis::set($this->client->logKey, 1);
106107
Redis::expireat($this->client->logKey, $this->timeOutAt);
107-
108-
ProcessLogWithIpInfo::dispatch($this->client, "BLOCKED", $this->options);
108+
ProcessLogWithIpInfo::dispatch($this->client, "BLOCKED", $this->options, $this->limit);
109109
}
110110
}
111111

@@ -193,7 +193,6 @@ public function passesBotRules()
193193
if ($this->isWhitelisted()) {
194194
return true;
195195
}
196-
197196
//Lets block fake bots
198197
if (Redis::sismember($this->options->fake_bot_list_key, $this->client->ip)) {
199198
return false;
@@ -203,7 +202,7 @@ public function passesBotRules()
203202
// While the bot is on pending_list, it's unchecked, so we allow this bot to pass-thru
204203
if (!Redis::sismember($this->options->pending_bot_list_key, $this->client->ip)) {
205204
// If we got here, it is an unknown bot. Let's create a job to test it
206-
CheckIfBotIsReal::dispatch($this->client, $this->getAllowedBots());
205+
CheckIfBotIsReal::dispatch($this->client, $this->getAllowedBots(), $this->options);
207206
Redis::sadd($this->options->pending_bot_list_key, $this->client->ip);
208207
}
209208

0 commit comments

Comments
 (0)