ircop/antiflood

为Laravel请求洪流保护

v0.1.4 2017-07-28 13:16 UTC

This package is not auto-updated.

Last update: 2024-09-28 18:53:08 UTC


README

此工具设计用于简单限制某些用户请求。它使得添加来自数据库DDoS或暴力破解攻击的保护变得容易。

安装

1: 使用composer安装

composer require ircop/antiflood

2: 在config/app.phpproviders数组中添加服务提供者

Ircop\Antiflood\AntifloodServiceProvider::class,

3: 在config/app.phpaliases数组中添加外观别名

'Antiflood' => Ircop\Antiflood\Facade\Antiflood::class,

用法

使用给定的标识检查记录是否存在。

标识可以是IP地址、登录名、任何唯一标识。

$maximum - 为此标识允许的最大值。默认为1。

\Antiflood::check( $ident, $maximum = 1 );

// Or with IP identity

\Antiflood::checkIP( $maximum = 1 );

将给定的标识的记录放置在给定的$minutes分钟内

\Antiflood::put( $ident, $minutes = 10 );

// Or with IP identity

\Antiflood::putIP( $minutes = 10 );

示例

限制给定IP地址的错误登录尝试

此示例限制从单个IP地址在20分钟内尝试5次错误登录

public function postLogin()
{
	$key = $_SERVER['REMOTE_ADDR'];
	
	// If this ip has >= 5 failed login attempts in last 20 minutes, redirect user
	// back with error:
	if( \Antiflood::check( $key, 5 ) === FALSE )
		return redirect()->back()->withErrors(['Too many login attempts! Try again later.']);
	
	// ....
	// ....
	// ....

	// After failed login put user ipaddr to antiflood cacte on 20 min.
	// If there is no records with this ident, record will be added with value=1, else
	// it will be increased.
	\Antiflood::put( $key, 20 );
}

限制每30分钟尝试一次密码恢复

此代码演示了如何限制某些功能(例如电子邮件等),以防止我们的服务器发生洪流,例如。

public function postPasswordRecover()
{
	$key = \Input::get('email');
	if( \Antiflood::check( $key ) === FALSE )
		return redirect()->back()->withErrors(['.....']);
	
	// ....
	// ....
	// ....

	\Antiflood::put( $key, 20 );
}