Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return local IP when proxy IP detection fails #4

Merged
merged 1 commit into from
Jul 13, 2017
Merged
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
71 changes: 58 additions & 13 deletions src/ClientIp.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,32 +107,77 @@ public function process(ServerRequestInterface $request, DelegateInterface $dele
*/
private function getIp(ServerRequestInterface $request)
{
if ($this->remote) {
$ip = file_get_contents('http://ipecho.net/plain');
$remoteIp = $this->getRemoteIp();

if (self::isValid($ip)) {
return $ip;
}
if (!empty($remoteIp)) {
// Found IP address via remote service.
return $remoteIp;
}

$ip = null;
$server = $request->getServerParams();
$localIp = $this->getLocalIp($request);

if (!empty($server['REMOTE_ADDR']) && self::isValid($server['REMOTE_ADDR'])) {
$ip = $server['REMOTE_ADDR'];
if ($this->proxyIps && !in_array($localIp, $this->proxyIps)) {
// Local IP address does not point at a known proxy, do not attempt
// to read proxied IP address.
return $localIp;
}

if (empty($this->proxyHeaders) || (!empty($this->proxyIps) && !in_array($ip, $this->proxyIps))) {
return $ip;
$proxiedIp = $this->getProxiedIp($request);

if (!empty($proxiedIp)) {
// Found IP address via proxy-defined headers.
return $proxiedIp;
}

foreach ($this->proxyHeaders as $name) {
if ($request->hasHeader($name) && ($ip = self::getHeaderIp($request->getHeaderLine($name))) !== null) {
return $localIp;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@oscarotero okay, functionality should be a match with existing code. Verify for me?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems good to me

}

/**
* Returns the IP address from remote service.
*
* @return string|null
*/
private function getRemoteIp()
{
if ($this->remote) {
$ip = file_get_contents('http://ipecho.net/plain');
if (self::isValid($ip)) {
return $ip;
}
}
}

/**
* Returns the first valid proxied IP found.
*
* @return string|null
*/
private function getProxiedIp(ServerRequestInterface $request)
{
foreach ($this->proxyHeaders as $name) {
if ($request->hasHeader($name)) {
$ip = self::getHeaderIp($request->getHeaderLine($name));
if (self::isValid($ip)) {
return $ip;
}
}
}
}

/**
* Returns the remote address of the request, if valid.
*
* @return string|null
*/
private function getLocalIp(ServerRequestInterface $request)
{
$server = $request->getServerParams();

if (!empty($server['REMOTE_ADDR']) && self::isValid($server['REMOTE_ADDR'])) {
return $server['REMOTE_ADDR'];
}
}

/**
* Returns the first valid ip found in the header.
*
Expand Down