middlewares / firewall
Middleware to provide IP filtering
Installs: 275 462
Dependents: 4
Suggesters: 0
Security: 0
Stars: 13
Watchers: 3
Forks: 3
Open Issues: 0
Requires
- php: ^7.2 || ^8.0
- middlewares/utils: ^3.0
- mlocati/ip-lib: ^1.18
- psr/http-server-middleware: ^1.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.0
- laminas/laminas-diactoros: ^2.3
- oscarotero/php-cs-fixer-config: ^1.0
- phpstan/phpstan: ^0.12 || ^1.0
- phpunit/phpunit: ^8|^9
- squizlabs/php_codesniffer: ^3.0
README
Middleware to provide IP filtering.
Requirements
- PHP >= 7.2
- A PSR-7 http library
- A PSR-15 middleware dispatcher
Installation
This package is installable and autoloadable via Composer as middlewares/firewall.
composer require middlewares/firewall
Example
Dispatcher::run([ (new Middlewares\Firewall(['123.0.0.*'])) ->blacklist([ '123.0.0.1', '123.0.0.2', ]) ]);
Usage
The constructor accepts an array with the whitelist ips. See the ip formats allowed.
$firewall = new Middlewares\Firewall([ '127.0.0.1', '198.168.0.*', ]);
Optionally, you can provide a Psr\Http\Message\ResponseFactoryInterface
as the second argument to create the error response (403
). If it's not defined, Middleware\Utils\Factory will be used to detect it automatically.
$responseFactory = new MyOwnResponseFactory(); $firewall = new Middlewares\Firewall($whitelist, $responseFactory);
blacklist
The blacklist ips. The ip format is the same than whitelist.
$whitelist = [ '127.0.0.1', '198.168.0.*', ]; $blacklist = [ '192.168.0.50', ]; $firewall = (new Middlewares\Firewall($whitelist))->blacklist($blacklist);
ipAttribute
By default uses the REMOTE_ADDR
server parameter to get the client ip. Use this option if you want to use a request attribute. Useful to combine with any ip detection middleware, for example client-ip:
Dispatcher::run([ //detect the client ip and save it in client-ip attribute new Middlewares\ClientIP(), //use that attribute (new Middlewares\Firewall(['123.0.0.*'])) ->ipAttribute('client-ip') ]);
Please see CHANGELOG for more information about recent changes and CONTRIBUTING for contributing details.
The MIT License (MIT). Please see LICENSE for more information.