This commit is contained in:
2021-06-09 20:08:15 +03:00
parent 411fabb1f5
commit e3c64f6f09
3 changed files with 28 additions and 3 deletions

View File

@ -33,7 +33,7 @@ Features
- [X] Feature Negotiation - [X] Feature Negotiation
- [ ] Discovery - [ ] Discovery
- [ ] Backoff - [ ] Backoff
- [ ] TLS - [X] TLS
- [ ] Deflate - [ ] Deflate
- [X] Snappy - [X] Snappy
- [X] Sampling - [X] Sampling

View File

@ -60,6 +60,17 @@ abstract class Connection
$response = yield $this->response($stream, $buffer); $response = yield $this->response($stream, $buffer);
$serverConfig = ServerConfig::fromArray($response->toArray()); $serverConfig = ServerConfig::fromArray($response->toArray());
if ($serverConfig->tls) {
yield $stream->setupTls();
/** @var Response $response */
$response = yield $this->response($stream, $buffer);
if (!$response->isOk()) {
throw new NsqException();
}
}
if ($serverConfig->snappy) { if ($serverConfig->snappy) {
$stream = new SnappyStream($stream, $buffer->flush()); $stream = new SnappyStream($stream, $buffer->flush());

View File

@ -5,15 +5,16 @@ declare(strict_types=1);
namespace Nsq\Stream; namespace Nsq\Stream;
use Amp\Promise; use Amp\Promise;
use Amp\Socket\ClientTlsContext;
use Amp\Socket\ConnectContext; use Amp\Socket\ConnectContext;
use Amp\Socket\Socket; use Amp\Socket\EncryptableSocket;
use Nsq\Stream; use Nsq\Stream;
use function Amp\call; use function Amp\call;
use function Amp\Socket\connect; use function Amp\Socket\connect;
class SocketStream implements Stream class SocketStream implements Stream
{ {
public function __construct(private Socket $socket) public function __construct(private EncryptableSocket $socket)
{ {
} }
@ -37,6 +38,11 @@ class SocketStream implements Stream
$context = $context->withTcpNoDelay(); $context = $context->withTcpNoDelay();
} }
$context = $context->withTlsContext(
(new ClientTlsContext(''))
->withoutPeerVerification()
);
return new self(yield connect($uri, $context)); return new self(yield connect($uri, $context));
}); });
} }
@ -61,4 +67,12 @@ class SocketStream implements Stream
{ {
$this->socket->close(); $this->socket->close();
} }
/**
* @return Promise<void>
*/
public function setupTls(): Promise
{
return $this->socket->setupTls();
}
} }