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

Commit 3ec3406

Browse files
author
tomponline
committed
Adds ability to use custom stream context to allow TLS mode
1 parent 396781a commit 3ec3406

2 files changed

Lines changed: 49 additions & 8 deletions

File tree

src/Nats/Connection.php

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -443,12 +443,9 @@ public function connect($timeout = null)
443443
$timeout = intval(ini_get('default_socket_timeout'));
444444
}
445445

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-
450446
$this->timeout = $timeout;
451-
$this->streamSocket = $this->getStream($this->options->getAddress(), $timeout, $context);
447+
$this->streamSocket = $this->getStream(
448+
$this->options->getAddress(), $timeout, $this->options->getStreamContext());
452449
$this->setStreamTimeout($timeout);
453450

454451
$infoResponse = $this->receive();
@@ -458,10 +455,18 @@ public function connect($timeout = null)
458455
} else {
459456
$this->processServerInfo($infoResponse);
460457
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');
458+
set_error_handler(
459+
function ($errno, $errstr, $errfile, $errline) {
460+
throw Exception::forFailedConnection($errstr);
461+
});
462+
463+
if (!stream_socket_enable_crypto(
464+
$this->streamSocket, true, STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT)) {
465+
throw Exception::forFailedConnection(
466+
'Error negotiating crypto');
464467
}
468+
469+
restore_error_handler();
465470
}
466471
}
467472

src/Nats/ConnectionOptions.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,13 @@ class ConnectionOptions
8181
*/
8282
private $reconnect = true;
8383

84+
/**
85+
* Stream context to use.
86+
*
87+
* @var resource
88+
*/
89+
private $streamContext = null;
90+
8491
/**
8592
* Allows to define parameters which can be set by passing them to the class constructor.
8693
*
@@ -97,6 +104,7 @@ class ConnectionOptions
97104
'verbose',
98105
'pedantic',
99106
'reconnect',
107+
'streamContext',
100108
];
101109

102110

@@ -120,6 +128,9 @@ class ConnectionOptions
120128
*/
121129
public function __construct($options = null)
122130
{
131+
//Default stream context
132+
$this->streamContext = stream_context_create();
133+
123134
if (empty($options) === false) {
124135
$this->initialize($options);
125136
}
@@ -420,6 +431,31 @@ public function setReconnect($reconnect)
420431
return $this;
421432
}
422433

434+
/**
435+
* Get stream context.
436+
*
437+
* @return resource
438+
*/
439+
public function getStreamContext()
440+
{
441+
return $this->streamContext;
442+
}
443+
444+
445+
/**
446+
* Set stream context.
447+
*
448+
* @param resource $streamContext Stream context.
449+
*
450+
* @return $this
451+
*/
452+
public function setStreamContext($streamContext)
453+
{
454+
$this->streamContext = $streamContext;
455+
456+
return $this;
457+
}
458+
423459
/**
424460
* Set the connection options.
425461
*

0 commit comments

Comments
 (0)