mirror of
https://github.com/bitinflow/expose.git
synced 2026-03-13 21:45:55 +00:00
wip
This commit is contained in:
39
app/Client/Client.php
Normal file
39
app/Client/Client.php
Normal file
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
namespace App\Client;
|
||||
|
||||
use React\EventLoop\LoopInterface;
|
||||
use React\Socket\ConnectionInterface;
|
||||
use React\Socket\Connector;
|
||||
|
||||
class Client
|
||||
{
|
||||
/** @var LoopInterface */
|
||||
protected $loop;
|
||||
protected $host;
|
||||
protected $port;
|
||||
|
||||
public function __construct(LoopInterface $loop, $host, $port)
|
||||
{
|
||||
$this->loop = $loop;
|
||||
$this->host = $host;
|
||||
$this->port = $port;
|
||||
}
|
||||
|
||||
public function share($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) {
|
||||
dump("Connected to http://$data->subdomain.{$this->host}:{$this->port}");
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
74
app/Client/Connection.php
Normal file
74
app/Client/Connection.php
Normal file
@@ -0,0 +1,74 @@
|
||||
<?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,
|
||||
],
|
||||
]));
|
||||
}
|
||||
}
|
||||
101
app/Client/Factory.php
Normal file
101
app/Client/Factory.php
Normal file
@@ -0,0 +1,101 @@
|
||||
<?php
|
||||
|
||||
namespace App\Client;
|
||||
|
||||
use App\HttpServer\App;
|
||||
use App\HttpServer\Controllers\DashboardController;
|
||||
use App\HttpServer\Controllers\LogController;
|
||||
use App\HttpServer\Controllers\ReplayLogController;
|
||||
use App\HttpServer\Controllers\StoreLogController;
|
||||
use App\WebSockets\Socket;
|
||||
use Ratchet\WebSocket\WsServer;
|
||||
use React\EventLoop\LoopInterface;
|
||||
use Symfony\Component\Routing\Route;
|
||||
use React\EventLoop\Factory as LoopFactory;
|
||||
|
||||
class Factory
|
||||
{
|
||||
/** @var string */
|
||||
protected $host = 'localhost';
|
||||
|
||||
/** @var int */
|
||||
protected $port = 8080;
|
||||
|
||||
/** @var \React\EventLoop\LoopInterface */
|
||||
protected $loop;
|
||||
|
||||
/** @var App */
|
||||
protected $app;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->loop = LoopFactory::create();
|
||||
}
|
||||
|
||||
public function setHost(string $host)
|
||||
{
|
||||
$this->host = $host;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setPort(int $port)
|
||||
{
|
||||
$this->port = $port;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setLoop(LoopInterface $loop)
|
||||
{
|
||||
$this->loop = $loop;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function createClient($sharedUrl, $subdomain = null)
|
||||
{
|
||||
$client = new Client($this->loop, $this->host, $this->port);
|
||||
$client->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']);
|
||||
|
||||
$this->app->route('/socket', new WsServer(new Socket()), ['*']);
|
||||
|
||||
$this->app->routes->add('dashboard', $dashboardRoute);
|
||||
$this->app->routes->add('logs', $logRoute);
|
||||
$this->app->routes->add('storeLogs', $storeLogRoute);
|
||||
$this->app->routes->add('replayLog', $replayLogRoute);
|
||||
}
|
||||
|
||||
public function createHttpServer()
|
||||
{
|
||||
$this->loop->futureTick(function () {
|
||||
$dashboardUrl = 'http://127.0.0.1:4040/';
|
||||
|
||||
echo('Started Dashboard on port 4040'. PHP_EOL);
|
||||
|
||||
echo('If the dashboard does not automatically open, visit: '.$dashboardUrl . PHP_EOL);
|
||||
});
|
||||
|
||||
$this->app = new App('127.0.0.1', 4040, '0.0.0.0', $this->loop);
|
||||
|
||||
$this->addRoutes();
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function run()
|
||||
{
|
||||
$this->loop->run();
|
||||
}
|
||||
|
||||
}
|
||||
84
app/Client/ProxyManager.php
Normal file
84
app/Client/ProxyManager.php
Normal file
@@ -0,0 +1,84 @@
|
||||
<?php
|
||||
|
||||
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;
|
||||
|
||||
class ProxyManager
|
||||
{
|
||||
private $host;
|
||||
private $port;
|
||||
private $loop;
|
||||
|
||||
public function __construct($host, $port, $loop)
|
||||
{
|
||||
$this->host = $host;
|
||||
$this->port = $port;
|
||||
$this->loop = $loop;
|
||||
}
|
||||
|
||||
public function createProxy(ConnectionInterface $clientConnection, $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,
|
||||
],
|
||||
]));
|
||||
|
||||
$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);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
private function parseResponse(string $response)
|
||||
{
|
||||
try {
|
||||
return gPsr\parse_response($response);
|
||||
} catch (\Throwable $e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private function parseRequest($data)
|
||||
{
|
||||
return gPsr\parse_request($data);
|
||||
}
|
||||
|
||||
protected function getContentLength($proxyConnection): ?int
|
||||
{
|
||||
$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);
|
||||
}
|
||||
}
|
||||
89
app/Client/TunnelConnection.php
Normal file
89
app/Client/TunnelConnection.php
Normal file
@@ -0,0 +1,89 @@
|
||||
<?php
|
||||
|
||||
namespace App\Client;
|
||||
|
||||
use App\Logger\RequestLogger;
|
||||
use Laminas\Http\Request;
|
||||
use Laminas\Http\Response;
|
||||
use React\EventLoop\LoopInterface;
|
||||
use React\Socket\ConnectionInterface;
|
||||
use React\Socket\Connector;
|
||||
use React\Stream\Util;
|
||||
|
||||
class TunnelConnection
|
||||
{
|
||||
/** @var LoopInterface */
|
||||
protected $loop;
|
||||
|
||||
/** @var RequestLogger */
|
||||
protected $logger;
|
||||
protected $request;
|
||||
|
||||
public function __construct(LoopInterface $loop, RequestLogger $logger)
|
||||
{
|
||||
$this->loop = $loop;
|
||||
$this->logger = $logger;
|
||||
}
|
||||
|
||||
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 (! is_null($proxyConnection)) {
|
||||
$proxyConnection->pause();
|
||||
}
|
||||
|
||||
(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;
|
||||
|
||||
try {
|
||||
$response = $this->parseResponse($connection->httpBuffer);
|
||||
|
||||
$this->logger->logResponse($this->request, $connection->httpBuffer, $response);
|
||||
|
||||
unset($connection->httpBuffer);
|
||||
} catch (\Throwable $e) {
|
||||
//
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
if (! is_null($proxyConnection)) {
|
||||
Util::pipe($connection, $proxyConnection, ['end' => true]);
|
||||
}
|
||||
|
||||
$connection->write($requestData);
|
||||
|
||||
if (! is_null($proxyConnection)) {
|
||||
$proxyConnection->resume();
|
||||
|
||||
unset($proxyConnection->buffer);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
protected function parseResponse(string $response)
|
||||
{
|
||||
try {
|
||||
return Response::fromString($response);
|
||||
} catch (\Throwable $e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
protected function parseRequest($data)
|
||||
{
|
||||
return Request::fromString($data);
|
||||
}
|
||||
}
|
||||
0
app/Commands/.gitkeep
Normal file
0
app/Commands/.gitkeep
Normal file
21
app/Commands/ServeCommand.php
Normal file
21
app/Commands/ServeCommand.php
Normal file
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
namespace App\Commands;
|
||||
|
||||
use App\Server\Factory;
|
||||
use Illuminate\Console\Scheduling\Schedule;
|
||||
use LaravelZero\Framework\Commands\Command;
|
||||
|
||||
class ServeCommand extends Command
|
||||
{
|
||||
protected $signature = 'serve';
|
||||
|
||||
protected $description = 'Start the shaft server';
|
||||
|
||||
public function handle()
|
||||
{
|
||||
(new Factory())
|
||||
->createServer()
|
||||
->run();
|
||||
}
|
||||
}
|
||||
24
app/Commands/ShareCommand.php
Normal file
24
app/Commands/ShareCommand.php
Normal file
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
namespace App\Commands;
|
||||
|
||||
use App\Client\Factory;
|
||||
use Illuminate\Console\Scheduling\Schedule;
|
||||
use LaravelZero\Framework\Commands\Command;
|
||||
use React\EventLoop\LoopInterface;
|
||||
|
||||
class ShareCommand extends Command
|
||||
{
|
||||
protected $signature = 'share {host} {--subdomain=}';
|
||||
|
||||
protected $description = 'Share a local url with a remote shaft server';
|
||||
|
||||
public function handle()
|
||||
{
|
||||
(new Factory())
|
||||
->setLoop(app(LoopInterface::class))
|
||||
->createClient($this->argument('host'), explode(',', $this->option('subdomain')))
|
||||
->createHttpServer()
|
||||
->run();
|
||||
}
|
||||
}
|
||||
32
app/HttpServer/App.php
Normal file
32
app/HttpServer/App.php
Normal file
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
namespace App\HttpServer;
|
||||
|
||||
use Ratchet\Http\Router;
|
||||
use Ratchet\Server\IoServer;
|
||||
use React\EventLoop\LoopInterface;
|
||||
use React\Socket\Server as Reactor;
|
||||
use Symfony\Component\Routing\Matcher\UrlMatcher;
|
||||
use Symfony\Component\Routing\RequestContext;
|
||||
use Symfony\Component\Routing\RouteCollection;
|
||||
|
||||
class App extends \Ratchet\App
|
||||
{
|
||||
public function __construct($httpHost, $port, $address, LoopInterface $loop)
|
||||
{
|
||||
$this->httpHost = $httpHost;
|
||||
$this->port = $port;
|
||||
|
||||
$socket = new Reactor($address.':'.$port, $loop);
|
||||
|
||||
$this->routes = new RouteCollection;
|
||||
|
||||
$urlMatcher = new UrlMatcher($this->routes, new RequestContext);
|
||||
|
||||
$router = new Router($urlMatcher);
|
||||
|
||||
$httpServer = new HttpServer($router);
|
||||
|
||||
$this->_server = new IoServer($httpServer, $socket, $loop);
|
||||
}
|
||||
}
|
||||
22
app/HttpServer/Controllers/Controller.php
Normal file
22
app/HttpServer/Controllers/Controller.php
Normal file
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
namespace App\HttpServer\Controllers;
|
||||
|
||||
use Exception;
|
||||
use Ratchet\ConnectionInterface;
|
||||
use Ratchet\Http\HttpServerInterface;
|
||||
|
||||
abstract class Controller implements HttpServerInterface
|
||||
{
|
||||
public function onClose(ConnectionInterface $connection)
|
||||
{
|
||||
}
|
||||
|
||||
public function onError(ConnectionInterface $connection, Exception $e)
|
||||
{
|
||||
}
|
||||
|
||||
public function onMessage(ConnectionInterface $from, $msg)
|
||||
{
|
||||
}
|
||||
}
|
||||
24
app/HttpServer/Controllers/DashboardController.php
Normal file
24
app/HttpServer/Controllers/DashboardController.php
Normal file
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
namespace App\HttpServer\Controllers;
|
||||
|
||||
use GuzzleHttp\Psr7\Response;
|
||||
use function GuzzleHttp\Psr7\str;
|
||||
use Psr\Http\Message\RequestInterface;
|
||||
use Ratchet\ConnectionInterface;
|
||||
|
||||
class DashboardController extends Controller
|
||||
{
|
||||
public function onOpen(ConnectionInterface $connection, RequestInterface $request = null)
|
||||
{
|
||||
$connection->send(
|
||||
str(new Response(
|
||||
200,
|
||||
['Content-Type' => 'text/html'],
|
||||
file_get_contents(base_path('resources/views/index.html'))
|
||||
))
|
||||
);
|
||||
|
||||
$connection->close();
|
||||
}
|
||||
}
|
||||
28
app/HttpServer/Controllers/LogController.php
Normal file
28
app/HttpServer/Controllers/LogController.php
Normal file
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace App\HttpServer\Controllers;
|
||||
|
||||
use App\Logger\RequestLogger;
|
||||
use GuzzleHttp\Psr7\Response;
|
||||
use Ratchet\ConnectionInterface;
|
||||
use function GuzzleHttp\Psr7\str;
|
||||
use Psr\Http\Message\RequestInterface;
|
||||
|
||||
class LogController extends Controller
|
||||
{
|
||||
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)
|
||||
))
|
||||
);
|
||||
|
||||
$connection->close();
|
||||
}
|
||||
}
|
||||
35
app/HttpServer/Controllers/ReplayLogController.php
Normal file
35
app/HttpServer/Controllers/ReplayLogController.php
Normal file
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
namespace App\HttpServer\Controllers;
|
||||
|
||||
use App\Client\TunnelConnection;
|
||||
use App\HttpServer\QueryParameters;
|
||||
use App\Logger\RequestLogger;
|
||||
use GuzzleHttp\Psr7\Response;
|
||||
use Ratchet\ConnectionInterface;
|
||||
use function GuzzleHttp\Psr7\str;
|
||||
use Psr\Http\Message\RequestInterface;
|
||||
|
||||
class ReplayLogController extends Controller
|
||||
{
|
||||
public function onOpen(ConnectionInterface $connection, RequestInterface $request = null)
|
||||
{
|
||||
/** @var RequestLogger $logger */
|
||||
$logger = app(RequestLogger::class);
|
||||
$requestData = $logger->findLoggedRequest(QueryParameters::create($request)->get('log'))->getRequestData();
|
||||
|
||||
/** @var TunnelConnection $tunnel */
|
||||
$tunnel = app(TunnelConnection::class);
|
||||
$tunnel->performRequest($requestData);
|
||||
|
||||
$connection->send(
|
||||
str(new Response(
|
||||
200,
|
||||
['Content-Type' => 'application/json'],
|
||||
''
|
||||
))
|
||||
);
|
||||
|
||||
$connection->close();
|
||||
}
|
||||
}
|
||||
62
app/HttpServer/Controllers/StoreLogController.php
Normal file
62
app/HttpServer/Controllers/StoreLogController.php
Normal file
@@ -0,0 +1,62 @@
|
||||
<?php
|
||||
|
||||
namespace App\HttpServer\Controllers;
|
||||
|
||||
use Exception;
|
||||
use App\WebSockets\Socket;
|
||||
use GuzzleHttp\Psr7\Response;
|
||||
use Illuminate\Support\Collection;
|
||||
use Ratchet\ConnectionInterface;
|
||||
use function GuzzleHttp\Psr7\str;
|
||||
use Psr\Http\Message\RequestInterface;
|
||||
|
||||
class StoreLogController extends Controller
|
||||
{
|
||||
|
||||
public function onOpen(ConnectionInterface $connection, RequestInterface $request = null)
|
||||
{
|
||||
$connection->contentLength = $this->findContentLength($request->getHeaders());
|
||||
|
||||
$connection->requestBuffer = (string) $request->getBody();
|
||||
|
||||
$this->checkContentLength($connection);
|
||||
}
|
||||
|
||||
public function onMessage(ConnectionInterface $from, $msg)
|
||||
{
|
||||
$from->requestBuffer .= $msg;
|
||||
|
||||
$this->checkContentLength($from);
|
||||
}
|
||||
|
||||
protected function findContentLength(array $headers): int
|
||||
{
|
||||
return Collection::make($headers)->first(function ($values, $header) {
|
||||
return strtolower($header) === 'content-length';
|
||||
})[0] ?? 0;
|
||||
}
|
||||
|
||||
protected function checkContentLength(ConnectionInterface $connection)
|
||||
{
|
||||
if (strlen($connection->requestBuffer) === $connection->contentLength) {
|
||||
try {
|
||||
/*
|
||||
* This is the post payload from our PHPUnit tests.
|
||||
* Send it to the connected connections.
|
||||
*/
|
||||
foreach (Socket::$connections as $webSocketConnection) {
|
||||
$webSocketConnection->send($connection->requestBuffer);
|
||||
}
|
||||
|
||||
$connection->send(str(new Response(200)));
|
||||
} catch (Exception $e) {
|
||||
$connection->send(str(new Response(500, [], $e->getMessage())));
|
||||
}
|
||||
|
||||
$connection->close();
|
||||
|
||||
unset($connection->requestBuffer);
|
||||
unset($connection->contentLength);
|
||||
}
|
||||
}
|
||||
}
|
||||
15
app/HttpServer/HttpServer.php
Normal file
15
app/HttpServer/HttpServer.php
Normal file
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
namespace App\HttpServer;
|
||||
|
||||
use Ratchet\Http\HttpServerInterface;
|
||||
|
||||
class HttpServer extends \Ratchet\Http\HttpServer
|
||||
{
|
||||
public function __construct(HttpServerInterface $component)
|
||||
{
|
||||
parent::__construct($component);
|
||||
|
||||
$this->_reqParser->maxSize = 15242880;
|
||||
}
|
||||
}
|
||||
35
app/HttpServer/QueryParameters.php
Normal file
35
app/HttpServer/QueryParameters.php
Normal file
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
namespace App\HttpServer;
|
||||
|
||||
use Psr\Http\Message\RequestInterface;
|
||||
|
||||
class QueryParameters
|
||||
{
|
||||
/** @var \Psr\Http\Message\RequestInterface */
|
||||
protected $request;
|
||||
|
||||
public static function create(RequestInterface $request)
|
||||
{
|
||||
return new static($request);
|
||||
}
|
||||
|
||||
public function __construct(RequestInterface $request)
|
||||
{
|
||||
$this->request = $request;
|
||||
}
|
||||
|
||||
public function all(): array
|
||||
{
|
||||
$queryParameters = [];
|
||||
|
||||
parse_str($this->request->getUri()->getQuery(), $queryParameters);
|
||||
|
||||
return $queryParameters;
|
||||
}
|
||||
|
||||
public function get(string $name): string
|
||||
{
|
||||
return $this->all()[$name] ?? '';
|
||||
}
|
||||
}
|
||||
177
app/Logger/LoggedRequest.php
Normal file
177
app/Logger/LoggedRequest.php
Normal file
@@ -0,0 +1,177 @@
|
||||
<?php
|
||||
|
||||
namespace App\Logger;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Str;
|
||||
use Laminas\Http\Request;
|
||||
use Laminas\Http\Response;
|
||||
use Riverline\MultiPartParser\StreamedPart;
|
||||
|
||||
class LoggedRequest implements \JsonSerializable
|
||||
{
|
||||
/** @var string */
|
||||
protected $rawRequest;
|
||||
|
||||
/** @var Request */
|
||||
protected $parsedRequest;
|
||||
|
||||
/** @var string */
|
||||
protected $rawResponse;
|
||||
|
||||
/** @var Response */
|
||||
protected $parsedResponse;
|
||||
|
||||
/** @var string */
|
||||
protected $id;
|
||||
|
||||
/** @var Carbon */
|
||||
protected $startTime;
|
||||
|
||||
/** @var Carbon */
|
||||
protected $stopTime;
|
||||
|
||||
/** @var string */
|
||||
protected $subdomain;
|
||||
|
||||
public function __construct(string $rawRequest, Request $parsedRequest)
|
||||
{
|
||||
$this->id = (string)Str::uuid();
|
||||
$this->startTime = now();
|
||||
$this->rawRequest = $rawRequest;
|
||||
$this->parsedRequest = $parsedRequest;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function jsonSerialize()
|
||||
{
|
||||
$data = [
|
||||
'id' => $this->id,
|
||||
'performed_at' => $this->startTime->toDateTimeString(),
|
||||
'duration' => $this->startTime->diffInMilliseconds($this->stopTime, false),
|
||||
'subdomain' => $this->detectSubdomain(),
|
||||
'request' => [
|
||||
'raw' => $this->isBinary($this->rawRequest) ? 'BINARY' : $this->rawRequest,
|
||||
'method' => $this->parsedRequest->getMethod(),
|
||||
'uri' => $this->parsedRequest->getUri()->getPath(),
|
||||
'headers' => $this->parsedRequest->getHeaders()->toArray(),
|
||||
'body' => $this->isBinary($this->rawRequest) ? 'BINARY' : $this->parsedRequest->getContent(),
|
||||
'query' => $this->parsedRequest->getQuery()->toArray(),
|
||||
'post' => $this->getPost(),
|
||||
],
|
||||
];
|
||||
|
||||
if ($this->parsedResponse) {
|
||||
$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',
|
||||
];
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
protected function isBinary(string $string): bool
|
||||
{
|
||||
return preg_match('~[^\x20-\x7E\t\r\n]~', $string) > 0;
|
||||
}
|
||||
|
||||
protected function shouldReturnBody()
|
||||
{
|
||||
$contentType = Arr::get($this->parsedResponse->getHeaders()->toArray(), 'Content-Type');
|
||||
|
||||
return $contentType === 'application/json' || Str::is('text/*', $contentType) || Str::is('*javascript*', $contentType);
|
||||
}
|
||||
|
||||
public function getRequest()
|
||||
{
|
||||
return $this->parsedRequest;
|
||||
}
|
||||
|
||||
public function setResponse(string $rawResponse, Response $response)
|
||||
{
|
||||
$this->parsedResponse = $response;
|
||||
|
||||
$this->rawResponse = $rawResponse;
|
||||
|
||||
$this->stopTime = now();
|
||||
}
|
||||
|
||||
public function id()
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
public function getRequestData()
|
||||
{
|
||||
return $this->rawRequest;
|
||||
}
|
||||
|
||||
protected function getResponseBody()
|
||||
{
|
||||
return \Laminas\Http\Response::fromString($this->rawResponse)->getBody();
|
||||
}
|
||||
|
||||
protected function getPost()
|
||||
{
|
||||
$postData = [];
|
||||
|
||||
$contentType = Arr::get($this->parsedRequest->getHeaders()->toArray(), 'Content-Type');
|
||||
|
||||
switch ($contentType) {
|
||||
case 'application/x-www-form-urlencoded':
|
||||
parse_str($this->parsedRequest->getContent(), $postData);
|
||||
$postData = collect($postData)->map(function ($key, $value) {
|
||||
return [
|
||||
'name' => $key,
|
||||
'value' => $value,
|
||||
];
|
||||
})->toArray();
|
||||
break;
|
||||
case 'application/json':
|
||||
$postData = collect(json_decode($this->parsedRequest->getContent(), true))->map(function ($key, $value) {
|
||||
return [
|
||||
'name' => $key,
|
||||
'value' => $value,
|
||||
];
|
||||
})->toArray();
|
||||
|
||||
break;
|
||||
default:
|
||||
$stream = fopen('php://temp', 'rw');
|
||||
fwrite($stream, $this->rawRequest);
|
||||
rewind($stream);
|
||||
|
||||
try {
|
||||
$document = new StreamedPart($stream);
|
||||
if ($document->isMultiPart()) {
|
||||
$postData = collect($document->getParts())->map(function (StreamedPart $part) {
|
||||
return [
|
||||
'name' => $part->getName(),
|
||||
'value' => $part->isFile() ? null : $part->getBody(),
|
||||
'is_file' => $part->isFile(),
|
||||
'filename' => $part->isFile() ? $part->getFileName() : null,
|
||||
'mime_type' => $part->isFile() ? $part->getMimeType() : null,
|
||||
];
|
||||
})->toArray();
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
//
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
return $postData;
|
||||
}
|
||||
|
||||
protected function detectSubdomain()
|
||||
{
|
||||
return Arr::get($this->parsedRequest->getHeaders()->toArray(), 'X-Original-Host');
|
||||
}
|
||||
}
|
||||
65
app/Logger/Logger.php
Normal file
65
app/Logger/Logger.php
Normal file
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
|
||||
namespace App\Logger;
|
||||
|
||||
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
|
||||
class Logger
|
||||
{
|
||||
/** @var \Symfony\Component\Console\Output\OutputInterface */
|
||||
protected $consoleOutput;
|
||||
|
||||
/** @var bool */
|
||||
protected $enabled = false;
|
||||
|
||||
/** @var bool */
|
||||
protected $verbose = false;
|
||||
|
||||
public function __construct(OutputInterface $consoleOutput)
|
||||
{
|
||||
$this->consoleOutput = $consoleOutput;
|
||||
}
|
||||
|
||||
public function enable($enabled = true)
|
||||
{
|
||||
$this->enabled = $enabled;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function verbose($verbose = false)
|
||||
{
|
||||
$this->verbose = $verbose;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
protected function info(string $message)
|
||||
{
|
||||
$this->line($message, 'info');
|
||||
}
|
||||
|
||||
protected function warn(string $message)
|
||||
{
|
||||
if (! $this->consoleOutput->getFormatter()->hasStyle('warning')) {
|
||||
$style = new OutputFormatterStyle('yellow');
|
||||
|
||||
$this->consoleOutput->getFormatter()->setStyle('warning', $style);
|
||||
}
|
||||
|
||||
$this->line($message, 'warning');
|
||||
}
|
||||
|
||||
protected function error(string $message)
|
||||
{
|
||||
$this->line($message, 'error');
|
||||
}
|
||||
|
||||
protected function line(string $message, string $style)
|
||||
{
|
||||
$styled = $style ? "<$style>$message</$style>" : $message;
|
||||
|
||||
$this->consoleOutput->writeln($styled);
|
||||
}
|
||||
}
|
||||
64
app/Logger/RequestLogger.php
Normal file
64
app/Logger/RequestLogger.php
Normal file
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
|
||||
namespace App\Logger;
|
||||
|
||||
use Clue\React\Buzz\Browser;
|
||||
use GuzzleHttp\RequestOptions;
|
||||
use Laminas\Http\Request;
|
||||
use Laminas\Http\Response;
|
||||
use function GuzzleHttp\Psr7\stream_for;
|
||||
|
||||
class RequestLogger
|
||||
{
|
||||
protected $requests = [];
|
||||
protected $responses = [];
|
||||
|
||||
public function __construct(Browser $browser)
|
||||
{
|
||||
$this->client = $browser;
|
||||
}
|
||||
|
||||
public function findLoggedRequest(string $id): ?LoggedRequest
|
||||
{
|
||||
return collect($this->requests)->first(function (LoggedRequest $loggedRequest) use ($id) {
|
||||
return $loggedRequest->id() === $id;
|
||||
});
|
||||
}
|
||||
|
||||
public function logRequest(string $rawRequest, Request $request)
|
||||
{
|
||||
array_unshift($this->requests, new LoggedRequest($rawRequest, $request));
|
||||
|
||||
$this->requests = array_slice($this->requests, 0, 10);
|
||||
|
||||
$this->pushLogs();
|
||||
}
|
||||
|
||||
public function logResponse(Request $request, string $rawResponse, Response $response)
|
||||
{
|
||||
$loggedRequest = collect($this->requests)->first(function (LoggedRequest $loggedRequest) use ($request) {
|
||||
return $loggedRequest->getRequest() === $request;
|
||||
});
|
||||
if ($loggedRequest) {
|
||||
$loggedRequest->setResponse($rawResponse, $response);
|
||||
|
||||
$this->pushLogs();
|
||||
}
|
||||
}
|
||||
|
||||
public function getData()
|
||||
{
|
||||
return $this->requests;
|
||||
}
|
||||
|
||||
protected function pushLogs()
|
||||
{
|
||||
$this
|
||||
->client
|
||||
->post(
|
||||
'http://127.0.0.1:4040/logs',
|
||||
['Content-Type' => 'application/json'],
|
||||
json_encode($this->getData(), JSON_INVALID_UTF8_IGNORE)
|
||||
);
|
||||
}
|
||||
}
|
||||
29
app/Providers/AppServiceProvider.php
Normal file
29
app/Providers/AppServiceProvider.php
Normal file
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace App\Providers;
|
||||
|
||||
use App\Logger\RequestLogger;
|
||||
use Clue\React\Buzz\Browser;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
use React\EventLoop\Factory as LoopFactory;
|
||||
use React\EventLoop\LoopInterface;
|
||||
|
||||
class AppServiceProvider extends ServiceProvider
|
||||
{
|
||||
public function boot()
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
public function register()
|
||||
{
|
||||
$this->app->singleton(LoopInterface::class, function () {
|
||||
return LoopFactory::create();
|
||||
});
|
||||
|
||||
$this->app->singleton(RequestLogger::class, function () {
|
||||
$browser = new Browser(app(LoopInterface::class));
|
||||
return new RequestLogger($browser);
|
||||
});
|
||||
}
|
||||
}
|
||||
48
app/Server/Connections/Connection.php
Normal file
48
app/Server/Connections/Connection.php
Normal file
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
namespace App\Server\Connections;
|
||||
|
||||
use Illuminate\Support\Str;
|
||||
use Ratchet\ConnectionInterface;
|
||||
|
||||
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 setProxy(ConnectionInterface $proxy)
|
||||
{
|
||||
$this->proxies[] = $proxy;
|
||||
}
|
||||
|
||||
public function getProxy(): ?ConnectionInterface
|
||||
{
|
||||
return array_pop($this->proxies);
|
||||
}
|
||||
|
||||
public function rewriteHostInformation($serverHost, $port, string $data)
|
||||
{
|
||||
$appName = config('app.name');
|
||||
$appVersion = config('app.version');
|
||||
|
||||
return str_replace(
|
||||
"Host: {$this->subdomain}.{$serverHost}:{$port}\r\n",
|
||||
"Host: {$this->host}\r\n" .
|
||||
"X-Tunnel-By: {$appName} {$appVersion}\r\n" .
|
||||
"X-Original-Host: {$this->subdomain}.{$serverHost}:{$port}\r\n",
|
||||
$data
|
||||
);
|
||||
}
|
||||
}
|
||||
60
app/Server/Connections/ConnectionManager.php
Normal file
60
app/Server/Connections/ConnectionManager.php
Normal file
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
namespace App\Server\Connections;
|
||||
|
||||
use Illuminate\Support\Str;
|
||||
use Ratchet\ConnectionInterface;
|
||||
|
||||
class ConnectionManager
|
||||
{
|
||||
/** @var array */
|
||||
protected $connections = [];
|
||||
protected $host;
|
||||
protected $port;
|
||||
|
||||
public function __construct($host, $port)
|
||||
{
|
||||
$this->host = $host;
|
||||
$this->port = $port;
|
||||
}
|
||||
|
||||
public function storeConnection(string $host, ?string $subdomain, IoConnection $connection)
|
||||
{
|
||||
$clientId = (string)uniqid();
|
||||
|
||||
$storedConnection = new Connection($connection, $host, $subdomain ?? $this->generateSubdomain(), $clientId);
|
||||
|
||||
$this->connections[] = $storedConnection;
|
||||
|
||||
return $storedConnection;
|
||||
}
|
||||
|
||||
public function findConnectionForSubdomain($subdomain): ?Connection
|
||||
{
|
||||
return collect($this->connections)->last(function ($connection) use ($subdomain) {
|
||||
return $connection->subdomain == $subdomain;
|
||||
});
|
||||
}
|
||||
|
||||
public function findConnectionForClientId(string $clientId): ?Connection
|
||||
{
|
||||
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->host === '127.0.0.1' ? 'localhost' : $this->host;
|
||||
}
|
||||
|
||||
public function port()
|
||||
{
|
||||
return $this->port;
|
||||
}
|
||||
}
|
||||
45
app/Server/Connections/IoConnection.php
Normal file
45
app/Server/Connections/IoConnection.php
Normal file
@@ -0,0 +1,45 @@
|
||||
<?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();
|
||||
}
|
||||
}
|
||||
58
app/Server/Factory.php
Normal file
58
app/Server/Factory.php
Normal file
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
namespace App\Server;
|
||||
|
||||
use App\Server\Connections\ConnectionManager;
|
||||
use React\Socket\Server;
|
||||
use React\EventLoop\LoopInterface;
|
||||
use React\EventLoop\Factory as LoopFactory;
|
||||
|
||||
class Factory
|
||||
{
|
||||
/** @var string */
|
||||
protected $host = '127.0.0.1';
|
||||
|
||||
/** @var int */
|
||||
protected $port = 8080;
|
||||
|
||||
/** @var \React\EventLoop\LoopInterface */
|
||||
protected $loop;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->loop = LoopFactory::create();
|
||||
}
|
||||
|
||||
public function setHost(string $host)
|
||||
{
|
||||
$this->host = $host;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setPort(int $port)
|
||||
{
|
||||
$this->port = $port;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setLoop(LoopInterface $loop)
|
||||
{
|
||||
$this->loop = $loop;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function createServer()
|
||||
{
|
||||
$socket = new Server("{$this->host}:{$this->port}", $this->loop);
|
||||
|
||||
$connectionManager = new ConnectionManager($this->host, $this->port);
|
||||
|
||||
$app = new Shaft($connectionManager);
|
||||
|
||||
return new IoServer($app, $socket, $this->loop);
|
||||
}
|
||||
|
||||
}
|
||||
32
app/Server/IoServer.php
Normal file
32
app/Server/IoServer.php
Normal file
@@ -0,0 +1,32 @@
|
||||
<?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);
|
||||
});
|
||||
}
|
||||
}
|
||||
60
app/Server/Messages/ControlMessage.php
Normal file
60
app/Server/Messages/ControlMessage.php
Normal file
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
namespace App\Server\Messages;
|
||||
|
||||
use App\Server\Connections\ConnectionManager;
|
||||
use Illuminate\Support\Str;
|
||||
use Ratchet\ConnectionInterface;
|
||||
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
|
||||
]));
|
||||
}
|
||||
|
||||
protected function registerProxy(ConnectionInterface $connection, $data)
|
||||
{
|
||||
$connectionInfo = $this->connectionManager->findConnectionForClientId($data->client_id);
|
||||
|
||||
$connectionInfo->socket->getConnection()->emit('proxy_ready_'.$data->request_id, [
|
||||
$connection,
|
||||
]);
|
||||
|
||||
$connectionInfo->setProxy($connection);
|
||||
}
|
||||
}
|
||||
8
app/Server/Messages/Message.php
Normal file
8
app/Server/Messages/Message.php
Normal file
@@ -0,0 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace App\Server\Messages;
|
||||
|
||||
interface Message
|
||||
{
|
||||
public function respond();
|
||||
}
|
||||
18
app/Server/Messages/MessageFactory.php
Normal file
18
app/Server/Messages/MessageFactory.php
Normal file
@@ -0,0 +1,18 @@
|
||||
<?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);
|
||||
}
|
||||
}
|
||||
96
app/Server/Messages/TunnelMessage.php
Normal file
96
app/Server/Messages/TunnelMessage.php
Normal file
@@ -0,0 +1,96 @@
|
||||
<?php
|
||||
|
||||
namespace App\Server\Messages;
|
||||
|
||||
use App\Server\Connections\Connection;
|
||||
use App\Server\Connections\ConnectionManager;
|
||||
use App\Server\Connections\IoConnection;
|
||||
use BFunky\HttpParser\HttpRequestParser;
|
||||
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
|
||||
{
|
||||
/** string */
|
||||
protected $payload;
|
||||
|
||||
/** @var \Ratchet\ConnectionInterface */
|
||||
protected $connection;
|
||||
|
||||
/** @var ConnectionManager */
|
||||
private $connectionManager;
|
||||
|
||||
public function __construct($payload, ConnectionInterface $connection, ConnectionManager $connectionManager)
|
||||
{
|
||||
$this->payload = $payload;
|
||||
|
||||
$this->connection = $connection;
|
||||
|
||||
$this->connectionManager = $connectionManager;
|
||||
}
|
||||
|
||||
public function respond()
|
||||
{
|
||||
$clientConnection = $this->connectionManager->findConnectionForSubdomain($this->detectSubdomain());
|
||||
|
||||
if (is_null($clientConnection)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($this->hasBufferedAllData()) {
|
||||
$this->copyDataToClient($clientConnection);
|
||||
}
|
||||
}
|
||||
|
||||
protected function getContentLength(): ?int
|
||||
{
|
||||
$request = parse_request($this->connection->buffer);
|
||||
|
||||
return Arr::first($request->getHeader('Content-Length'));
|
||||
}
|
||||
|
||||
protected function detectSubdomain(): ?string
|
||||
{
|
||||
$subdomain = '';
|
||||
|
||||
$headers = collect(explode("\r\n", $this->connection->buffer))->map(function ($header) use (&$subdomain) {
|
||||
$headerData = explode(':', $header);
|
||||
if ($headerData[0] === 'Host') {
|
||||
$domainParts = explode('.', $headerData[1]);
|
||||
$subdomain = trim($domainParts[0]);
|
||||
}
|
||||
});
|
||||
|
||||
return $subdomain;
|
||||
}
|
||||
|
||||
private function copyDataToClient(Connection $clientConnection)
|
||||
{
|
||||
$data = $clientConnection->rewriteHostInformation($this->connectionManager->host(), $this->connectionManager->port(), $this->connection->buffer);
|
||||
|
||||
$requestId = uniqid();
|
||||
|
||||
// Ask client to create a new proxy
|
||||
$clientConnection->socket->send(json_encode([
|
||||
'event' => 'createProxy',
|
||||
'request_id' => $requestId,
|
||||
'client_id' => $clientConnection->client_id,
|
||||
]) . "||");
|
||||
|
||||
$clientConnection->socket->getConnection()->once('proxy_ready_' . $requestId, function (IoConnection $proxy) use ($data, $requestId) {
|
||||
Util::pipe($proxy->getConnection(), $this->connection->getConnection());
|
||||
|
||||
$proxy->send($data);
|
||||
});
|
||||
|
||||
unset($this->connection->buffer);
|
||||
}
|
||||
|
||||
protected function hasBufferedAllData()
|
||||
{
|
||||
return is_null($this->getContentLength()) || strlen(Str::after($this->connection->buffer, "\r\n\r\n")) === $this->getContentLength();
|
||||
}
|
||||
}
|
||||
54
app/Server/Shaft.php
Normal file
54
app/Server/Shaft.php
Normal file
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
namespace App\Server;
|
||||
|
||||
use App\Server\Connections\ConnectionManager;
|
||||
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 Shaft 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 {
|
||||
if (! isset($connection->buffer)) {
|
||||
$connection->buffer = '';
|
||||
}
|
||||
$connection->buffer .= $message;
|
||||
|
||||
$message = new TunnelMessage($connection->buffer, $connection, $this->connectionManager);
|
||||
$message->respond();
|
||||
}
|
||||
}
|
||||
}
|
||||
29
app/WebSockets/Socket.php
Normal file
29
app/WebSockets/Socket.php
Normal file
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace App\WebSockets;
|
||||
|
||||
use Ratchet\ConnectionInterface;
|
||||
use Ratchet\RFC6455\Messaging\MessageInterface;
|
||||
use Ratchet\WebSocket\MessageComponentInterface;
|
||||
|
||||
class Socket implements MessageComponentInterface
|
||||
{
|
||||
public static $connections = [];
|
||||
|
||||
public function onOpen(ConnectionInterface $connection)
|
||||
{
|
||||
self::$connections[] = $connection;
|
||||
}
|
||||
|
||||
public function onMessage(ConnectionInterface $from, MessageInterface $msg)
|
||||
{
|
||||
}
|
||||
|
||||
public function onClose(ConnectionInterface $connection)
|
||||
{
|
||||
}
|
||||
|
||||
public function onError(ConnectionInterface $connection, \Exception $e)
|
||||
{
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user