Skip to content
This repository was archived by the owner on Oct 30, 2024. It is now read-only.

Commit 396781a

Browse files
author
tomponline
committed
Initial commit for adding TLS support
1 parent 68e6d24 commit 396781a

1 file changed

Lines changed: 25 additions & 14 deletions

File tree

src/Nats/Connection.php

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -229,10 +229,10 @@ public function isConnected()
229229
* @param string $address Server url string.
230230
* @param float $timeout Number of seconds until the connect() system call should timeout.
231231
*
232-
* @return resource
233232
* @throws \Exception Exception raised if connection fails.
233+
* @return resource
234234
*/
235-
private function getStream($address, $timeout)
235+
private function getStream($address, $timeout, $context)
236236
{
237237
$errno = null;
238238
$errstr = null;
@@ -242,7 +242,8 @@ function () {
242242
return true;
243243
}
244244
);
245-
$fp = stream_socket_client($address, $errno, $errstr, $timeout, STREAM_CLIENT_CONNECT);
245+
246+
$fp = stream_socket_client($address, $errno, $errstr, $timeout, STREAM_CLIENT_CONNECT, $context);
246247
restore_error_handler();
247248

248249
if ($fp === false) {
@@ -315,8 +316,8 @@ public function __construct(ConnectionOptions $options = null)
315316
*
316317
* @param string $payload Message data.
317318
*
318-
* @return void
319319
* @throws \Exception Raises if fails sending data.
320+
* @return void
320321
*/
321322
private function send($payload)
322323
{
@@ -394,8 +395,8 @@ private function handlePING()
394395
*
395396
* @param string $line Message command from Nats.
396397
*
397-
* @return void
398398
* @throws Exception If subscription not found.
399+
* @return void
399400
* @codeCoverageIgnore
400401
*/
401402
private function handleMSG($line)
@@ -408,7 +409,7 @@ private function handleMSG($line)
408409
if (count($parts) === 5) {
409410
$length = trim($parts[4]);
410411
$subject = $parts[3];
411-
} else if (count($parts) === 4) {
412+
} elseif (count($parts) === 4) {
412413
$length = trim($parts[3]);
413414
$subject = $parts[1];
414415
}
@@ -442,20 +443,30 @@ public function connect($timeout = null)
442443
$timeout = intval(ini_get('default_socket_timeout'));
443444
}
444445

446+
$context = stream_context_create();
447+
stream_context_set_option($context, 'ssl', 'verify_peer', false);
448+
//stream_context_set_option($context, 'ssl', 'cafile', '/var/lib/puppet/ssl/certs/ca.pem');
449+
445450
$this->timeout = $timeout;
446-
$this->streamSocket = $this->getStream($this->options->getAddress(), $timeout);
451+
$this->streamSocket = $this->getStream($this->options->getAddress(), $timeout, $context);
447452
$this->setStreamTimeout($timeout);
448453

449-
$msg = 'CONNECT '.$this->options;
450-
$this->send($msg);
451-
$connectResponse = $this->receive();
454+
$infoResponse = $this->receive();
452455

453-
if ($this->isErrorResponse($connectResponse) === true) {
454-
throw Exception::forFailedConnection($connectResponse);
456+
if ($this->isErrorResponse($infoResponse) === true) {
457+
throw Exception::forFailedConnection($infoResponse);
455458
} else {
456-
$this->processServerInfo($connectResponse);
459+
$this->processServerInfo($infoResponse);
460+
if ($this->serverInfo->isTLSRequired()) {
461+
if (!stream_socket_enable_crypto($this->streamSocket, true,
462+
STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT)) {
463+
throw Exception('Couldnt enable crypto');
464+
}
465+
}
457466
}
458467

468+
$msg = 'CONNECT '.$this->options;
469+
$this->send($msg);
459470
$this->ping();
460471
$pingResponse = $this->receive();
461472

@@ -560,9 +571,9 @@ public function unsubscribe($sid, $quantity = null)
560571
* @param string $payload Message data.
561572
* @param string $inbox Message inbox.
562573
*
574+
* @throws Exception If subscription not found.
563575
* @return void
564576
*
565-
* @throws Exception If subscription not found.
566577
*/
567578
public function publish($subject, $payload = null, $inbox = null)
568579
{

0 commit comments

Comments
 (0)