-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Expand file tree
/
Copy pathSocket.php
More file actions
528 lines (465 loc) · 15 KB
/
Copy pathSocket.php
File metadata and controls
528 lines (465 loc) · 15 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
<?php
declare(strict_types=1);
/**
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
* @link https://cakephp.org CakePHP(tm) Project
* @since 1.2.0
* @license https://opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Network;
use Cake\Core\Exception\CakeException;
use Cake\Core\InstanceConfigTrait;
use Cake\Network\Exception\SocketException;
use Cake\Validation\Validation;
use Composer\CaBundle\CaBundle;
use Exception;
use InvalidArgumentException;
/**
* CakePHP network socket connection class.
*
* Core base class for network communication.
*/
class Socket
{
use InstanceConfigTrait;
/**
* Default configuration settings for the socket connection
*
* @var array<string, mixed>
*/
protected array $_defaultConfig = [
'persistent' => false,
'host' => 'localhost',
'protocol' => 'tcp',
'port' => 80,
'timeout' => 30,
];
/**
* Reference to socket connection resource
*
* @var resource|null
*/
protected $connection;
/**
* This boolean contains the current state of the Socket class
*
* @var bool
* @deprecated 5.2.9 Use isConnected() instead.
*/
protected bool $connected = false;
/**
* This variable contains an array with the last error number (num) and string (str)
*
* @var array<string, mixed>
*/
protected array $lastError = [];
/**
* True if the socket stream is encrypted after a {@link \Cake\Network\Socket::enableCrypto()} call
*
* @var bool
*/
protected bool $encrypted = false;
/**
* Contains all the encryption methods available
*
* @var array<string, int>
*/
protected array $_encryptMethods = [
'sslv23_client' => STREAM_CRYPTO_METHOD_SSLv23_CLIENT,
'tls_client' => STREAM_CRYPTO_METHOD_TLS_CLIENT,
'tlsv10_client' => STREAM_CRYPTO_METHOD_TLSv1_0_CLIENT,
'tlsv11_client' => STREAM_CRYPTO_METHOD_TLSv1_1_CLIENT,
'tlsv12_client' => STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT,
'sslv23_server' => STREAM_CRYPTO_METHOD_SSLv23_SERVER,
'tls_server' => STREAM_CRYPTO_METHOD_TLS_SERVER,
'tlsv10_server' => STREAM_CRYPTO_METHOD_TLSv1_0_SERVER,
'tlsv11_server' => STREAM_CRYPTO_METHOD_TLSv1_1_SERVER,
'tlsv12_server' => STREAM_CRYPTO_METHOD_TLSv1_2_SERVER,
];
/**
* Used to capture connection warnings which can happen when there are
* SSL errors for example.
*
* @var array<string>
*/
protected array $_connectionErrors = [];
/**
* Constructor.
*
* @param array<string, mixed> $config Socket configuration, which will be merged with the base configuration
* @see \Cake\Network\Socket::$_defaultConfig
*/
public function __construct(array $config = [])
{
$this->setConfig($config);
}
/**
* Connect the socket to the given host and port.
*
* @return bool Success
* @throws \Cake\Network\Exception\SocketException
*/
public function connect(): bool
{
if ($this->connection) {
$this->disconnect();
}
if (str_contains($this->_config['host'], '://')) {
[$this->_config['protocol'], $this->_config['host']] = explode('://', $this->_config['host']);
}
$scheme = null;
if (!empty($this->_config['protocol'])) {
$scheme = $this->_config['protocol'] . '://';
}
$this->_setSslContext($this->_config['host']);
if (!empty($this->_config['context'])) {
$context = stream_context_create($this->_config['context']);
} else {
$context = stream_context_create();
}
$connectAs = STREAM_CLIENT_CONNECT;
if ($this->_config['persistent']) {
$connectAs |= STREAM_CLIENT_PERSISTENT;
}
/**
* @phpstan-ignore-next-line
*/
set_error_handler($this->_connectionErrorHandler(...));
$remoteSocketTarget = $scheme . $this->_config['host'];
$port = (int)$this->_config['port'];
if ($port > 0) {
$remoteSocketTarget .= ':' . $port;
}
$errNum = 0;
$errStr = '';
$this->connection = $this->_getStreamSocketClient(
$remoteSocketTarget,
$errNum,
$errStr,
(int)$this->_config['timeout'],
$connectAs,
$context,
);
restore_error_handler();
if ($this->connection === null && (!$errNum || !$errStr)) {
$this->setLastError($errNum ?? 0, $errStr ?? '');
throw new SocketException($errStr ?? '', $errNum ?? 0);
}
if ($this->connection === null && $this->_connectionErrors) {
$message = implode("\n", $this->_connectionErrors);
throw new SocketException($message, E_WARNING);
}
$connected = is_resource($this->connection);
$this->connected = $connected;
if ($connected) {
assert($this->connection !== null);
stream_set_timeout($this->connection, (int)$this->_config['timeout']);
}
return $connected;
}
/**
* Check the connection status after calling `connect()`.
*
* @return bool
*/
public function isConnected(): bool
{
return is_resource($this->connection);
}
/**
* Create a stream socket client. Mock utility.
*
* @param string $remoteSocketTarget remote socket
* @param int|null $errNum error number
* @param string|null $errStr error string
* @param int $timeout timeout
* @param int<0, 7> $connectAs flags
* @param resource $context context
* @return resource|null
*/
protected function _getStreamSocketClient(
string $remoteSocketTarget,
?int &$errNum,
?string &$errStr,
int $timeout,
int $connectAs,
$context,
) {
$resource = stream_socket_client(
$remoteSocketTarget,
$errNum,
$errStr,
$timeout,
$connectAs,
$context,
);
if (!$resource) {
return null;
}
return $resource;
}
/**
* Configure the SSL context options.
*
* @param string $host The host name being connected to.
* @return void
*/
protected function _setSslContext(string $host): void
{
foreach ($this->_config as $key => $value) {
if (!str_starts_with($key, 'ssl_')) {
continue;
}
$contextKey = substr($key, 4);
if (empty($this->_config['context']['ssl'][$contextKey])) {
$this->_config['context']['ssl'][$contextKey] = $value;
}
unset($this->_config[$key]);
}
$this->_config['context']['ssl']['SNI_enabled'] ??= true;
if (empty($this->_config['context']['ssl']['peer_name'])) {
$this->_config['context']['ssl']['peer_name'] = $host;
}
if (empty($this->_config['context']['ssl']['cafile'])) {
$this->_config['context']['ssl']['cafile'] = CaBundle::getBundledCaBundlePath();
}
if (!empty($this->_config['context']['ssl']['verify_host'])) {
$this->_config['context']['ssl']['CN_match'] = $host;
}
unset($this->_config['context']['ssl']['verify_host']);
}
/**
* stream_socket_client() does not populate errNum, or $errStr when there are
* connection errors, as in the case of SSL verification failure.
*
* Instead, we need to handle those errors manually.
*
* @param int $code Code number.
* @param string $message Message.
* @return void
*/
protected function _connectionErrorHandler(int $code, string $message): void
{
$this->_connectionErrors[] = $message;
}
/**
* Get the connection context.
*
* @return array<string, mixed>|null Null when there is no connection, an array when there is.
*/
public function context(): ?array
{
if (!$this->connection) {
return null;
}
return stream_context_get_options($this->connection);
}
/**
* Get the host name of the current connection.
*
* @return string Host name
*/
public function host(): string
{
if (Validation::ip($this->_config['host'])) {
return (string)gethostbyaddr($this->_config['host']);
}
return (string)gethostbyaddr($this->address());
}
/**
* Get the IP address of the current connection.
*
* @return string IP address
*/
public function address(): string
{
if (Validation::ip($this->_config['host'])) {
return $this->_config['host'];
}
return gethostbyname($this->_config['host']);
}
/**
* Get all IP addresses associated with the current connection.
*
* @return array<string> IP addresses
*/
public function addresses(): array
{
if (Validation::ip($this->_config['host'])) {
return [$this->_config['host']];
}
return gethostbynamel($this->_config['host']) ?: [];
}
/**
* Get the last error as a string.
*
* @return string|null Last error
*/
public function lastError(): ?string
{
if (!$this->lastError) {
return null;
}
return $this->lastError['num'] . ': ' . $this->lastError['str'];
}
/**
* Set the last error.
*
* @param int|null $errNum Error code
* @param string $errStr Error string
* @return void
*/
public function setLastError(?int $errNum, string $errStr): void
{
$this->lastError = ['num' => $errNum, 'str' => $errStr];
}
/**
* Write data to the socket.
*
* @param string $data The data to write to the socket.
* @return int Bytes written.
*/
public function write(string $data): int
{
if (!$this->isConnected() && !$this->connect()) {
return 0;
}
$totalBytes = strlen($data);
$written = 0;
while ($written < $totalBytes) {
assert($this->connection !== null);
$rv = fwrite($this->connection, substr($data, $written));
if ($rv === false || $rv === 0) {
return $written;
}
$written += $rv;
}
return $written;
}
/**
* Read data from the socket. Returns null if no data is available or no connection could be
* established.
*
* @param int $length Optional buffer length to read; defaults to 1024
* @return string|null Socket data
*/
public function read(int $length = 1024): ?string
{
if ($length < 1) {
throw new InvalidArgumentException('Length must be greater than `0`');
}
if (!$this->isConnected() && !$this->connect()) {
return null;
}
assert($this->connection !== null);
if (feof($this->connection)) {
return null;
}
$buffer = fread($this->connection, $length);
$info = stream_get_meta_data($this->connection);
if ($info['timed_out']) {
$this->setLastError(E_WARNING, 'Connection timed out');
return null;
}
return $buffer === false ? null : $buffer;
}
/**
* Disconnect the socket from the current connection.
*
* @return bool Success
*/
public function disconnect(): bool
{
if (!is_resource($this->connection)) {
$this->connected = false;
return true;
}
$this->connected = !fclose($this->connection);
if (!$this->connected) {
$this->connection = null;
}
return !$this->connected;
}
/**
* Destructor, used to disconnect from current connection.
*/
public function __destruct()
{
$this->disconnect();
}
/**
* Resets the state of this Socket instance to its initial state (before __construct() got executed)
*
* @param array|null $state Array with key and values to reset
* @return void
*/
public function reset(?array $state = null): void
{
if (!$state) {
static $initialState = [];
if (!$initialState) {
$initialState = get_class_vars(self::class);
}
$state = $initialState;
}
foreach ($state as $property => $value) {
$this->{$property} = $value;
}
}
/**
* Encrypts current stream socket, using one of the defined encryption methods
*
* @param string $type can be one of 'ssl2', 'ssl3', 'ssl23' or 'tls'
* @param string $clientOrServer can be one of 'client', 'server'. Default is 'client'
* @param bool $enable enable or disable encryption. Default is true (enable)
* @return void
* @throws \InvalidArgumentException When an invalid encryption scheme is chosen.
* @throws \Cake\Network\Exception\SocketException When attempting to enable SSL/TLS fails
* @see stream_socket_enable_crypto
*/
public function enableCrypto(string $type, string $clientOrServer = 'client', bool $enable = true): void
{
if (!array_key_exists($type . '_' . $clientOrServer, $this->_encryptMethods)) {
throw new InvalidArgumentException('Invalid encryption scheme chosen');
}
$method = $this->_encryptMethods[$type . '_' . $clientOrServer];
if ($method === STREAM_CRYPTO_METHOD_TLS_CLIENT) {
$method |= STREAM_CRYPTO_METHOD_TLSv1_1_CLIENT | STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT;
}
if ($method === STREAM_CRYPTO_METHOD_TLS_SERVER) {
$method |= STREAM_CRYPTO_METHOD_TLSv1_1_SERVER | STREAM_CRYPTO_METHOD_TLSv1_2_SERVER;
}
try {
if ($this->connection === null) {
throw new CakeException('You must call connect() first.');
}
$enableCryptoResult = stream_socket_enable_crypto($this->connection, $enable, $method);
} catch (Exception $e) {
$this->setLastError(null, $e->getMessage());
throw new SocketException($e->getMessage(), null, $e);
}
if ($enableCryptoResult === true) {
$this->encrypted = $enable;
return;
}
$errorMessage = 'Unable to perform enableCrypto operation on the current socket';
$this->setLastError(null, $errorMessage);
throw new SocketException($errorMessage);
}
/**
* Check the encryption status after calling `enableCrypto()`.
*
* @return bool
*/
public function isEncrypted(): bool
{
return $this->encrypted;
}
}