tramtro / m6web-firewall
Library providing IP filtering features
v1.0.5
2024-09-21 20:34 UTC
Requires
- php: >=5.4.0
- ext-bcmath: *
Requires (Dev)
- atoum/atoum: ^2.8|^3.0
README
Firewall by m6web
This PHP 5.4+ library provides IP filtering features.
A lot of filters can be used.
It is also possible to customize the error handling.
Installation
Add this line in your composer.json
:
{ "require": { "tramtro/m6web-firewall": "^1.0" } }
Update your vendors :
$ composer update tramtro/m6web-firewall
Usage
Basic usage
use nguyenanhung\Component\Firewall\Firewall; $whiteList = array( '127.0.0.1', '192.168.0.*', ); $blackList = array( '192.168.0.50', ); $firewall = new Firewall(); $connAllowed = $firewall ->setDefaultState(false) ->addList($whiteList, 'local', true) ->addList($blackList, 'localBad', false) ->setIpAddress('195.88.195.146') ->handle() ; if (!$connAllowed) { http_response_code(403); // Forbidden exit(); }
In this example, only IPs starting with 192.168.0 (but not 192.168.0.50) and 127.0.0.1 will be allowed by the firewall.
In all other case handle()
return false.
setDefaultState(false)
defines default firewall response (Optional - Default false),addList($whiteList, 'local', true)
defines$whiteList
list, calledlocal
as allowed (true
),addList($blackList, 'localBad', false);
defines$blackList
list, calledlocalBad
as rejected (false
).
Entries Formats
Custom error handling
use nguyenanhung\Component\Firewall\Firewall; function handleFirewallReturn(Firewall $firewall, $response) { if (false === $response) { header($_SERVER["SERVER_PROTOCOL"]." 403 Forbiden"); exit(); } return $response; } $whiteList = array( '127.0.0.1', '198.168.0.*', ); $blackList = array( '192.168.0.50', ); $firewall = new Firewall(); $firewall ->setDefaultState(true) ->addList($whiteList, 'local', true) ->addList($blackList, 'localBad', false) ->setIpAddress('195.88.195.146') ->handle('handleFirewallReturn') ;
handle('handleFirewallReturn')
calls handleFirewallReturn
with Firewall object and response as arguments (true or false).
Running the tests
$ php composer.phar install --dev $ ./vendor/bin/atoum -d Tests
Credits
Developped by the Cytron Team of M6 Web.
Tested with atoum.
License
Firewall is licensed under the MIT license.