From 880259657fb8a618c11db4a37891cdc6fb3e7384 Mon Sep 17 00:00:00 2001 From: Marcel Pociot Date: Sun, 25 Oct 2020 23:40:45 +0100 Subject: [PATCH] Rewrite location header --- app/Client/Configuration.php | 12 +++++++ app/Client/Http/HttpClient.php | 37 ++++++++++++++++++-- app/Client/ProxyManager.php | 6 ++-- app/Server/Connections/ControlConnection.php | 2 ++ tests/Feature/Client/DashboardTest.php | 5 +++ 5 files changed, 56 insertions(+), 6 deletions(-) diff --git a/app/Client/Configuration.php b/app/Client/Configuration.php index 6d78fc5..069861e 100644 --- a/app/Client/Configuration.php +++ b/app/Client/Configuration.php @@ -36,4 +36,16 @@ class Configuration { return intval($this->port); } + + public function getUrl(string $subdomain): string + { + $httpProtocol = $this->port() === 443 ? 'https' : 'http'; + $host = $this->host(); + + if ($httpProtocol !== 'https') { + $host .= ":{$this->port()}"; + } + + return "{$subdomain}.{$host}"; + } } diff --git a/app/Client/Http/HttpClient.php b/app/Client/Http/HttpClient.php index dd48868..e9146e5 100644 --- a/app/Client/Http/HttpClient.php +++ b/app/Client/Http/HttpClient.php @@ -2,6 +2,7 @@ namespace App\Client\Http; +use App\Client\Configuration; use App\Client\Http\Modifiers\CheckBasicAuthentication; use App\Logger\RequestLogger; use Clue\React\Buzz\Browser; @@ -26,19 +27,26 @@ class HttpClient /** @var Request */ protected $request; + protected $connectionData; + /** @var array */ protected $modifiers = [ CheckBasicAuthentication::class, ]; + /** @var Configuration */ + protected $configuration; - public function __construct(LoopInterface $loop, RequestLogger $logger) + public function __construct(LoopInterface $loop, RequestLogger $logger, Configuration $configuration) { $this->loop = $loop; $this->logger = $logger; + $this->configuration = $configuration; } - public function performRequest(string $requestData, WebSocket $proxyConnection = null, string $requestId = null) + public function performRequest(string $requestData, WebSocket $proxyConnection = null, $connectionData = null) { + $this->connectionData = $connectionData; + $this->request = $this->parseRequest($requestData); $this->logger->logRequest($requestData, $this->request); @@ -84,7 +92,9 @@ class HttpClient ]) ->send($request) ->then(function (ResponseInterface $response) use ($proxyConnection) { - if (! isset($response->buffer)) { + if (!isset($response->buffer)) { + $response = $this->rewriteResponseHeaders($response); + $response->buffer = str($response); } @@ -126,4 +136,25 @@ class HttpClient { return Request::fromString($data); } + + protected function rewriteResponseHeaders(ResponseInterface $response) + { + if (!$response->hasHeader('Location')) { + return $response; + } + + $location = $response->getHeaderLine('Location'); + + if (!strstr($location, $this->connectionData->host)) { + return $response; + } + + $location = str_replace( + $this->connectionData->host, + $this->configuration->getUrl($this->connectionData->subdomain), + $location + ); + + return $response->withHeader('Location', $location); + } } diff --git a/app/Client/ProxyManager.php b/app/Client/ProxyManager.php index b59664d..1b4ac35 100644 --- a/app/Client/ProxyManager.php +++ b/app/Client/ProxyManager.php @@ -32,7 +32,7 @@ class ProxyManager ], $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); + $this->performRequest($proxyConnection, (string) $message, $connectionData); }); $proxyConnection->send(json_encode([ @@ -76,8 +76,8 @@ class ProxyManager }); } - protected function performRequest(WebSocket $proxyConnection, $requestId, string $requestData) + protected function performRequest(WebSocket $proxyConnection, string $requestData, $connectionData) { - app(HttpClient::class)->performRequest((string) $requestData, $proxyConnection, $requestId); + app(HttpClient::class)->performRequest((string) $requestData, $proxyConnection, $connectionData); } } diff --git a/app/Server/Connections/ControlConnection.php b/app/Server/Connections/ControlConnection.php index b95c8a9..80b1d31 100644 --- a/app/Server/Connections/ControlConnection.php +++ b/app/Server/Connections/ControlConnection.php @@ -43,6 +43,8 @@ class ControlConnection $this->socket->send(json_encode([ 'event' => 'createProxy', 'data' => [ + 'host' => $this->host, + 'subdomain' => $this->subdomain, 'request_id' => $requestId, 'client_id' => $this->client_id, ], diff --git a/tests/Feature/Client/DashboardTest.php b/tests/Feature/Client/DashboardTest.php index ea2a557..cb91b81 100755 --- a/tests/Feature/Client/DashboardTest.php +++ b/tests/Feature/Client/DashboardTest.php @@ -2,6 +2,7 @@ namespace Tests\Feature\Client; +use App\Client\Configuration; use App\Client\Factory; use App\Client\Http\HttpClient; use App\Logger\LoggedRequest; @@ -129,6 +130,10 @@ class DashboardTest extends TestCase protected function startDashboard() { + app()->singleton(Configuration::class, function ($app) { + return new Configuration('localhost', '8080', false); + }); + $this->dashboardFactory = (new Factory()) ->setLoop($this->loop) ->createHttpServer();