-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathBlockBots.php
205 lines (177 loc) · 5.78 KB
/
BlockBots.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
<?php
namespace Potelo\LaravelBlockBots\Middleware;
use Closure;
use Carbon\Carbon;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Redis;
use Potelo\LaravelBlockBots\Jobs\CheckIfBotIsReal;
use Potelo\LaravelBlockBots\Events\UserBlockedEvent;
use Potelo\LaravelBlockBots\Jobs\ProcessLogWithIpInfo;
use Potelo\LaravelBlockBots\Abstracts\AbstractBlockBots;
use Potelo\LaravelBlockBots\Events\BotBlockedEvent;
class BlockBots extends AbstractBlockBots
{
/**
* Executes the middleware
*
* @param \Illuminate\Http\Request $request
* @param Closure $next
* @param int $limit
* @param string $frequency
* @return mixed
*/
public function handle($request, Closure $next, $limit = 100, $frequency = 'daily')
{
$this->setUp($request, $limit, $frequency);
$this->beforeHandle();
if (!$this->options->enabled) {
return $next($request);
}
$this->hits = $this->client->countHits($this->incrementHits, $frequency);
return $this->isAllowed() ? $next($request) : $this->notAllowed();
}
/**
* Responses to disallowed client
*
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\JsonResponse|\Illuminate\Http\Response
*/
protected function notAllowed()
{
if ($this->options->log) {
if (!$this->options->log_only_guest || Auth::guest()) {
$this->client->logDisallowance($this->limit, $this->frequency);
}
}
if (Auth::check() && $this->isTheFirstOverflow()) {
event(new UserBlockedEvent(Auth::user(), $this->hits, Carbon::now()));
}
if (Auth::guest() && $this->isTheFirstOverflow()) {
event(new BotBlockedEvent($this->client->ip, $this->hits, Carbon::now()));
}
if ($this->request->expectsJson()) {
return response()->json($this->options->json_response, 429);
}
return response(view('block-bots::error'), 429);
}
/**
* Check if the client access is allowed
*
* @return boolean
*/
protected function isAllowed()
{
if ($this->options->mode === 'never') {
return true;
} elseif ($this->options->mode === 'always') {
return false;
} elseif (Auth::check()) {
return $this->passesAuthRules() && !$this->isLimitExceeded();
} elseif (Auth::guest() && $this->passesGuestRules() && !$this->isLimitExceeded()) {
return true;
}
return $this->passesBotRules();
}
/**
* Check if the client is a preseted allowed bot.
*
* @return boolean
*/
private function isAllowedBot()
{
$allowedBotsKeys = array_keys($this->getAllowedBots());
$userAgent = strtolower($this->client->userAgent);
$pattern = strtolower('(' . implode('|', $allowedBotsKeys) . ')');
return preg_match($pattern, $userAgent);
}
/**
* @return mixed|string|null
*/
public function getMatchedBot()
{
foreach (array_keys($this->getAllowedBots()) as $bot) {
if (strpos(strtolower($this->client->userAgent), strtolower($bot)) !== false) {
return $bot;
}
}
}
/**
* Check if the client is whitelisted.
*
* @return bool
*/
public function isWhitelisted()
{
if (Redis::sismember($this->options->whitelist_key, $this->client->ip)) {
return true;
}
//Lets verify if its on our whitelist
if (in_array($this->client->ip, $this->options->whitelist_ips)) {
//Add this to the redis list as it is faster
Redis::sadd($this->options->whitelist_key, $this->client->ip);
if ($this->options->log) {
ProcessLogWithIpInfo::dispatch($this->client, 'WHITELISTED', $this->options);
}
return true;
}
return false;
}
/**
* Determine if the request passes the auth check.
*
* @return bool
*/
protected function passesAuthRules()
{
if (method_exists($this, 'authRules')) {
if (!call_user_func_array([$this, 'authRules'], [])) {
return false;
}
}
return true;
}
/**
* Determine if the request passes the guest check.
*
* @return bool
*/
protected function passesGuestRules()
{
if (method_exists($this, 'guestRules')) {
if (!call_user_func_array([$this, 'guestRules'], [])) {
return false;
}
}
return true;
}
/**
* Determine if the request passes the bot check.
*
* @return bool
*/
public function passesBotRules()
{
if (method_exists($this, 'botRules')) {
if (!call_user_func_array([$this, 'botRules'], [])) {
return false;
}
}
//We fast-track allowed IPs
if ($this->isWhitelisted()) {
return true;
}
//Lets block fake bots
if (Redis::sismember($this->options->fake_bot_list_key, $this->client->ip)) {
return false;
}
if ($this->isAllowedBot()) {
// 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(), $this->options);
Redis::sadd($this->options->pending_bot_list_key, $this->client->ip);
}
return true;
}
return false;
}
}