Skip to content

Commit 1d69548

Browse files
authored
Merge pull request #6 from jprodrigues70/master
It increases code readability, makes it easier to use and adds some new features
2 parents b80f353 + 18c0e37 commit 1d69548

14 files changed

+388
-238
lines changed

README.md

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,62 @@
11
# Laravel Block Bots
22

3-
43
[![Latest Version on Packagist][ico-version]][link-packagist]
54
[![Software License][ico-license]](LICENSE.md)
65
[![Total Downloads][ico-downloads]][link-downloads]
76

8-
97
## Introduction
10-
Laravel Block bots is a pacakge that block bad crawlers, people trying to scrape your website or high-usage users, but lets good and important crawlers such as GoogleBot and Bing pass-thu.
118

9+
Laravel Block bots is a pacakge that block bad crawlers, people trying to scrape your website or high-usage users, but lets good and important crawlers such as GoogleBot and Bing pass-thu.
1210

1311
## Features
12+
1413
- ULTRA fast, less than 1ms increase in each request.
1514
- Verify Crawlers using reverse DNS
1615
- Highly configurable
1716
- Redirect users to a page when they got blocked
1817
- Allow Logged users to always bypass blocks
1918

20-
21-
2219
## Install
2320

2421
Via Composer
25-
``` bash
22+
23+
```bash
2624
composer require potelo/laravel-block-bots
2725
```
26+
2827
#### Requirement
29-
- This package rely on heavly on Redis. To use it, make sure that Redis is configured and ready. (see [Laravel Redis Configuration](https://laravel.com/docs/5.6/redis#configuration))
3028

29+
- This package rely on heavly on Redis. To use it, make sure that Redis is configured and ready. (see [Laravel Redis Configuration](https://laravel.com/docs/5.6/redis#configuration))
3130

3231
#### Before Laravel 5.5
32+
3333
In Laravel 5.4. you'll manually need to register the `\Potelo\LaravelBlockBots\BlockBots::class` service provider in `config/app.php`.
3434

3535
#### Config
36+
3637
To adjust the library, you can publish the config file to your project using:
38+
3739
```
3840
php artisan vendor:publish --provider="Potelo\LaravelBlockBots\BlockBotsServiceProvider"
3941
```
42+
4043
Configure variables in your .env file:
44+
4145
```
42-
BLOCK_BOTS_ENABLED=false
43-
BLOCK_BOTS_ALLOW_LOGGED_USER=true
44-
BLOCK_BOTS_FAKE_MODE=false
45-
BLOCK_BOTS_LOG_BLOCKED_REQUESTS=true
46+
BLOCK_BOTS_ENABLED=true // Enables block bots
47+
BLOCK_BOTS_MODE=production // options: `production` (like a charm), `never` (bypass every route), `always` (blocks every routes)
48+
BLOCK_BOTS_USE_DEFAULT_ALLOWED_BOTS=true // if you want to use our preseted whitelist
49+
BLOCK_BOTS_WHITELIST_KEY=block_bot:whitelist // key for whitelist in Redis
50+
BLOCK_BOTS_FAKE_BOTS_KEY=block_bot:fake_bots // key for fake bots in Redis
51+
BLOCK_BOTS_PENDING_BOTS_KEY=block_bot:pending_bots // key for pending bots in Redis
52+
BLOCK_BOTS_LOG_ENABLED=true // Enables log
53+
4654
```
4755

4856
## Usage
4957

50-
It's simple. Go to `Kernel.php` and add to the `$routeMiddleware` block as :
58+
It's simple. Go to `Kernel.php` and add to the `$routeMiddleware` block as :
59+
5160
```
5261
protected $routeMiddleware = [
5362
...
@@ -68,15 +77,14 @@ Than you can put in the desired groups. For exemple, lets set to the Wrb group:
6877
```
6978

7079
Where:
80+
7181
- **100**: is the number of pages an IP can access every day
7282
- **/limit**: Is the route we going to redirect the IP after the limit
7383

74-
7584
## Change log
7685

7786
Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.
7887

79-
8088
## Contributing
8189

8290
Please see [CONTRIBUTING](CONTRIBUTING.md) and [CODE_OF_CONDUCT](CODE_OF_CONDUCT.md) for details.
@@ -92,7 +100,6 @@ The MIT License (MIT). Please see [License File](LICENSE.md) for more informatio
92100
[ico-version]: https://img.shields.io/packagist/v/potelo/laravel-block-bots.svg?style=flat-square
93101
[ico-license]: https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square
94102
[ico-downloads]: https://img.shields.io/packagist/dt/potelo/laravel-block-bots.svg?style=flat-square
95-
96103
[link-packagist]: https://packagist.org/packages/potelo/laravel-block-bots
97104
[link-downloads]: https://packagist.org/packages/potelo/laravel-block-bots
98105
[link-author]: https://github.com/potelo

src/Abstracts/AbstractBlockBots.php

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
<?php
2+
3+
namespace Potelo\LaravelBlockBots\Abstracts;
4+
5+
use Potelo\LaravelBlockBots\Contracts\Configuration;
6+
use Potelo\LaravelBlockBots\Contracts\Client;
7+
use Carbon\Carbon;
8+
9+
abstract class AbstractBlockBots
10+
{
11+
private $allowedBots = [];
12+
protected $request;
13+
protected $limit;
14+
protected $frequency;
15+
protected $options;
16+
protected $client;
17+
protected $timeOutAt;
18+
protected $hits = 1;
19+
20+
public function __construct()
21+
{
22+
$this->options = new Configuration();
23+
}
24+
25+
public function setUp($request, $limit, $frequency)
26+
{
27+
$this->setRequest($request);
28+
$this->setLimit($limit);
29+
$this->setFrequency($frequency);
30+
$this->setTimeOut($frequency);
31+
$this->setClient();
32+
}
33+
34+
protected function beforeHandle()
35+
{
36+
}
37+
38+
protected function setTimeOut()
39+
{
40+
switch ($this->frequency) {
41+
case 'hourly':
42+
$this->timeOutAt = Carbon::now()->addHour(1)->timestamp;
43+
break;
44+
case 'daily':
45+
$this->timeOutAt = Carbon::tomorrow()->startOfDay()->timestamp;
46+
break;
47+
case 'monthly':
48+
$this->timeOutAt = (new Carbon('first day of next month'))->firstOfMonth()->startOfDay()->timestamp;
49+
break;
50+
case 'annually':
51+
$this->timeOutAt = (new Carbon('next year'))->startOfYear()->firstOfMonth()->startOfDay()->timestamp;
52+
break;
53+
}
54+
}
55+
56+
protected function setRequest($request)
57+
{
58+
$this->request = $request;
59+
}
60+
61+
protected function setLimit($limit)
62+
{
63+
$this->limit = $limit;
64+
}
65+
66+
protected function setFrequency($frequency)
67+
{
68+
$this->frequency = $frequency;
69+
}
70+
71+
protected function setClient()
72+
{
73+
$this->client = new Client($this->request);
74+
}
75+
76+
final protected function getAllowedBots()
77+
{
78+
if ($this->options->use_default_allowed_bots) {
79+
return array_merge($this->options->allowed_bots, $this->allowedBots);
80+
}
81+
return $this->allowedBots;
82+
}
83+
84+
final protected function setAllowedBots($bots)
85+
{
86+
$this->allowedBots = $bots;
87+
}
88+
89+
final protected function isLimitExceeded()
90+
{
91+
return $this->hits > $this->limit;
92+
}
93+
94+
final protected function isTheFirstOverflow()
95+
{
96+
return $this->hits === $this->limit + 1;
97+
}
98+
99+
abstract protected function countHits();
100+
101+
abstract protected function isAllowed();
102+
103+
abstract protected function notAllowed();
104+
}

src/BlockBotsServiceProvider.php

100644100755
Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ public function boot()
2727
__DIR__ . '/config/block-bots.php' => config_path('block-bots.php'),
2828
]);
2929

30-
$this->loadViewsFrom(__DIR__.'/views', 'block-bots');
30+
$this->loadViewsFrom(__DIR__ . '/views', 'block-bots');
3131

3232
$this->publishes([
33-
__DIR__.'/views' => resource_path('views/vendor/block-bots'),
33+
__DIR__ . '/views' => resource_path('views/vendor/block-bots'),
3434
]);
3535
}
3636

@@ -42,9 +42,8 @@ public function boot()
4242
public function register()
4343
{
4444
$this->mergeConfigFrom(
45-
__DIR__.'/config/block-bots.php', 'block-bots'
45+
__DIR__ . '/config/block-bots.php',
46+
'block-bots'
4647
);
4748
}
4849
}
49-
50-

src/Commands/ClearWhitelist.php

100644100755
Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Illuminate\Console\Command;
66
use Illuminate\Support\Facades\Redis;
7+
use Potelo\LaravelBlockBots\Contracts\Configuration;
78

89
class ClearWhitelist extends Command
910
{
@@ -29,6 +30,7 @@ class ClearWhitelist extends Command
2930
public function __construct()
3031
{
3132
parent::__construct();
33+
$this->options = new Configuration();
3234
}
3335

3436
/**
@@ -38,11 +40,7 @@ public function __construct()
3840
*/
3941
public function handle()
4042
{
41-
//
42-
$key_whitelist = "block_bot:whitelist";
43-
$whitelist = Redis::del($key_whitelist);
43+
Redis::del($this->options->whitelist_key);
4444
$this->info("Whitelist cleared");
45-
46-
4745
}
4846
}

src/Commands/ListWhitelist.php

100644100755
Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Illuminate\Console\Command;
66
use Illuminate\Support\Facades\Redis;
7+
use Potelo\LaravelBlockBots\Contracts\Configuration;
78

89
class ListWhitelist extends Command
910
{
@@ -29,6 +30,7 @@ class ListWhitelist extends Command
2930
public function __construct()
3031
{
3132
parent::__construct();
33+
$this->options = new Configuration();
3234
}
3335

3436
/**
@@ -38,14 +40,11 @@ public function __construct()
3840
*/
3941
public function handle()
4042
{
41-
//
42-
$key_whitelist = "block_bot:whitelist";
43-
$whitelist = Redis::smembers($key_whitelist);
43+
$whitelist = Redis::smembers($this->options->whitelist_key);
44+
4445
$this->info("List of IPs whitelisted:");
45-
foreach ($whitelist as $ip)
46-
{
46+
foreach ($whitelist as $ip) {
4747
$this->info($ip);
4848
}
49-
5049
}
5150
}

src/Contracts/Client.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Potelo\LaravelBlockBots\Contracts;
4+
5+
use Illuminate\Support\Facades\Auth;
6+
7+
class Client
8+
{
9+
public $id;
10+
public $ip;
11+
public $userAgent;
12+
13+
public function __construct($request)
14+
{
15+
$this->id = Auth::check() ? Auth::id() : $this->ip;
16+
$this->ip = $request->getClientIp();
17+
$this->userAgent = $request->header('User-Agent');
18+
$this->key = "block_bot:{$this->id}";
19+
$this->logKey = "block_bot:notified:{$this->ip}";
20+
$this->url = substr($request->fullUrl(), strlen($request->getScheme() . "://"));
21+
}
22+
}

src/Contracts/Configuration.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Potelo\LaravelBlockBots\Contracts;
4+
5+
class Configuration
6+
{
7+
public function __construct()
8+
{
9+
foreach (config('block-bots') as $key => $value) {
10+
$this->{$key} = $value;
11+
}
12+
}
13+
}

src/Events/UserBlockedEvent.php

100644100755
File mode changed.

0 commit comments

Comments
 (0)