This commit is contained in:
Marcel Pociot
2020-04-24 12:37:31 +02:00
parent 018d778e5f
commit 9ce19f975e
28 changed files with 1160 additions and 91 deletions

View File

@@ -3,6 +3,7 @@
namespace App\Client;
use App\Client\Connections\ControlConnection;
use App\Logger\CliRequestLogger;
use Ratchet\Client\WebSocket;
use React\EventLoop\LoopInterface;
use function Ratchet\Client\connect;
@@ -15,16 +16,22 @@ class Client
/** @var Configuration */
protected $configuration;
/** @var CliRequestLogger */
protected $logger;
public static $subdomains = [];
public function __construct(LoopInterface $loop, Configuration $configuration)
public function __construct(LoopInterface $loop, Configuration $configuration, CliRequestLogger $logger)
{
$this->loop = $loop;
$this->configuration = $configuration;
$this->logger = $logger;
}
public function share(string $sharedUrl, array $subdomains = [])
{
$this->logger->info("Sharing http://{$sharedUrl}");
foreach ($subdomains as $subdomain) {
$this->connectToServer($sharedUrl, $subdomain);
}
@@ -32,7 +39,7 @@ class Client
protected function connectToServer(string $sharedUrl, $subdomain)
{
connect("ws://{$this->configuration->host()}:{$this->configuration->port()}/__expose_control__", [], [
connect("ws://{$this->configuration->host()}:{$this->configuration->port()}/__expose_control__?authToken={$this->configuration->authToken()}", [], [
'X-Expose-Control' => 'enabled',
], $this->loop)
->then(function (WebSocket $clientConnection) use ($sharedUrl, $subdomain) {
@@ -40,8 +47,14 @@ class Client
$connection->authenticate($sharedUrl, $subdomain);
$connection->on('authenticationFailed', function ($data) {
$this->logger->error("Authentication failed. Please check your authentication token and try again.");
exit(1);
});
$connection->on('authenticated', function ($data) {
dump("Connected to http://$data->subdomain.{$this->configuration->host()}:{$this->configuration->port()}");
$this->logger->info("Connected to http://$data->subdomain.{$this->configuration->host()}:{$this->configuration->port()}");
static::$subdomains[] = "$data->subdomain.{$this->configuration->host()}:{$this->configuration->port()}";
});

View File

@@ -13,13 +13,18 @@ class Configuration
/** @var string|null */
protected $auth;
public function __construct(string $host, int $port, ?string $auth = null)
/** @var string|null */
protected $authToken;
public function __construct(string $host, int $port, ?string $auth = null, string $authToken = null)
{
$this->host = $host;
$this->port = $port;
$this->auth = $auth;
$this->authToken = $authToken;
}
public function host(): string
@@ -36,4 +41,9 @@ class Configuration
{
return $this->port;
}
public function authToken()
{
return $this->authToken;
}
}

View File

@@ -34,9 +34,8 @@ class ControlConnection
$this->socket->on('message', function (Message $message) {
$decodedEntry = json_decode($message);
$this->emit($decodedEntry->event ?? '', [$decodedEntry]);
if (method_exists($this, $decodedEntry->event ?? '')) {
$this->emit($decodedEntry->event, [$decodedEntry]);
call_user_func([$this, $decodedEntry->event], $decodedEntry);
}
});

View File

@@ -27,6 +27,9 @@ class Factory
/** @var string */
protected $auth = '';
/** @var string */
protected $authToken = '';
/** @var \React\EventLoop\LoopInterface */
protected $loop;
@@ -52,13 +55,20 @@ class Factory
return $this;
}
public function setAuth(string $auth)
public function setAuth(?string $auth)
{
$this->auth = $auth;
return $this;
}
public function setAuthToken(?string $authToken)
{
$this->authToken = $authToken;
return $this;
}
public function setLoop(LoopInterface $loop)
{
$this->loop = $loop;
@@ -69,7 +79,7 @@ class Factory
protected function bindConfiguration()
{
app()->singleton(Configuration::class, function ($app) {
return new Configuration($this->host, $this->port, $this->auth);
return new Configuration($this->host, $this->port, $this->auth, $this->authToken);
});
}

View File

@@ -47,8 +47,6 @@ class HttpClient
$request = $this->passRequestThroughModifiers(parse_request($requestData), $proxyConnection);
dump($this->request->getMethod() . ' ' . $this->request->getUri()->getPath());
transform($request, function ($request) use ($proxyConnection) {
$this->sendRequestToApplication($request, $proxyConnection);
});