This commit is contained in:
Marcel Pociot
2020-04-24 16:10:38 +02:00
19 changed files with 1873 additions and 1562 deletions

View File

@@ -39,7 +39,9 @@ class Client
protected function connectToServer(string $sharedUrl, $subdomain)
{
connect("ws://{$this->configuration->host()}:{$this->configuration->port()}/__expose_control__?authToken={$this->configuration->authToken()}", [], [
$token = config('expose.auth_token');
connect("ws://{$this->configuration->host()}:{$this->configuration->port()}/__expose_control__?authToken={$token}", [], [
'X-Expose-Control' => 'enabled',
], $this->loop)
->then(function (WebSocket $clientConnection) use ($sharedUrl, $subdomain) {
@@ -47,6 +49,11 @@ class Client
$connection->authenticate($sharedUrl, $subdomain);
$clientConnection->on('close', function() {
$this->logger->error('Connection to server closed.');
exit(1);
});
$connection->on('authenticationFailed', function ($data) {
$this->logger->error("Authentication failed. Please check your authentication token and try again.");
exit(1);

View File

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

View File

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

View File

@@ -14,8 +14,15 @@ class ServeCommand extends Command
public function handle()
{
/** @var LoopInterface $loop */
$loop = app(LoopInterface::class);
$loop->futureTick(function () {
$this->info("Expose server running.");
});
(new Factory())
->setLoop(app(LoopInterface::class))
->setLoop($loop)
->setHost($this->argument('host'))
->setHostname($this->argument('hostname'))
->validateAuthTokens($this->option('validateAuthTokens'))

View File

@@ -11,7 +11,7 @@ use Symfony\Component\Console\Output\ConsoleOutput;
class ShareCommand extends Command
{
protected $signature = 'share {host} {--subdomain=} {--auth=} {--token=}';
protected $signature = 'share {host} {--subdomain=} {--auth=}';
protected $description = 'Share a local url with a remote shaft server';
@@ -33,7 +33,6 @@ class ShareCommand extends Command
// ->setHost('beyond.sh') // TODO: Read from (local/global) config file
// ->setPort(8080) // TODO: Read from (local/global) config file
->setAuth($this->option('auth'))
->setAuthToken($this->option('token'))
->createClient($this->argument('host'), explode(',', $this->option('subdomain')))
->createHttpServer()
->run();

View File

@@ -0,0 +1,40 @@
<?php
namespace App\Commands;
use Illuminate\Console\Command;
class StoreAuthenticationTokenCommand extends Command
{
protected $signature = 'token {token?}';
protected $description = 'Set or retrieve the authentication token to use with expose.';
public function handle()
{
$config = config('expose', []);
if (!is_null($this->argument('token'))) {
$this->info('Setting the expose authentication token to "' . $this->argument('token') . '"');
$config['auth_token'] = $this->argument('token');
$configFile = implode(DIRECTORY_SEPARATOR, [
$_SERVER['HOME'],
'.expose',
'config.php'
]);
@mkdir(dirname($configFile), 0777, true);
file_put_contents($configFile, '<?php return ' . var_export($config, true) . ';');
return;
}
if (is_null($token = config('expose.auth_token'))) {
$this->info('There is no authentication token specified.');
} else {
$this->info('Current authentication token: ' . $token);
}
}
}

View File

@@ -18,4 +18,6 @@ interface ConnectionManager
public function findControlConnectionForSubdomain($subdomain): ?ControlConnection;
public function findControlConnectionForClientId(string $clientId): ?ControlConnection;
public function getConnections(): array;
}

View File

@@ -18,6 +18,8 @@ class AppServiceProvider extends ServiceProvider
public function register()
{
$this->loadConfigurationFile();
$this->app->singleton(LoopInterface::class, function () {
return LoopFactory::create();
});
@@ -26,4 +28,23 @@ class AppServiceProvider extends ServiceProvider
return new RequestLogger($app->make(Browser::class), $app->make(CliRequestLogger::class));
});
}
protected function loadConfigurationFile()
{
$configFile = implode(DIRECTORY_SEPARATOR, [
$_SERVER['HOME'],
'.expose',
'config.php'
]);
if (file_exists($configFile)) {
config()->set('expose', require_once $configFile);
return;
}
$localConfigFile = getcwd() . DIRECTORY_SEPARATOR . '.expose.php';
if (file_exists($localConfigFile)) {
config()->set('expose', require_once $localConfigFile);
}
}
}

View File

@@ -74,4 +74,9 @@ class ConnectionManager implements ConnectionManagerContract
return $connection->client_id == $clientId;
});
}
public function getConnections(): array
{
return $this->connections;
}
}

View File

@@ -30,6 +30,7 @@ class ControlConnection
$this->host = $host;
$this->subdomain = $subdomain;
$this->client_id = $clientId;
$this->shared_at = now()->toDateTimeString();
}
public function registerProxy($requestId)

