1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the slince/spike package. |
5
|
|
|
* |
6
|
|
|
* (c) Slince <[email protected]> |
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 Spike\Client\Handler; |
13
|
|
|
|
14
|
|
|
use Spike\Client\Event\Events; |
15
|
|
|
use Spike\Common\Protocol\Spike; |
16
|
|
|
use Spike\Common\Protocol\SpikeInterface; |
17
|
|
|
use Slince\EventDispatcher\Event; |
18
|
|
|
|
19
|
|
|
class AuthResponseHandler extends MessageActionHandler |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* {@inheritdoc} |
23
|
|
|
*/ |
24
|
|
|
public function handle(SpikeInterface $message) |
25
|
|
|
{ |
26
|
|
|
if (200 !== $message->getHeader('code')) { |
|
|
|
|
27
|
|
|
$this->getEventDispatcher()->dispatch(new Event(Events::AUTH_ERROR, $this->client, [ |
28
|
|
|
'message' => $message, |
29
|
|
|
])); |
30
|
|
|
//Close client if auth error |
31
|
|
|
$this->client->close(); |
32
|
|
|
} else { |
33
|
|
|
$this->getEventDispatcher()->dispatch(new Event(Events::AUTH_SUCCESS, $this->client, [ |
34
|
|
|
'message' => $message, |
35
|
|
|
])); |
36
|
|
|
$clientInfo = $message->getBody(); |
37
|
|
|
$this->client->setId($clientInfo['id']); |
38
|
|
|
Spike::setGlobalHeader('client-id', $clientInfo['id']); |
39
|
|
|
$this->registerTunnels(); |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Reports the proxy hosts to the server. |
45
|
|
|
*/ |
46
|
|
|
protected function registerTunnels() |
47
|
|
|
{ |
48
|
|
|
$tunnels = $this->client->getConfiguration()->getTunnels(); |
49
|
|
|
$this->getEventDispatcher()->dispatch(new Event(Events::REGISTER_TUNNELS, $this, [ |
50
|
|
|
'tunnels' => $tunnels, |
51
|
|
|
])); |
52
|
|
|
foreach ($tunnels as $tunnel) { |
53
|
|
|
$this->connection->write(new Spike('register_tunnel', $tunnel)); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
} |