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

Fix SOCKS5 client receiving destination hostnames #64

Merged
merged 1 commit into from
Sep 1, 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
8 changes: 4 additions & 4 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -349,15 +349,15 @@ private function handleSocks5(ConnectionInterface $stream, $host, $port, StreamR
if ($data['type'] === 0x01) {
// IPv4 address => skip IP and port
return $reader->readLength(6);
} else if ($data['type'] === 0x03) {
} elseif ($data['type'] === 0x03) {
// domain name => read domain name length
return $reader->readBinary(array(
'length' => 'C'
))->then(function ($data) use ($that) {
))->then(function ($data) use ($reader) {
// skip domain name and port
return $that->readLength($data['length'] + 2);
return $reader->readLength($data['length'] + 2);
});
} else if ($data['type'] === 0x04) {
} elseif ($data['type'] === 0x04) {
// IPv6 address => skip IP and port
return $reader->readLength(18);
} else {
Expand Down
36 changes: 36 additions & 0 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,42 @@ public function testEmitSocks5DataErrorDuringSessionWillRejectConnection()
$promise->then(null, $this->expectCallableOnceWithExceptionCode(SOCKET_ECONNREFUSED));
}

public function testEmitSocks5DataInvalidAddressTypeWillRejectConnection()
{
$stream = $this->getMockBuilder('React\Socket\Connection')->disableOriginalConstructor()->setMethods(array('write', 'close'))->getMock();
$stream->expects($this->once())->method('close');

$promise = \React\Promise\resolve($stream);

$this->connector->expects($this->once())->method('connect')->with('127.0.0.1:1080?hostname=google.com')->willReturn($promise);

$this->client = new Client('socks5://127.0.0.1:1080', $this->connector);

$promise = $this->client->connect('google.com:80');

$stream->emit('data', array("\x05\x00" . "\x05\x00\x00\x00"));

$promise->then(null, $this->expectCallableOnceWithExceptionCode(SOCKET_EBADMSG));
}

public function testEmitSocks5DataHostnameAddressWillResolveConnection()
{
$stream = $this->getMockBuilder('React\Socket\Connection')->disableOriginalConstructor()->setMethods(array('write', 'close'))->getMock();
$stream->expects($this->never())->method('close');

$promise = \React\Promise\resolve($stream);

$this->connector->expects($this->once())->method('connect')->with('127.0.0.1:1080?hostname=google.com')->willReturn($promise);

$this->client = new Client('socks5://127.0.0.1:1080', $this->connector);

$promise = $this->client->connect('google.com:80');

$stream->emit('data', array("\x05\x00" . "\x05\x00\x00\x03\x0Agoogle.com\x00\x50"));

$promise->then($this->expectCallableOnce());
}

public function provideConnectionErrors()
{
return array(
Expand Down