Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
composer.phar
/vendor/

# Commit your application's lock file http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file
# You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file
# composer.lock
/.idea/
/composer.lock
61 changes: 0 additions & 61 deletions IpRateLimiter.php

This file was deleted.

56 changes: 33 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ The preferred way to install this extension is through [composer](http://getcomp
Either run

```
php composer.phar require ethercreative/yii2-ip-ratelimiter "1.*"
php composer.phar require andreyv/yii2-ip-ratelimiter "2.*"
```

or add

```
"ethercreative/yii2-ip-ratelimiter": "1.*"
"andreyv/yii2-ip-ratelimiter": "2.*"
```

to the require section of your `composer.json` file.
Expand All @@ -28,26 +28,36 @@ Modify the bahavior method of the controller you want to rate limit
```
public function behaviors()
{
$behaviors = parent::behaviors();
$behaviors['rateLimiter'] = [
// Use class
'class' => \ethercreative\ratelimiter\RateLimiter::className(),

// The maximum number of allowed requests
'rateLimit' => 100,

// The time period for the rates to apply to
'timePeriod' => 600,

// Separate rate limiting for guests and authenticated users
// Defaults to true
// - false: use one set of rates, whether you are authenticated or not
// - true: use separate ratesfor guests and authenticated users
'separateRates' => false,

// Whether to return HTTP headers containing the current rate limiting information
'enableRateLimitHeaders' => false,
];
return $behaviors;
$behaviors = parent::behaviors();
$behaviors['rateLimiter'] = [
// Use class
'class' => \andreyv\ratelimiter\IpRateLimiter::class,

// The maximum number of allowed requests
'rateLimit' => 100,

// The time period for the rates to apply to
'timePeriod' => 600,

// Separate rate limiting for guests and authenticated users
// Defaults to false
// - false: use one set of rates, whether you are authenticated or not
// - true: use separate ratesfor guests and authenticated users
'separateRates' => true,

// Whether to return HTTP headers containing the current rate limiting information
'enableRateLimitHeaders' => false,

// Array of actions on which to apply ratelimiter, if empty - applies to all actions
'actions' => ['index'],

// Allows to skip rate limiting for test environment
'testMode' => true,
// Defines whether proxy enabled, list of headers getting from request ipHeaders. By default ['X-Forwarded-For']
'proxyEnabled' => false
];
return $behaviors;
}
```

Forked from ethercreative/yii2-ip-ratelimiter.
40 changes: 23 additions & 17 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,23 +1,29 @@
{
"name": "ethercreative/yii2-ip-ratelimiter",
"description": "Allow guest clients to be rate limited, using their IP as the identifier.",
"type": "yii2-module",
"keywords": ["yii2", "ratelimiter"],
"license": "MIT",
"authors": [
{
"name": "Matt Edmonston",
"email": "matt@ethercreative.co.uk"
},
"name": "andreyv/yii2-ip-ratelimiter",
"description": "Allow guest clients to be rate limited, using their IP as the identifier.",
"type": "yii2-module",
"keywords": ["yii2", "ratelimiter"],
"license": "MIT",
"authors": [
{
"name": "Matt Edmonston",
"email": "matt@ethercreative.co.uk"
},
{
"name": "Yaroslav Lukyanov",
"email": "c_sharp@mail.ru"
},
{
"name": "Andreyv V",
"email": "skifbrt@gmail.com"
}
],
"require": {
"yiisoft/yii2": "*"
},
"autoload": {
"psr-4": {
"andreyv\\ratelimiter\\": "src"
}
],
"require": {},
"autoload": {
"psr-4": {
"ethercreative\\ratelimiter\\": ""
}
}
}
}
9 changes: 2 additions & 7 deletions IpRateLimitInterface.php → src/IpRateLimitInterface.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
<?php

namespace ethercreative\ratelimiter;
namespace andreyv\ratelimiter;

use yii\filters\RateLimitInterface;

/**
* Interface IpRateLimitInterface
*
* @package ethercreative\ratelimiter
*/
interface IpRateLimitInterface extends RateLimitInterface
{
/**
Expand All @@ -20,5 +15,5 @@ interface IpRateLimitInterface extends RateLimitInterface
*
* @return static
*/
public static function findByIp($ip, $rateLimit, $timePeriod);
public static function create($ip, $rateLimit, $timePeriod);
}
72 changes: 72 additions & 0 deletions src/IpRateLimiter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

namespace andreyv\ratelimiter;

use Yii;
use yii\filters\RateLimiter;

class IpRateLimiter extends RateLimiter
{
/**
* @var boolean whether to separate rate limiting between non and authenticated users
*/
public $separateRates = false;

/**
* @var integer the maximum number of allowed requests
*/
public $rateLimit = 5;

/**
* @var integer the time period for the rates to apply to
*/
public $timePeriod = 1;

/**
* @var array list of actions on which to apply ratelimiter, if empty - applies to all actions
*/
public $actions = [];

/**
* @var bool allows to skip rate limiting for test environment
*/
public $testMode = false;

/**
* @var bool defines whether proxy enabled
*/
public $proxyEnabled = false;

/**
* @inheritdoc
*/
public function beforeAction($action)
{
if ($this->testMode) {
return true;
}

if (is_array($this->actions) && (empty($this->actions) || in_array($action->id, $this->actions))) {
if ($this->separateRates && !$this->user) {
$this->user = Yii::$app->getUser() ? Yii::$app->getUser()->getIdentity(false) : null;
}

if (!$this->user) {
/** @var IpRateLimitInterface $identityClass */
$identityClass = Yii::$app->getUser()->identityClass;
if (!in_array(UserIdentity::class, class_implements($identityClass))) {
$identityClass = UserIdentity::class;
}

$this->user = $identityClass::create(
$this->request->getUserIP(),
$this->rateLimit,
$this->timePeriod
);
}

return parent::beforeAction($action);
}
return true;
}
}
11 changes: 3 additions & 8 deletions UserExample.php → src/UserIdentity.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
<?php

namespace ethercreative\ratelimiter;
namespace andreyv\ratelimiter;

use Yii;

/**
* Class UserExample
*
* @package ethercreative\ratelimiter
*/
class UserExample extends \yii\base\Model implements IpRateLimitInterface
class UserIdentity implements IpRateLimitInterface
{
/**
* @var string IP of the user
Expand All @@ -29,7 +24,7 @@ class UserExample extends \yii\base\Model implements IpRateLimitInterface
/**
* @inheritdoc
*/
public static function findByIp($ip, $rateLimit, $timePeriod)
public static function create($ip, $rateLimit, $timePeriod)
{
$user = new static();

Expand Down