Add ability to expose TCP connections (#123)

This commit is contained in:
Marcel Pociot
2020-09-08 16:27:39 +02:00
committed by GitHub
parent c8cfe7b8b4
commit 2f57fa1952
29 changed files with 1430 additions and 1159 deletions

View File

@@ -49,6 +49,11 @@ class Client
}
}
public function sharePort(int $port)
{
$this->connectToServerAndShareTcp($port, config('expose.auth_token'));
}
protected function prepareSharedUrl(string $sharedUrl): string
{
if (! $parsedUrl = parse_url($sharedUrl)) {
@@ -87,14 +92,12 @@ class Client
$clientConnection->on('close', function () use ($sharedUrl, $subdomain, $authToken) {
$this->logger->error('Connection to server closed.');
$this->retryConnectionOrExit($sharedUrl, $subdomain, $authToken);
$this->retryConnectionOrExit(function () use ($sharedUrl, $subdomain, $authToken) {
$this->connectToServer($sharedUrl, $subdomain, $authToken);
});
});
$connection->on('authenticationFailed', function ($data) use ($deferred) {
$this->logger->error($data->message);
$this->exit($deferred);
});
$this->attachCommonConnectionListeners($connection, $deferred);
$connection->on('subdomainTaken', function ($data) use ($deferred) {
$this->logger->error($data->message);
@@ -102,20 +105,6 @@ class Client
$this->exit($deferred);
});
$connection->on('setMaximumConnectionLength', function ($data) {
$timeoutSection = $this->logger->getOutput()->section();
$this->loop->addPeriodicTimer(1, function () use ($data, $timeoutSection) {
$this->timeConnected++;
$secondsRemaining = $data->length * 60 - $this->timeConnected;
$remaining = Carbon::now()->diff(Carbon::now()->addSeconds($secondsRemaining));
$timeoutSection->clear();
$timeoutSection->writeln('Remaining time: '.$remaining->format('%H:%I:%S'));
});
});
$connection->on('authenticated', function ($data) use ($deferred, $sharedUrl) {
$httpProtocol = $this->configuration->port() === 443 ? 'https' : 'http';
$host = $this->configuration->host();
@@ -136,7 +125,9 @@ class Client
});
}, function (\Exception $e) use ($deferred, $sharedUrl, $subdomain, $authToken) {
if ($this->connectionRetries > 0) {
$this->retryConnectionOrExit($sharedUrl, $subdomain, $authToken);
$this->retryConnectionOrExit(function () use ($sharedUrl, $subdomain, $authToken) {
$this->connectToServer($sharedUrl, $subdomain, $authToken);
});
return;
}
@@ -149,6 +140,84 @@ class Client
return $promise;
}
public function connectToServerAndShareTcp(int $port, $authToken = ''): PromiseInterface
{
$deferred = new Deferred();
$promise = $deferred->promise();
$wsProtocol = $this->configuration->port() === 443 ? 'wss' : 'ws';
connect($wsProtocol."://{$this->configuration->host()}:{$this->configuration->port()}/expose/control?authToken={$authToken}", [], [
'X-Expose-Control' => 'enabled',
], $this->loop)
->then(function (WebSocket $clientConnection) use ($port, $deferred, $authToken) {
$this->connectionRetries = 0;
$connection = ControlConnection::create($clientConnection);
$connection->authenticateTcp($port);
$this->attachCommonConnectionListeners($connection, $deferred);
$clientConnection->on('close', function () use ($port, $authToken) {
$this->logger->error('Connection to server closed.');
$this->retryConnectionOrExit(function () use ($port, $authToken) {
$this->connectToServerAndShareTcp($port, $authToken);
});
});
$connection->on('authenticated', function ($data) use ($deferred, $port) {
$host = $this->configuration->host();
$this->logger->info($data->message);
$this->logger->info("Local-Port:\t\t{$port}");
$this->logger->info("Shared-Port:\t\t{$data->shared_port}");
$this->logger->info("Expose-URL:\t\ttcp://{$host}:{$data->shared_port}.");
$this->logger->line('');
$deferred->resolve($data);
});
}, function (\Exception $e) use ($deferred, $port, $authToken) {
if ($this->connectionRetries > 0) {
$this->retryConnectionOrExit(function () use ($port, $authToken) {
$this->connectToServerAndShareTcp($port, $authToken);
});
return;
}
$this->logger->error('Could not connect to the server.');
$this->logger->error($e->getMessage());
$this->exit($deferred);
});
return $promise;
}
protected function attachCommonConnectionListeners(ControlConnection $connection, Deferred $deferred)
{
$connection->on('authenticationFailed', function ($data) use ($deferred) {
$this->logger->error($data->message);
$this->exit($deferred);
});
$connection->on('setMaximumConnectionLength', function ($data) {
$timeoutSection = $this->logger->getOutput()->section();
$this->loop->addPeriodicTimer(1, function () use ($data, $timeoutSection) {
$this->timeConnected++;
$secondsRemaining = $data->length * 60 - $this->timeConnected;
$remaining = Carbon::now()->diff(Carbon::now()->addSeconds($secondsRemaining));
$timeoutSection->clear();
$timeoutSection->writeln('Remaining time: '.$remaining->format('%H:%I:%S'));
});
});
}
protected function exit(Deferred $deferred)
{
$deferred->reject();
@@ -158,15 +227,15 @@ class Client
});
}
protected function retryConnectionOrExit(string $sharedUrl, $subdomain, $authToken = '')
protected function retryConnectionOrExit(callable $retry)
{
$this->connectionRetries++;
if ($this->connectionRetries <= static::MAX_CONNECTION_RETRIES) {
$this->loop->addTimer($this->connectionRetries, function () use ($sharedUrl, $subdomain, $authToken) {
$this->loop->addTimer($this->connectionRetries, function () use ($retry) {
$this->logger->info("Retrying connection ({$this->connectionRetries}/".static::MAX_CONNECTION_RETRIES.')');
$this->connectToServer($sharedUrl, $subdomain, $authToken);
$retry();
});
} else {
exit(1);