Skip to content

Commit f176425

Browse files
committed
Fix connecting to IPv6 address via SOCSK5
1 parent f42f2db commit f176425

2 files changed

Lines changed: 34 additions & 2 deletions

File tree

src/Server.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,9 +320,15 @@ public function handleSocks5(ConnectionInterface $stream, $auth=null, StreamRead
320320

321321
public function connectTarget(ConnectionInterface $stream, array $target)
322322
{
323+
$uri = $target[0];
324+
if (strpos($uri, ':') !== false) {
325+
$uri = '[' . $uri . ']';
326+
}
327+
$uri = $uri . ':' . $target[1];
328+
323329
$stream->emit('target', $target);
324330
$that = $this;
325-
$connecting = $this->connector->connect($target[0] . ':' . $target[1]);
331+
$connecting = $this->connector->connect($uri);
326332

327333
$stream->on('close', function () use ($connecting) {
328334
$connecting->cancel();

tests/ServerTest.php

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ public function testHandleSocksConnectionWillEndOnInvalidData()
152152
$connection->emit('data', array('asdasdasdasdasd'));
153153
}
154154

155-
public function testHandleSocksConnectionWillEstablishOutgoingConnection()
155+
public function testHandleSocks4ConnectionWillEstablishOutgoingConnection()
156156
{
157157
$connection = $this->getMockBuilder('React\Socket\Connection')->disableOriginalConstructor()->setMethods(array('pause', 'end'))->getMock();
158158

@@ -165,6 +165,32 @@ public function testHandleSocksConnectionWillEstablishOutgoingConnection()
165165
$connection->emit('data', array("\x04\x01" . "\x00\x50" . pack('N', ip2long('127.0.0.1')) . "\x00"));
166166
}
167167

168+
public function testHandleSocks5ConnectionWillEstablishOutgoingConnection()
169+
{
170+
$connection = $this->getMockBuilder('React\Socket\Connection')->disableOriginalConstructor()->setMethods(array('pause', 'end', 'write'))->getMock();
171+
172+
$promise = new Promise(function () { });
173+
174+
$this->connector->expects($this->once())->method('connect')->with('127.0.0.1:80')->willReturn($promise);
175+
176+
$this->server->onConnection($connection);
177+
178+
$connection->emit('data', array("\x05\x01\x00" . "\x05\x01\x00\x01" . pack('N', ip2long('127.0.0.1')) . "\x00\x50"));
179+
}
180+
181+
public function testHandleSocks5ConnectionWithIpv6WillEstablishOutgoingConnection()
182+
{
183+
$connection = $this->getMockBuilder('React\Socket\Connection')->disableOriginalConstructor()->setMethods(array('pause', 'end', 'write'))->getMock();
184+
185+
$promise = new Promise(function () { });
186+
187+
$this->connector->expects($this->once())->method('connect')->with('[::1]:80')->willReturn($promise);
188+
189+
$this->server->onConnection($connection);
190+
191+
$connection->emit('data', array("\x05\x01\x00" . "\x05\x01\x00\x04" . inet_pton('::1') . "\x00\x50"));
192+
}
193+
168194
public function testHandleSocksConnectionWillCancelOutputConnectionIfIncomingCloses()
169195
{
170196
$connection = $this->getMockBuilder('React\Socket\Connection')->disableOriginalConstructor()->setMethods(array('pause', 'end'))->getMock();

0 commit comments

Comments
 (0)