Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Store host port in connection data fixes beyondcode|expose#42 #378

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions app/Client/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,17 +93,19 @@ public function connectToServer(string $sharedUrl, $subdomain, $serverHost = nul

$exposeVersion = config('app.version');

$serverPort = $this->configuration->port();

$wsProtocol = $this->configuration->port() === 443 ? 'wss' : 'ws';

connect($wsProtocol."://{$this->configuration->host()}:{$this->configuration->port()}/expose/control?authToken={$authToken}&version={$exposeVersion}", [], [
'X-Expose-Control' => 'enabled',
], $this->loop)
->then(function (WebSocket $clientConnection) use ($sharedUrl, $subdomain, $serverHost, $deferred, $authToken) {
->then(function (WebSocket $clientConnection) use ($sharedUrl, $subdomain, $serverHost, $serverPort, $deferred, $authToken) {
$this->connectionRetries = 0;

$connection = ControlConnection::create($clientConnection);

$connection->authenticate($sharedUrl, $subdomain, $serverHost);
$connection->authenticate($sharedUrl, $subdomain, $serverHost, $serverPort);

$clientConnection->on('close', function () use ($sharedUrl, $subdomain, $serverHost, $authToken) {
$this->logger->error('Connection to server closed.');
Expand Down
3 changes: 2 additions & 1 deletion app/Client/Connections/ControlConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,15 @@ public function createTcpProxy($data)
$this->proxyManager->createTcpProxy($this->clientId, $data);
}

public function authenticate(string $sharedHost, string $subdomain, $serverHost = null)
public function authenticate(string $sharedHost, string $subdomain, $serverHost = null, $serverPort = null)
{
$this->socket->send(json_encode([
'event' => 'authenticate',
'data' => [
'type' => 'http',
'host' => $sharedHost,
'server_host' => $serverHost,
'server_port' => $serverPort,
'subdomain' => empty($subdomain) ? null : $subdomain,
],
]));
Expand Down
2 changes: 1 addition & 1 deletion app/Contracts/ConnectionManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

interface ConnectionManager
{
public function storeConnection(string $host, ?string $subdomain, ?string $serverHost, ConnectionInterface $connection): ControlConnection;
public function storeConnection(string $host, ?string $subdomain, ?string $serverHost, ?string $serverPort, ConnectionInterface $connection): ControlConnection;

public function storeTcpConnection(int $port, ConnectionInterface $connection): ControlConnection;

Expand Down
3 changes: 2 additions & 1 deletion app/Server/Connections/ConnectionManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function limitConnectionLength(ControlConnection $connection, int $maximu
});
}

public function storeConnection(string $host, ?string $subdomain, ?string $serverHost, ConnectionInterface $connection): ControlConnection
public function storeConnection(string $host, ?string $subdomain, ?string $serverHost, ?string $serverPort, ConnectionInterface $connection): ControlConnection
{
$clientId = (string) uniqid();

Expand All @@ -65,6 +65,7 @@ public function storeConnection(string $host, ?string $subdomain, ?string $serve
$subdomain ?? $this->subdomainGenerator->generateSubdomain(),
$clientId,
$serverHost,
$serverPort,
$this->getAuthTokenFromConnection($connection)
);

Expand Down
5 changes: 4 additions & 1 deletion app/Server/Connections/ControlConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class ControlConnection
public $socket;
public $host;
public $serverHost;
public $serverPort;
public $authToken;
public $subdomain;
public $client_id;
Expand All @@ -22,14 +23,15 @@ class ControlConnection
public $proxies = [];
protected $shared_at;

public function __construct(ConnectionInterface $socket, string $host, string $subdomain, string $clientId, string $serverHost, string $authToken = '')
public function __construct(ConnectionInterface $socket, string $host, string $subdomain, string $clientId, string $serverHost, string $serverPort = '', string $authToken = '')
{
$this->socket = $socket;
$this->host = $host;
$this->subdomain = $subdomain;
$this->client_id = $clientId;
$this->authToken = $authToken;
$this->serverHost = $serverHost;
$this->serverPort = $serverPort;
$this->shared_at = now()->toDateTimeString();
$this->client_version = QueryParameters::create($socket->httpRequest)->get('version');
}
Expand Down Expand Up @@ -69,6 +71,7 @@ public function toArray()
'host' => $this->host,
'remote_address' => $this->socket->remoteAddress ?? null,
'server_host' => $this->serverHost,
'server_port' => $this->serverPort,
'client_id' => $this->client_id,
'client_version' => $this->client_version,
'auth_token' => $this->authToken,
Expand Down
2 changes: 1 addition & 1 deletion app/Server/Http/Controllers/ControlMessageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ protected function handleHttpConnection(ConnectionInterface $connection, $data,

$data->subdomain = $subdomain;

$connectionInfo = $this->connectionManager->storeConnection($data->host, $data->subdomain, $data->server_host, $connection);
$connectionInfo = $this->connectionManager->storeConnection($data->host, $data->subdomain, $data->server_host, $data->server_port, $connection);

$this->connectionManager->limitConnectionLength($connectionInfo, config('expose.admin.maximum_connection_length'));

Expand Down
2 changes: 1 addition & 1 deletion resources/views/server/sites/index.twig
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
@{ site.host }
</td>
<td class="px-6 py-4 whitespace-no-wrap border-b border-gray-200 text-sm leading-5 text-gray-500">
@{ site.subdomain }.{{ configuration.hostname()}}:{{ configuration.port() }}
@{ site.subdomain }.@{ site.server_host}:@{ site.server_port }
</td>
<td class="px-6 py-4 whitespace-no-wrap border-b border-gray-200 text-sm leading-5 text-gray-500">
@{ site.user?.name }
Expand Down