tramtro / m6web-firewall
提供IP过滤功能的库
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
m6web防火墙
此PHP 5.4+库提供IP过滤功能。
可以使用很多过滤器。
还可以自定义错误处理。
安装
在您的composer.json
中添加此行
{ "require": { "tramtro/m6web-firewall": "^1.0" } }
更新您的供应商
$ composer update tramtro/m6web-firewall
使用方法
基本用法
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(); }
在此示例中,只有以192.168.0开头(但不包括192.168.0.50)和127.0.0.1开头的IP将通过防火墙。
在其他所有情况下,handle()
返回false。
setDefaultState(false)
定义默认防火墙响应(可选 - 默认false)addList($whiteList, 'local', true)
定义$whiteList
列表,称为local
,表示允许(true
)addList($blackList, 'localBad', false);
定义$blackList
列表,称为localBad
,表示拒绝(false
)
条目格式
自定义错误处理
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')
使用防火墙对象和响应(true或false)作为参数调用handleFirewallReturn
运行测试
$ php composer.phar install --dev $ ./vendor/bin/atoum -d Tests
致谢
许可证
防火墙采用MIT许可证。