@@ -9,23 +9,45 @@ class SecureConnector implements ConnectorInterface
99{
1010 private $ connector ;
1111 private $ streamEncryption ;
12+ private $ context = array ();
1213
1314 public function __construct (ConnectorInterface $ connector , LoopInterface $ loop )
1415 {
1516 $ this ->connector = $ connector ;
1617 $ this ->streamEncryption = new StreamEncryption ($ loop );
1718 }
1819
20+ /**
21+ * sets additional context options (for SSL context wrapper)
22+ *
23+ * @param array $sslContextOptions assosiative array of additional context options
24+ * @return self returns a new instance with the additional context options applied
25+ * @link http://php.net/manual/en/context.ssl.php
26+ */
27+ public function withContext (array $ sslContextOptions )
28+ {
29+ $ connector = clone $ this ;
30+ $ connector ->context = array_filter ($ sslContextOptions + $ connector ->context , function ($ value ) {
31+ return ($ value !== null );
32+ });
33+
34+ return $ connector ;
35+ }
36+
1937 public function create ($ host , $ port )
2038 {
21- return $ this ->connector ->create ($ host , $ port )->then (function (Stream $ stream ) use ($ host ) {
39+ // merge explicit context options with default context
40+ $ context = $ this ->context + array (
41+ 'SNI_enabled ' => true ,
42+ 'SNI_server_name ' => $ host ,
43+ 'peer_name ' => $ host
44+ );
45+
46+ return $ this ->connector ->create ($ host , $ port )->then (function (Stream $ stream ) use ($ context ) {
2247 // (unencrypted) TCP/IP connection succeeded
2348
2449 // set required SSL/TLS context options
25- $ resource = $ stream ->stream ;
26- stream_context_set_option ($ resource , 'ssl ' , 'SNI_enabled ' , true );
27- stream_context_set_option ($ resource , 'ssl ' , 'SNI_server_name ' , $ host );
28- stream_context_set_option ($ resource , 'ssl ' , 'peer_name ' , $ host );
50+ stream_context_set_option ($ stream ->stream , array ('ssl ' => $ context ));
2951
3052 // try to enable encryption
3153 return $ this ->streamEncryption ->enable ($ stream )->then (null , function ($ error ) use ($ stream ) {
0 commit comments