Skip to content

Commit

Permalink
Merge pull request #6 from jprodrigues70/master
Browse files Browse the repository at this point in the history
It increases code readability, makes it easier to use and adds some new features
  • Loading branch information
bratao authored May 8, 2020
2 parents b80f353 + 18c0e37 commit 1d69548
Show file tree
Hide file tree
Showing 14 changed files with 388 additions and 238 deletions.
37 changes: 22 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,53 +1,62 @@
# Laravel Block Bots


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


## Introduction
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.

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.

## Features

- ULTRA fast, less than 1ms increase in each request.
- Verify Crawlers using reverse DNS
- Highly configurable
- Redirect users to a page when they got blocked
- Allow Logged users to always bypass blocks



## Install

Via Composer
``` bash

```bash
composer require potelo/laravel-block-bots
```

#### Requirement
- 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))

- 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))

#### Before Laravel 5.5

In Laravel 5.4. you'll manually need to register the `\Potelo\LaravelBlockBots\BlockBots::class` service provider in `config/app.php`.

#### Config

To adjust the library, you can publish the config file to your project using:

```
php artisan vendor:publish --provider="Potelo\LaravelBlockBots\BlockBotsServiceProvider"
```

Configure variables in your .env file:

```
BLOCK_BOTS_ENABLED=false
BLOCK_BOTS_ALLOW_LOGGED_USER=true
BLOCK_BOTS_FAKE_MODE=false
BLOCK_BOTS_LOG_BLOCKED_REQUESTS=true
BLOCK_BOTS_ENABLED=true // Enables block bots
BLOCK_BOTS_MODE=production // options: `production` (like a charm), `never` (bypass every route), `always` (blocks every routes)
BLOCK_BOTS_USE_DEFAULT_ALLOWED_BOTS=true // if you want to use our preseted whitelist
BLOCK_BOTS_WHITELIST_KEY=block_bot:whitelist // key for whitelist in Redis
BLOCK_BOTS_FAKE_BOTS_KEY=block_bot:fake_bots // key for fake bots in Redis
BLOCK_BOTS_PENDING_BOTS_KEY=block_bot:pending_bots // key for pending bots in Redis
BLOCK_BOTS_LOG_ENABLED=true // Enables log
```

## Usage

It's simple. Go to `Kernel.php` and add to the `$routeMiddleware` block as :
It's simple. Go to `Kernel.php` and add to the `$routeMiddleware` block as :

```
protected $routeMiddleware = [
...
Expand All @@ -68,15 +77,14 @@ Than you can put in the desired groups. For exemple, lets set to the Wrb group:
```

Where:

- **100**: is the number of pages an IP can access every day
- **/limit**: Is the route we going to redirect the IP after the limit


## Change log

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


## Contributing

Please see [CONTRIBUTING](CONTRIBUTING.md) and [CODE_OF_CONDUCT](CODE_OF_CONDUCT.md) for details.
Expand All @@ -92,7 +100,6 @@ The MIT License (MIT). Please see [License File](LICENSE.md) for more informatio
[ico-version]: https://img.shields.io/packagist/v/potelo/laravel-block-bots.svg?style=flat-square
[ico-license]: https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square
[ico-downloads]: https://img.shields.io/packagist/dt/potelo/laravel-block-bots.svg?style=flat-square

[link-packagist]: https://packagist.org/packages/potelo/laravel-block-bots
[link-downloads]: https://packagist.org/packages/potelo/laravel-block-bots
[link-author]: https://github.com/potelo
104 changes: 104 additions & 0 deletions src/Abstracts/AbstractBlockBots.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php

namespace Potelo\LaravelBlockBots\Abstracts;

use Potelo\LaravelBlockBots\Contracts\Configuration;
use Potelo\LaravelBlockBots\Contracts\Client;
use Carbon\Carbon;

abstract class AbstractBlockBots
{
private $allowedBots = [];
protected $request;
protected $limit;
protected $frequency;
protected $options;
protected $client;
protected $timeOutAt;
protected $hits = 1;

public function __construct()
{
$this->options = new Configuration();
}

public function setUp($request, $limit, $frequency)
{
$this->setRequest($request);
$this->setLimit($limit);
$this->setFrequency($frequency);
$this->setTimeOut($frequency);
$this->setClient();
}

protected function beforeHandle()
{
}

protected function setTimeOut()
{
switch ($this->frequency) {
case 'hourly':
$this->timeOutAt = Carbon::now()->addHour(1)->timestamp;
break;
case 'daily':
$this->timeOutAt = Carbon::tomorrow()->startOfDay()->timestamp;
break;
case 'monthly':
$this->timeOutAt = (new Carbon('first day of next month'))->firstOfMonth()->startOfDay()->timestamp;
break;
case 'annually':
$this->timeOutAt = (new Carbon('next year'))->startOfYear()->firstOfMonth()->startOfDay()->timestamp;
break;
}
}

protected function setRequest($request)
{
$this->request = $request;
}

protected function setLimit($limit)
{
$this->limit = $limit;
}

protected function setFrequency($frequency)
{
$this->frequency = $frequency;
}

protected function setClient()
{
$this->client = new Client($this->request);
}

final protected function getAllowedBots()
{
if ($this->options->use_default_allowed_bots) {
return array_merge($this->options->allowed_bots, $this->allowedBots);
}
return $this->allowedBots;
}

final protected function setAllowedBots($bots)
{
$this->allowedBots = $bots;
}

final protected function isLimitExceeded()
{
return $this->hits > $this->limit;
}

final protected function isTheFirstOverflow()
{
return $this->hits === $this->limit + 1;
}

abstract protected function countHits();

abstract protected function isAllowed();

abstract protected function notAllowed();
}
9 changes: 4 additions & 5 deletions src/BlockBotsServiceProvider.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ public function boot()
__DIR__ . '/config/block-bots.php' => config_path('block-bots.php'),
]);

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

$this->publishes([
__DIR__.'/views' => resource_path('views/vendor/block-bots'),
__DIR__ . '/views' => resource_path('views/vendor/block-bots'),
]);
}

Expand All @@ -42,9 +42,8 @@ public function boot()
public function register()
{
$this->mergeConfigFrom(
__DIR__.'/config/block-bots.php', 'block-bots'
__DIR__ . '/config/block-bots.php',
'block-bots'
);
}
}


8 changes: 3 additions & 5 deletions src/Commands/ClearWhitelist.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Redis;
use Potelo\LaravelBlockBots\Contracts\Configuration;

class ClearWhitelist extends Command
{
Expand All @@ -29,6 +30,7 @@ class ClearWhitelist extends Command
public function __construct()
{
parent::__construct();
$this->options = new Configuration();
}

/**
Expand All @@ -38,11 +40,7 @@ public function __construct()
*/
public function handle()
{
//
$key_whitelist = "block_bot:whitelist";
$whitelist = Redis::del($key_whitelist);
Redis::del($this->options->whitelist_key);
$this->info("Whitelist cleared");


}
}
11 changes: 5 additions & 6 deletions src/Commands/ListWhitelist.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Redis;
use Potelo\LaravelBlockBots\Contracts\Configuration;

class ListWhitelist extends Command
{
Expand All @@ -29,6 +30,7 @@ class ListWhitelist extends Command
public function __construct()
{
parent::__construct();
$this->options = new Configuration();
}

/**
Expand All @@ -38,14 +40,11 @@ public function __construct()
*/
public function handle()
{
//
$key_whitelist = "block_bot:whitelist";
$whitelist = Redis::smembers($key_whitelist);
$whitelist = Redis::smembers($this->options->whitelist_key);

$this->info("List of IPs whitelisted:");
foreach ($whitelist as $ip)
{
foreach ($whitelist as $ip) {
$this->info($ip);
}

}
}
22 changes: 22 additions & 0 deletions src/Contracts/Client.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Potelo\LaravelBlockBots\Contracts;

use Illuminate\Support\Facades\Auth;

class Client
{
public $id;
public $ip;
public $userAgent;

public function __construct($request)
{
$this->id = Auth::check() ? Auth::id() : $this->ip;
$this->ip = $request->getClientIp();
$this->userAgent = $request->header('User-Agent');
$this->key = "block_bot:{$this->id}";
$this->logKey = "block_bot:notified:{$this->ip}";
$this->url = substr($request->fullUrl(), strlen($request->getScheme() . "://"));
}
}
13 changes: 13 additions & 0 deletions src/Contracts/Configuration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Potelo\LaravelBlockBots\Contracts;

class Configuration
{
public function __construct()
{
foreach (config('block-bots') as $key => $value) {
$this->{$key} = $value;
}
}
}
Empty file modified src/Events/UserBlockedEvent.php
100644 → 100755
Empty file.
Loading

0 comments on commit 1d69548

Please sign in to comment.