1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of Ratchet. |
5
|
|
|
* |
6
|
|
|
** (c) 2016 Cees-Jan Kiewiet |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace WyriHaximus\Ratchet\Event; |
13
|
|
|
|
14
|
|
|
use Cake\Core\Configure; |
15
|
|
|
use Cake\Event\EventListenerInterface; |
16
|
|
|
use Cake\Event\EventManager; |
17
|
|
|
use React\EventLoop\LoopInterface; |
18
|
|
|
use Thruway\Authentication\AuthenticationManager; |
19
|
|
|
use Thruway\Peer\Router; |
20
|
|
|
use Thruway\Transport\RatchetTransportProvider; |
21
|
|
|
use WyriHaximus\Ratchet\Security\AuthorizationManager; |
22
|
|
|
use WyriHaximus\Ratchet\Security\JWTAuthProvider; |
23
|
|
|
use WyriHaximus\Ratchet\Security\WampCraAuthProvider; |
24
|
|
|
use WyriHaximus\Ratchet\Websocket\InternalClient; |
25
|
|
|
|
26
|
|
|
final class ConstructListener implements EventListenerInterface |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @var Router |
30
|
|
|
*/ |
31
|
|
|
private $router; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var LoopInterface |
35
|
|
|
*/ |
36
|
|
|
private $loop; |
37
|
|
|
|
38
|
|
|
private $authRealms = []; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @return array |
42
|
|
|
*/ |
43
|
|
|
public function implementedEvents() |
44
|
|
|
{ |
45
|
|
|
return [ |
46
|
|
|
ConstructEvent::EVENT => 'construct', |
47
|
|
|
]; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param ConstructEvent $event |
52
|
|
|
*/ |
53
|
|
|
public function construct(ConstructEvent $event) |
54
|
|
|
{ |
55
|
|
|
$this->loop = $event->getLoop(); |
56
|
|
|
$this->router = new Router($this->loop); |
57
|
|
|
|
58
|
|
|
$this->router->registerModule(new AuthenticationManager()); |
59
|
|
|
|
60
|
|
|
foreach (Configure::read('WyriHaximus.Ratchet.realms') as $realm => $config) { |
61
|
|
|
$this->setUpRealm($realm, $config, $event->getEventManager()); |
62
|
|
|
} |
63
|
|
|
$this->router->addTransportProvider( |
64
|
|
|
new RatchetTransportProvider( |
65
|
|
|
Configure::read('WyriHaximus.Ratchet.internal.address'), |
66
|
|
|
Configure::read('WyriHaximus.Ratchet.internal.port') |
67
|
|
|
) |
68
|
|
|
); |
69
|
|
|
|
70
|
|
|
if (count($this->authRealms) > 0) { |
71
|
|
|
$this->router->addInternalClient((new JWTAuthProvider($this->authRealms, $this->loop))); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$event->getEventManager()->dispatch(WebsocketStartEvent::create($this->loop)); |
75
|
|
|
|
76
|
|
|
$this->router->start(false); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
protected function setUpRealm($realm, array $config, EventManager $eventManager) |
80
|
|
|
{ |
81
|
|
|
$internalClient = new InternalClient($realm, $this->loop); |
82
|
|
|
$internalClient->setEventManager($eventManager); |
83
|
|
|
$this->router->addInternalClient($internalClient); |
84
|
|
|
if (!\igorw\get_in($config, ['auth'], false)) { |
85
|
|
|
return; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$this->router->registerModule((new AuthorizationManager($realm, $this->loop))->setEventManager($eventManager)); |
89
|
|
|
$this->authRealms[] = $realm; |
90
|
|
|
} |
91
|
|
|
} |