Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/Jobs/CheckIfBotIsReal.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class CheckIfBotIsReal implements ShouldQueue

protected $client;
protected $options;
protected $allowedBots;


/**
Expand Down Expand Up @@ -52,7 +53,7 @@ public function handle()
}

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

Expand Down
21 changes: 14 additions & 7 deletions src/Jobs/ProcessLogWithIpInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,22 @@ class ProcessLogWithIpInfo implements ShouldQueue
protected $action;
protected $client;
protected $options;

protected $accessLimit;

/**
* Checks whether the given IP address really belongs to a valid host or not
*
* @param $ip the IP address to check
* @return bool true if the given IP address belongs to any of the valid hosts, otherwise false
*/
public function __construct($client, $action, $options = null)
public function __construct($client, $action, $options = null, $accessLimit = null)
{
$this->action = $action;
$this->client = $client;
$this->options = $options;
if (!is_null($accessLimit)) {
$this->accessLimit = $accessLimit;
}
}

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

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

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

$messsage .= "Org: {$org} | city: {$city} | region: {$region} | country: {$country} ";
$message .= "Org: {$org} | city: {$city} | region: {$region} | country: {$country} ";
}
}

if ($this->client->url) {
$messsage .= " when accessing the URL: {$this->client->url} ";
$message .= " when accessing the URL: {$this->client->url} ";
}

if (($this->action === 'WHITELISTED') || ($this->action === 'GOOD_CRAWLER')) {
Log::stack($this->options->channels_info)->info($messsage);
Log::stack($this->options->channels_info)->info($message);
} else {
Log::stack($this->options->channels_info)->error($messsage);
Log::stack($this->options->channels_info)->error($message);
}
}
}
17 changes: 9 additions & 8 deletions src/Middleware/BlockBots.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function handle($request, Closure $next, $limit = 100, $frequency = 'dail
$this->setUp($request, $limit, $frequency);
$this->countHits();

return $this->isAllowed() ? $next($this->request) : $this->notAllowed();
return $this->isAllowed() ? $next($request) : $this->notAllowed();
}

/**
Expand All @@ -49,7 +49,9 @@ public function handle($request, Closure $next, $limit = 100, $frequency = 'dail
protected function notAllowed()
{
if ($this->options->log) {
$this->logDisallowance();
if (!$this->options->log_only_guest || Auth::guest()) {
$this->logDisallowance();
}
}

if (Auth::check() && $this->isTheFirstOverflow()) {
Expand Down Expand Up @@ -77,8 +79,8 @@ protected function isAllowed()
return false;
} elseif (Auth::check()) {
return $this->passesAuthRules() && !$this->isLimitExceeded();
} elseif (Auth::guest()) {
return $this->passesGuestRules() && !$this->isLimitExceeded();
} elseif (Auth::guest() && $this->passesGuestRules() && !$this->isLimitExceeded()) {
return true;
}

return $this->passesBotRules();
Expand All @@ -94,6 +96,7 @@ protected function countHits()
if (!Redis::exists($this->client->key)) {
Redis::set($this->client->key, 1);
Redis::expireat($this->client->key, $this->timeOutAt);
return $this->hits = 1;
}

return $this->hits = Redis::incr($this->client->key);
Expand All @@ -104,8 +107,7 @@ private function logDisallowance()
if (!Redis::exists($this->client->logKey)) {
Redis::set($this->client->logKey, 1);
Redis::expireat($this->client->logKey, $this->timeOutAt);

ProcessLogWithIpInfo::dispatch($this->client, "BLOCKED", $this->options);
ProcessLogWithIpInfo::dispatch($this->client, "BLOCKED", $this->options, $this->limit);
}
}

Expand Down Expand Up @@ -193,7 +195,6 @@ public function passesBotRules()
if ($this->isWhitelisted()) {
return true;
}

//Lets block fake bots
if (Redis::sismember($this->options->fake_bot_list_key, $this->client->ip)) {
return false;
Expand All @@ -203,7 +204,7 @@ public function passesBotRules()
// While the bot is on pending_list, it's unchecked, so we allow this bot to pass-thru
if (!Redis::sismember($this->options->pending_bot_list_key, $this->client->ip)) {
// If we got here, it is an unknown bot. Let's create a job to test it
CheckIfBotIsReal::dispatch($this->client, $this->getAllowedBots());
CheckIfBotIsReal::dispatch($this->client, $this->getAllowedBots(), $this->options);
Redis::sadd($this->options->pending_bot_list_key, $this->client->ip);
}

Expand Down
1 change: 1 addition & 0 deletions src/config/block-bots.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
*/

'log' => env('BLOCK_BOTS_LOG_ENABLED', env('BLOCK_BOTS_LOG_BLOCKED_REQUESTS', true)),
'log_only_guest' => env('BLOCK_BOTS_LOG_ONLY_GUEST', true),

/*
* The list of allowed user-agents. The value of the key should be a keyword in hostname or * for enable to everyone
Expand Down