@@ -51,13 +51,14 @@ class SocketListener extends BaseEventEmitter implements SocketListenerInterface
5151 * @param mixed[] $config
5252 * @throws InstantiationException
5353 */
54- public function __construct ($ endpointOrResource , LoopInterface $ loop , $ config = [])
54+ public function __construct ($ endpointOrResource , LoopInterface $ loop , $ config = [' ssl ' => false ])
5555 {
5656 $ this ->endpoint = $ endpointOrResource ;
5757 $ this ->socket = null ;
5858 $ this ->loop = $ loop ;
5959 $ this ->open = false ;
6060 $ this ->paused = true ;
61+ $ this ->config = $ config ;
6162 }
6263
6364 /**
@@ -97,7 +98,18 @@ public function start()
9798 else
9899 {
99100 $ this ->socket = &$ this ->endpoint ;
101+
102+ if ($ this ->config ['ssl ' ] === true )
103+ {
104+ stream_context_set_option ($ this ->socket , 'ssl ' , 'verify_peer ' , false );
105+ stream_context_set_option ($ this ->socket , 'ssl ' , 'allow_self_signed ' , true );
106+ stream_context_set_option ($ this ->socket ,'ssl ' ,'verify_peer_name ' ,false );
107+ stream_context_set_option ($ this ->socket , 'ssl ' , 'local_cert ' , $ this ->config ['local_cert ' ]);
108+ stream_context_set_option ($ this ->socket , 'ssl ' , 'local_pk ' , $ this ->config ['local_pk ' ]);
109+ stream_context_set_option ($ this ->socket , 'ssl ' , 'passphrase ' , $ this ->config ['passphrase ' ]);
110+ }
100111 }
112+
101113 $ this ->resume ();
102114 $ this ->open = true ;
103115 }
@@ -111,7 +123,6 @@ public function start()
111123 }
112124 }
113125
114-
115126 /**
116127 * @override
117128 * @inheritDoc
@@ -163,6 +174,17 @@ public function getLocalPort()
163174 return isset ($ address [1 ]) ? $ address [1 ] : '' ;
164175 }
165176
177+ /**
178+ * @override
179+ * @inheritDoc
180+ */
181+ public function getLocalTransport ()
182+ {
183+ $ endpoint = explode (':// ' , $ this ->getLocalEndpoint (), 2 );
184+
185+ return isset ($ endpoint [0 ])?$ endpoint [0 ]:'' ;
186+ }
187+
166188 /**
167189 * @override
168190 * @inheritDoc
@@ -234,6 +256,15 @@ public function isPaused()
234256 return $ this ->paused ;
235257 }
236258
259+ /**
260+ * @internal
261+ * @return bool
262+ */
263+ public function isEncrypt ()
264+ {
265+ return $ this ->encrypt && true ;
266+ }
267+
237268 /**
238269 * @override
239270 * @inheritDoc
@@ -316,26 +347,41 @@ protected function createServer($endpoint, $config = [])
316347 }
317348 }
318349
350+ $ ssl = (bool ) ((isset ($ config ['ssl ' ])) ? $ config ['ssl ' ] : false );
319351 $ backlog = (int ) (isset ($ config ['backlog ' ]) ? $ config ['backlog ' ] : self ::DEFAULT_BACKLOG );
320352 $ reuseaddr = (bool ) (isset ($ config ['reuseaddr ' ]) ? $ config ['reuseaddr ' ] : false );
321353 $ reuseport = (bool ) (isset ($ config ['reuseport ' ]) ? $ config ['reuseport ' ] : false );
322354
323- $ context = [];
324355 $ context ['socket ' ] = [
325- 'bindto ' => $ endpoint ,
326- 'backlog ' => $ backlog ,
327- 'ipv6_v6only ' => true ,
328- 'so_reuseaddr ' => $ reuseaddr ,
329- 'so_reuseport ' => $ reuseport ,
356+ 'bindto ' => $ endpoint ,
357+ 'backlog ' => $ backlog ,
358+ 'ipv6_v6only ' => true ,
359+ 'so_reuseaddr ' => $ reuseaddr ,
360+ 'so_reuseport ' => $ reuseport ,
330361 ];
331362
363+ if ($ ssl ) {
364+ $ context ['ssl ' ] = [
365+ 'verify_peer ' =>false ,
366+ 'verify_peer_name ' => false ,
367+ 'allow_self_signed ' => true ,
368+ 'local_cert ' => $ config ['local_cert ' ],
369+ 'local_pk ' => $ config ['local_pk ' ],
370+ 'passphrase ' => $ config ['passphrase ' ],
371+ ];
372+
373+ }
374+
375+ $ bitmask = STREAM_SERVER_BIND | STREAM_SERVER_LISTEN ;
376+
332377 $ context = stream_context_create ($ context );
378+
333379 // Error reporting suppressed since stream_socket_server() emits an E_WARNING on failure.
334- $ socket = @ stream_socket_server (
380+ $ socket = stream_socket_server (
335381 $ endpoint ,
336382 $ errno ,
337383 $ errstr ,
338- STREAM_SERVER_BIND | STREAM_SERVER_LISTEN ,
384+ $ bitmask ,
339385 $ context
340386 );
341387
@@ -414,21 +460,32 @@ private function parseEndpoint()
414460 $ name = stream_socket_get_name ($ this ->socket , false );
415461 $ type = $ this ->getStreamType ();
416462
417- switch ($ type ) {
463+ switch ($ type )
464+ {
418465 case Socket::TYPE_UNIX :
419- $ endpoint = 'unix:// ' . $ name ;
466+ $ transport = 'unix:// ' ;
467+ $ endpoint = $ transport . $ name ;
420468 break ;
421469
422470 case Socket::TYPE_TCP :
423- if (substr_count ($ name , ': ' ) > 1 ) {
471+ $ transport = 'tcp:// ' ;
472+ if (substr_count ($ name , ': ' ) > 1 )
473+ {
424474 $ parts = explode (': ' , $ name );
425475 $ count = count ($ parts );
426476 $ port = $ parts [$ count - 1 ];
427477 unset($ parts [$ count - 1 ]);
428- $ endpoint = 'tcp://[ ' . implode (': ' , $ parts ) . ']: ' . $ port ;
429- } else {
430- $ endpoint = 'tcp:// ' . $ name ;
478+ $ endpoint = $ transport .'[ ' . implode (': ' , $ parts ) . ']: ' . $ port ;
431479 }
480+ else
481+ {
482+ $ endpoint = $ transport . $ name ;
483+ }
484+ break ;
485+
486+ case Socket::TYPE_UDP :
487+ $ transport = 'udp:// ' ;
488+ $ endpoint = $ transport . $ name ;
432489 break ;
433490
434491 default :
0 commit comments