PHPackages                             ryudith/mezzio-block-ip - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [HTTP &amp; Networking](/categories/http)
4. /
5. ryudith/mezzio-block-ip

ActiveLibrary[HTTP &amp; Networking](/categories/http)

ryudith/mezzio-block-ip
=======================

Middleware library to block IP request for Mezzio framework.

v1.0.0(3y ago)04Apache-2.0PHPPHP ^8.0.0

Since May 30Pushed 3y ago1 watchersCompare

[ Source](https://github.com/ryudith/mezzio-block-ip)[ Packagist](https://packagist.org/packages/ryudith/mezzio-block-ip)[ RSS](/packages/ryudith-mezzio-block-ip/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (2)Dependencies (4)Versions (2)Used By (0)

**mezzio-block-ip**
===================

[](#mezzio-block-ip)

**`Ryudith\MezzioBlockIp`** is middleware to block IP based on request.

> Version 1.1.0 remove code auto register route when helper configuration is enable (`enable_helper`) also remove `enable_helper` from configuration and add CLI helper version.
> To enable helper please read [enable helper](#enable_helper) below.

**Installation**
----------------

[](#installation)

To install run command :

```
$ composer require ryudith/mezzio-block-ip
```

**Usage**
---------

[](#usage)

#### **Add `Ryudith\MezzioBlockIp\ConfigProvider`** to **`config/config.php`**

[](#add-ryudithmezzioblockipconfigprovider-to-configconfigphp)

```
...

$aggregator = new ConfigAggregator([
    ...
    \Laminas\Diactoros\ConfigProvider::class,

    \Ryudith\MezzioBlockIp\ConfigProvider::class,  // pipe(ErrorHandler::class);
    $app->pipe(BlockIPMiddleware::class);  //  You can place `$app->pipe(BlockIPMiddleware::class)` before `$app->pipe(ErrorHandler::class)` if you want.

**Custom Configuration**
------------------------

[](#custom-configuration)

Configuration is locate in **`vendor/ryudith/mezzio-block-ip/ConfigProvider.php`** :

```
...

return [
    'dependencies' => [
        'factories' => [
            FileSystemStorage::class => FileSystemStorageFactory::class,
            SimpleResponse::class => SimpleResponseFactory::class,
            BlockIPMiddleware::class => BlockIPMiddlewareFactory::class,
        ],
    ],
    'mezzio_block_ip' => [
        'limit_hit' => 100,
        'limit_duration' => 60,
        'request_real_ip_key' => 'REMOTE_ADDR',
        'ip_data_dir' => './data/blockip',
        'blacklist_data_dir' => './data/blacklistip',
        'whitelist_data_dir' => './data/whitelistip',
        'file_data_delimiter' => '||',
        'ip_storage_class' => FileSystemStorage::class,
        'ip_response_class' => SimpleResponse::class,
        'admin_whitelist_ip' => []
    ],
];

...
```

Detail :

1. **`limit_hit`**
    is how many request hit per **limit\_duration** before blocked.
2. **`limit_duration`**
    is duration for **limit\_hit** in second.
3. **`request_real_ip_key`**
    is assoc key for get IP from $\_SERVER or $\_ENV variable.
4. **`ip_data_dir`**
    is directory location to save record request IP, since the storage implementation is file system based.
5. **`blacklist_data_dir`**
    is directory location to save blacklist file.
6. **`whitelist_data_dir`**
    is directory location to save whitelist file.
7. **`file_data_delimiter`**
    is data delimiter inside file.
8. **`ip_storage_class`**
    is implementation class of **`Ryudith\MezzioBlockIp\Storage\StorageInterface`** that will do work to check, create, delete blacklist or whitelist data.
9. **`ip_response_class`**
    is implementation class of **`Ryudith\MezzioBlockIp\SimpleResponse\SimpleResponseInterface`** that will give response from blacklist IP.
10. **`admin_whitelist_ip`**
    is string array whitelist permanent IP for admin.

Enable Helper
------------------------------------------------------

[](#enable-helper)

To enable helper add the following items to `factories` configuration (usually in `config/dependencies.global.php`) :

```
...

'factories' => [

    ...

    // add this to enable web version helper
    Ryudith\MezzioBlockIp\Helper\BlockIPHandler::class => \Ryudith\MezzioBlockIp\Helper\BlockIPHandlerFactory::class,

    // add this to enable cli version helper
    Ryudith\MezzioBlockIp\Helper\BlockIPCli::class => \Ryudith\MezzioBlockIp\Helper\BlockIPCliFactory::class,

    ...
]

...
```

Then for web helper register helper to `routes.php`

```
$app->get('/blockip/blacklist', Ryudith\MezzioBlockIp\Helper\BlockIPHandler::class);
$app->get('/blockip/whitelist', Ryudith\MezzioBlockIp\Helper\BlockIPHandler::class);
```

> Make sure your `blacklist_uri_path` and `whitelist_uri_path` value is match to your route path.

For web helper you can change default configuration values as listed below by add array key to `mezzio_block_ip` configuration, also **don't forget** to add your IP to whitelist to be able access helper.

1. **`helper_url_param_op`**
    is URL query parameter key for helper operation. Default '**op**' and the option value is '**add**' or '**delete**'.
2. **`helper_url_param_ip`**
    is URL query parameter key for helper data IP, default value '**ip**'.
3. **`blacklist_uri_path`**
    is URI path for blacklist helper operation.
4. **`whitelist_uri_path`**
    is URI path for whitelist helper operation.

> ### Change default configuration web helper value
>
> [](#change-default-configuration-web-helper-value)
>
> Create new file `config/autoload/blockip.local.php` and add :
>
> ```
>
