Skip to content

Commit 726d503

Browse files
authored
Merge pull request #65 from clue-labs/remote-address
Return full remote address instead of only IP
2 parents f1e457d + f00e480 commit 726d503

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
@@ -309,17 +309,27 @@ For more details, see the
309309

310310
#### getRemoteAddress()
311311

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

315315
```php
316-
$ip = $connection->getRemoteAddress();
316+
$address = $connection->getRemoteAddress();
317+
echo 'Connection from ' . $address . PHP_EOL;
317318
```
318319

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

323+
Otherwise, it will return the full remote address as a string value.
324+
If this is a TCP/IP based connection and you only want the remote IP, you may
325+
use something like this:
326+
327+
```php
328+
$address = $connection->getRemoteAddress();
329+
$ip = trim(parse_url('tcp://' . $address, PHP_URL_HOST), '[]');
330+
echo 'Connection from ' . $ip . PHP_EOL;
331+
```
332+
323333
## Install
324334

325335
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,7 +160,7 @@ public function testEmitsConnectionWithRemoteIpv6()
160160

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

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

166166
public function testAppliesContextOptionsToSocketStreamResource()

0 commit comments

Comments
 (0)