mirror of
https://github.com/bitinflow/expose.git
synced 2026-03-14 22:15:55 +00:00
wip
This commit is contained in:
@@ -1,46 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Server\Connections;
|
||||
|
||||
use GuzzleHttp\Psr7\Request;
|
||||
use Illuminate\Support\Str;
|
||||
use Ratchet\ConnectionInterface;
|
||||
use React\Stream\Util;
|
||||
|
||||
class Connection
|
||||
{
|
||||
/** @var IoConnection */
|
||||
public $socket;
|
||||
public $host;
|
||||
public $subdomain;
|
||||
public $client_id;
|
||||
public $proxies = [];
|
||||
|
||||
public function __construct(IoConnection $socket, string $host, string $subdomain, string $clientId)
|
||||
{
|
||||
$this->socket = $socket;
|
||||
$this->host = $host;
|
||||
$this->subdomain = $subdomain;
|
||||
$this->client_id = $clientId;
|
||||
}
|
||||
|
||||
public function registerProxy($requestId)
|
||||
{
|
||||
$this->socket->send(json_encode([
|
||||
'event' => 'createProxy',
|
||||
'request_id' => $requestId,
|
||||
'client_id' => $this->client_id,
|
||||
]) . "||");
|
||||
}
|
||||
|
||||
public function pipeRequestThroughProxy(HttpRequestConnection $httpConnection, string $requestId, Request $request)
|
||||
{
|
||||
$this->registerProxy($requestId);
|
||||
|
||||
$this->socket->getConnection()->once('proxy_ready_' . $requestId, function (IoConnection $proxy) use ($request, $requestId, $httpConnection) {
|
||||
Util::pipe($proxy->getConnection(), $httpConnection->getConnection());
|
||||
|
||||
$proxy->send(\GuzzleHttp\Psr7\str($request));
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -2,59 +2,76 @@
|
||||
|
||||
namespace App\Server\Connections;
|
||||
|
||||
use Illuminate\Support\Str;
|
||||
use App\Contracts\ConnectionManager as ConnectionManagerContract;
|
||||
use App\Contracts\SubdomainGenerator;
|
||||
use Ratchet\ConnectionInterface;
|
||||
|
||||
class ConnectionManager
|
||||
class ConnectionManager implements ConnectionManagerContract
|
||||
{
|
||||
/** @var array */
|
||||
protected $connections = [];
|
||||
protected $hostname;
|
||||
protected $port;
|
||||
|
||||
public function __construct($hostname, $port)
|
||||
/** @var array */
|
||||
protected $httpConnections = [];
|
||||
|
||||
/** @var SubdomainGenerator */
|
||||
protected $subdomainGenerator;
|
||||
|
||||
public function __construct(SubdomainGenerator $subdomainGenerator)
|
||||
{
|
||||
$this->hostname = $hostname;
|
||||
$this->port = $port;
|
||||
$this->subdomainGenerator = $subdomainGenerator;
|
||||
}
|
||||
|
||||
public function storeConnection(string $host, ?string $subdomain, IoConnection $connection)
|
||||
public function storeConnection(string $host, ?string $subdomain, ConnectionInterface $connection): ControlConnection
|
||||
{
|
||||
$clientId = (string)uniqid();
|
||||
|
||||
$storedConnection = new Connection($connection, $host, $subdomain ?? $this->generateSubdomain(), $clientId);
|
||||
$connection->client_id = $clientId;
|
||||
|
||||
$storedConnection = new ControlConnection($connection, $host, $subdomain ?? $this->subdomainGenerator->generateSubdomain(), $clientId);
|
||||
|
||||
$this->connections[] = $storedConnection;
|
||||
|
||||
return $storedConnection;
|
||||
}
|
||||
|
||||
public function findConnectionForSubdomain($subdomain): ?Connection
|
||||
public function storeHttpConnection(ConnectionInterface $httpConnection, $requestId): ConnectionInterface
|
||||
{
|
||||
$this->httpConnections[$requestId] = $httpConnection;
|
||||
|
||||
return $httpConnection;
|
||||
}
|
||||
|
||||
public function getHttpConnectionForRequestId(string $requestId): ?ConnectionInterface
|
||||
{
|
||||
return $this->httpConnections[$requestId] ?? null;
|
||||
}
|
||||
|
||||
public function removeControlConnection($connection)
|
||||
{
|
||||
if (isset($this->httpConnections[$connection->request_id])) {
|
||||
unset($this->httpConnections[$connection->request_id]);
|
||||
}
|
||||
|
||||
if (isset($connection->client_id)) {
|
||||
$clientId = $connection->client_id;
|
||||
$this->collections = collect($this->connections)->reject(function ($connection) use ($clientId) {
|
||||
return $connection->client_id == $clientId;
|
||||
})->toArray();
|
||||
}
|
||||
}
|
||||
|
||||
public function findControlConnectionForSubdomain($subdomain): ?ControlConnection
|
||||
{
|
||||
return collect($this->connections)->last(function ($connection) use ($subdomain) {
|
||||
return $connection->subdomain == $subdomain;
|
||||
});
|
||||
}
|
||||
|
||||
public function findConnectionForClientId(string $clientId): ?Connection
|
||||
public function findControlConnectionForClientId(string $clientId): ?ControlConnection
|
||||
{
|
||||
return collect($this->connections)->last(function ($connection) use ($clientId) {
|
||||
return $connection->client_id == $clientId;
|
||||
});
|
||||
}
|
||||
|
||||
protected function generateSubdomain(): string
|
||||
{
|
||||
return strtolower(Str::random(10));
|
||||
}
|
||||
|
||||
public function host()
|
||||
{
|
||||
return $this->hostname;
|
||||
}
|
||||
|
||||
public function port()
|
||||
{
|
||||
return $this->port;
|
||||
}
|
||||
}
|
||||
|
||||
43
app/Server/Connections/ControlConnection.php
Normal file
43
app/Server/Connections/ControlConnection.php
Normal file
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
namespace App\Server\Connections;
|
||||
|
||||
use Evenement\EventEmitterTrait;
|
||||
use GuzzleHttp\Psr7\Request;
|
||||
use Illuminate\Support\Str;
|
||||
use Nyholm\Psr7\Factory\Psr17Factory;
|
||||
use Ratchet\Client\WebSocket;
|
||||
use Ratchet\ConnectionInterface;
|
||||
use Ratchet\RFC6455\Messaging\Frame;
|
||||
use Ratchet\WebSocket\WsConnection;
|
||||
use React\Stream\Util;
|
||||
use Symfony\Bridge\PsrHttpMessage\Factory\PsrHttpFactory;
|
||||
|
||||
class ControlConnection
|
||||
{
|
||||
use EventEmitterTrait;
|
||||
|
||||
/** @var ConnectionInterface */
|
||||
public $socket;
|
||||
public $host;
|
||||
public $subdomain;
|
||||
public $client_id;
|
||||
public $proxies = [];
|
||||
|
||||
public function __construct(ConnectionInterface $socket, string $host, string $subdomain, string $clientId)
|
||||
{
|
||||
$this->socket = $socket;
|
||||
$this->host = $host;
|
||||
$this->subdomain = $subdomain;
|
||||
$this->client_id = $clientId;
|
||||
}
|
||||
|
||||
public function registerProxy($requestId)
|
||||
{
|
||||
$this->socket->send(json_encode([
|
||||
'event' => 'createProxy',
|
||||
'request_id' => $requestId,
|
||||
'client_id' => $this->client_id,
|
||||
]));
|
||||
}
|
||||
}
|
||||
@@ -1,76 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Server\Connections;
|
||||
|
||||
use GuzzleHttp\Psr7\Request;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Str;
|
||||
use Ratchet\ConnectionInterface;
|
||||
use function GuzzleHttp\Psr7\parse_request;
|
||||
|
||||
class HttpRequestConnection implements ConnectionInterface
|
||||
{
|
||||
/** @var IoConnection */
|
||||
protected $connection;
|
||||
|
||||
public static function wrap(ConnectionInterface $connection, $message)
|
||||
{
|
||||
return new static($connection, $message);
|
||||
}
|
||||
|
||||
public function __construct(ConnectionInterface $connection, $message)
|
||||
{
|
||||
$this->connection = $connection;
|
||||
|
||||
if (! isset($this->connection->buffer)) {
|
||||
$this->connection->buffer = '';
|
||||
}
|
||||
|
||||
$this->connection->buffer .= $message;
|
||||
}
|
||||
|
||||
public function getRequest(): Request
|
||||
{
|
||||
return parse_request($this->connection->buffer);
|
||||
}
|
||||
|
||||
protected function getContentLength(): ?int
|
||||
{
|
||||
return Arr::first($this->getRequest()->getHeader('Content-Length'));
|
||||
}
|
||||
|
||||
public function hasBufferedAllData()
|
||||
{
|
||||
return is_null($this->getContentLength()) || strlen(Str::after($this->connection->buffer, "\r\n\r\n")) === $this->getContentLength();
|
||||
}
|
||||
|
||||
public function getConnection()
|
||||
{
|
||||
return $this->connection->getConnection();
|
||||
}
|
||||
|
||||
public function __get($key)
|
||||
{
|
||||
return $this->connection->$key;
|
||||
}
|
||||
|
||||
public function __set($key, $value)
|
||||
{
|
||||
return $this->connection->$key = $value;
|
||||
}
|
||||
|
||||
public function __unset($key)
|
||||
{
|
||||
unset($this->connection->$key);
|
||||
}
|
||||
|
||||
public function send($data)
|
||||
{
|
||||
return $this->connection->send($data);
|
||||
}
|
||||
|
||||
public function close()
|
||||
{
|
||||
return $this->connection->close();
|
||||
}
|
||||
}
|
||||
@@ -1,44 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Server\Connections;
|
||||
|
||||
use Ratchet\ConnectionInterface;
|
||||
use React\Socket\ConnectionInterface as ReactConn;
|
||||
|
||||
class IoConnection implements ConnectionInterface {
|
||||
/**
|
||||
* @var \React\Socket\ConnectionInterface
|
||||
*/
|
||||
protected $conn;
|
||||
|
||||
/**
|
||||
* @param \React\Socket\ConnectionInterface $conn
|
||||
*/
|
||||
public function __construct(ReactConn $conn) {
|
||||
$this->conn = $conn;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return ReactConn
|
||||
*/
|
||||
public function getConnection(): ReactConn
|
||||
{
|
||||
return $this->conn;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function send($data) {
|
||||
$this->conn->write($data);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function close() {
|
||||
$this->conn->end();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user