Skip to content

Commit f00e480

Browse files
committed
Return full remote address instead of only IP
1 parent a967ef1 commit f00e480

File tree

5 files changed

+45
-84
lines changed

5 files changed

+45
-84
lines changed

README.md

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -182,17 +182,27 @@ For more details, see the
182182

183183
#### getRemoteAddress()
184184

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

188188
```php
189-
$ip = $connection->getRemoteAddress();
189+
$address = $connection->getRemoteAddress();
190+
echo 'Connection from ' . $address . PHP_EOL;
190191
```
191192

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

196+
Otherwise, it will return the full remote address as a string value.
197+
If this is a TCP/IP based connection and you only want the remote IP, you may
198+
use something like this:
199+
200+
```php
201+
$address = $connection->getRemoteAddress();
202+
$ip = trim(parse_url('tcp://' . $address, PHP_URL_HOST), '[]');
203+
echo 'Connection from ' . $ip . PHP_EOL;
204+
```
205+
196206
## Install
197207

198208
The recommended way to install this library is [through Composer](http://getcomposer.org).

src/Connection.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@ private function parseAddress($address)
3535
return null;
3636
}
3737

38-
return trim(substr($address, 0, strrpos($address, ':')), '[]');
38+
// check if this is an IPv6 address which includes multiple colons but no square brackets
39+
$pos = strrpos($address, ':');
40+
if ($pos !== false && strpos($address, ':') < $pos && substr($address, 0, 1) !== '[') {
41+
$port = substr($address, $pos + 1);
42+
$address = '[' . substr($address, 0, $pos) . ']:' . $port;
43+
}
44+
45+
return $address;
3946
}
4047
}

src/ConnectionInterface.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,27 @@
2727
interface ConnectionInterface extends DuplexStreamInterface
2828
{
2929
/**
30-
* Returns the remote address (client IP) where this connection has been established from
30+
* Returns the remote address (client IP and port) where this connection has been established from
3131
*
32-
* @return string|null remote address (client IP) or null if unknown
32+
* ```php
33+
* $address = $connection->getRemoteAddress();
34+
* echo 'Connection from ' . $address . PHP_EOL;
35+
* ```
36+
*
37+
* If the remote address can not be determined or is unknown at this time (such as
38+
* after the connection has been closed), it MAY return a `NULL` value instead.
39+
*
40+
* Otherwise, it will return the full remote address as a string value.
41+
* If this is a TCP/IP based connection and you only want the remote IP, you may
42+
* use something like this:
43+
*
44+
* ```php
45+
* $address = $connection->getRemoteAddress();
46+
* $ip = trim(parse_url('tcp://' . $address, PHP_URL_HOST), '[]');
47+
* echo 'Connection from ' . $ip . PHP_EOL;
48+
* ```
49+
*
50+
* @return string|null remote address (client IP and port) or null if unknown
3351
*/
3452
public function getRemoteAddress();
3553
}

tests/ConnectionTest.php

Lines changed: 0 additions & 74 deletions
This file was deleted.

tests/FunctionalServerTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public function testEmitsConnectionWithRemoteIp()
4747

4848
Block\sleep(0.1, $loop);
4949

50-
$this->assertEquals('127.0.0.1', $peer);
50+
$this->assertContains('127.0.0.1:', $peer);
5151
}
5252

5353
public function testEmitsConnectionWithRemoteIpAfterConnectionIsClosedByPeer()
@@ -72,7 +72,7 @@ public function testEmitsConnectionWithRemoteIpAfterConnectionIsClosedByPeer()
7272

7373
Block\sleep(0.1, $loop);
7474

75-
$this->assertEquals('127.0.0.1', $peer);
75+
$this->assertContains('127.0.0.1:', $peer);
7676
}
7777

7878
public function testEmitsConnectionWithRemoteNullAddressAfterConnectionIsClosedLocally()
@@ -160,6 +160,6 @@ public function testEmitsConnectionWithRemoteIpv6()
160160

161161
Block\sleep(0.1, $loop);
162162

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

0 commit comments

Comments
 (0)