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 full remote address instead of only IP #65

Merged
merged 1 commit into from
Jan 27, 2017
Merged
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
18 changes: 14 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,17 +182,27 @@ For more details, see the

#### getRemoteAddress()

The `getRemoteAddress(): ?string` method returns the remote address
(client IP) where this connection has been established from.
The `getRemoteAddress(): ?string` method returns the full remote address
(client IP and port) where this connection has been established from.

```php
$ip = $connection->getRemoteAddress();
$address = $connection->getRemoteAddress();
echo 'Connection from ' . $address . PHP_EOL;
```

It will return the remote address as a string value.
If the remote address can not be determined or is unknown at this time (such as
after the connection has been closed), it MAY return a `NULL` value instead.

Otherwise, it will return the full remote address as a string value.
If this is a TCP/IP based connection and you only want the remote IP, you may
use something like this:

```php
$address = $connection->getRemoteAddress();
$ip = trim(parse_url('tcp://' . $address, PHP_URL_HOST), '[]');
echo 'Connection from ' . $ip . PHP_EOL;
```

## Install

The recommended way to install this library is [through Composer](http://getcomposer.org).
Expand Down
9 changes: 8 additions & 1 deletion src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ private function parseAddress($address)
return null;
}

return trim(substr($address, 0, strrpos($address, ':')), '[]');
// check if this is an IPv6 address which includes multiple colons but no square brackets
$pos = strrpos($address, ':');
if ($pos !== false && strpos($address, ':') < $pos && substr($address, 0, 1) !== '[') {
$port = substr($address, $pos + 1);
$address = '[' . substr($address, 0, $pos) . ']:' . $port;
}

return $address;
}
}
22 changes: 20 additions & 2 deletions src/ConnectionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,27 @@
interface ConnectionInterface extends DuplexStreamInterface
{
/**
* Returns the remote address (client IP) where this connection has been established from
* Returns the remote address (client IP and port) where this connection has been established from
*
* @return string|null remote address (client IP) or null if unknown
* ```php
* $address = $connection->getRemoteAddress();
* echo 'Connection from ' . $address . PHP_EOL;
* ```
*
* If the remote address can not be determined or is unknown at this time (such as
* after the connection has been closed), it MAY return a `NULL` value instead.
*
* Otherwise, it will return the full remote address as a string value.
* If this is a TCP/IP based connection and you only want the remote IP, you may
* use something like this:
*
* ```php
* $address = $connection->getRemoteAddress();
* $ip = trim(parse_url('tcp://' . $address, PHP_URL_HOST), '[]');
* echo 'Connection from ' . $ip . PHP_EOL;
* ```
*
* @return string|null remote address (client IP and port) or null if unknown
*/
public function getRemoteAddress();
}
74 changes: 0 additions & 74 deletions tests/ConnectionTest.php

This file was deleted.

6 changes: 3 additions & 3 deletions tests/FunctionalServerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function testEmitsConnectionWithRemoteIp()

Block\sleep(0.1, $loop);

$this->assertEquals('127.0.0.1', $peer);
$this->assertContains('127.0.0.1:', $peer);
}

public function testEmitsConnectionWithRemoteIpAfterConnectionIsClosedByPeer()
Expand All @@ -72,7 +72,7 @@ public function testEmitsConnectionWithRemoteIpAfterConnectionIsClosedByPeer()

Block\sleep(0.1, $loop);

$this->assertEquals('127.0.0.1', $peer);
$this->assertContains('127.0.0.1:', $peer);
}

public function testEmitsConnectionWithRemoteNullAddressAfterConnectionIsClosedLocally()
Expand Down Expand Up @@ -160,6 +160,6 @@ public function testEmitsConnectionWithRemoteIpv6()

Block\sleep(0.1, $loop);

$this->assertEquals('::1', $peer);
$this->assertContains('[::1]:', $peer);
}
}