-
-
Notifications
You must be signed in to change notification settings - Fork 158
Expand file tree
/
Copy path31-opportunistic-tls.php
More file actions
68 lines (59 loc) · 2.37 KB
/
31-opportunistic-tls.php
File metadata and controls
68 lines (59 loc) · 2.37 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
<?php
// Opportunistic TLS example showing a basic negotiation before enabling the encryption. It starts out as an
// unencrypted TCP connection. After both parties agreed to encrypt the connection they both enable the encryption.
// After which any communication over the line is encrypted.
//
// This example is design to show both sides in one go, as such the server stops listening for new connection after
// the first, this makes sure the loop shuts down after the example connection has closed.
//
// $ php examples/31-opportunistic-tls.php
use React\EventLoop\Loop;
use React\Socket\ConnectionInterface;
use React\Socket\Connector;
use React\Socket\OpportunisticTlsConnectionInterface;
use React\Socket\SocketServer;
require __DIR__ . '/../vendor/autoload.php';
$server = new SocketServer('opportunistic+tls://127.0.0.1:0', array(
'tls' => array(
'local_cert' => __DIR__ . '/localhost.pem',
)
));
$server->on('connection', static function (OpportunisticTlsConnectionInterface $connection) use ($server) {
$server->close();
$connection->on('data', function ($data) {
echo 'From Client: ', $data, PHP_EOL;
});
React\Promise\Stream\first($connection)->then(function ($data) use ($connection) {
if ($data === 'Let\'s encrypt?') {
$connection->write('yes');
return $connection->enableEncryption();
}
return $connection;
})->then(static function (ConnectionInterface $connection) {
$connection->write('Encryption enabled!');
})->done();
});
$client = new Connector(array(
'tls' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true,
),
));
$client->connect($server->getAddress())->then(static function (OpportunisticTlsConnectionInterface $connection) {
$connection->on('data', function ($data) {
echo 'From Server: ', $data, PHP_EOL;
});
$connection->write('Let\'s encrypt?');
return React\Promise\Stream\first($connection)->then(function ($data) use ($connection) {
if ($data === 'yes') {
return $connection->enableEncryption();
}
return $connection;
});
})->then(function (ConnectionInterface $connection) {
$connection->write('Encryption enabled!');
Loop::addTimer(1, static function () use ($connection) {
$connection->end('Cool! Bye!');
});
})->done();