Skip to content

Commit 4ce582c

Browse files
author
Bruno Cabral
committed
Fixes
1 parent ce75c5f commit 4ce582c

File tree

5 files changed

+89
-24
lines changed

5 files changed

+89
-24
lines changed

src/BlockBotsServiceProvider.php

+9-3
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,14 @@ class BlockBotsServiceProvider extends ServiceProvider
1414
public function boot()
1515
{
1616
$this->publishes([
17-
__DIR__ . '/config/config.php' => config_path('config.php'),
18-
], 'config');
17+
__DIR__ . '/config/block-bots.php' => config_path('block-bots.php'),
18+
]);
19+
20+
$this->loadViewsFrom(__DIR__.'/views', 'block-bots');
21+
22+
$this->publishes([
23+
__DIR__.'/views' => resource_path('views/vendor/block-bots'),
24+
]);
1925
}
2026

2127
/**
@@ -26,7 +32,7 @@ public function boot()
2632
public function register()
2733
{
2834
$this->mergeConfigFrom(
29-
__DIR__.'/config/config.php', 'block'
35+
__DIR__.'/config/block-bots.php', 'block-bots'
3036
);
3137
}
3238
}

src/Jobs/CheckIfBotIsReal.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,11 @@ class CheckIfBotIsReal implements ShouldQueue
1515
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
1616

1717
protected $ip;
18+
1819
protected $user_agent;
1920

21+
protected $config;
22+
2023

2124
/**
2225
* Checks whether the given IP address really belongs to a valid host or not
@@ -28,7 +31,7 @@ public function __construct($ip, $user_agent)
2831
{
2932
$this->ip = $ip;
3033
$this->user_agent = $user_agent;
31-
$this->config = config('block');
34+
$this->config = config('block-bots');
3235
}
3336

3437
/**

src/Middleware/BlockBots.php

+17-19
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Potelo\LaravelBlockBots;
3+
namespace Potelo\LaravelBlockBots\Middleware;
44

55
use Closure;
66
use Illuminate\Support\Facades\Redirect;
@@ -10,19 +10,15 @@
1010

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

1415
class BlockBots
1516
{
1617
protected $config;
1718

1819
public function __construct()
1920
{
20-
$this->config = config('block');
21-
}
22-
23-
private function enabled()
24-
{
25-
return $this->config['enabled'];
21+
$this->config = config('block-bots');
2622
}
2723

2824
/**
@@ -33,22 +29,21 @@ private function enabled()
3329
*
3430
* @return mixed
3531
*/
36-
public function handle($request, Closure $next, $dailyLimit, $redirectUrl)
32+
public function handle($request, Closure $next, $dailyLimit)
3733
{
34+
try {
35+
$blocked = $this->blocked($request, $dailyLimit);
3836

39-
if ($this->enabled() && ($request->getRequestUri() != $redirectUrl)) {
40-
41-
try {
42-
$blocked = $this->blocked($request, $dailyLimit);
43-
44-
} catch (Exception $e) {
45-
Log::error("[Block-Bots] Error at handling request: {$e->getMessage()}");
46-
$blocked = false;
47-
}
37+
} catch (Exception $e) {
38+
Log::error("[Block-Bots] Error at handling request: {$e->getMessage()}");
39+
$blocked = false;
40+
}
4841

49-
if ($blocked) {
50-
return Redirect::to($redirectUrl);
42+
if ($blocked) {
43+
if ($request->expectsJson()) {
44+
return response()->json(['message' => 'You are over the specified limit.'], 403);
5145
}
46+
return response(view('block-bots::error'), 403);
5247
}
5348

5449
return $next($request);
@@ -160,6 +155,9 @@ public function isWhitelisted($ip, $user_agent)
160155

161156
// If we got here, it is an unverified bot. Lets create a job to test it
162157
\Potelo\LaravelBlockBots\Jobs\CheckIfBotIsReal::dispatch($ip, $user_agent);
158+
Redis::sadd($key_pending_bot, $ip);
159+
return true;
160+
163161

164162
}
165163

src/config/config.php renamed to src/config/block-bots.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
*/
99

1010
'enabled' => env('BLOCK_BOTS_ENABLED', true),
11+
1112
/*
1213
* Whitelisted IP addresses
1314
* '127.0.0.1',
@@ -19,7 +20,7 @@
1920
],
2021

2122
'allow_logged_user' => env('BLOCK_BOTS_ALLOW_LOGGED_USER', true),
22-
'fake_mode' => env('BLOCK_BOTS_FAKE_MODE', true), //
23+
'fake_mode' => env('BLOCK_BOTS_FAKE_MODE', true), // minutes - disabled by default
2324

2425
/*
2526
* Send suspicious events to log?

src/views/error.blade.php

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8">
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
6+
<meta name="viewport" content="width=device-width, initial-scale=1">
7+
8+
<title>Forbidden</title>
9+
10+
<!-- Fonts -->
11+
<link href="https://fonts.googleapis.com/css?family=Raleway:100,600" rel="stylesheet" type="text/css">
12+
13+
<!-- Styles -->
14+
<style>
15+
html, body {
16+
background-color: #fff;
17+
color: #636b6f;
18+
font-family: 'Raleway', sans-serif;
19+
font-weight: 100;
20+
height: 100vh;
21+
margin: 0;
22+
}
23+
24+
.full-height {
25+
height: 100vh;
26+
}
27+
28+
.flex-center {
29+
align-items: center;
30+
display: flex;
31+
justify-content: center;
32+
}
33+
34+
.position-ref {
35+
position: relative;
36+
}
37+
38+
.content {
39+
text-align: center;
40+
}
41+
42+
.title {
43+
font-size: 36px;
44+
padding: 20px;
45+
}
46+
</style>
47+
</head>
48+
<body>
49+
<div class="flex-center position-ref full-height">
50+
<div class="content">
51+
<div class="title">
52+
You are over the specified limit.
53+
</div>
54+
</div>
55+
</div>
56+
</body>
57+
</html>

0 commit comments

Comments
 (0)