mirror of
https://github.com/bitinflow/expose.git
synced 2026-03-13 13:35:54 +00:00
wip
This commit is contained in:
29
app/Server/Configuration.php
Normal file
29
app/Server/Configuration.php
Normal file
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace App\Server;
|
||||
|
||||
class Configuration
|
||||
{
|
||||
/** @var string */
|
||||
protected $hostname;
|
||||
|
||||
/** @var int */
|
||||
protected $port;
|
||||
|
||||
public function __construct(string $hostname, int $port)
|
||||
{
|
||||
$this->hostname = $hostname;
|
||||
|
||||
$this->port = $port;
|
||||
}
|
||||
|
||||
public function hostname(): string
|
||||
{
|
||||
return $this->hostname;
|
||||
}
|
||||
|
||||
public function port(): int
|
||||
{
|
||||
return $this->port;
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -1,50 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Server;
|
||||
|
||||
use App\Server\Connections\ConnectionManager;
|
||||
use App\Server\Connections\HttpRequestConnection;
|
||||
use App\Server\Messages\ControlMessage;
|
||||
use App\Server\Messages\MessageFactory;
|
||||
use App\Server\Messages\TunnelMessage;
|
||||
use Ratchet\ConnectionInterface;
|
||||
use Ratchet\MessageComponentInterface;
|
||||
use function GuzzleHttp\Psr7\parse_request;
|
||||
|
||||
class Expose implements MessageComponentInterface
|
||||
{
|
||||
protected $connectionManager;
|
||||
|
||||
public function __construct(ConnectionManager $connectionManager)
|
||||
{
|
||||
$this->connectionManager = $connectionManager;
|
||||
}
|
||||
|
||||
public function onOpen(ConnectionInterface $conn)
|
||||
{
|
||||
// TODO: Implement onOpen() method.
|
||||
}
|
||||
|
||||
public function onClose(ConnectionInterface $conn)
|
||||
{
|
||||
dump("close connection");
|
||||
}
|
||||
|
||||
public function onError(ConnectionInterface $conn, \Exception $e)
|
||||
{
|
||||
// TODO: Implement onError() method.
|
||||
}
|
||||
|
||||
public function onMessage(ConnectionInterface $connection, $message)
|
||||
{
|
||||
$payload = json_decode($message);
|
||||
|
||||
if (json_last_error() === JSON_ERROR_NONE) {
|
||||
$message = new ControlMessage($payload, $connection, $this->connectionManager);
|
||||
$message->respond();
|
||||
} else {
|
||||
$message = new TunnelMessage(HttpRequestConnection::wrap($connection, $message), $this->connectionManager);
|
||||
$message->respond();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,10 +2,23 @@
|
||||
|
||||
namespace App\Server;
|
||||
|
||||
use App\Contracts\ConnectionManager as ConnectionManagerContract;
|
||||
use App\Contracts\SubdomainGenerator;
|
||||
use App\HttpServer\HttpServer;
|
||||
use App\Server\Connections\ConnectionManager;
|
||||
use App\Server\Http\Controllers\ControlMessageController;
|
||||
use App\Server\Http\Controllers\TunnelMessageController;
|
||||
use App\Server\SubdomainGenerator\RandomSubdomainGenerator;
|
||||
use Ratchet\Http\Router;
|
||||
use Ratchet\Server\IoServer;
|
||||
use Ratchet\WebSocket\WsServer;
|
||||
use React\Socket\Server;
|
||||
use React\EventLoop\LoopInterface;
|
||||
use React\EventLoop\Factory as LoopFactory;
|
||||
use Symfony\Component\Routing\Matcher\UrlMatcher;
|
||||
use Symfony\Component\Routing\RequestContext;
|
||||
use Symfony\Component\Routing\Route;
|
||||
use Symfony\Component\Routing\RouteCollection;
|
||||
|
||||
class Factory
|
||||
{
|
||||
@@ -54,15 +67,65 @@ class Factory
|
||||
return $this;
|
||||
}
|
||||
|
||||
protected function getRoutes(): RouteCollection
|
||||
{
|
||||
$routes = new RouteCollection();
|
||||
|
||||
$routes->add('control',
|
||||
new Route('/__expose_control__', [
|
||||
'_controller' => new WsServer(app(ControlMessageController::class))
|
||||
], [], [], null, [], []
|
||||
)
|
||||
);
|
||||
|
||||
$routes->add('tunnel',
|
||||
new Route('/{__catchall__}', [
|
||||
'_controller' => app(TunnelMessageController::class),
|
||||
], [
|
||||
'__catchall__' => '.*'
|
||||
]));
|
||||
|
||||
return $routes;
|
||||
}
|
||||
|
||||
protected function bindConfiguration()
|
||||
{
|
||||
app()->singleton(Configuration::class, function ($app) {
|
||||
return new Configuration($this->hostname, $this->port);
|
||||
});
|
||||
}
|
||||
|
||||
protected function bindSubdomainGenerator()
|
||||
{
|
||||
app()->singleton(SubdomainGenerator::class, function ($app) {
|
||||
return $app->make(RandomSubdomainGenerator::class);
|
||||
});
|
||||
}
|
||||
|
||||
protected function bindConnectionManager()
|
||||
{
|
||||
app()->singleton(ConnectionManagerContract::class, function ($app) {
|
||||
return $app->make(ConnectionManager::class);
|
||||
});
|
||||
}
|
||||
|
||||
public function createServer()
|
||||
{
|
||||
$socket = new Server("{$this->host}:{$this->port}", $this->loop);
|
||||
|
||||
$connectionManager = new ConnectionManager($this->hostname, $this->port);
|
||||
$this->bindConfiguration();
|
||||
|
||||
$app = new Expose($connectionManager);
|
||||
$this->bindSubdomainGenerator();
|
||||
|
||||
return new IoServer($app, $socket, $this->loop);
|
||||
$this->bindConnectionManager();
|
||||
|
||||
$urlMatcher = new UrlMatcher($this->getRoutes(), new RequestContext);
|
||||
|
||||
$router = new Router($urlMatcher);
|
||||
|
||||
$http = new HttpServer($router);
|
||||
|
||||
return new IoServer($http, $socket, $this->loop);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
98
app/Server/Http/Controllers/ControlMessageController.php
Normal file
98
app/Server/Http/Controllers/ControlMessageController.php
Normal file
@@ -0,0 +1,98 @@
|
||||
<?php
|
||||
|
||||
namespace App\Server\Http\Controllers;
|
||||
|
||||
use App\Contracts\ConnectionManager;
|
||||
use stdClass;
|
||||
use Ratchet\ConnectionInterface;
|
||||
use Ratchet\MessageComponentInterface;
|
||||
|
||||
class ControlMessageController implements MessageComponentInterface
|
||||
{
|
||||
|
||||
/** @var ConnectionManager */
|
||||
protected $connectionManager;
|
||||
|
||||
public function __construct(ConnectionManager $connectionManager)
|
||||
{
|
||||
$this->connectionManager = $connectionManager;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
function onClose(ConnectionInterface $connection)
|
||||
{
|
||||
if (isset($connection->request_id)) {
|
||||
$httpConnection = $this->connectionManager->getHttpConnectionForRequestId($connection->request_id);
|
||||
$httpConnection->close();
|
||||
}
|
||||
|
||||
$this->connectionManager->removeControlConnection($connection);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
function onMessage(ConnectionInterface $connection, $msg)
|
||||
{
|
||||
if (isset($connection->request_id)) {
|
||||
return $this->sendRequestToHttpConnection($connection->request_id, $msg);
|
||||
}
|
||||
|
||||
try {
|
||||
$payload = json_decode($msg);
|
||||
$eventName = $payload->event;
|
||||
|
||||
if (method_exists($this, $eventName)) {
|
||||
return call_user_func([$this, $eventName], $connection, $payload->data ?? new stdClass());
|
||||
}
|
||||
} catch (\Throwable $exception) {
|
||||
//
|
||||
}
|
||||
}
|
||||
|
||||
protected function sendRequestToHttpConnection(string $requestId, $request)
|
||||
{
|
||||
$httpConnection = $this->connectionManager->getHttpConnectionForRequestId($requestId);
|
||||
$httpConnection->send($request);
|
||||
}
|
||||
|
||||
protected function authenticate(ConnectionInterface $connection, $data)
|
||||
{
|
||||
$connectionInfo = $this->connectionManager->storeConnection($data->host, $data->subdomain, $connection);
|
||||
|
||||
$connection->send(json_encode([
|
||||
'event' => 'authenticated',
|
||||
'subdomain' => $connectionInfo->subdomain,
|
||||
'client_id' => $connectionInfo->client_id
|
||||
]));
|
||||
}
|
||||
|
||||
protected function registerProxy(ConnectionInterface $connection, $data)
|
||||
{
|
||||
$connection->request_id = $data->request_id;
|
||||
|
||||
$connectionInfo = $this->connectionManager->findControlConnectionForClientId($data->client_id);
|
||||
|
||||
$connectionInfo->emit('proxy_ready_' . $data->request_id, [
|
||||
$connection,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
function onOpen(ConnectionInterface $conn)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
function onError(ConnectionInterface $conn, \Exception $e)
|
||||
{
|
||||
//
|
||||
}
|
||||
}
|
||||
91
app/Server/Http/Controllers/TunnelMessageController.php
Normal file
91
app/Server/Http/Controllers/TunnelMessageController.php
Normal file
@@ -0,0 +1,91 @@
|
||||
<?php
|
||||
|
||||
namespace App\Server\Http\Controllers;
|
||||
|
||||
use App\Contracts\ConnectionManager;
|
||||
use App\HttpServer\Controllers\PostController;
|
||||
use App\Server\Configuration;
|
||||
use App\Server\Connections\ControlConnection;
|
||||
use GuzzleHttp\Psr7\Response;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Pipeline\Pipeline;
|
||||
use Nyholm\Psr7\Factory\Psr17Factory;
|
||||
use Ratchet\ConnectionInterface;
|
||||
use Ratchet\RFC6455\Messaging\Frame;
|
||||
use Symfony\Bridge\PsrHttpMessage\Factory\PsrHttpFactory;
|
||||
use function GuzzleHttp\Psr7\str;
|
||||
|
||||
class TunnelMessageController extends PostController
|
||||
{
|
||||
/** @var ConnectionManager */
|
||||
protected $connectionManager;
|
||||
|
||||
/** @var Configuration */
|
||||
private $configuration;
|
||||
|
||||
protected $keepConnectionOpen = true;
|
||||
|
||||
protected $middleware = [
|
||||
|
||||
];
|
||||
|
||||
public function __construct(ConnectionManager $connectionManager, Configuration $configuration)
|
||||
{
|
||||
$this->connectionManager = $connectionManager;
|
||||
$this->configuration = $configuration;
|
||||
}
|
||||
|
||||
public function handle(Request $request, ConnectionInterface $httpConnection)
|
||||
{
|
||||
$controlConnection = $this->connectionManager->findControlConnectionForSubdomain($this->detectSubdomain($request));
|
||||
|
||||
if (is_null($controlConnection)) {
|
||||
$httpConnection->send(str(new Response(404, [], 'Not found')));
|
||||
$httpConnection->close();
|
||||
return;
|
||||
}
|
||||
|
||||
$this->sendRequestToClient($request, $controlConnection, $httpConnection);
|
||||
}
|
||||
|
||||
protected function detectSubdomain(Request $request): ?string
|
||||
{
|
||||
$domainParts = explode('.', $request->getHost());
|
||||
|
||||
return trim($domainParts[0]);
|
||||
}
|
||||
|
||||
protected function sendRequestToClient(Request $request, ControlConnection $controlConnection, ConnectionInterface $httpConnection)
|
||||
{
|
||||
(new Pipeline(app()))
|
||||
->send($this->prepareRequest($request, $controlConnection))
|
||||
->through($this->middleware)
|
||||
->then(function ($request) use ($controlConnection, $httpConnection) {
|
||||
$requestId = $request->header('X-Expose-Request-ID');
|
||||
|
||||
$this->connectionManager->storeHttpConnection($httpConnection, $requestId);
|
||||
|
||||
$controlConnection->once('proxy_ready_' . $requestId, function (ConnectionInterface $proxy) use ($request) {
|
||||
// Convert the Laravel request into a PSR7 request
|
||||
$psr17Factory = new Psr17Factory();
|
||||
$psrHttpFactory = new PsrHttpFactory($psr17Factory, $psr17Factory, $psr17Factory, $psr17Factory);
|
||||
$request = $psrHttpFactory->createRequest($request);
|
||||
|
||||
$binaryMsg = new Frame(str($request), true, Frame::OP_BINARY);
|
||||
$proxy->send($binaryMsg);
|
||||
});
|
||||
|
||||
$controlConnection->registerProxy($requestId);
|
||||
});
|
||||
}
|
||||
|
||||
protected function prepareRequest(Request $request, ControlConnection $controlConnection): Request
|
||||
{
|
||||
$request->headers->set('Host', $controlConnection->host);
|
||||
$request->headers->set('X-Expose-Request-ID', uniqid());
|
||||
$request->headers->set('X-Exposed-By', config('app.name') . ' '. config('app.version'));
|
||||
$request->headers->set('X-Original-Host', "{$controlConnection->subdomain}.{$this->configuration->hostname()}:{$this->configuration->port()}");
|
||||
|
||||
return $request;
|
||||
}
|
||||
}
|
||||
@@ -1,32 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Server;
|
||||
|
||||
|
||||
use App\Server\Connections\IoConnection;
|
||||
|
||||
class IoServer extends \Ratchet\Server\IoServer
|
||||
{
|
||||
public function handleConnect($conn) {
|
||||
$conn->decor = new IoConnection($conn);
|
||||
$conn->decor->resourceId = (int)$conn->stream;
|
||||
|
||||
$uri = $conn->getRemoteAddress();
|
||||
$conn->decor->remoteAddress = trim(
|
||||
parse_url((strpos($uri, '://') === false ? 'tcp://' : '') . $uri, PHP_URL_HOST),
|
||||
'[]'
|
||||
);
|
||||
|
||||
$this->app->onOpen($conn->decor);
|
||||
|
||||
$conn->on('data', function ($data) use ($conn) {
|
||||
$this->handleData($data, $conn);
|
||||
});
|
||||
$conn->on('close', function () use ($conn) {
|
||||
$this->handleEnd($conn);
|
||||
});
|
||||
$conn->on('error', function (\Exception $e) use ($conn) {
|
||||
$this->handleError($e, $conn);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -1,66 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Server\Messages;
|
||||
|
||||
use App\Server\Connections\ConnectionManager;
|
||||
use Illuminate\Support\Str;
|
||||
use Ratchet\ConnectionInterface;
|
||||
use React\EventLoop\LoopInterface;
|
||||
use stdClass;
|
||||
|
||||
class ControlMessage implements Message
|
||||
{
|
||||
/** \stdClass */
|
||||
protected $payload;
|
||||
|
||||
/** @var \Ratchet\ConnectionInterface */
|
||||
protected $connection;
|
||||
|
||||
/** @var ConnectionManager */
|
||||
protected $connectionManager;
|
||||
|
||||
public function __construct($payload, ConnectionInterface $connection, ConnectionManager $connectionManager)
|
||||
{
|
||||
$this->payload = $payload;
|
||||
|
||||
$this->connection = $connection;
|
||||
|
||||
$this->connectionManager = $connectionManager;
|
||||
}
|
||||
|
||||
public function respond()
|
||||
{
|
||||
$eventName = $this->payload->event;
|
||||
|
||||
if (method_exists($this, $eventName)) {
|
||||
call_user_func([$this, $eventName], $this->connection, $this->payload->data ?? new stdClass());
|
||||
}
|
||||
}
|
||||
|
||||
protected function authenticate(ConnectionInterface $connection, $data)
|
||||
{
|
||||
$connectionInfo = $this->connectionManager->storeConnection($data->host, $data->subdomain, $connection);
|
||||
|
||||
$connection->send(json_encode([
|
||||
'event' => 'authenticated',
|
||||
'subdomain' => $connectionInfo->subdomain,
|
||||
'client_id' => $connectionInfo->client_id
|
||||
]));
|
||||
|
||||
$loop = app(LoopInterface::class);
|
||||
$timer = $loop->addPeriodicTimer(5, function () use ($connection) {
|
||||
$connection->send(json_encode([
|
||||
'event' => 'ping'
|
||||
]));
|
||||
});
|
||||
}
|
||||
|
||||
protected function registerProxy(ConnectionInterface $connection, $data)
|
||||
{
|
||||
$connectionInfo = $this->connectionManager->findConnectionForClientId($data->client_id);
|
||||
|
||||
$connectionInfo->socket->getConnection()->emit('proxy_ready_'.$data->request_id, [
|
||||
$connection,
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Server\Messages;
|
||||
|
||||
interface Message
|
||||
{
|
||||
public function respond();
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Server\Messages;
|
||||
|
||||
use App\Server\Connections\ConnectionManager;
|
||||
use Ratchet\ConnectionInterface;
|
||||
|
||||
class MessageFactory
|
||||
{
|
||||
public static function createForMessage(string $message, ConnectionInterface $connection, ConnectionManager $connectionManager)
|
||||
{
|
||||
$payload = json_decode($message);
|
||||
|
||||
return json_last_error() === JSON_ERROR_NONE
|
||||
? new ControlMessage($payload, $connection, $connectionManager)
|
||||
: new TunnelMessage($message, $connection, $connectionManager);
|
||||
}
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Server\Messages\RequestModifiers;
|
||||
|
||||
use App\Server\Connections\Connection;
|
||||
use App\Server\Connections\ConnectionManager;
|
||||
use GuzzleHttp\Psr7\Request;
|
||||
use Psr\Http\Message\RequestInterface;
|
||||
use function GuzzleHttp\Psr7\modify_request;
|
||||
|
||||
class ModifyHeaders implements RequestModifier
|
||||
{
|
||||
public function modify(RequestInterface $request, string $requestId, Connection $clientConnection, ConnectionManager $connectionManager): RequestInterface
|
||||
{
|
||||
return modify_request($request, [
|
||||
'set_headers' => [
|
||||
'Host' => $clientConnection->host,
|
||||
'X-Expose-Request-ID' => $requestId,
|
||||
'X-Exposed-By' => config('app.name') . ' '. config('app.version'),
|
||||
'X-Original-Host' => "{$clientConnection->subdomain}.{$connectionManager->host()}:{$connectionManager->port()}",
|
||||
]
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Server\Messages\RequestModifiers;
|
||||
|
||||
use App\Server\Connections\Connection;
|
||||
use App\Server\Connections\ConnectionManager;
|
||||
use Psr\Http\Message\RequestInterface;
|
||||
|
||||
interface RequestModifier
|
||||
{
|
||||
public function modify(RequestInterface $request, string $requestId, Connection $clientConnection, ConnectionManager $connectionManager): RequestInterface;
|
||||
}
|
||||
@@ -1,81 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Server\Messages;
|
||||
|
||||
use App\Server\Connections\Connection;
|
||||
use App\Server\Connections\ConnectionManager;
|
||||
use App\Server\Connections\HttpRequestConnection;
|
||||
use App\Server\Connections\IoConnection;
|
||||
use App\Server\Messages\RequestModifiers\ModifyHeaders;
|
||||
use BFunky\HttpParser\HttpRequestParser;
|
||||
use GuzzleHttp\Psr7\Request;
|
||||
use GuzzleHttp\Psr7\Response;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Str;
|
||||
use Ratchet\ConnectionInterface;
|
||||
use React\Stream\Util;
|
||||
use function GuzzleHttp\Psr7\parse_request;
|
||||
|
||||
class TunnelMessage implements Message
|
||||
{
|
||||
/** @var HttpRequestConnection */
|
||||
protected $connection;
|
||||
|
||||
/** @var ConnectionManager */
|
||||
private $connectionManager;
|
||||
|
||||
protected $requestModifiers = [
|
||||
ModifyHeaders::class,
|
||||
];
|
||||
|
||||
public function __construct(HttpRequestConnection $connection, ConnectionManager $connectionManager)
|
||||
{
|
||||
$this->connection = $connection;
|
||||
|
||||
$this->connectionManager = $connectionManager;
|
||||
}
|
||||
|
||||
public function respond()
|
||||
{
|
||||
if ($this->connection->hasBufferedAllData()) {
|
||||
$clientConnection = $this->connectionManager->findConnectionForSubdomain($this->detectSubdomain());
|
||||
|
||||
if (is_null($clientConnection)) {
|
||||
$this->connection->send(\GuzzleHttp\Psr7\str(new Response(404, [], 'Not found')));
|
||||
$this->connection->close();
|
||||
return;
|
||||
}
|
||||
|
||||
$this->copyDataToClient($clientConnection);
|
||||
}
|
||||
}
|
||||
|
||||
protected function detectSubdomain(): ?string
|
||||
{
|
||||
$host = $this->connection->getRequest()->getHeader('Host')[0];
|
||||
|
||||
$domainParts = explode('.', $host);
|
||||
|
||||
return trim($domainParts[0]);
|
||||
}
|
||||
|
||||
protected function passRequestThroughModifiers(string $requestId, Request $request, Connection $clientConnection): Request
|
||||
{
|
||||
foreach ($this->requestModifiers as $requestModifier) {
|
||||
$request = app($requestModifier)->modify($request, $requestId, $clientConnection, $this->connectionManager);
|
||||
}
|
||||
|
||||
return $request;
|
||||
}
|
||||
|
||||
protected function copyDataToClient(Connection $clientConnection)
|
||||
{
|
||||
$requestId = uniqid();
|
||||
|
||||
$request = $this->passRequestThroughModifiers($requestId, $this->connection->getRequest(), $clientConnection);
|
||||
|
||||
$clientConnection->pipeRequestThroughProxy($this->connection, $requestId, $request);
|
||||
|
||||
unset($this->connection->buffer);
|
||||
}
|
||||
}
|
||||
14
app/Server/SubdomainGenerator/RandomSubdomainGenerator.php
Normal file
14
app/Server/SubdomainGenerator/RandomSubdomainGenerator.php
Normal file
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
namespace App\Server\SubdomainGenerator;
|
||||
|
||||
use Illuminate\Support\Str;
|
||||
use App\Contracts\SubdomainGenerator;
|
||||
|
||||
class RandomSubdomainGenerator implements SubdomainGenerator
|
||||
{
|
||||
public function generateSubdomain(): string
|
||||
{
|
||||
return strtolower(Str::random(10));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user