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

Update SocketClient to v0.5 and fix secure connection via TLS #38

Merged
merged 4 commits into from
Sep 4, 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
29 changes: 25 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,18 +104,39 @@ It helps with establishing a plain TCP/IP or secure SSL/TLS connection to the AM
and issuing an initial `login` action.

```php
$factory->createClient('user:secret@localhost')->then(
$factory->createClient($amiUrl)->then(
function (Client $client) {
// client connected and authenticated
// client connected (and authenticated)
},
function (Exception $e) {
// an error occured while trying to connect or authorize client
}
);
```

> Note: The given $amiUrl *must* include a host, it *should* include a username and secret
> and it *can* include a scheme (tcp/ssl) and port definition.
The `$amiUrl` contains the host and optional port to connect to:

```php
$factory->createClient('127.0.0.1:5038');
```

> If the `$amiUrl` is `null` (or omitted) this method defaults to connecting
to your local host (`127.0.0.1:5038`).

The above examples to not pass any authentication details, so you may have to
call `ActionSender::login()` after connecting or use the recommended shortcut
to pass a username and secret for your AMI login details like this:

```php
$factory->createClient('user:secret@localhost');
```

The `Factory` defaults to establishing a plaintext TCP connection.
If you want to connect through a secure TLS proxy, you can use the `tls` scheme:

```php
$factory->createClient('tls://user:secret@localhost:12345');
```

### Client

Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@
],
"require": {
"php": ">=5.3",
"evenement/evenement": "~1.0|~2.0",
"react/promise": "~1.0|~2.0",
"react/socket-client": "0.3.*|0.4.*",
"react/socket-client": "^0.5 || ^0.4 || ^0.3",
"react/event-loop": "0.3.*|0.4.*"
},
"require-dev": {
Expand Down
5 changes: 3 additions & 2 deletions src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Clue\React\Ami;

use React\EventLoop\LoopInterface;
use React\SocketClient\ConnectorInterface;
use React\SocketClient\Connector;
use React\SocketClient\SecureConnector;
use React\Dns\Resolver\Factory as ResolverFactory;
Expand All @@ -15,7 +16,7 @@ class Factory
private $connector;
private $secureConnector;

public function __construct(LoopInterface $loop, Connector $connector = null, SecureConnector $secureConnector = null)
public function __construct(LoopInterface $loop, ConnectorInterface $connector = null, ConnectorInterface $secureConnector = null)
{
if ($connector === null) {
$resolverFactory = new ResolverFactory();
Expand All @@ -34,7 +35,7 @@ public function createClient($address = null)
{
$parts = $this->parseUrl($address);

$secure = (isset($parts['schema']) && $parts['schema'] !== 'tcp');
$secure = (isset($parts['scheme']) && $parts['scheme'] !== 'tcp');
$connector = $secure ? $this->secureConnector : $this->connector;

$promise = $connector->create($parts['host'], $parts['port'])->then(function (Stream $stream) {
Expand Down
49 changes: 49 additions & 0 deletions tests/FactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

use Clue\React\Ami\Factory;
use React\Promise\Promise;
class FactoryTest extends TestCase
{
private $loop;
private $tcp;
private $tls;
private $factory;

public function setUp()
{
$this->loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
$this->tcp = $this->getMockBuilder('React\SocketClient\ConnectorInterface')->getMock();
$this->tls = $this->getMockBuilder('React\SocketClient\ConnectorInterface')->getMock();

$this->factory = new Factory($this->loop, $this->tcp, $this->tls);
}

public function testDefaultCtor()
{
$this->factory = new Factory($this->loop);
}

public function testCreateClientUsesTcpConnectorWithDefaultLocation()
{
$promise = new Promise(function () { });
$this->tcp->expects($this->once())->method('create')->with('127.0.0.1', 5038)->willReturn($promise);

$this->factory->createClient();
}

public function testCreateClientUsesTcpConnectorWithLocalhostLocation()
{
$promise = new Promise(function () { });
$this->tcp->expects($this->once())->method('create')->with('127.0.0.1', 5038)->willReturn($promise);

$this->factory->createClient('localhost');
}

public function testCreateClientUsesTlsConnectorWithTlsLocation()
{
$promise = new Promise(function () { });
$this->tls->expects($this->once())->method('create')->with('ami.local', 1234)->willReturn($promise);

$this->factory->createClient('tls://ami.local:1234');
}
}