-
Notifications
You must be signed in to change notification settings - Fork 308
Expand file tree
/
Copy pathProxyManager.php
More file actions
109 lines (91 loc) · 4.3 KB
/
ProxyManager.php
File metadata and controls
109 lines (91 loc) · 4.3 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
<?php
namespace Expose\Client;
use Psr\Http\Message\ResponseInterface;
use Expose\Client\Http\HttpClient;
use Ratchet\Client\WebSocket;
use Ratchet\RFC6455\Messaging\Frame;
use React\EventLoop\LoopInterface;
use React\Socket\Connector;
use function Ratchet\Client\connect;
class ProxyManager
{
/** @var Configuration */
protected $configuration;
/** @var LoopInterface */
protected $loop;
public function __construct(Configuration $configuration, LoopInterface $loop)
{
$this->configuration = $configuration;
$this->loop = $loop;
}
public function createProxy(string $clientId, $connectionData)
{
$protocol = $this->configuration->port() === 443 ? 'wss' : 'ws';
connect($protocol."://{$this->configuration->host()}:{$this->configuration->port()}/expose/control", [], [
'X-Expose-Control' => 'enabled',
], $this->loop)
->then(function (WebSocket $proxyConnection) use ($clientId, $connectionData) {
$localRequestConnection = null;
$proxyConnection->on('message', function ($message) use (&$localRequestConnection, $proxyConnection, $connectionData) {
if ($localRequestConnection) {
$localRequestConnection->write($message);
return;
}
$this->performRequest($proxyConnection, (string) $message, $connectionData)
->then(function ($response) use ($proxyConnection, &$localRequestConnection) {
if (is_null($response)) {
return;
}
/** @var $body \React\Stream\DuplexStreamInterface */
$body = $response->getBody();
if ($body) {
$localRequestConnection = $body;
}
if ($body->isWritable()) {
$body->on('data', function ($chunk) use ($proxyConnection) {
$binaryMsg = new Frame($chunk, true, Frame::OP_BINARY);
$proxyConnection->send($binaryMsg);
});
}
});
});
$proxyConnection->send(json_encode([
'event' => 'registerProxy',
'data' => [
'request_id' => $connectionData->request_id ?? null,
'client_id' => $clientId,
],
]));
});
}
public function createTcpProxy(string $clientId, $connectionData)
{
$protocol = $this->configuration->port() === 443 ? 'wss' : 'ws';
connect($protocol."://{$this->configuration->host()}:{$this->configuration->port()}/expose/control", [], [
'X-Expose-Control' => 'enabled',
], $this->loop)
->then(function (WebSocket $proxyConnection) use ($clientId, $connectionData) {
$connector = new Connector($this->loop);
$connector->connect('127.0.0.1:'.$connectionData->port)->then(function ($connection) use ($proxyConnection) {
$connection->on('data', function ($data) use ($proxyConnection) {
$binaryMsg = new Frame($data, true, Frame::OP_BINARY);
$proxyConnection->send($binaryMsg);
});
$proxyConnection->on('message', function ($message) use ($connection) {
$connection->write($message);
});
});
$proxyConnection->send(json_encode([
'event' => 'registerTcpProxy',
'data' => [
'tcp_request_id' => $connectionData->tcp_request_id ?? null,
'client_id' => $clientId,
],
]));
});
}
protected function performRequest(WebSocket $proxyConnection, string $requestData, $connectionData)
{
return app(HttpClient::class)->performRequest((string) $requestData, $proxyConnection, $connectionData);
}
}