mirror of
https://github.com/bitinflow/expose.git
synced 2026-03-13 13:35:54 +00:00
Rewrite location header
This commit is contained in:
@@ -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}";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
@@ -85,6 +93,8 @@ class HttpClient
|
||||
->send($request)
|
||||
->then(function (ResponseInterface $response) use ($proxyConnection) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
],
|
||||
|
||||
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user