forked from PocketDock/PocketDockConsole
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConnection.php
More file actions
533 lines (464 loc) · 13.9 KB
/
Connection.php
File metadata and controls
533 lines (464 loc) · 13.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
<?php
namespace Wrench;
use Wrench\Payload\PayloadHandler;
use Wrench\Protocol\Protocol;
use Wrench\Payload\Payload;
use Wrench\Util\Configurable;
use Wrench\Socket\ServerClientSocket;
use Wrench\Server;
use Wrench\Exception as WrenchException;
use Wrench\Exception\CloseException;
use Wrench\Exception\ConnectionException;
use Wrench\Exception\HandshakeException;
use Wrench\Exception\BadRequestException;
use \Exception;
use \RuntimeException;
/**
* Represents a client connection on the server side
*
* i.e. the `Server` manages a bunch of `Connection`s
*/
class Connection extends Configurable
{
/**
* The connection manager
*
* @var ConnectionManager
*/
protected $manager;
/**
* Socket object
*
* Wraps the client connection resource
*
* @var ServerClientSocket
*/
protected $socket;
/**
* Whether the connection has successfully handshaken
*
* @var boolean
*/
protected $handshaked = false;
/**
* The application this connection belongs to
*
* @var Application\Application
*/
protected $application = null;
/**
* The IP address of the client
*
* @var string
*/
protected $ip;
/**
* The port of the client
*
* @var int
*/
protected $port;
/**
* The array of headers included with the original request (like Cookie for example)
* The headers specific to the web sockets handshaking have been stripped out
*
* @var array
*/
protected $headers = null;
/**
* The array of query parameters included in the original request
* The array is in the format 'key' => 'value'
*
* @var array
*/
protected $queryParams = null;
/**
* Connection ID
*
* @var string|null
*/
protected $id = null;
/**
* @var PayloadHandler
*/
protected $payloadHandler;
/**
* Constructor
*
* @param ConnectionManager $manager
* @param ServerClientSocket $socket
* @param array $options
*/
public function __construct(
ConnectionManager $manager,
ServerClientSocket $socket,
array $options = array()
) {
$this->manager = $manager;
$this->socket = $socket;
parent::__construct($options);
$this->configureClientInformation();
$this->configurePayloadHandler();
$this->log('Connected');
}
/**
* Gets the connection manager of this connection
*
* @return \Wrench\ConnectionManager
*/
public function getConnectionManager()
{
return $this->manager;
}
/**
* @see Wrench\Util.Configurable::configure()
*/
protected function configure(array $options)
{
$options = array_merge(array(
'connection_id_secret' => 'asu5gj656h64Da(0crt8pud%^WAYWW$u76dwb',
'connection_id_algo' => 'sha512',
), $options);
parent::configure($options);
}
protected function configurePayloadHandler()
{
$this->payloadHandler = new PayloadHandler(
array($this, 'handlePayload'),
$this->options
);
}
/**
* @throws RuntimeException
*/
protected function configureClientInformation()
{
$this->ip = $this->socket->getIp();
$this->port = $this->socket->getPort();
$this->configureClientId();
}
/**
* Configures the client ID
*
* We hash the client ID to prevent leakage of information if another client
* happens to get a hold of an ID. The secret *must* be lengthy, and must
* be kept secret for this to work: otherwise it's trivial to search the space
* of possible IP addresses/ports (well, if not trivial, at least very fast).
*/
protected function configureClientId()
{
$message = sprintf(
'%s:uri=%s&ip=%s&port=%s',
$this->options['connection_id_secret'],
rawurlencode($this->manager->getUri()),
rawurlencode($this->ip),
rawurlencode($this->port)
);
$algo = $this->options['connection_id_algo'];
if (extension_loaded('gmp')) {
$hash = hash($algo, $message);
$hash = gmp_strval(gmp_init('0x' . $hash, 16), 62);
} else {
// @codeCoverageIgnoreStart
$hash = hash($algo, $message);
// @codeCoverageIgnoreEnd
}
$this->id = $hash;
}
/**
* Data receiver
*
* Called by the connection manager when the connection has received data
*
* @param string $data
*/
public function onData($data)
{
if (!$this->handshaked) {
return $this->handshake($data);
}
return $this->handle($data);
}
/**
* Performs a websocket handshake
*
* @param string $data
* @throws BadRequestException
* @throws HandshakeException
* @throws WrenchException
*/
public function handshake($data)
{
try {
list($path, $origin, $key, $extensions, $protocol, $headers, $params)
= $this->protocol->validateRequestHandshake($data);
$this->headers = $headers;
$this->queryParams = $params;
$this->application = $this->manager->getApplicationForPath($path);
if (!$this->application) {
throw new BadRequestException('Invalid application');
}
$this->manager->getServer()->notify(
Server::EVENT_HANDSHAKE_REQUEST,
array($this, $path, $origin, $key, $extensions)
);
$response = $this->protocol->getResponseHandshake($key);
if (!$this->socket->isConnected()) {
throw new HandshakeException('Socket is not connected');
}
if ($this->socket->send($response) === false) {
throw new HandshakeException('Could not send handshake response');
}
$this->handshaked = true;
$this->log(sprintf(
'Handshake successful: %s:%d (%s) connected to %s',
$this->getIp(),
$this->getPort(),
$this->getId(),
$path
), 'info');
$this->manager->getServer()->notify(
Server::EVENT_HANDSHAKE_SUCCESSFUL,
array($this)
);
if (method_exists($this->application, 'onConnect')) {
$this->application->onConnect($this);
}
} catch (WrenchException $e) {
$this->log('Handshake failed: ' . $e, 'err');
$this->close($e);
}
}
/**
* Returns a string export of the given binary data
*
* @param string $data
* @return string
*/
protected function export($data)
{
$export = '';
foreach (str_split($data) as $chr) {
$export .= '\\x' . ord($chr);
}
}
/**
* Handle data received from the client
*
* The data passed in may belong to several different frames across one or
* more protocols. It may not even contain a single complete frame. This method
* manages slotting the data into separate payload objects.
*
* @todo An endpoint MUST be capable of handling control frames in the
* middle of a fragmented message.
* @param string $data
* @return void
*/
public function handle($data)
{
$this->payloadHandler->handle($data);
}
/**
* Handle a complete payload received from the client
*
* Public because called from our PayloadHandler
*
* @param Payload $payload
*/
public function handlePayload(Payload $payload)
{
$app = $this->getClientApplication();
$this->log('Handling payload: ' . $payload->getPayload(), 'debug');
switch ($type = $payload->getType()) {
case Protocol::TYPE_TEXT:
if (method_exists($app, 'onData')) {
$app->onData($payload, $this);
}
return;
case Protocol::TYPE_BINARY:
if(method_exists($app, 'onBinaryData')) {
$app->onBinaryData($payload, $this);
} else {
$this->close(1003);
}
break;
case Protocol::TYPE_PING:
$this->log('Ping received', 'notice');
$this->send($payload->getPayload(), Protocol::TYPE_PONG);
$this->log('Pong!', 'debug');
break;
/**
* A Pong frame MAY be sent unsolicited. This serves as a
* unidirectional heartbeat. A response to an unsolicited Pong
* frame is not expected.
*/
case Protocol::TYPE_PONG:
$this->log('Received unsolicited pong', 'info');
break;
case Protocol::TYPE_CLOSE:
$this->log('Close frame received', 'notice');
$this->close();
$this->log('Disconnected', 'info');
break;
default:
throw new ConnectionException('Unhandled payload type');
}
}
/**
* Sends the payload to the connection
*
* @param string $type
* @param string $data
* @throws HandshakeException
* @throws ConnectionException
* @return boolean
*/
public function send($data, $type = Protocol::TYPE_TEXT)
{
if (!$this->handshaked) {
throw new HandshakeException('Connection is not handshaked');
}
$payload = $this->protocol->getPayload();
// Servers don't send masked payloads
$payload->encode($data, $type, false);
if (!$payload->sendToSocket($this->socket)) {
$this->log('Could not send payload to client', 'warn');
throw new ConnectionException('Could not send data to connection: ' . $this->socket->getLastError());
}
return true;
}
/**
* Processes data on the socket
*
* @throws CloseException
*/
public function process()
{
$data = $this->socket->receive();
$bytes = strlen($data);
if ($bytes === 0 || $data === false) {
throw new CloseException('Error reading data from socket: ' . $this->socket->getLastError());
}
$this->onData($data);
}
/**
* Closes the connection according to the WebSocket protocol
*
* If an endpoint receives a Close frame and that endpoint did not
* previously send a Close frame, the endpoint MUST send a Close frame
* in response. It SHOULD do so as soon as is practical. An endpoint
* MAY delay sending a close frame until its current message is sent
* (for instance, if the majority of a fragmented message is already
* sent, an endpoint MAY send the remaining fragments before sending a
* Close frame). However, there is no guarantee that the endpoint which
* has already sent a Close frame will continue to process data.
* After both sending and receiving a close message, an endpoint
* considers the WebSocket connection closed, and MUST close the
* underlying TCP connection. The server MUST close the underlying TCP
* connection immediately; the client SHOULD wait for the server to
* close the connection but MAY close the connection at any time after
* sending and receiving a close message, e.g. if it has not received a
* TCP close from the server in a reasonable time period.
*
* @return boolean|null
*/
public function close($code = Protocol::CLOSE_NORMAL)
{
try {
if (!$this->handshaked) {
$response = $this->protocol->getResponseError($code);
$this->socket->send($response);
} else {
$response = $this->protocol->getClosePayload($code);
$this->socket->send($response->getPayload());
}
} catch (Exception $e) {
$this->log('Unable to send close message', 'warning');
}
if ($this->application && method_exists($this->application, 'onDisconnect')) {
$this->application->onDisconnect($this);
}
$this->socket->disconnect();
$this->manager->removeConnection($this);
}
/**
* Logs a message
*
* @param string $message
* @param string $priority
*/
public function log($message, $priority = 'info')
{
$this->manager->log(sprintf(
'%s: %s:%d (%s): %s',
__CLASS__,
$this->getIp(),
$this->getPort(),
$this->getId(),
$message
), $priority);
}
/**
* Gets the IP address of the connection
*
* @return string Usually dotted quad notation
*/
public function getIp()
{
return $this->ip;
}
/**
* Gets the port of the connection
*
* @return int
*/
public function getPort()
{
return $this->port;
}
/**
* Gets the non-web-sockets headers included with the original request
*
* @return array
*/
public function getHeaders()
{
return $this->headers;
}
/**
* Gets the query parameters included with the original request
*
* @return array
*/
public function getQueryParams()
{
return $this->queryParams;
}
/**
* Gets the connection ID
*
* @return string
*/
public function getId()
{
return $this->id;
}
/**
* Gets the socket object
*
* @return Socket\ServerClientSocket
*/
public function getSocket()
{
return $this->socket;
}
/**
* Gets the client application
*
* @return Application
*/
public function getClientApplication()
{
return (isset($this->application)) ? $this->application : false;
}
}