1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Thruster\Component\SocketClient; |
4
|
|
|
|
5
|
|
|
use Thruster\Component\EventLoop\EventLoopInterface; |
6
|
|
|
use Thruster\Component\Promise\PromiseInterface; |
7
|
|
|
use Thruster\Component\Stream\Stream; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class SecureConnector |
11
|
|
|
* |
12
|
|
|
* @package Thruster\Component\SocketClient |
13
|
|
|
* @author Aurimas Niekis <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
class SecureConnector implements ConnectorInterface |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var ConnectorInterface |
19
|
|
|
*/ |
20
|
|
|
private $connector; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var StreamEncryption |
24
|
|
|
*/ |
25
|
|
|
private $streamEncryption; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var array |
29
|
|
|
*/ |
30
|
|
|
private $context; |
31
|
|
|
|
32
|
1 |
|
public function __construct(ConnectorInterface $connector, EventLoopInterface $loop) |
33
|
|
|
{ |
34
|
1 |
|
$this->context = []; |
35
|
|
|
|
36
|
1 |
|
$this->connector = $connector; |
37
|
1 |
|
$this->streamEncryption = new StreamEncryption($loop); |
38
|
1 |
|
} |
39
|
|
|
|
40
|
|
|
public function withContext(array $contextOptions) : self |
41
|
|
|
{ |
42
|
|
|
$connector = clone $this; |
43
|
|
|
|
44
|
|
|
$connector->context = array_filter($contextOptions + $connector->context, function ($value) { |
45
|
|
|
return ($value !== null); |
46
|
|
|
}); |
47
|
|
|
|
48
|
|
|
return $connector; |
49
|
|
|
} |
50
|
|
|
|
51
|
1 |
|
public function create(string $host, int $port) : PromiseInterface |
52
|
|
|
{ |
53
|
|
|
|
54
|
1 |
|
$sslContext = $this->context['ssl'] ?? []; |
55
|
|
|
|
56
|
1 |
|
$this->context['ssl'] = $sslContext + [ |
57
|
1 |
|
'SNI_enabled' => true, |
58
|
1 |
|
'SNI_server_name' => $host, |
59
|
1 |
|
'peer_name' => $host, |
60
|
|
|
]; |
61
|
|
|
|
62
|
|
|
return $this->connector->create($host, $port)->then(function (Stream $stream) use ($host) { |
63
|
|
|
// (unencrypted) TCP/IP connection succeeded |
64
|
|
|
|
65
|
|
|
// set required SSL/TLS context options |
66
|
1 |
|
$resource = $stream->getStream(); |
|
|
|
|
67
|
|
|
|
68
|
1 |
|
stream_context_set_option($stream->getStream(), $this->context); |
69
|
|
|
|
70
|
|
|
// try to enable encryption |
71
|
1 |
|
return $this->streamEncryption->enable($stream)->then(null, function ($error) use ($stream) { |
72
|
|
|
|
73
|
|
|
// establishing encryption failed => close invalid connection and return error |
74
|
|
|
$stream->close(); |
75
|
|
|
|
76
|
|
|
throw $error; |
77
|
1 |
|
}); |
78
|
1 |
|
}); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.