From 1e51a37107bde4764ac79d74835fc6b0cd4f88b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=BCck?= Date: Tue, 27 Jul 2021 20:32:25 +0200 Subject: [PATCH] Simplify usage by supporting new Socket API without nullable loop --- README.md | 16 +++++++------- composer.json | 4 ++-- examples/01-https-request.php | 4 ++-- examples/02-optional-proxy-https-request.php | 4 ++-- examples/11-proxy-raw-http-protocol.php | 2 +- .../12-optional-proxy-raw-http-protocol.php | 2 +- examples/21-proxy-raw-https-protocol.php | 2 +- .../22-optional-proxy-raw-https-protocol.php | 2 +- tests/FunctionalSshProcessConnectorTest.php | 16 ++++++-------- tests/FunctionalSshSocksConnectorTest.php | 22 +++++++++---------- tests/IntegrationSshProcessConnectorTest.php | 22 ++++++++----------- 11 files changed, 44 insertions(+), 52 deletions(-) diff --git a/README.md b/README.md index b4f432b..f6f514a 100644 --- a/README.md +++ b/README.md @@ -77,7 +77,7 @@ plaintext HTTP request to google.com through a remote SSH server: ```php $proxy = new Clue\React\SshProxy\SshProcessConnector('user@example.com'); -$connector = new React\Socket\Connector(null, array( +$connector = new React\Socket\Connector(array( 'tcp' => $proxy, 'dns' => false )); @@ -287,7 +287,7 @@ $proxy = new Clue\React\SshProxy\SshProcessConnector('user@example.com'); // or $proxy = new Clue\React\SshProxy\SshSocksConnector('user@example.com'); -$connector = new React\Socket\Connector(null, array( +$connector = new React\Socket\Connector(array( 'tcp' => $proxy, 'dns' => false )); @@ -318,7 +318,7 @@ low-level [`SecureConnector`](https://github.com/reactphp/socket#secureconnector ```php $proxy = new Clue\React\SshProxy\SshSocksConnector('user@example.com'); -$connector = new React\Socket\Connector(null, array( +$connector = new React\Socket\Connector(array( 'tcp' => $proxy, 'dns' => false )); @@ -350,12 +350,12 @@ This allows you to send both plain HTTP and TLS-encrypted HTTPS requests like th ```php $proxy = new Clue\React\SshProxy\SshSocksConnector('user@example.com'); -$connector = new React\Socket\Connector(null, array( +$connector = new React\Socket\Connector(array( 'tcp' => $proxy, 'dns' => false )); -$browser = new React\Http\Browser(null, $connector); +$browser = new React\Http\Browser($connector); $browser->get('https://example.com/')->then(function (Psr\Http\Message\ResponseInterface $response) { var_dump($response->getHeaders(), (string) $response->getBody()); @@ -435,7 +435,7 @@ $proxy = new Clue\React\SshProxy\SshProcessConnector('user@example.com'); // or $proxy = new Clue\React\SshProxy\SshSocksConnector('user@example.com'); -$connector = new React\Socket\Connector(null, array( +$connector = new React\Socket\Connector(array( 'tcp' => $proxy, 'dns' => false, 'timeout' => 3.0 @@ -481,7 +481,7 @@ $proxy = new Clue\React\SshProxy\SshProcessConnector('user@example.com'); // or $proxy = new Clue\React\SshProxy\SshSocksConnector('user@example.com'); -$connector = new React\Socket\Connector(null, array( +$connector = new React\Socket\Connector(array( 'tcp' => $proxy, 'dns' => false )); @@ -495,7 +495,7 @@ $proxy = new Clue\React\SshProxy\SshProcessConnector('user@example.com'); $proxy = new Clue\React\SshProxy\SshSocksConnector('user@example.com'); // set up Connector which uses Google's public DNS (8.8.8.8) -$connector = new React\Socket\Connector(null, array( +$connector = new React\Socket\Connector(array( 'tcp' => $proxy, 'dns' => '8.8.8.8' )); diff --git a/composer.json b/composer.json index a312692..722ccde 100644 --- a/composer.json +++ b/composer.json @@ -23,13 +23,13 @@ "react/child-process": "^0.6", "react/event-loop": "^1.2", "react/promise": "^2.1 || ^1.2.1", - "react/socket": "^1.8", + "react/socket": "^1.9", "react/stream": "^1.2" }, "require-dev": { "clue/block-react": "^1.3", "phpunit/phpunit": "^9.3 || ^5.7 || ^4.8.36", - "react/http": "^1.4", + "react/http": "^1.5", "react/mysql": "^0.5.5" } } diff --git a/examples/01-https-request.php b/examples/01-https-request.php index 7def3b3..0f8ebc3 100644 --- a/examples/01-https-request.php +++ b/examples/01-https-request.php @@ -15,13 +15,13 @@ $proxy = new Clue\React\SshProxy\SshProcessConnector($url); -$connector = new React\Socket\Connector(null, array( +$connector = new React\Socket\Connector(array( 'tcp' => $proxy, 'timeout' => 3.0, 'dns' => false )); -$browser = new React\Http\Browser(null, $connector); +$browser = new React\Http\Browser($connector); $browser->get('https://example.com/')->then(function (Psr\Http\Message\ResponseInterface $response) { var_dump($response->getHeaders(), (string) $response->getBody()); diff --git a/examples/02-optional-proxy-https-request.php b/examples/02-optional-proxy-https-request.php index d83fc16..a2d226e 100644 --- a/examples/02-optional-proxy-https-request.php +++ b/examples/02-optional-proxy-https-request.php @@ -16,14 +16,14 @@ if (getenv('SSH_PROXY') !== false) { $proxy = new Clue\React\SshProxy\SshProcessConnector(getenv('SSH_PROXY')); - $connector = new React\Socket\Connector(null, array( + $connector = new React\Socket\Connector(array( 'tcp' => $proxy, 'timeout' => 3.0, 'dns' => false )); } -$browser = new React\Http\Browser(null, $connector); +$browser = new React\Http\Browser($connector); $browser->get('https://example.com/')->then(function (Psr\Http\Message\ResponseInterface $response) { var_dump($response->getHeaders(), (string) $response->getBody()); diff --git a/examples/11-proxy-raw-http-protocol.php b/examples/11-proxy-raw-http-protocol.php index 7964914..605fb4a 100644 --- a/examples/11-proxy-raw-http-protocol.php +++ b/examples/11-proxy-raw-http-protocol.php @@ -18,7 +18,7 @@ $proxy = new Clue\React\SshProxy\SshProcessConnector($url); -$connector = new React\Socket\Connector(null, array( +$connector = new React\Socket\Connector(array( 'tcp' => $proxy, 'timeout' => 3.0, 'dns' => false diff --git a/examples/12-optional-proxy-raw-http-protocol.php b/examples/12-optional-proxy-raw-http-protocol.php index 84a78e7..17a4096 100644 --- a/examples/12-optional-proxy-raw-http-protocol.php +++ b/examples/12-optional-proxy-raw-http-protocol.php @@ -22,7 +22,7 @@ if (getenv('SSH_PROXY') !== false) { $proxy = new Clue\React\SshProxy\SshProcessConnector(getenv('SSH_PROXY')); - $connector = new React\Socket\Connector(null, array( + $connector = new React\Socket\Connector(array( 'tcp' => $proxy, 'timeout' => 3.0, 'dns' => false diff --git a/examples/21-proxy-raw-https-protocol.php b/examples/21-proxy-raw-https-protocol.php index 41028c3..a1dfffe 100644 --- a/examples/21-proxy-raw-https-protocol.php +++ b/examples/21-proxy-raw-https-protocol.php @@ -18,7 +18,7 @@ $proxy = new Clue\React\SshProxy\SshSocksConnector($url); -$connector = new React\Socket\Connector(null, array( +$connector = new React\Socket\Connector(array( 'tcp' => $proxy, 'timeout' => 3.0, 'dns' => false diff --git a/examples/22-optional-proxy-raw-https-protocol.php b/examples/22-optional-proxy-raw-https-protocol.php index 645fc1a..7235690 100644 --- a/examples/22-optional-proxy-raw-https-protocol.php +++ b/examples/22-optional-proxy-raw-https-protocol.php @@ -22,7 +22,7 @@ if (getenv('SSH_PROXY') !== false) { $proxy = new Clue\React\SshProxy\SshProcessConnector(getenv('SSH_PROXY')); - $connector = new React\Socket\Connector(null, array( + $connector = new React\Socket\Connector(array( 'tcp' => $proxy, 'timeout' => 3.0, 'dns' => false diff --git a/tests/FunctionalSshProcessConnectorTest.php b/tests/FunctionalSshProcessConnectorTest.php index 6e9dec9..1558630 100644 --- a/tests/FunctionalSshProcessConnectorTest.php +++ b/tests/FunctionalSshProcessConnectorTest.php @@ -2,14 +2,13 @@ namespace Clue\Tests\React\SshProxy; -use React\EventLoop\Factory; use Clue\React\SshProxy\SshProcessConnector; +use React\EventLoop\Loop; class FunctionalSshProcessConnectorTest extends TestCase { const TIMEOUT = 10.0; - private $loop; private $connector; /** @@ -22,17 +21,16 @@ public function setUpConnector() $this->markTestSkipped('No SSH_PROXY env set'); } - $this->loop = Factory::create(); - $this->connector = new SshProcessConnector($url, $this->loop); + $this->connector = new SshProcessConnector($url); } public function testConnectInvalidProxyUriWillReturnRejectedPromise() { - $this->connector = new SshProcessConnector(getenv('SSH_PROXY') . '.invalid', $this->loop); + $this->connector = new SshProcessConnector(getenv('SSH_PROXY') . '.invalid'); $promise = $this->connector->connect('example.com:80'); $this->setExpectedException('RuntimeException', 'Connection to example.com:80 failed because SSH client died'); - \Clue\React\Block\await($promise, $this->loop, self::TIMEOUT); + \Clue\React\Block\await($promise, Loop::get(), self::TIMEOUT); } public function testConnectInvalidTargetWillReturnRejectedPromise() @@ -40,7 +38,7 @@ public function testConnectInvalidTargetWillReturnRejectedPromise() $promise = $this->connector->connect('example.invalid:80'); $this->setExpectedException('RuntimeException', 'Connection to example.invalid:80 rejected:'); - \Clue\React\Block\await($promise, $this->loop, self::TIMEOUT); + \Clue\React\Block\await($promise, Loop::get(), self::TIMEOUT); } public function testCancelConnectWillReturnRejectedPromise() @@ -49,14 +47,14 @@ public function testCancelConnectWillReturnRejectedPromise() $promise->cancel(); $this->setExpectedException('RuntimeException', 'Connection to example.com:80 cancelled while waiting for SSH client'); - \Clue\React\Block\await($promise, $this->loop, 0); + \Clue\React\Block\await($promise, Loop::get(), 0); } public function testConnectValidTargetWillReturnPromiseWhichResolvesToConnection() { $promise = $this->connector->connect('example.com:80'); - $connection = \Clue\React\Block\await($promise, $this->loop, self::TIMEOUT); + $connection = \Clue\React\Block\await($promise, Loop::get(), self::TIMEOUT); $this->assertInstanceOf('React\Socket\ConnectionInterface', $connection); $this->assertTrue($connection->isReadable()); diff --git a/tests/FunctionalSshSocksConnectorTest.php b/tests/FunctionalSshSocksConnectorTest.php index 5228837..a305361 100644 --- a/tests/FunctionalSshSocksConnectorTest.php +++ b/tests/FunctionalSshSocksConnectorTest.php @@ -2,14 +2,13 @@ namespace Clue\Tests\React\SshProxy; -use React\EventLoop\Factory; use Clue\React\SshProxy\SshSocksConnector; +use React\EventLoop\Loop; class FunctionalSshSocksConnectorTest extends TestCase { const TIMEOUT = 10.0; - private $loop; private $connector; /** @@ -22,8 +21,7 @@ public function setUpConnector() $this->markTestSkipped('No SSH_PROXY env set'); } - $this->loop = Factory::create(); - $this->connector = new SshSocksConnector($url, $this->loop); + $this->connector = new SshSocksConnector($url); } /** @@ -32,17 +30,17 @@ public function setUpConnector() public function tearDownSSHClientProcess() { // run loop in order to shut down SSH client process again - \Clue\React\Block\sleep(0.001, $this->loop); + \Clue\React\Block\sleep(0.001, Loop::get()); } public function testConnectInvalidProxyUriWillReturnRejectedPromise() { - $this->connector = new SshSocksConnector(getenv('SSH_PROXY') . '.invalid', $this->loop); + $this->connector = new SshSocksConnector(getenv('SSH_PROXY') . '.invalid'); $promise = $this->connector->connect('example.com:80'); $this->setExpectedException('RuntimeException', 'Connection to example.com:80 failed because SSH client process died'); - \Clue\React\Block\await($promise, $this->loop, self::TIMEOUT); + \Clue\React\Block\await($promise, Loop::get(), self::TIMEOUT); } public function testConnectInvalidTargetWillReturnRejectedPromise() @@ -50,7 +48,7 @@ public function testConnectInvalidTargetWillReturnRejectedPromise() $promise = $this->connector->connect('example.invalid:80'); $this->setExpectedException('RuntimeException', 'Connection to tcp://example.invalid:80 failed because connection to proxy was lost'); - \Clue\React\Block\await($promise, $this->loop, self::TIMEOUT); + \Clue\React\Block\await($promise, Loop::get(), self::TIMEOUT); } public function testCancelConnectWillReturnRejectedPromise() @@ -59,14 +57,14 @@ public function testCancelConnectWillReturnRejectedPromise() $promise->cancel(); $this->setExpectedException('RuntimeException', 'Connection to example.com:80 cancelled while waiting for SSH client'); - \Clue\React\Block\await($promise, $this->loop, 0); + \Clue\React\Block\await($promise, Loop::get(), 0); } public function testConnectValidTargetWillReturnPromiseWhichResolvesToConnection() { $promise = $this->connector->connect('example.com:80'); - $connection = \Clue\React\Block\await($promise, $this->loop, self::TIMEOUT); + $connection = \Clue\React\Block\await($promise, Loop::get(), self::TIMEOUT); $this->assertInstanceOf('React\Socket\ConnectionInterface', $connection); $this->assertTrue($connection->isReadable()); @@ -76,10 +74,10 @@ public function testConnectValidTargetWillReturnPromiseWhichResolvesToConnection public function testConnectValidTargetWillReturnPromiseWhichResolvesToConnectionForCustomBindAddress() { - $this->connector = new SshSocksConnector(getenv('SSH_PROXY') . '?bind=127.0.0.1:1081', $this->loop); + $this->connector = new SshSocksConnector(getenv('SSH_PROXY') . '?bind=127.0.0.1:1081', Loop::get()); $promise = $this->connector->connect('example.com:80'); - $connection = \Clue\React\Block\await($promise, $this->loop, self::TIMEOUT); + $connection = \Clue\React\Block\await($promise, Loop::get(), self::TIMEOUT); $this->assertInstanceOf('React\Socket\ConnectionInterface', $connection); $this->assertTrue($connection->isReadable()); diff --git a/tests/IntegrationSshProcessConnectorTest.php b/tests/IntegrationSshProcessConnectorTest.php index a712c36..ce3f331 100644 --- a/tests/IntegrationSshProcessConnectorTest.php +++ b/tests/IntegrationSshProcessConnectorTest.php @@ -3,15 +3,14 @@ namespace Clue\Tests\React\SshProxy; use Clue\React\SshProxy\SshProcessConnector; -use React\EventLoop\Factory; +use React\EventLoop\Loop; use React\Socket\ConnectionInterface; class IntegrationSshProcessConnectorTest extends TestCase { public function testConnectWillResolveWithConnectionInterfaceWhenProcessOutputsChannelOpenConfirmMessage() { - $loop = Factory::create(); - $connector = new SshProcessConnector('host', $loop); + $connector = new SshProcessConnector('host'); $ref = new \ReflectionProperty($connector, 'cmd'); $ref->setAccessible(true); @@ -20,13 +19,12 @@ public function testConnectWillResolveWithConnectionInterfaceWhenProcessOutputsC $promise = $connector->connect('example.com:80'); $promise->then($this->expectCallableOnceWith($this->isInstanceOf('React\Socket\ConnectionInterface'))); - $loop->run(); + Loop::run(); } public function testConnectWillRejectWithExceptionWhenProcessOutputsChannelOpenFailedMessage() { - $loop = Factory::create(); - $connector = new SshProcessConnector('host', $loop); + $connector = new SshProcessConnector('host'); $ref = new \ReflectionProperty($connector, 'cmd'); $ref->setAccessible(true); @@ -35,13 +33,12 @@ public function testConnectWillRejectWithExceptionWhenProcessOutputsChannelOpenF $promise = $connector->connect('example.com:80'); $promise->then(null, $this->expectCallableOnceWith($this->isInstanceOf('RuntimeException'))); - $loop->run(); + Loop::run(); } public function testConnectWillRejectWithExceptionWhenProcessOutputsEndsWithoutChannelMessage() { - $loop = Factory::create(); - $connector = new SshProcessConnector('host', $loop); + $connector = new SshProcessConnector('host'); $ref = new \ReflectionProperty($connector, 'cmd'); $ref->setAccessible(true); @@ -50,13 +47,12 @@ public function testConnectWillRejectWithExceptionWhenProcessOutputsEndsWithoutC $promise = $connector->connect('example.com:80'); $promise->then(null, $this->expectCallableOnceWith($this->isInstanceOf('RuntimeException'))); - $loop->run(); + Loop::run(); } public function testConnectWillResolveWithConnectionThatWillEmitImmediateDataFromProcessStdoutAfterChannelOpenConfirmMessage() { - $loop = Factory::create(); - $connector = new SshProcessConnector('host', $loop); + $connector = new SshProcessConnector('host'); $ref = new \ReflectionProperty($connector, 'cmd'); $ref->setAccessible(true); @@ -69,6 +65,6 @@ public function testConnectWillResolveWithConnectionThatWillEmitImmediateDataFro $connection->on('data', $data); }); - $loop->run(); + Loop::run(); } }