metarush/firewall
Composer 安装命令:
composer require metarush/firewall
包简介
A firewall for web applications that wants to blacklist/whitelist IP addresses
README 文档
README
A firewall library that web apps can use to ban IP addresses temporarily or for an extended period of time
Sample Use Case
When a user fails an action in your app (e.g., login) for 5 times, this library will lock them temporarily for 15 minutes. After that, they can try again for a maximum of 5 temporary locks. After that, they will be locked for a extended period of 24 hours.
Summary
- 5 fails within a 15-minute period = 15-minute temporary lock
- 5 temporary locks = 24-hour lock
Note: These settings can be changed
Install
Install via composer as metarush/firewall
Setup
-
Create a database (with PDO support e.g., MySQL, SQLite).
-
Create tables with these names:
-
mrfwTempBan -
mrfwExtendedBan -
mrfwWhitelist -
mrfwFailCount -
mrfwBlockCountNote: You can use different table names but these are the default names
-
-
Each table must have the following fields:
ip(STRINGwith46length )dateTime(STRINGwith19length)
Use the appropriate column type for your database flavor. E.g., dateTime
will store dates in Y-m-d H:i:s format so use DATETIME column type if your
database is MySQL.
Sample create table query for MySQL
CREATE TABLE `tempBan` (
`ip` VARCHAR(46),
`dateTime` DATETIME
) ENGINE=MyISAM;
Usage with default settings
Init library
<?php
$builder = (new \MetaRush\Firewall\Builder)
->setDsn('mysql:host=localhost;dbname=yourfirewalldb')
->setDbUser('user')
->setDbPass('pass');
$fw = $builder->build();
Basic usage in your login code
$fw->flushExpired(); // put this on top
if ($fw->banned($_SERVER['REMOTE_ADDR'])) {
exit('Forbidden'); // or redirect somewhere else
}
if ($_POST['password'] != 'foo') {
$fw->preventBruteForce($_SERVER['REMOTE_ADDR']);
// show your error page
exit('Invalid login');
} else {
// release IP from block counters
$fw->flushIp($_SERVER['REMOTE_ADDR']);
// proceed to login...
}
Custom settings
You can append the following methods upon class initialization:
Table names
If you named your tables differently, let the system know via:
->setTempBanTable('tempBan')
->setExtendedBanTable('extendedBan')
->setWhitelistTable('whitelist')
->setFailCountTable('failCount')
->setBlockCountTable('blockCount')
Max fail count before temporary ban
->setMaxFailCount(5)
Temporary ban seconds
->setTempBanSeconds(900) // 15 minutes
Max temporary ban before extended ban
->setMaxBlockCount(5)
Extended ban seconds
->setExtendedBanSeconds(86400) // 1 day
Period wherein failed attempts are counted as candidate for temporary ban
->setFailCountSeconds(900) // 15 minutes
Period wherein temporary locks are counted as candidate for extended ban
->setBlockCountSeconds(86400) // 1 day
Whitelist seconds
->setWhitelistSeconds(2592000) // 30 days
Note: The values displayed in the parameter are their default values. Each of these setter methods have their corresponding getter methods. E.g., getMaxFailCount();
Set a PSR-3 compatible logger of your choice
->setLogger(use Psr\Log\LoggerInterface $logger);
Apply custom settings example
$builder = (new \MetaRush\Firewall\Builder)
->setDsn('mysql:host=localhost;dbname=foo')
->setDbUser('user')
->setDbPass('pass')
->setTempBanTable('tempBan')
->setExtendedBanTable('extendedBan')
->setWhitelistTable('whitelist')
->setFailCountTable('failCount')
->setBlockCountTable('blockCount')
->setMaxFailCount(5)
->setTempBanSeconds(900)
->setMaxBlockCount(5)
->setExtendedBanSeconds(86400)
->setWhitelistSeconds(2592000)
->setLogger(new Psr\Log\NullLogger()); // use your own logger
$fw = $builder->build();
Service methods
You can use the following methods for your custom needs:
tempBan
Ban $ip temporarily
tempBan(string $ip): void
extendedBan
Ban $ip for an extended period
extendedBan(string $ip): void
banned
Returns true if $ip is banned (temporarily or extended), false otherwise
banned(string $ip): bool
whitelist
Whitelist $ip so it won't be banned no matter what
whitelist(string $ip): void
whitelisted
Returns true if $ip is whitelisted, false otherwise
whitelisted(string $ip): bool
preventBruteForce
Temporarily ban $ip if getMaxFailCount() is reached then ban $ip for an extended period if getMaxBlockCount() is reached
preventBruteForce(string $ip): void
flushExpired
Release all IPs that are banned (temp/extended) and whitelisted for more than the set limit
flushExpired(): void
Note: Run this on top of your script or via cron regularly
flushTempBanned
Release IPs that are temporarily banned regardless of expiration time
flushTempBanned(): void
flushExtendedBanned
Release IPs that are banned for an extended period regardless of expiration time
flushExtendedBanned(): void
flushWhitelisted
flushWhitelisted(): void
Release IPs that are whitelisted regardless of expiration time
flushIp
Release IP in all "block" tables and optionally release in whitelist table
flushIp(string $ip, bool $alsoWhitelistTable = false): void
metarush/firewall 适用场景与选型建议
metarush/firewall 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 234 次下载、GitHub Stars 达 4, 最近一次更新时间为 2019 年 02 月 02 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「firewall」 「throttle」 「rate limiting」 「blacklist」 「whitelist」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 metarush/firewall 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 metarush/firewall 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 metarush/firewall 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
PHP Basic Firewall
A Laravel Pulse card to track users & URL's that are being throttled by your Rate Limiters
hyperf cache counter rate limiter
Web Application Firewall (WAF) package for Laravel
PHPIDS (PHP-Intrusion Detection System) is a simple to use, well structured, fast and state-of-the-art security layer for your PHP based web application
Alfabank REST API integration
统计信息
- 总下载量: 234
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 5
- 点击次数: 26
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-02-02