mirror of
https://github.com/bitinflow/expose.git
synced 2026-03-13 21:45:55 +00:00
wip
This commit is contained in:
@@ -2,40 +2,51 @@
|
||||
|
||||
namespace App\Client;
|
||||
|
||||
use App\Client\Connections\ControlConnection;
|
||||
use Ratchet\Client\WebSocket;
|
||||
use React\EventLoop\LoopInterface;
|
||||
use React\Socket\ConnectionInterface;
|
||||
use React\Socket\Connector;
|
||||
use function Ratchet\Client\connect;
|
||||
|
||||
class Client
|
||||
{
|
||||
/** @var LoopInterface */
|
||||
protected $loop;
|
||||
protected $host;
|
||||
protected $port;
|
||||
|
||||
/** @var Configuration */
|
||||
protected $configuration;
|
||||
|
||||
public static $subdomains = [];
|
||||
|
||||
public function __construct(LoopInterface $loop, $host, $port)
|
||||
public function __construct(LoopInterface $loop, Configuration $configuration)
|
||||
{
|
||||
$this->loop = $loop;
|
||||
$this->host = $host;
|
||||
$this->port = $port;
|
||||
$this->configuration = $configuration;
|
||||
}
|
||||
|
||||
public function share($sharedUrl, array $subdomains = [])
|
||||
public function share(string $sharedUrl, array $subdomains = [])
|
||||
{
|
||||
foreach ($subdomains as $subdomain) {
|
||||
$connector = new Connector($this->loop);
|
||||
|
||||
$connector->connect("{$this->host}:{$this->port}")
|
||||
->then(function (ConnectionInterface $clientConnection) use ($sharedUrl, $subdomain) {
|
||||
$connection = Connection::create($clientConnection, new ProxyManager($this->host, $this->port, $this->loop));
|
||||
$connection->authenticate($sharedUrl, $subdomain);
|
||||
|
||||
$clientConnection->on('authenticated', function ($data) {
|
||||
static::$subdomains[] = "$data->subdomain.{$this->host}:{$this->port}";
|
||||
dump("Connected to http://$data->subdomain.{$this->host}:{$this->port}");
|
||||
});
|
||||
});
|
||||
$this->connectToServer($sharedUrl, $subdomain);
|
||||
}
|
||||
}
|
||||
|
||||
protected function connectToServer(string $sharedUrl, $subdomain)
|
||||
{
|
||||
connect("ws://{$this->configuration->host()}:{$this->configuration->port()}/__expose_control__", [], [
|
||||
'X-Expose-Control' => 'enabled',
|
||||
], $this->loop)
|
||||
->then(function (WebSocket $clientConnection) use ($sharedUrl, $subdomain) {
|
||||
$connection = ControlConnection::create($clientConnection);
|
||||
|
||||
$connection->authenticate($sharedUrl, $subdomain);
|
||||
|
||||
$connection->on('authenticated', function ($data) {
|
||||
dump("Connected to http://$data->subdomain.{$this->configuration->host()}:{$this->configuration->port()}");
|
||||
static::$subdomains[] = "$data->subdomain.{$this->configuration->host()}:{$this->configuration->port()}";
|
||||
});
|
||||
|
||||
}, function ($e) {
|
||||
echo "Could not connect: {$e->getMessage()}\n";
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
39
app/Client/Configuration.php
Normal file
39
app/Client/Configuration.php
Normal file
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
namespace App\Client;
|
||||
|
||||
class Configuration
|
||||
{
|
||||
/** @var string */
|
||||
protected $host;
|
||||
|
||||
/** @var int */
|
||||
protected $port;
|
||||
|
||||
/** @var string|null */
|
||||
protected $auth;
|
||||
|
||||
public function __construct(string $host, int $port, ?string $auth = null)
|
||||
{
|
||||
$this->host = $host;
|
||||
|
||||
$this->port = $port;
|
||||
|
||||
$this->auth = $auth;
|
||||
}
|
||||
|
||||
public function host(): string
|
||||
{
|
||||
return $this->host;
|
||||
}
|
||||
|
||||
public function auth(): ?string
|
||||
{
|
||||
return $this->auth;
|
||||
}
|
||||
|
||||
public function port(): int
|
||||
{
|
||||
return $this->port;
|
||||
}
|
||||
}
|
||||
@@ -1,81 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Client;
|
||||
|
||||
use Throwable;
|
||||
use React\Socket\ConnectionInterface;
|
||||
|
||||
class Connection
|
||||
{
|
||||
/** @var ConnectionInterface */
|
||||
protected $socket;
|
||||
|
||||
/** @var ProxyManager */
|
||||
protected $proxyManager;
|
||||
|
||||
public static function create(ConnectionInterface $socketConnection, ProxyManager $proxyManager)
|
||||
{
|
||||
return new static($socketConnection, $proxyManager);
|
||||
}
|
||||
|
||||
public function __construct(ConnectionInterface $socketConnection, ProxyManager $proxyManager)
|
||||
{
|
||||
$this->socket = $socketConnection;
|
||||
$this->proxyManager = $proxyManager;
|
||||
|
||||
$this->socket->on('data', function ($data) {
|
||||
$jsonStrings = explode("||", $data);
|
||||
|
||||
$decodedEntries = [];
|
||||
|
||||
foreach ($jsonStrings as $jsonString) {
|
||||
try {
|
||||
$decodedJsonObject = json_decode($jsonString);
|
||||
if (is_object($decodedJsonObject)) {
|
||||
$decodedEntries[] = $decodedJsonObject;
|
||||
}
|
||||
} catch (Throwable $e) {
|
||||
// Ignore payload
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($decodedEntries as $decodedEntry) {
|
||||
if (method_exists($this, $decodedEntry->event ?? '')) {
|
||||
$this->socket->emit($decodedEntry->event, [$decodedEntry]);
|
||||
|
||||
call_user_func([$this, $decodedEntry->event], $decodedEntry);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public function authenticated($data)
|
||||
{
|
||||
$this->socket->_id = $data->client_id;
|
||||
|
||||
$this->createProxy($data);
|
||||
}
|
||||
|
||||
public function createProxy($data)
|
||||
{
|
||||
$this->proxyManager->createProxy($this->socket, $data);
|
||||
}
|
||||
|
||||
public function authenticate(string $sharedHost, string $subdomain)
|
||||
{
|
||||
$this->socket->write(json_encode([
|
||||
'event' => 'authenticate',
|
||||
'data' => [
|
||||
'host' => $sharedHost,
|
||||
'subdomain' => empty($subdomain) ? null : $subdomain,
|
||||
],
|
||||
]));
|
||||
}
|
||||
|
||||
public function ping()
|
||||
{
|
||||
$this->socket->write(json_encode([
|
||||
'event' => 'pong',
|
||||
]));
|
||||
}
|
||||
}
|
||||
72
app/Client/Connections/ControlConnection.php
Normal file
72
app/Client/Connections/ControlConnection.php
Normal file
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
|
||||
namespace App\Client\Connections;
|
||||
|
||||
use App\Client\ProxyManager;
|
||||
use Ratchet\Client\WebSocket;
|
||||
use Ratchet\ConnectionInterface;
|
||||
use Evenement\EventEmitterTrait;
|
||||
use Ratchet\RFC6455\Messaging\Message;
|
||||
|
||||
class ControlConnection
|
||||
{
|
||||
use EventEmitterTrait;
|
||||
|
||||
/** @var ConnectionInterface */
|
||||
protected $socket;
|
||||
|
||||
/** @var ProxyManager */
|
||||
protected $proxyManager;
|
||||
|
||||
/** @var string */
|
||||
protected $clientId;
|
||||
|
||||
public static function create(WebSocket $socketConnection)
|
||||
{
|
||||
return new static($socketConnection, app(ProxyManager::class));
|
||||
}
|
||||
|
||||
public function __construct(WebSocket $socketConnection, ProxyManager $proxyManager)
|
||||
{
|
||||
$this->socket = $socketConnection;
|
||||
$this->proxyManager = $proxyManager;
|
||||
|
||||
$this->socket->on('message', function (Message $message) {
|
||||
$decodedEntry = json_decode($message);
|
||||
|
||||
if (method_exists($this, $decodedEntry->event ?? '')) {
|
||||
$this->emit($decodedEntry->event, [$decodedEntry]);
|
||||
|
||||
call_user_func([$this, $decodedEntry->event], $decodedEntry);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public function authenticated($data)
|
||||
{
|
||||
$this->clientId = $data->client_id;
|
||||
}
|
||||
|
||||
public function createProxy($data)
|
||||
{
|
||||
$this->proxyManager->createProxy($this->clientId, $data);
|
||||
}
|
||||
|
||||
public function authenticate(string $sharedHost, string $subdomain)
|
||||
{
|
||||
$this->socket->send(json_encode([
|
||||
'event' => 'authenticate',
|
||||
'data' => [
|
||||
'host' => $sharedHost,
|
||||
'subdomain' => empty($subdomain) ? null : $subdomain,
|
||||
],
|
||||
]));
|
||||
}
|
||||
|
||||
public function ping()
|
||||
{
|
||||
$this->socket->send(json_encode([
|
||||
'event' => 'pong',
|
||||
]));
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Client;
|
||||
|
||||
use App\Client\Http\HttpClient;
|
||||
use App\HttpServer\App;
|
||||
use App\HttpServer\Controllers\AttachDataToLogController;
|
||||
use App\HttpServer\Controllers\ClearLogsController;
|
||||
@@ -23,6 +24,9 @@ class Factory
|
||||
/** @var int */
|
||||
protected $port = 8080;
|
||||
|
||||
/** @var string */
|
||||
protected $auth = '';
|
||||
|
||||
/** @var \React\EventLoop\LoopInterface */
|
||||
protected $loop;
|
||||
|
||||
@@ -48,6 +52,13 @@ class Factory
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setAuth(string $auth)
|
||||
{
|
||||
$this->auth = $auth;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setLoop(LoopInterface $loop)
|
||||
{
|
||||
$this->loop = $loop;
|
||||
@@ -55,22 +66,39 @@ class Factory
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function createClient($sharedUrl, $subdomain = null)
|
||||
protected function bindConfiguration()
|
||||
{
|
||||
$client = new Client($this->loop, $this->host, $this->port);
|
||||
$client->share($sharedUrl, $subdomain);
|
||||
app()->singleton(Configuration::class, function ($app) {
|
||||
return new Configuration($this->host, $this->port, $this->auth);
|
||||
});
|
||||
}
|
||||
|
||||
protected function bindProxyManager()
|
||||
{
|
||||
app()->singleton(ProxyManager::class, function ($app) {
|
||||
return new ProxyManager($app->make(Configuration::class), $this->loop, $app->make(HttpClient::class));
|
||||
});
|
||||
}
|
||||
|
||||
public function createClient($sharedUrl, $subdomain = null, $auth = null)
|
||||
{
|
||||
$this->bindConfiguration();
|
||||
|
||||
$this->bindProxyManager();
|
||||
|
||||
app(Client::class)->share($sharedUrl, $subdomain);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
protected function addRoutes()
|
||||
{
|
||||
$dashboardRoute = new Route('/', ['_controller' => new DashboardController()], [], [], null, [], ['GET']);
|
||||
$logRoute = new Route('/logs', ['_controller' => new LogController()], [], [], null, [], ['GET']);
|
||||
$storeLogRoute = new Route('/logs', ['_controller' => new StoreLogController()], [], [], null, [], ['POST']);
|
||||
$replayLogRoute = new Route('/replay/{log}', ['_controller' => new ReplayLogController()], [], [], null, [], ['GET']);
|
||||
$attachLogDataRoute = new Route('/logs/{request_id}/data', ['_controller' => new AttachDataToLogController()], [], [], null, [], ['POST']);
|
||||
$clearLogsRoute = new Route('/logs/clear', ['_controller' => new ClearLogsController()], [], [], null, [], ['GET']);
|
||||
$dashboardRoute = new Route('/', ['_controller' => app(DashboardController::class)], [], [], null, [], ['GET']);
|
||||
$logRoute = new Route('/logs', ['_controller' => app(LogController::class)], [], [], null, [], ['GET']);
|
||||
$storeLogRoute = new Route('/logs', ['_controller' => app(StoreLogController::class)], [], [], null, [], ['POST']);
|
||||
$replayLogRoute = new Route('/replay/{log}', ['_controller' => app(ReplayLogController::class)], [], [], null, [], ['GET']);
|
||||
$attachLogDataRoute = new Route('/logs/{request_id}/data', ['_controller' => app(AttachDataToLogController::class)], [], [], null, [], ['POST']);
|
||||
$clearLogsRoute = new Route('/logs/clear', ['_controller' => app(ClearLogsController::class)], [], [], null, [], ['GET']);
|
||||
|
||||
$this->app->route('/socket', new WsServer(new Socket()), ['*']);
|
||||
|
||||
|
||||
147
app/Client/Http/HttpClient.php
Normal file
147
app/Client/Http/HttpClient.php
Normal file
@@ -0,0 +1,147 @@
|
||||
<?php
|
||||
|
||||
namespace App\Client\Http;
|
||||
|
||||
use App\Client\Http\Modifiers\CheckBasicAuthentication;
|
||||
use App\Logger\RequestLogger;
|
||||
use Clue\React\Buzz\Browser;
|
||||
use Illuminate\Pipeline\Pipeline;
|
||||
use Illuminate\Support\Arr;
|
||||
use Laminas\Http\Request;
|
||||
use Psr\Http\Message\RequestInterface;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Ratchet\Client\WebSocket;
|
||||
use Ratchet\RFC6455\Messaging\Frame;
|
||||
use React\EventLoop\LoopInterface;
|
||||
use React\Socket\Connector;
|
||||
use function GuzzleHttp\Psr7\parse_request;
|
||||
use function GuzzleHttp\Psr7\str;
|
||||
|
||||
class HttpClient
|
||||
{
|
||||
/** @var LoopInterface */
|
||||
protected $loop;
|
||||
|
||||
/** @var RequestLogger */
|
||||
protected $logger;
|
||||
|
||||
/** @var Request */
|
||||
protected $request;
|
||||
|
||||
/** @var array */
|
||||
protected $modifiers = [
|
||||
CheckBasicAuthentication::class,
|
||||
];
|
||||
|
||||
public function __construct(LoopInterface $loop, RequestLogger $logger)
|
||||
{
|
||||
$this->loop = $loop;
|
||||
$this->logger = $logger;
|
||||
}
|
||||
|
||||
public function performRequest(string $requestData, WebSocket $proxyConnection = null, string $requestId = null)
|
||||
{
|
||||
$this->request = $this->parseRequest($requestData);
|
||||
|
||||
$this->logger->logRequest($requestData, $this->request);
|
||||
|
||||
$request = $this->passRequestThroughModifiers(parse_request($requestData), $proxyConnection);
|
||||
|
||||
dump($this->request->getMethod() . ' ' . $this->request->getUri()->getPath());
|
||||
|
||||
/**
|
||||
* Modifiers can already send a response to the proxy connection,
|
||||
* which would result in the request being null.
|
||||
*/
|
||||
if (is_null($request)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->sendRequestToApplication($request, $proxyConnection);
|
||||
}
|
||||
|
||||
protected function passRequestThroughModifiers(RequestInterface $request, ?WebSocket $proxyConnection = null): ?RequestInterface
|
||||
{
|
||||
foreach ($this->modifiers as $modifier) {
|
||||
$request = app($modifier)->handle($request, $proxyConnection);
|
||||
|
||||
if (is_null($request)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return $request;
|
||||
}
|
||||
|
||||
protected function createConnector(): Connector
|
||||
{
|
||||
return new Connector($this->loop, array(
|
||||
'dns' => '127.0.0.1',
|
||||
'tls' => array(
|
||||
'verify_peer' => false,
|
||||
'verify_peer_name' => false
|
||||
)
|
||||
));
|
||||
}
|
||||
|
||||
protected function sendRequestToApplication(RequestInterface $request, $proxyConnection = null)
|
||||
{
|
||||
(new Browser($this->loop, $this->createConnector()))
|
||||
->withOptions([
|
||||
'followRedirects' => false,
|
||||
'obeySuccessCode' => false,
|
||||
'streaming' => true,
|
||||
])
|
||||
->send($request)
|
||||
->then(function (ResponseInterface $response) use ($proxyConnection) {
|
||||
if (! isset($response->buffer)) {
|
||||
$response->buffer = str($response);
|
||||
}
|
||||
|
||||
$this->sendChunkToServer($response->buffer, $proxyConnection);
|
||||
|
||||
/* @var $body \React\Stream\ReadableStreamInterface */
|
||||
$body = $response->getBody();
|
||||
|
||||
$this->logResponse($response->buffer);
|
||||
|
||||
$body->on('data', function ($chunk) use ($proxyConnection, $response) {
|
||||
$response->buffer .= $chunk;
|
||||
|
||||
$this->sendChunkToServer($chunk, $proxyConnection);
|
||||
|
||||
if ($chunk === "") {
|
||||
$this->logResponse($response->buffer);
|
||||
|
||||
optional($proxyConnection)->close();
|
||||
}
|
||||
});
|
||||
|
||||
$body->on('close', function () use ($proxyConnection, $response) {
|
||||
$this->logResponse($response->buffer);
|
||||
|
||||
optional($proxyConnection)->close();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
protected function sendChunkToServer(string $chunk, ?WebSocket $proxyConnection = null)
|
||||
{
|
||||
if (is_null($proxyConnection)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$binaryMsg = new Frame($chunk, true, Frame::OP_BINARY);
|
||||
$proxyConnection->send($binaryMsg);
|
||||
}
|
||||
|
||||
protected function logResponse(string $rawResponse)
|
||||
{
|
||||
$this->logger->logResponse($this->request, $rawResponse);
|
||||
}
|
||||
|
||||
protected function parseRequest($data): Request
|
||||
{
|
||||
return Request::fromString($data);
|
||||
}
|
||||
}
|
||||
98
app/Client/Http/Modifiers/CheckBasicAuthentication.php
Normal file
98
app/Client/Http/Modifiers/CheckBasicAuthentication.php
Normal file
@@ -0,0 +1,98 @@
|
||||
<?php
|
||||
|
||||
namespace App\Client\Http\Modifiers;
|
||||
|
||||
use App\Client\Configuration;
|
||||
use Illuminate\Support\Arr;
|
||||
use Psr\Http\Message\RequestInterface;
|
||||
use Ratchet\Client\WebSocket;
|
||||
use function GuzzleHttp\Psr7\str;
|
||||
|
||||
class CheckBasicAuthentication
|
||||
{
|
||||
/** @var Configuration */
|
||||
protected $configuration;
|
||||
|
||||
public function __construct(Configuration $configuration)
|
||||
{
|
||||
$this->configuration = $configuration;
|
||||
}
|
||||
|
||||
public function handle(RequestInterface $request, WebSocket $proxyConnection): ?RequestInterface
|
||||
{
|
||||
if (! $this->requiresAuthentication() || is_null($proxyConnection)) {
|
||||
return $request;
|
||||
}
|
||||
|
||||
$username = $this->getAuthorizationUsername($request);
|
||||
|
||||
if (is_null($username)) {
|
||||
$proxyConnection->send(
|
||||
str(new \GuzzleHttp\Psr7\Response(401, [
|
||||
'WWW-Authenticate' => 'Basic realm=Expose'
|
||||
], 'Unauthorized'))
|
||||
);
|
||||
$proxyConnection->close();
|
||||
return null;
|
||||
}
|
||||
|
||||
return $request;
|
||||
}
|
||||
|
||||
protected function getAuthorizationUsername(RequestInterface $request): ?string
|
||||
{
|
||||
$authorization = $this->parseAuthorizationHeader(Arr::get($request->getHeaders(), 'authorization.0', ''));
|
||||
$credentials = $this->getCredentials();
|
||||
|
||||
if (empty($authorization)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!array_key_exists($authorization['username'], $credentials)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if ($credentials[$authorization['username']] !== $authorization['password']) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $authorization['username'];
|
||||
}
|
||||
|
||||
protected function parseAuthorizationHeader(string $header)
|
||||
{
|
||||
if (strpos($header, 'Basic') !== 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$header = base64_decode(substr($header, 6));
|
||||
|
||||
if ($header === false) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$header = explode(':', $header, 2);
|
||||
|
||||
return [
|
||||
'username' => $header[0],
|
||||
'password' => isset($header[1]) ? $header[1] : null,
|
||||
];
|
||||
}
|
||||
|
||||
protected function requiresAuthentication(): bool
|
||||
{
|
||||
return !empty($this->getCredentials());
|
||||
}
|
||||
|
||||
protected function getCredentials()
|
||||
{
|
||||
try {
|
||||
$credentials = explode(':', $this->configuration->auth());
|
||||
return [
|
||||
$credentials[0] => $credentials[1],
|
||||
];
|
||||
} catch (\Exception $e) {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,69 +2,52 @@
|
||||
|
||||
namespace App\Client;
|
||||
|
||||
use App\Logger\RequestLogger;
|
||||
use BFunky\HttpParser\HttpRequestParser;
|
||||
use BFunky\HttpParser\HttpResponseParser;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Str;
|
||||
use React\Socket\ConnectionInterface;
|
||||
use React\Socket\Connector;
|
||||
use React\Stream\ThroughStream;
|
||||
use React\Stream\Util;
|
||||
use React\Stream\WritableResourceStream;
|
||||
use GuzzleHttp\Psr7 as gPsr;
|
||||
use function GuzzleHttp\Psr7\parse_request;
|
||||
use App\Client\Http\HttpClient;
|
||||
use Ratchet\Client\WebSocket;
|
||||
use Ratchet\ConnectionInterface;
|
||||
use React\EventLoop\LoopInterface;
|
||||
use function Ratchet\Client\connect;
|
||||
|
||||
class ProxyManager
|
||||
{
|
||||
private $host;
|
||||
private $port;
|
||||
private $loop;
|
||||
/** @var Configuration */
|
||||
protected $configuration;
|
||||
|
||||
public function __construct($host, $port, $loop)
|
||||
/** @var LoopInterface */
|
||||
protected $loop;
|
||||
|
||||
/** @var HttpClient */
|
||||
protected $httpClient;
|
||||
|
||||
public function __construct(Configuration $configuration, LoopInterface $loop, HttpClient $httpClient)
|
||||
{
|
||||
$this->host = $host;
|
||||
$this->port = $port;
|
||||
$this->configuration = $configuration;
|
||||
$this->loop = $loop;
|
||||
$this->httpClient = $httpClient;
|
||||
}
|
||||
|
||||
public function createProxy(ConnectionInterface $clientConnection, $connectionData)
|
||||
public function createProxy(string $clientId, $connectionData)
|
||||
{
|
||||
$connector = new Connector($this->loop);
|
||||
$connector->connect("{$this->host}:{$this->port}")->then(function (ConnectionInterface $proxyConnection) use ($clientConnection, $connector, $connectionData) {
|
||||
$proxyConnection->write(json_encode([
|
||||
'event' => 'registerProxy',
|
||||
'data' => [
|
||||
'request_id' => $connectionData->request_id ?? null,
|
||||
'client_id' => $clientConnection->_id,
|
||||
],
|
||||
]));
|
||||
connect("ws://{$this->configuration->host()}:{$this->configuration->port()}/__expose_control__", [], [
|
||||
'X-Expose-Control' => 'enabled',
|
||||
], $this->loop)
|
||||
->then(function (WebSocket $proxyConnection) use ($clientId, $connectionData) {
|
||||
$proxyConnection->on('message', function ($message) use ($proxyConnection, $connectionData) {
|
||||
$this->performRequest($proxyConnection, $connectionData->request_id, (string)$message);
|
||||
});
|
||||
|
||||
$proxyConnection->on('data', function ($data) use (&$proxyData, $proxyConnection, $connector) {
|
||||
if (!isset($proxyConnection->buffer)) {
|
||||
$proxyConnection->buffer = '';
|
||||
}
|
||||
|
||||
$proxyConnection->buffer .= $data;
|
||||
|
||||
if ($this->hasBufferedAllData($proxyConnection)) {
|
||||
$tunnel = app(TunnelConnection::class);
|
||||
|
||||
$tunnel->performRequest($proxyConnection->buffer, $proxyConnection);
|
||||
}
|
||||
$proxyConnection->send(json_encode([
|
||||
'event' => 'registerProxy',
|
||||
'data' => [
|
||||
'request_id' => $connectionData->request_id ?? null,
|
||||
'client_id' => $clientId,
|
||||
],
|
||||
]));
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
protected function getContentLength($proxyConnection): ?int
|
||||
protected function performRequest(WebSocket $proxyConnection, $requestId, string $requestData)
|
||||
{
|
||||
$request = parse_request($proxyConnection->buffer);
|
||||
|
||||
return Arr::first($request->getHeader('Content-Length'));
|
||||
}
|
||||
|
||||
protected function hasBufferedAllData($proxyConnection)
|
||||
{
|
||||
return is_null($this->getContentLength($proxyConnection)) || strlen(Str::after($proxyConnection->buffer, "\r\n\r\n")) >= $this->getContentLength($proxyConnection);
|
||||
$this->httpClient->performRequest((string)$requestData, $proxyConnection, $requestId);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,166 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Client;
|
||||
|
||||
use App\Logger\RequestLogger;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Str;
|
||||
use Laminas\Http\Request;
|
||||
use Laminas\Http\Response;
|
||||
use React\EventLoop\LoopInterface;
|
||||
use React\Socket\ConnectionInterface;
|
||||
use React\Socket\Connector;
|
||||
use React\Stream\Util;
|
||||
use function GuzzleHttp\Psr7\str;
|
||||
|
||||
class TunnelConnection
|
||||
{
|
||||
/** @var LoopInterface */
|
||||
protected $loop;
|
||||
|
||||
/** @var RequestLogger */
|
||||
protected $logger;
|
||||
|
||||
/** @var Request */
|
||||
protected $request;
|
||||
|
||||
public function __construct(LoopInterface $loop, RequestLogger $logger)
|
||||
{
|
||||
$this->loop = $loop;
|
||||
$this->logger = $logger;
|
||||
}
|
||||
|
||||
protected function requiresAuthentication(): bool
|
||||
{
|
||||
return !empty($this->getCredentials());
|
||||
}
|
||||
|
||||
public function performRequest($requestData, ConnectionInterface $proxyConnection = null)
|
||||
{
|
||||
$this->request = $this->parseRequest($requestData);
|
||||
|
||||
$this->logger->logRequest($requestData, $this->request);
|
||||
|
||||
dump($this->request->getMethod() . ' ' . $this->request->getUri()->getPath());
|
||||
|
||||
if ($this->requiresAuthentication() && !is_null($proxyConnection)) {
|
||||
$username = $this->getAuthorizationUsername();
|
||||
if (is_null($username)) {
|
||||
$proxyConnection->write(
|
||||
str(new \GuzzleHttp\Psr7\Response(401, [
|
||||
'WWW-Authenticate' => 'Basic realm=Expose'
|
||||
], 'Unauthorized'))
|
||||
);
|
||||
$proxyConnection->end();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
(new Connector($this->loop))
|
||||
->connect("localhost:80")
|
||||
->then(function (ConnectionInterface $connection) use ($requestData, $proxyConnection) {
|
||||
$connection->on('data', function ($data) use (&$chunks, &$contentLength, $connection, $proxyConnection) {
|
||||
if (!isset($connection->httpBuffer)) {
|
||||
$connection->httpBuffer = '';
|
||||
}
|
||||
|
||||
$connection->httpBuffer .= $data;
|
||||
|
||||
$response = $this->parseResponse($connection->httpBuffer);
|
||||
|
||||
if (! is_null($response) && $this->hasBufferedAllData($connection)) {
|
||||
|
||||
$this->logger->logResponse($this->request, $connection->httpBuffer, $response);
|
||||
|
||||
if (! is_null($proxyConnection)) {
|
||||
$proxyConnection->write($connection->httpBuffer);
|
||||
}
|
||||
|
||||
unset($proxyConnection->buffer);
|
||||
|
||||
unset($connection->httpBuffer);
|
||||
}
|
||||
|
||||
});
|
||||
$connection->write($requestData);
|
||||
});
|
||||
}
|
||||
|
||||
protected function getContentLength($connection): ?int
|
||||
{
|
||||
$response = $this->parseResponse($connection->httpBuffer);
|
||||
|
||||
return Arr::get($response->getHeaders()->toArray(), 'Content-Length');
|
||||
}
|
||||
|
||||
protected function hasBufferedAllData($connection)
|
||||
{
|
||||
return is_null($this->getContentLength($connection)) || strlen(Str::after($connection->httpBuffer, "\r\n\r\n")) === $this->getContentLength($connection);
|
||||
}
|
||||
|
||||
protected function parseResponse(string $response)
|
||||
{
|
||||
try {
|
||||
return Response::fromString($response);
|
||||
} catch (\Throwable $e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
protected function parseRequest($data): Request
|
||||
{
|
||||
return Request::fromString($data);
|
||||
}
|
||||
|
||||
protected function getCredentials()
|
||||
{
|
||||
try {
|
||||
$credentials = explode(':', $GLOBALS['expose.auth']);
|
||||
return [
|
||||
$credentials[0] => $credentials[1],
|
||||
];
|
||||
} catch (\Exception $e) {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
protected function getAuthorizationUsername(): ?string
|
||||
{
|
||||
$authorization = $this->parseAuthorizationHeader(Arr::get($this->request->getHeaders()->toArray(), 'Authorization', ''));
|
||||
$credentials = $this->getCredentials();
|
||||
|
||||
if (empty($authorization)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!array_key_exists($authorization['username'], $credentials)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if ($credentials[$authorization['username']] !== $authorization['password']) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $authorization['username'];
|
||||
}
|
||||
|
||||
protected function parseAuthorizationHeader(string $header)
|
||||
{
|
||||
if (strpos($header, 'Basic') !== 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$header = base64_decode(substr($header, 6));
|
||||
|
||||
if ($header === false) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$header = explode(':', $header, 2);
|
||||
|
||||
return [
|
||||
'username' => $header[0],
|
||||
'password' => isset($header[1]) ? $header[1] : null,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -15,15 +15,11 @@ class ShareCommand extends Command
|
||||
|
||||
public function handle()
|
||||
{
|
||||
// TODO: Hacky workaround just to see if it works haha
|
||||
if ($this->option('auth')) {
|
||||
$GLOBALS['expose.auth'] = $this->option('auth');
|
||||
}
|
||||
|
||||
(new Factory())
|
||||
->setLoop(app(LoopInterface::class))
|
||||
// ->setHost('beyond.sh') // TODO: Read from (local/global) config file
|
||||
// ->setPort(8080) // TODO: Read from (local/global) config file
|
||||
->setAuth($this->option('auth'))
|
||||
->createClient($this->argument('host'), explode(',', $this->option('subdomain')))
|
||||
->createHttpServer()
|
||||
->run();
|
||||
|
||||
21
app/Contracts/ConnectionManager.php
Normal file
21
app/Contracts/ConnectionManager.php
Normal file
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
namespace App\Contracts;
|
||||
|
||||
use App\Server\Connections\ControlConnection;
|
||||
use Ratchet\ConnectionInterface;
|
||||
|
||||
interface ConnectionManager
|
||||
{
|
||||
public function storeConnection(string $host, ?string $subdomain, ConnectionInterface $connection): ControlConnection;
|
||||
|
||||
public function storeHttpConnection(ConnectionInterface $httpConnection, $requestId): ConnectionInterface;
|
||||
|
||||
public function getHttpConnectionForRequestId(string $requestId): ?ConnectionInterface;
|
||||
|
||||
public function removeControlConnection($connection);
|
||||
|
||||
public function findControlConnectionForSubdomain($subdomain): ?ControlConnection;
|
||||
|
||||
public function findControlConnectionForClientId(string $clientId): ?ControlConnection;
|
||||
}
|
||||
8
app/Contracts/SubdomainGenerator.php
Normal file
8
app/Contracts/SubdomainGenerator.php
Normal file
@@ -0,0 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace App\Contracts;
|
||||
|
||||
interface SubdomainGenerator
|
||||
{
|
||||
public function generateSubdomain(): string;
|
||||
}
|
||||
@@ -4,19 +4,26 @@ namespace App\HttpServer\Controllers;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use App\Logger\RequestLogger;
|
||||
use Ratchet\ConnectionInterface;
|
||||
|
||||
class AttachDataToLogController extends PostController
|
||||
{
|
||||
public function handle(Request $request)
|
||||
/** @var RequestLogger */
|
||||
protected $requestLogger;
|
||||
|
||||
public function __construct(RequestLogger $requestLogger)
|
||||
{
|
||||
/** @var RequestLogger $requestLogger */
|
||||
$requestLogger = app(RequestLogger::class);
|
||||
$loggedRequest = $requestLogger->findLoggedRequest($request->get('request_id', ''));
|
||||
$this->requestLogger = $requestLogger;
|
||||
}
|
||||
|
||||
public function handle(Request $request, ConnectionInterface $httpConnection)
|
||||
{
|
||||
$loggedRequest = $this->requestLogger->findLoggedRequest($request->get('request_id', ''));
|
||||
|
||||
if (! is_null($loggedRequest)) {
|
||||
$loggedRequest->setAdditionalData((array)$request->get('data', []));
|
||||
|
||||
$requestLogger->pushLogs();
|
||||
$this->requestLogger->pushLogs();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
namespace App\HttpServer\Controllers;
|
||||
|
||||
use App\Client\TunnelConnection;
|
||||
use App\Client\Http\HttpClient;
|
||||
use App\HttpServer\QueryParameters;
|
||||
use App\Logger\RequestLogger;
|
||||
use GuzzleHttp\Psr7\Response;
|
||||
@@ -12,11 +12,17 @@ use Psr\Http\Message\RequestInterface;
|
||||
|
||||
class ClearLogsController extends Controller
|
||||
{
|
||||
/** @var RequestLogger */
|
||||
protected $requestLogger;
|
||||
|
||||
public function __construct(RequestLogger $requestLogger)
|
||||
{
|
||||
$this->requestLogger = $requestLogger;
|
||||
}
|
||||
|
||||
public function onOpen(ConnectionInterface $connection, RequestInterface $request = null)
|
||||
{
|
||||
/** @var RequestLogger $logger */
|
||||
$logger = app(RequestLogger::class);
|
||||
$logger->clear();
|
||||
$this->requestLogger->clear();
|
||||
|
||||
$connection->send(
|
||||
str(new Response(
|
||||
|
||||
@@ -10,6 +10,9 @@ abstract class Controller implements HttpServerInterface
|
||||
{
|
||||
public function onClose(ConnectionInterface $connection)
|
||||
{
|
||||
unset($connection->requestBuffer);
|
||||
unset($connection->contentLength);
|
||||
unset($connection->request);
|
||||
}
|
||||
|
||||
public function onError(ConnectionInterface $connection, Exception $e)
|
||||
|
||||
@@ -10,16 +10,21 @@ use Psr\Http\Message\RequestInterface;
|
||||
|
||||
class LogController extends Controller
|
||||
{
|
||||
/** @var RequestLogger */
|
||||
protected $requestLogger;
|
||||
|
||||
public function __construct(RequestLogger $requestLogger)
|
||||
{
|
||||
$this->requestLogger = $requestLogger;
|
||||
}
|
||||
|
||||
public function onOpen(ConnectionInterface $connection, RequestInterface $request = null)
|
||||
{
|
||||
/** @var RequestLogger $logger */
|
||||
$logger = app(RequestLogger::class);
|
||||
|
||||
$connection->send(
|
||||
str(new Response(
|
||||
200,
|
||||
['Content-Type' => 'application/json'],
|
||||
json_encode($logger->getData(), JSON_INVALID_UTF8_IGNORE)
|
||||
json_encode($this->requestLogger->getData(), JSON_INVALID_UTF8_IGNORE)
|
||||
))
|
||||
);
|
||||
|
||||
|
||||
@@ -9,11 +9,15 @@ use Illuminate\Support\Collection;
|
||||
use Psr\Http\Message\RequestInterface;
|
||||
use Ratchet\ConnectionInterface;
|
||||
use Symfony\Bridge\PsrHttpMessage\Factory\HttpFoundationFactory;
|
||||
use function GuzzleHttp\Psr7\parse_request;
|
||||
|
||||
abstract class PostController extends Controller
|
||||
{
|
||||
protected $keepConnectionOpen = false;
|
||||
|
||||
public function onOpen(ConnectionInterface $connection, RequestInterface $request = null)
|
||||
{
|
||||
dump(memory_get_usage(true));
|
||||
$connection->contentLength = $this->findContentLength($request->getHeaders());
|
||||
|
||||
$connection->requestBuffer = (string) $request->getBody();
|
||||
@@ -25,7 +29,14 @@ abstract class PostController extends Controller
|
||||
|
||||
public function onMessage(ConnectionInterface $from, $msg)
|
||||
{
|
||||
$from->requestBuffer .= $msg;
|
||||
if (! isset($from->requestBuffer)) {
|
||||
$request = parse_request($msg);
|
||||
$from->contentLength = $this->findContentLength($request->getHeaders());
|
||||
$from->request = $request;
|
||||
$from->requestBuffer = (string) $request->getBody();
|
||||
} else {
|
||||
$from->requestBuffer .= $msg;
|
||||
}
|
||||
|
||||
$this->checkContentLength($from);
|
||||
}
|
||||
@@ -42,9 +53,11 @@ abstract class PostController extends Controller
|
||||
if (strlen($connection->requestBuffer) === $connection->contentLength) {
|
||||
$laravelRequest = $this->createLaravelRequest($connection);
|
||||
|
||||
$this->handle($laravelRequest);
|
||||
$this->handle($laravelRequest, $connection);
|
||||
|
||||
$connection->close();
|
||||
if (! $this->keepConnectionOpen) {
|
||||
$connection->close();
|
||||
}
|
||||
|
||||
unset($connection->requestBuffer);
|
||||
unset($connection->contentLength);
|
||||
@@ -52,7 +65,7 @@ abstract class PostController extends Controller
|
||||
}
|
||||
}
|
||||
|
||||
abstract public function handle(Request $request);
|
||||
abstract public function handle(Request $request, ConnectionInterface $httpConnection);
|
||||
|
||||
protected function createLaravelRequest(ConnectionInterface $connection): Request
|
||||
{
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
namespace App\HttpServer\Controllers;
|
||||
|
||||
use App\Client\TunnelConnection;
|
||||
use App\Client\Http\HttpClient;
|
||||
use App\HttpServer\QueryParameters;
|
||||
use App\Logger\RequestLogger;
|
||||
use GuzzleHttp\Psr7\Response;
|
||||
@@ -12,15 +12,25 @@ use Psr\Http\Message\RequestInterface;
|
||||
|
||||
class ReplayLogController extends Controller
|
||||
{
|
||||
/** @var RequestLogger */
|
||||
protected $requestLogger;
|
||||
|
||||
/** @var HttpClient */
|
||||
protected $httpClient;
|
||||
|
||||
public function __construct(RequestLogger $requestLogger, HttpClient $httpClient)
|
||||
{
|
||||
$this->requestLogger = $requestLogger;
|
||||
$this->httpClient = $httpClient;
|
||||
}
|
||||
|
||||
public function onOpen(ConnectionInterface $connection, RequestInterface $request = null)
|
||||
{
|
||||
/** @var RequestLogger $logger */
|
||||
$logger = app(RequestLogger::class);
|
||||
$requestData = $logger->findLoggedRequest(QueryParameters::create($request)->get('log'))->getRequestData();
|
||||
$requestData = $this->requestLogger->findLoggedRequest(QueryParameters::create($request)->get('log'))->getRequestData();
|
||||
|
||||
/** @var TunnelConnection $tunnel */
|
||||
$tunnel = app(TunnelConnection::class);
|
||||
$tunnel->performRequest($requestData);
|
||||
/** @var HttpClient $tunnel */
|
||||
$this->httpClient->performRequest($requestData);
|
||||
|
||||
$connection->send(
|
||||
str(new Response(
|
||||
|
||||
@@ -10,6 +10,6 @@ class HttpServer extends \Ratchet\Http\HttpServer
|
||||
{
|
||||
parent::__construct($component);
|
||||
|
||||
$this->_reqParser->maxSize = 15242880;
|
||||
$this->_reqParser->maxSize = 15242880000;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -66,18 +66,23 @@ class LoggedRequest implements \JsonSerializable
|
||||
'body' => $this->isBinary($this->rawRequest) ? 'BINARY' : $this->parsedRequest->getContent(),
|
||||
'query' => $this->parsedRequest->getQuery()->toArray(),
|
||||
'post' => $this->getPost(),
|
||||
'curl' => (new CurlFormatter())->format(parse_request($this->rawRequest)),
|
||||
'curl' => '', //(new CurlFormatter())->format(parse_request($this->rawRequest)),
|
||||
'additional_data' => $this->additionalData,
|
||||
],
|
||||
];
|
||||
|
||||
if ($this->parsedResponse) {
|
||||
try {
|
||||
$body = $this->parsedResponse->getBody();
|
||||
} catch (\Exception $e) {
|
||||
$body = '';
|
||||
}
|
||||
$data['response'] = [
|
||||
'raw' => $this->shouldReturnBody() ? $this->rawResponse : 'BINARY',
|
||||
'status' => $this->parsedResponse->getStatusCode(),
|
||||
'headers' => $this->parsedResponse->getHeaders()->toArray(),
|
||||
'reason' => $this->parsedResponse->getReasonPhrase(),
|
||||
'body' => $this->shouldReturnBody() ? $this->parsedResponse->getBody() : 'BINARY',
|
||||
'body' => $this->shouldReturnBody() ? $body : 'BINARY',
|
||||
];
|
||||
}
|
||||
|
||||
@@ -112,7 +117,9 @@ class LoggedRequest implements \JsonSerializable
|
||||
|
||||
$this->rawResponse = $rawResponse;
|
||||
|
||||
$this->stopTime = now();
|
||||
if (is_null($this->stopTime)) {
|
||||
$this->stopTime = now();
|
||||
}
|
||||
}
|
||||
|
||||
public function id()
|
||||
|
||||
@@ -34,13 +34,13 @@ class RequestLogger
|
||||
$this->pushLogs();
|
||||
}
|
||||
|
||||
public function logResponse(Request $request, string $rawResponse, Response $response)
|
||||
public function logResponse(Request $request, string $rawResponse)
|
||||
{
|
||||
$loggedRequest = collect($this->requests)->first(function (LoggedRequest $loggedRequest) use ($request) {
|
||||
return $loggedRequest->getRequest() === $request;
|
||||
});
|
||||
if ($loggedRequest) {
|
||||
$loggedRequest->setResponse($rawResponse, $response);
|
||||
$loggedRequest->setResponse($rawResponse, Response::fromString($rawResponse));
|
||||
|
||||
$this->pushLogs();
|
||||
}
|
||||
|
||||
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