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,
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user