View File

@@ -7,6 +7,7 @@ use App\Contracts\SubdomainGenerator;
use App\HttpServer\HttpServer;
use App\Server\Connections\ConnectionManager;
use App\Server\Http\Controllers\Admin\DeleteUsersController;
use App\Server\Http\Controllers\Admin\ListSitesController;
use App\Server\Http\Controllers\Admin\ListUsersController;
use App\Server\Http\Controllers\Admin\StoreUsersController;
use App\Server\Http\Controllers\ControlMessageController;
@@ -113,6 +114,12 @@ class Factory
'_controller' => app(DeleteUsersController::class),
], [], [], null, [], ['DELETE'])
);
$this->routes->add('admin.sites.index',
new Route('/expose/sites', [
'_controller' => app(ListSitesController::class),
], [], [], null, [], ['GET'])
);
}
protected function bindConfiguration()
@@ -190,7 +197,7 @@ class Factory
public function validateAuthTokens(bool $validate)
{
dump($validate);
config()->set('expose.validate_auth_tokens', $validate);
return $this;
}

View File

@@ -0,0 +1,44 @@
<?php
namespace App\Server\Http\Controllers\Admin;
use App\Contracts\ConnectionManager;
use App\HttpServer\Controllers\PostController;
use Clue\React\SQLite\DatabaseInterface;
use Clue\React\SQLite\Result;
use GuzzleHttp\Psr7\Response;
use Illuminate\Http\Request;
use Ratchet\ConnectionInterface;
use Twig\Environment;
use Twig\Loader\ArrayLoader;
use function GuzzleHttp\Psr7\str;
use function GuzzleHttp\Psr7\stream_for;
class ListSitesController extends PostController
{
/** @var ConnectionManager */
protected $connectionManager;
public function __construct(ConnectionManager $connectionManager)
{
$this->connectionManager = $connectionManager;
}
public function handle(Request $request, ConnectionInterface $httpConnection)
{
$httpConnection->send(
respond_html($this->getView(['sites' => $this->connectionManager->getConnections()]))
);
}
protected function getView(array $data)
{
$twig = new Environment(
new ArrayLoader([
'template' => file_get_contents(base_path('resources/views/admin/sites/index.twig')),
])
);
return stream_for($twig->render('template', $data));
}
}

View File

@@ -30,7 +30,6 @@ class ControlMessageController implements MessageComponentInterface
*/
function onOpen(ConnectionInterface $connection)
{
$this->verifyAuthToken($connection);
}
/**
@@ -75,6 +74,10 @@ class ControlMessageController implements MessageComponentInterface
protected function authenticate(ConnectionInterface $connection, $data)
{
if (config('expose.validate_auth_tokens') === true) {
$this->verifyAuthToken($connection);
}
$connectionInfo = $this->connectionManager->storeConnection($data->host, $data->subdomain, $connection);
$connection->send(json_encode([