Skip to content

Update to SocketClient v0.6+ and use connect($uri) #8

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
Apr 10, 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
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Async HTTP CONNECT proxy connector, use any TCP/IP protocol through an HTTP prox
* [Quickstart example](#quickstart-example)
* [Usage](#usage)
* [ConnectorInterface](#connectorinterface)
* [create()](#create)
* [connect()](#connect)
* [ProxyConnector](#proxyconnector)
* [Install](#install)
* [Tests](#tests)
Expand All @@ -25,7 +25,7 @@ $connector = new TcpConnector($loop);
$proxy = new ProxyConnector('127.0.0.1:8080', $connector);
$ssl = new SecureConnector($proxy, $loop);

$ssl->create('google.com', 443)->then(function (Stream $stream) {
$ssl->connect('google.com:443')->then(function (ConnectionInterface $stream) {
$stream->write("GET / HTTP/1.1\r\nHost: google.com\r\nConnection: close\r\n\r\n");
$stream->on('data', function ($chunk) {
echo $chunk;
Expand Down Expand Up @@ -59,17 +59,17 @@ HTTP CONNECT proxy.

The interface only offers a single method:

#### create()
#### connect()

The `create(string $host, int $port): PromiseInterface<Stream, Exception>` method
The `connect(string $uri): PromiseInterface<ConnectionInterface, Exception>` method
can be used to establish a streaming connection.
It returns a [Promise](https://github.com/reactphp/promise) which either
fulfills with a [Stream](https://github.com/reactphp/stream) or
fulfills with a [ConnectionInterface](https://github.com/reactphp/socket-client#connectioninterface) or
rejects with an `Exception`:

```php
$connector->create('google.com', 443)->then(
function (Stream $stream) {
$connector->connect('google.com:443')->then(
function (ConnectionInterface $stream) {
// connection successfully established
},
function (Exception $error) {
Expand Down Expand Up @@ -121,7 +121,7 @@ connector is actually inherently a general-purpose plain TCP/IP connector:
```php
$proxy = new ProxyConnector('127.0.0.1:8080', $connector);

$proxy->create('smtp.googlemail.com', 587)->then(function (Stream $stream) {
$proxy->connect('smtp.googlemail.com:587')->then(function (ConnectionInterface $stream) {
$stream->write("EHLO local\r\n");
$stream->on('data', function ($chunk) use ($stream) {
echo $chunk;
Expand All @@ -141,7 +141,7 @@ instance:
$proxy = new ProxyConnector('127.0.0.1:8080', $connector);
$ssl = new SecureConnector($proxy, $loop);

$ssl->create('smtp.googlemail.com', 465)->then(function (Stream $stream) {
$ssl->connect('smtp.googlemail.com:465')->then(function (ConnectionInterface $stream) {
$stream->write("EHLO local\r\n");
$stream->on('data', function ($chunk) use ($stream) {
echo $chunk;
Expand All @@ -163,7 +163,7 @@ instance to create a secure connection to the proxy:
$ssl = new SecureConnector($connector, $loop);
$proxy = new ProxyConnector('127.0.0.1:443', $ssl);

$proxy->create('smtp.googlemail.com', 587);
$proxy->connect('smtp.googlemail.com:587');
```

## Install
Expand Down
4 changes: 1 addition & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,13 @@
},
"require": {
"php": ">=5.3",
"react/socket-client": "^0.5 || ^0.4 || ^0.3",
"react/socket-client": "^0.7 || ^0.6",
"react/event-loop": "^0.4 || ^0.3",
"react/stream": "^0.4 || ^0.3",
"react/promise": " ^2.1 || ^1.2",
"ringcentral/psr7": "^1.2"
},
"require-dev": {
"phpunit/phpunit": "^5.0 || ^4.8",
"react/socket-client": "^0.5",
"clue/block-react": "^1.1"
}
}
4 changes: 2 additions & 2 deletions examples/01-proxy-https.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
// The proxy can be given as first argument and defaults to localhost:8080 otherwise.

use Clue\React\HttpProxy\ProxyConnector;
use React\Stream\Stream;
use React\SocketClient\TcpConnector;
use React\SocketClient\SecureConnector;
use React\SocketClient\ConnectionInterface;

require __DIR__ . '/../vendor/autoload.php';

Expand All @@ -18,7 +18,7 @@
$proxy = new ProxyConnector($url, $connector);
$ssl = new SecureConnector($proxy, $loop);

$ssl->create('google.com', 443)->then(function (Stream $stream) {
$ssl->connect('google.com:443')->then(function (ConnectionInterface $stream) {
$stream->write("GET / HTTP/1.1\r\nHost: google.com\r\nConnection: close\r\n\r\n");
$stream->on('data', function ($chunk) {
echo $chunk;
Expand Down
4 changes: 2 additions & 2 deletions examples/02-optional-proxy-https.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
// network protocol otherwise.

use Clue\React\HttpProxy\ProxyConnector;
use React\Stream\Stream;
use React\SocketClient\TcpConnector;
use React\SocketClient\SecureConnector;
use React\SocketClient\DnsConnector;
use React\Dns\Resolver\Factory;
use React\SocketClient\ConnectionInterface;

require __DIR__ . '/../vendor/autoload.php';

Expand All @@ -31,7 +31,7 @@
$connector = new SecureConnector($dns, $loop);
}

$connector->create('google.com', 443)->then(function (Stream $stream) {
$connector->connect('google.com:443')->then(function (ConnectionInterface $stream) {
$stream->write("GET / HTTP/1.1\r\nHost: google.com\r\nConnection: close\r\n\r\n");
$stream->on('data', function ($chunk) {
echo $chunk;
Expand Down
4 changes: 2 additions & 2 deletions examples/11-proxy-smtp.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
// Please note that MANY public proxies do not allow SMTP connections, YMMV.

use Clue\React\HttpProxy\ProxyConnector;
use React\Stream\Stream;
use React\SocketClient\TcpConnector;
use React\SocketClient\ConnectionInterface;

require __DIR__ . '/../vendor/autoload.php';

Expand All @@ -17,7 +17,7 @@
$connector = new TcpConnector($loop);
$proxy = new ProxyConnector($url, $connector);

$proxy->create('smtp.googlemail.com', 587)->then(function (Stream $stream) {
$proxy->connect('smtp.googlemail.com:587')->then(function (ConnectionInterface $stream) {
$stream->write("EHLO local\r\n");
$stream->on('data', function ($chunk) use ($stream) {
echo $chunk;
Expand Down
4 changes: 2 additions & 2 deletions examples/12-proxy-smtps.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
// Please note that MANY public proxies do not allow SMTP connections, YMMV.

use Clue\React\HttpProxy\ProxyConnector;
use React\Stream\Stream;
use React\SocketClient\TcpConnector;
use React\SocketClient\SecureConnector;
use React\SocketClient\ConnectionInterface;

require __DIR__ . '/../vendor/autoload.php';

Expand All @@ -22,7 +22,7 @@
$proxy = new ProxyConnector($url, $connector);
$ssl = new SecureConnector($proxy, $loop);

$ssl->create('smtp.googlemail.com', 465)->then(function (Stream $stream) {
$ssl->connect('smtp.googlemail.com:465')->then(function (ConnectionInterface $stream) {
$stream->write("EHLO local\r\n");
$stream->on('data', function ($chunk) use ($stream) {
echo $chunk;
Expand Down
53 changes: 45 additions & 8 deletions src/ProxyConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
namespace Clue\React\HttpProxy;

use React\SocketClient\ConnectorInterface;
use React\Stream\Stream;
use Exception;
use InvalidArgumentException;
use RuntimeException;
use RingCentral\Psr7;
use React\Promise\Deferred;
use React\SocketClient\ConnectionInterface;

/**
* A simple Connector that uses an HTTP CONNECT proxy to create plain TCP/IP connections to any destination
Expand Down Expand Up @@ -40,8 +40,7 @@
class ProxyConnector implements ConnectorInterface
{
private $connector;
private $proxyHost;
private $proxyPort;
private $proxyUri;

/**
* Instantiate a new ProxyConnector which uses the given $proxyUrl
Expand All @@ -61,7 +60,7 @@ public function __construct($proxyUrl, ConnectorInterface $connector)
}

$parts = parse_url($proxyUrl);
if (!$parts || !isset($parts['host'])) {
if (!$parts || !isset($parts['scheme'], $parts['host'])) {
throw new InvalidArgumentException('Invalid proxy URL');
}

Expand All @@ -70,13 +69,51 @@ public function __construct($proxyUrl, ConnectorInterface $connector)
}

$this->connector = $connector;
$this->proxyHost = $parts['host'];
$this->proxyPort = $parts['port'];
$this->proxyUri = $parts['host'] . ':' . $parts['port'];
}

public function create($host, $port)
public function connect($uri)
{
return $this->connector->create($this->proxyHost, $this->proxyPort)->then(function (Stream $stream) use ($host, $port) {
if (strpos($uri, '://') === false) {
$uri = 'tcp://' . $uri;
}

$parts = parse_url($uri);
if (!$parts || !isset($parts['scheme'], $parts['host'], $parts['port']) || $parts['scheme'] !== 'tcp') {
return Promise\reject(new InvalidArgumentException('Invalid target URI specified'));
}

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

// construct URI to HTTP CONNECT proxy server to connect to
$proxyUri = $this->proxyUri;

// append path from URI if given
if (isset($parts['path'])) {
$proxyUri .= $parts['path'];
}

// parse query args
$args = array();
if (isset($parts['query'])) {
parse_str($parts['query'], $args);
}

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

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

// append fragment from URI if given
if (isset($parts['fragment'])) {
$proxyUri .= '#' . $parts['fragment'];
}

return $this->connector->connect($proxyUri)->then(function (ConnectionInterface $stream) use ($host, $port) {
$deferred = new Deferred(function ($_, $reject) use ($stream) {
$reject(new RuntimeException('Operation canceled while waiting for response from proxy'));
$stream->close();
Expand Down
6 changes: 3 additions & 3 deletions tests/FunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function testPlainGoogleDoesNotAcceptConnectMethod()
{
$proxy = new ProxyConnector('google.com', $this->dnsConnector);

$promise = $proxy->create('google.com', 80);
$promise = $proxy->connect('google.com:80');

$this->setExpectedException('RuntimeException', 'Method Not Allowed', 405);
Block\await($promise, $this->loop, 3.0);
Expand All @@ -46,7 +46,7 @@ public function testSecureGoogleDoesNotAcceptConnectMethod()
$secure = new SecureConnector($this->dnsConnector, $this->loop);
$proxy = new ProxyConnector('google.com:443', $secure);

$promise = $proxy->create('google.com', 80);
$promise = $proxy->connect('google.com:80');

$this->setExpectedException('RuntimeException', 'Method Not Allowed', 405);
Block\await($promise, $this->loop, 3.0);
Expand All @@ -56,7 +56,7 @@ public function testSecureGoogleDoesNotAcceptPlainStream()
{
$proxy = new ProxyConnector('google.com:443', $this->dnsConnector);

$promise = $proxy->create('google.com', 80);
$promise = $proxy->connect('google.com:80');

$this->setExpectedException('RuntimeException', 'Connection to proxy lost');
Block\await($promise, $this->loop, 3.0);
Expand Down
Loading