Skip to content

Commit

Permalink
Merge pull request #2 from andrey-tm/fix-ip-header
Browse files Browse the repository at this point in the history
fixed getting user IP
  • Loading branch information
ch-tm authored Jan 18, 2023
2 parents ccc689b + af6386f commit b002df8
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 18 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ public function behaviors()
// 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;
}
Expand Down
19 changes: 1 addition & 18 deletions src/IpRateLimiter.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,6 @@ class IpRateLimiter extends RateLimiter
*/
public $proxyEnabled = false;

/**
* @var string tag in headers containing IP list
*/
public $proxyIpHeader = 'X-Forwarded-For';

/**
* @inheritdoc
*/
Expand All @@ -64,7 +59,7 @@ public function beforeAction($action)
}

$this->user = $identityClass::create(
$this->prepareIpListString(),
$this->request->getUserIP(),
$this->rateLimit,
$this->timePeriod
);
Expand All @@ -74,16 +69,4 @@ public function beforeAction($action)
}
return true;
}

/*
When used trusted proxy, we need use limit by combination ip proxy server and X-Forwarded-For header.
*/
protected function prepareIpListString()
{
$ipList = [$this->request->getRemoteIP()];
if ($this->proxyEnabled && $this->request->headers->has($this->proxyIpHeader)) {
$ipList[] = $this->request->headers->get($this->proxyIpHeader);
}
return implode(', ', $ipList);
}
}

0 comments on commit b002df8

Please sign in to comment.