mirror of
https://github.com/bitinflow/expose.git
synced 2026-03-13 21:45:55 +00:00
wip
This commit is contained in:
@@ -6,6 +6,7 @@ use App\Contracts\ConnectionManager as ConnectionManagerContract;
|
||||
use App\Contracts\SubdomainGenerator;
|
||||
use Ratchet\ConnectionInterface;
|
||||
use React\EventLoop\LoopInterface;
|
||||
use React\Socket\Server;
|
||||
|
||||
class ConnectionManager implements ConnectionManagerContract
|
||||
{
|
||||
@@ -53,6 +54,24 @@ class ConnectionManager implements ConnectionManagerContract
|
||||
return $storedConnection;
|
||||
}
|
||||
|
||||
public function storeTcpConnection(int $port, ConnectionInterface $connection): ControlConnection
|
||||
{
|
||||
$clientId = (string) uniqid();
|
||||
|
||||
$connection->client_id = $clientId;
|
||||
|
||||
$storedConnection = new TcpControlConnection($connection, $port, $this->getSharedTcpServer(), $clientId);
|
||||
|
||||
$this->connections[] = $storedConnection;
|
||||
|
||||
return $storedConnection;
|
||||
}
|
||||
|
||||
protected function getSharedTcpServer(): Server
|
||||
{
|
||||
return new Server(0, $this->loop);
|
||||
}
|
||||
|
||||
public function storeHttpConnection(ConnectionInterface $httpConnection, $requestId): HttpConnection
|
||||
{
|
||||
$this->httpConnections[$requestId] = new HttpConnection($httpConnection);
|
||||
|
||||
@@ -55,6 +55,7 @@ class ControlConnection
|
||||
public function toArray()
|
||||
{
|
||||
return [
|
||||
'type' => 'http',
|
||||
'host' => $this->host,
|
||||
'client_id' => $this->client_id,
|
||||
'subdomain' => $this->subdomain,
|
||||
|
||||
101
app/Server/Connections/TcpControlConnection.php
Normal file
101
app/Server/Connections/TcpControlConnection.php
Normal file
@@ -0,0 +1,101 @@
|
||||
<?php
|
||||
|
||||
namespace App\Server\Connections;
|
||||
|
||||
use Evenement\EventEmitterTrait;
|
||||
use Ratchet\ConnectionInterface;
|
||||
use React\Socket\Server;
|
||||
|
||||
class TcpControlConnection extends ControlConnection
|
||||
{
|
||||
public $proxy;
|
||||
public $proxyConnection;
|
||||
public $port;
|
||||
public $shared_port;
|
||||
public $shared_server;
|
||||
|
||||
public function __construct(ConnectionInterface $socket, int $port, Server $sharedServer, string $clientId)
|
||||
{
|
||||
$this->socket = $socket;
|
||||
$this->client_id = $clientId;
|
||||
$this->shared_server = $sharedServer;
|
||||
$this->port = $port;
|
||||
$this->shared_at = now()->toDateTimeString();
|
||||
$this->shared_port = parse_url($sharedServer->getAddress(), PHP_URL_PORT);
|
||||
|
||||
$this->configureServer($sharedServer);
|
||||
}
|
||||
|
||||
public function setMaximumConnectionLength(int $maximumConnectionLength)
|
||||
{
|
||||
$this->socket->send(json_encode([
|
||||
'event' => 'setMaximumConnectionLength',
|
||||
'data' => [
|
||||
'length' => $maximumConnectionLength,
|
||||
],
|
||||
]));
|
||||
}
|
||||
|
||||
public function registerProxy($requestId)
|
||||
{
|
||||
$this->socket->send(json_encode([
|
||||
'event' => 'createProxy',
|
||||
'data' => [
|
||||
'request_id' => $requestId,
|
||||
'client_id' => $this->client_id,
|
||||
],
|
||||
]));
|
||||
}
|
||||
|
||||
public function registerTcpProxy($requestId)
|
||||
{
|
||||
$this->socket->send(json_encode([
|
||||
'event' => 'createTcpProxy',
|
||||
'data' => [
|
||||
'port' => $this->port,
|
||||
'tcp_request_id' => $requestId,
|
||||
'client_id' => $this->client_id,
|
||||
],
|
||||
]));
|
||||
}
|
||||
|
||||
public function close()
|
||||
{
|
||||
$this->socket->close();
|
||||
}
|
||||
|
||||
public function toArray()
|
||||
{
|
||||
return [
|
||||
'type' => 'tcp',
|
||||
'port' => $this->port,
|
||||
'client_id' => $this->client_id,
|
||||
'shared_port' => $this->shared_port,
|
||||
'shared_at' => $this->shared_at,
|
||||
];
|
||||
}
|
||||
|
||||
protected function configureServer(Server $sharedServer)
|
||||
{
|
||||
$requestId = uniqid();
|
||||
|
||||
$sharedServer->on('connection', function(\React\Socket\ConnectionInterface $connection) use ($requestId) {
|
||||
|
||||
$this->proxyConnection = $connection;
|
||||
|
||||
$this->once('tcp_proxy_ready_'.$requestId, function (ConnectionInterface $proxy) use ($connection) {
|
||||
$this->proxy = $proxy;
|
||||
dump("Proxy ready");
|
||||
|
||||
$connection->on('data', function($data) use ($proxy) {
|
||||
$proxy->send($data);
|
||||
});
|
||||
|
||||
$connection->resume();
|
||||
});
|
||||
|
||||
$connection->pause();
|
||||
$this->registerTcpProxy($requestId);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -54,6 +54,10 @@ class ControlMessageController implements MessageComponentInterface
|
||||
if (isset($connection->request_id)) {
|
||||
return $this->sendResponseToHttpConnection($connection->request_id, $msg);
|
||||
}
|
||||
if (isset($connection->tcp_request_id)) {
|
||||
$connectionInfo = $this->connectionManager->findControlConnectionForClientId($connection->tcp_client_id);
|
||||
$connectionInfo->proxyConnection->write($msg);
|
||||
}
|
||||
|
||||
try {
|
||||
$payload = json_decode($msg);
|
||||
@@ -78,22 +82,11 @@ class ControlMessageController implements MessageComponentInterface
|
||||
{
|
||||
$this->verifyAuthToken($connection)
|
||||
->then(function () use ($connection, $data) {
|
||||
if (! $this->hasValidSubdomain($connection, $data->subdomain)) {
|
||||
return;
|
||||
if ($data->type === 'http') {
|
||||
$this->handleHttpConnection($connection, $data);
|
||||
} elseif($data->type === 'tcp') {
|
||||
$this->handleTcpConnection($connection, $data);
|
||||
}
|
||||
|
||||
$connectionInfo = $this->connectionManager->storeConnection($data->host, $data->subdomain, $connection);
|
||||
|
||||
$this->connectionManager->limitConnectionLength($connectionInfo, config('expose.admin.maximum_connection_length'));
|
||||
|
||||
$connection->send(json_encode([
|
||||
'event' => 'authenticated',
|
||||
'data' => [
|
||||
'message' => config('expose.admin.messages.message_of_the_day'),
|
||||
'subdomain' => $connectionInfo->subdomain,
|
||||
'client_id' => $connectionInfo->client_id,
|
||||
],
|
||||
]));
|
||||
}, function () use ($connection) {
|
||||
$connection->send(json_encode([
|
||||
'event' => 'authenticationFailed',
|
||||
@@ -105,6 +98,41 @@ class ControlMessageController implements MessageComponentInterface
|
||||
});
|
||||
}
|
||||
|
||||
protected function handleHttpConnection(ConnectionInterface $connection, $data)
|
||||
{
|
||||
if (! $this->hasValidSubdomain($connection, $data->subdomain)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$connectionInfo = $this->connectionManager->storeConnection($data->host, $data->subdomain, $connection);
|
||||
|
||||
$this->connectionManager->limitConnectionLength($connectionInfo, config('expose.admin.maximum_connection_length'));
|
||||
|
||||
$connection->send(json_encode([
|
||||
'event' => 'authenticated',
|
||||
'data' => [
|
||||
'message' => config('expose.admin.messages.message_of_the_day'),
|
||||
'subdomain' => $connectionInfo->subdomain,
|
||||
'client_id' => $connectionInfo->client_id,
|
||||
],
|
||||
]));
|
||||
}
|
||||
|
||||
protected function handleTcpConnection(ConnectionInterface $connection, $data)
|
||||
{
|
||||
$connectionInfo = $this->connectionManager->storeTcpConnection($data->port, $connection);
|
||||
|
||||
$connection->send(json_encode([
|
||||
'event' => 'authenticated',
|
||||
'data' => [
|
||||
'message' => config('expose.admin.messages.message_of_the_day'),
|
||||
'port' => $connectionInfo->port,
|
||||
'shared_port' => $connectionInfo->shared_port,
|
||||
'client_id' => $connectionInfo->client_id,
|
||||
],
|
||||
]));
|
||||
}
|
||||
|
||||
protected function registerProxy(ConnectionInterface $connection, $data)
|
||||
{
|
||||
$connection->request_id = $data->request_id;
|
||||
@@ -116,6 +144,18 @@ class ControlMessageController implements MessageComponentInterface
|
||||
]);
|
||||
}
|
||||
|
||||
protected function registerTcpProxy(ConnectionInterface $connection, $data)
|
||||
{
|
||||
$connection->tcp_client_id = $data->client_id;
|
||||
$connection->tcp_request_id = $data->tcp_request_id;
|
||||
|
||||
$connectionInfo = $this->connectionManager->findControlConnectionForClientId($data->client_id);
|
||||
|
||||
$connectionInfo->emit('tcp_proxy_ready_'.$data->tcp_request_id, [
|
||||
$connection,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user