Skip to content

Fix connecting to IPv6 destination hosts #22

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

Merged
merged 1 commit into from
Oct 22, 2018
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
11 changes: 5 additions & 6 deletions src/ProxyConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,7 @@ public function connect($uri)
return Promise\reject(new InvalidArgumentException('Invalid target URI specified'));
}

$host = trim($parts['host'], '[]');
$port = $parts['port'];
$target = $parts['host'] . ':' . $parts['port'];

// construct URI to HTTP CONNECT proxy server to connect to
$proxyUri = $this->proxyUri;
Expand All @@ -126,11 +125,11 @@ public function connect($uri)

// append hostname from URI to query string unless explicitly given
if (!isset($args['hostname'])) {
$args['hostname'] = $parts['host'];
$args['hostname'] = trim($parts['host'], '[]');
}

// append query string
$proxyUri .= '?' . http_build_query($args, '', '&');;
$proxyUri .= '?' . http_build_query($args, '', '&');

// append fragment from URI if given
if (isset($parts['fragment'])) {
Expand All @@ -139,7 +138,7 @@ public function connect($uri)

$auth = $this->proxyAuth;

return $this->connector->connect($proxyUri)->then(function (ConnectionInterface $stream) use ($host, $port, $auth) {
return $this->connector->connect($proxyUri)->then(function (ConnectionInterface $stream) use ($target, $auth) {
$deferred = new Deferred(function ($_, $reject) use ($stream) {
$reject(new RuntimeException('Connection canceled while waiting for response from proxy (ECONNABORTED)', defined('SOCKET_ECONNABORTED') ? SOCKET_ECONNABORTED : 103));
$stream->close();
Expand Down Expand Up @@ -199,7 +198,7 @@ public function connect($uri)
$deferred->reject(new RuntimeException('Connection to proxy lost while waiting for response (ECONNRESET)', defined('SOCKET_ECONNRESET') ? SOCKET_ECONNRESET : 104));
});

$stream->write("CONNECT " . $host . ":" . $port . " HTTP/1.1\r\nHost: " . $host . ":" . $port . "\r\n" . $auth . "\r\n");
$stream->write("CONNECT " . $target . " HTTP/1.1\r\nHost: " . $target . "\r\n" . $auth . "\r\n");

return $deferred->promise()->then(function (ConnectionInterface $stream) use ($fn) {
// Stop buffering when connection has been established.
Expand Down
43 changes: 43 additions & 0 deletions tests/ProxyConnectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,36 @@ public function testCreatesConnectionToHttpPortAndObeysExplicitHostname()
$proxy->connect('google.com:80?hostname=www.google.com');
}

public function testCreatesConnectionToIpv4Address()
{
$promise = new Promise(function () { });
$this->connector->expects($this->once())->method('connect')->with('tcp://proxy.example.com:80?hostname=127.0.0.1')->willReturn($promise);

$proxy = new ProxyConnector('proxy.example.com', $this->connector);

$proxy->connect('127.0.0.1:80');
}

public function testCreatesConnectionToIpv6Address()
{
$promise = new Promise(function () { });
$this->connector->expects($this->once())->method('connect')->with('tcp://proxy.example.com:80?hostname=%3A%3A1')->willReturn($promise);

$proxy = new ProxyConnector('proxy.example.com', $this->connector);

$proxy->connect('[::1]:80');
}

public function testCreatesConnectionToIpv4AddressOverIpv6Proxy()
{
$promise = new Promise(function () { });
$this->connector->expects($this->once())->method('connect')->with('tcp://[::1]:80?hostname=127.0.0.1')->willReturn($promise);

$proxy = new ProxyConnector('[::1]:80', $this->connector);

$proxy->connect('127.0.0.1:80');
}

public function testCreatesConnectionToHttpsPort()
{
$promise = new Promise(function () { });
Expand Down Expand Up @@ -116,6 +146,19 @@ public function testWillWriteToOpenConnection()
$proxy->connect('google.com:80');
}

public function testWillWriteIpv6HostToOpenConnection()
{
$stream = $this->getMockBuilder('React\Socket\Connection')->disableOriginalConstructor()->setMethods(array('close', 'write'))->getMock();
$stream->expects($this->once())->method('write')->with("CONNECT [::1]:80 HTTP/1.1\r\nHost: [::1]:80\r\n\r\n");

$promise = \React\Promise\resolve($stream);
$this->connector->expects($this->once())->method('connect')->willReturn($promise);

$proxy = new ProxyConnector('proxy.example.com', $this->connector);

$proxy->connect('[::1]:80');
}

public function testWillProxyAuthorizationHeaderIfProxyUriContainsAuthentication()
{
$stream = $this->getMockBuilder('React\Socket\Connection')->disableOriginalConstructor()->setMethods(array('close', 'write'))->getMock();
Expand Down