mirror of
https://github.com/bitinflow/expose.git
synced 2026-03-13 13:35:54 +00:00
wip
This commit is contained in:
@@ -2,8 +2,10 @@
|
||||
|
||||
namespace App\Server\Connections;
|
||||
|
||||
use GuzzleHttp\Psr7\Request;
|
||||
use Illuminate\Support\Str;
|
||||
use Ratchet\ConnectionInterface;
|
||||
use React\Stream\Util;
|
||||
|
||||
class Connection
|
||||
{
|
||||
@@ -22,28 +24,23 @@ class Connection
|
||||
$this->client_id = $clientId;
|
||||
}
|
||||
|
||||
public function setProxy(ConnectionInterface $proxy)
|
||||
public function registerProxy($requestId)
|
||||
{
|
||||
$this->proxies[] = $proxy;
|
||||
$this->socket->send(json_encode([
|
||||
'event' => 'createProxy',
|
||||
'request_id' => $requestId,
|
||||
'client_id' => $this->client_id,
|
||||
]) . "||");
|
||||
}
|
||||
|
||||
public function getProxy(): ?ConnectionInterface
|
||||
public function pipeRequestThroughProxy(HttpRequestConnection $httpConnection, string $requestId, Request $request)
|
||||
{
|
||||
return array_pop($this->proxies);
|
||||
}
|
||||
$this->registerProxy($requestId);
|
||||
|
||||
public function rewriteHostInformation($serverHost, $port, $requestId, string $data)
|
||||
{
|
||||
$appName = config('app.name');
|
||||
$appVersion = config('app.version');
|
||||
$this->socket->getConnection()->once('proxy_ready_' . $requestId, function (IoConnection $proxy) use ($request, $requestId, $httpConnection) {
|
||||
Util::pipe($proxy->getConnection(), $httpConnection->getConnection());
|
||||
|
||||
$originalHost = "{$this->subdomain}.{$serverHost}:{$port}";
|
||||
|
||||
$data = preg_replace('/Host: '.$this->subdomain.'.'.$serverHost.'(.*)\r\n/', "Host: {$this->host}\r\n" .
|
||||
"X-Exposed-By: {$appName} {$appVersion}\r\n" .
|
||||
"X-Expose-Request-ID: {$requestId}\r\n" .
|
||||
"X-Original-Host: {$originalHost}\r\n", $data);
|
||||
|
||||
return $data;
|
||||
$proxy->send(\GuzzleHttp\Psr7\str($request));
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
76
app/Server/Connections/HttpRequestConnection.php
Normal file
76
app/Server/Connections/HttpRequestConnection.php
Normal file
@@ -0,0 +1,76 @@
|
||||
<?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();
|
||||
}
|
||||
}
|
||||
@@ -11,7 +11,6 @@ class IoConnection implements ConnectionInterface {
|
||||
*/
|
||||
protected $conn;
|
||||
|
||||
|
||||
/**
|
||||
* @param \React\Socket\ConnectionInterface $conn
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user