Skip to content

Commit 086357c

Browse files
author
Bruno Cabral
committed
- Send LOG message as a JOB
1 parent a592ab9 commit 086357c

File tree

4 files changed

+111
-8
lines changed

4 files changed

+111
-8
lines changed

src/Jobs/CheckIfBotIsReal.php

+2-2
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::stack($this->config['channels_info'])->info("[Block-Bots] IP: {$ip} with User agent: {$user_agent} was detected as a GOOD crawler");
74+
\Potelo\LaravelBlockBots\Jobs\ProcessLogWithIpInfo::dispatch($ip, $user_agent, '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::stack($this->config['channels_info'])->info("[Block-Bots] IP: {$ip} with User agent: {$user_agent} was detected as a BAD crawler");
83+
\Potelo\LaravelBlockBots\Jobs\ProcessLogWithIpInfo::dispatch($ip, $user_agent, 'BAD_CRAWLER');
8484
}
8585
}
8686

src/Jobs/ProcessLogWithIpInfo.php

+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?php
2+
3+
namespace Potelo\LaravelBlockBots\Jobs;
4+
5+
use GuzzleHttp\Client;
6+
use Illuminate\Bus\Queueable;
7+
use Illuminate\Queue\SerializesModels;
8+
use Illuminate\Queue\InteractsWithQueue;
9+
use Illuminate\Contracts\Queue\ShouldQueue;
10+
use Illuminate\Foundation\Bus\Dispatchable;
11+
use Illuminate\Support\Facades\Redis;
12+
use Illuminate\Support\Facades\Log;
13+
14+
class ProcessLogWithIpInfo implements ShouldQueue
15+
{
16+
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
17+
18+
protected $ip;
19+
protected $user_agent;
20+
protected $config;
21+
protected $attemped_url;
22+
protected $action;
23+
24+
25+
/**
26+
* Checks whether the given IP address really belongs to a valid host or not
27+
*
28+
* @param $ip the IP address to check
29+
* @return bool true if the given IP address belongs to any of the valid hosts, otherwise false
30+
*/
31+
public function __construct($ip, $user_agent, $action, $attemped_url=null)
32+
{
33+
$this->ip = $ip;
34+
$this->user_agent = $user_agent;
35+
$this->attemped_url = $attemped_url;
36+
$this->action = $action;
37+
$this->config = config('block-bots');
38+
}
39+
40+
/**
41+
* Execute the job.
42+
*
43+
* @return void
44+
*/
45+
public function handle()
46+
{
47+
//
48+
$user_agent = $this->user_agent;
49+
$ip = $this->ip;
50+
$key_access_count = "block_bot:{$ip}";
51+
$number_of_requests = Redis::exists($key_access_count);
52+
53+
$host = strtolower(gethostbyaddr($ip));
54+
$messsage = "[Block-Bots] IP: {$ip}; After {$number_of_requests} requests , Host:{$host} \n with User agent: {$user_agent}; was {$this->action}";
55+
56+
$ip_info = $this->config['ip_info_key'];
57+
58+
if ($ip_info)
59+
{
60+
$client = new Client();
61+
$response = $client->get(
62+
"http://ipinfo.io/{$ip}" ,
63+
[
64+
'query' => [
65+
'token' => $ip_info,
66+
]
67+
]
68+
);
69+
$json_response = json_decode($response->getBody(), true);
70+
if(array_key_exists('org', $json_response) && array_key_exists('city', $json_response) &&
71+
array_key_exists('region', $json_response) && array_key_exists('country', $json_response))
72+
{
73+
$org = $json_response["org"];
74+
$city = $json_response["city"];
75+
$region = $json_response["region"];
76+
$country = $json_response["country"];
77+
78+
$messsage .= "Org: {$org} | city: {$city} | region: {$region} | country: {$country} ";
79+
}
80+
}
81+
82+
if ($this->attemped_url){
83+
$messsage .= " when accessing the URL:{$this->attemped_url} ";
84+
}
85+
86+
if (($this->action == 'GOOD_CRAWLER') || ($this->action == 'WHITELISTED'))
87+
{
88+
Log::stack($this->config['channels_info'])->info($messsage);
89+
}
90+
else{
91+
Log::stack($this->config['channels_info'])->error($messsage);
92+
}
93+
94+
95+
96+
}
97+
98+
}

src/Middleware/BlockBots.php

+7-6
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
namespace Potelo\LaravelBlockBots\Middleware;
44

55
use Closure;
6-
use Illuminate\Support\Facades\Redirect;
6+
77
use Illuminate\Support\Facades\Redis;
88
use Illuminate\Support\Facades\Log;
99
use Illuminate\Support\Facades\Auth;
1010

1111
use Carbon\Carbon;
1212
use Potelo\LaravelBlockBots\CheckIfBotIsReal;
13-
use Symfony\Component\HttpKernel\Exception\HttpException;
13+
1414

1515
class BlockBots
1616
{
@@ -46,9 +46,9 @@ public function handle($request, Closure $next, $dailyLimit)
4646

4747
if ($blocked) {
4848
if ($request->expectsJson()) {
49-
return response()->json(['message' => 'You are over the specified limit.'], 403);
49+
return response()->json(['message' => 'You are over the specified limit.'], 429);
5050
}
51-
return response(view('block-bots::error'), 403);
51+
return response(view('block-bots::error'), 429);
5252
}
5353

5454
return $next($request);
@@ -102,7 +102,8 @@ public function blocked($request, $dailyLimit)
102102
$end_of_day_unix_timestamp = Carbon::tomorrow()->startOfDay()->timestamp;
103103
Redis::set($key_notified, 1);
104104
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}");
105+
\Potelo\LaravelBlockBots\Jobs\ProcessLogWithIpInfo::dispatch($ip, $user_agent, 'BLOCKED', $full_url);
106+
106107
}
107108
}
108109

@@ -150,7 +151,7 @@ public function isWhitelisted($ip, $user_agent)
150151
//Add this to the redis list as it is faster
151152
Redis::sadd($key_whitelist, $ip);
152153
if ($this->config['log_blocked_requests']){
153-
Log::stack($this->config['channels_info'])->info("[Block-Bots] WHITELISTED IP: {$ip} with User agent: {$user_agent} because was on our config");
154+
\Potelo\LaravelBlockBots\Jobs\ProcessLogWithIpInfo::dispatch($ip, $user_agent, 'WHITELISTED');
154155
}
155156
}
156157

src/config/block-bots.php

+4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99

1010
'enabled' => env('BLOCK_BOTS_ENABLED', true),
1111

12+
'ip_info_key' => env('BLOCK_BOTS_IP_INFO_KEY', null),
13+
14+
15+
1216
/*
1317
* Whitelisted IP addresses
1418
* '127.0.0.1',

0 commit comments

Comments
 (0)