This commit is contained in:
Marcel Pociot
2020-04-30 15:17:25 +02:00
parent 0dfda0825c
commit a972c8581c
24 changed files with 444 additions and 29 deletions

View File

@@ -6,6 +6,7 @@ use App\Client\Connections\ControlConnection;
use App\Logger\CliRequestLogger;
use Ratchet\Client\WebSocket;
use React\EventLoop\LoopInterface;
use React\Promise\PromiseInterface;
use function Ratchet\Client\connect;
class Client
@@ -37,8 +38,11 @@ class Client
}
}
protected function connectToServer(string $sharedUrl, $subdomain)
public function connectToServer(string $sharedUrl, $subdomain): PromiseInterface
{
$deferred = new \React\Promise\Deferred();
$promise = $deferred->promise();
$token = config('expose.auth_token');
$wsProtocol = $this->configuration->port() === 443 ? "wss" : "ws";
@@ -46,7 +50,7 @@ class Client
connect($wsProtocol."://{$this->configuration->host()}:{$this->configuration->port()}/expose/control?authToken={$token}", [], [
'X-Expose-Control' => 'enabled',
], $this->loop)
->then(function (WebSocket $clientConnection) use ($sharedUrl, $subdomain) {
->then(function (WebSocket $clientConnection) use ($sharedUrl, $subdomain, $deferred) {
$connection = ControlConnection::create($clientConnection);
$connection->authenticate($sharedUrl, $subdomain);
@@ -66,7 +70,7 @@ class Client
exit(1);
});
$connection->on('authenticated', function ($data) {
$connection->on('authenticated', function ($data) use ($deferred) {
$httpProtocol = $this->configuration->port() === 443 ? "https" : "http";
$host = $this->configuration->host();
@@ -77,12 +81,19 @@ class Client
$this->logger->info("Connected to {$httpProtocol}://{$data->subdomain}.{$host}");
static::$subdomains[] = "$data->subdomain.{$this->configuration->host()}:{$this->configuration->port()}";
$deferred->resolve();
});
}, function (\Exception $e) {
}, function (\Exception $e) use ($deferred) {
$this->logger->error("Could not connect to the server.");
$this->logger->error($e->getMessage());
$deferred->reject();
exit(1);
});
return $promise;
}
}

View File

@@ -86,12 +86,17 @@ class Factory
});
}
public function createClient($sharedUrl, $subdomain = null, $auth = null)
public function createClient()
{
$this->bindConfiguration();
$this->bindProxyManager();
return $this;
}
public function share($sharedUrl, $subdomain = null)
{
app(Client::class)->share($sharedUrl, $subdomain);
return $this;
@@ -131,7 +136,7 @@ class Factory
echo("Started Dashboard on port {$dashboardPort}" . PHP_EOL);
echo('If the dashboard does not automatically open, visit: ' . $dashboardUrl . PHP_EOL);
echo('You can visit the dashboard at: ' . $dashboardUrl . PHP_EOL);
});
$this->app = new App('127.0.0.1', $dashboardPort, '0.0.0.0', $this->loop);

View File

@@ -15,7 +15,7 @@ class DashboardController extends Controller
public function handle(Request $request, ConnectionInterface $httpConnection)
{
$httpConnection->send(respond_html($this->getView('client.dashboard', [
$httpConnection->send(respond_html($this->getView($httpConnection, 'client.dashboard', [
'subdomains' => Client::$subdomains,
])));
}

View File

@@ -33,7 +33,8 @@ class ShareCommand extends Command
->setHost(config('expose.host', 'localhost'))
->setPort(config('expose.port', 8080))
->setAuth($this->option('auth'))
->createClient($this->argument('host'), explode(',', $this->option('subdomain')))
->createClient()
->share($this->argument('host'), explode(',', $this->option('subdomain')))
->createHttpServer()
->run();
}

View File

@@ -2,13 +2,14 @@
namespace App\Http\Controllers\Concerns;
use Ratchet\ConnectionInterface;
use Twig\Environment;
use Twig\Loader\ArrayLoader;
use function GuzzleHttp\Psr7\stream_for;
trait LoadsViews
{
protected function getView(string $view, array $data = [])
protected function getView(ConnectionInterface $connection, string $view, array $data = [])
{
$templatePath = implode(DIRECTORY_SEPARATOR, explode('.', $view));
@@ -19,6 +20,10 @@ trait LoadsViews
])
);
$data = array_merge($data, [
'request' => $connection->laravelRequest ?? null,
]);
return stream_for($twig->render('template', $data));
}
}

View File

@@ -26,10 +26,10 @@ trait ParsesIncomingRequest
protected function checkContentLength(ConnectionInterface $connection)
{
if (strlen($connection->requestBuffer) === $connection->contentLength) {
$laravelRequest = $this->createLaravelRequest($connection);
$connection->laravelRequest = $this->createLaravelRequest($connection);
if ($this->shouldHandleRequest($laravelRequest, $connection)) {
$this->handle($laravelRequest, $connection);
if ($this->shouldHandleRequest($connection->laravelRequest, $connection)) {
$this->handle($connection->laravelRequest, $connection);
}
if (!$this->keepConnectionOpen) {

View File

@@ -12,7 +12,8 @@ use function GuzzleHttp\Psr7\parse_request;
abstract class Controller implements HttpServerInterface
{
use LoadsViews, ParsesIncomingRequest;
use LoadsViews;
use ParsesIncomingRequest;
protected $keepConnectionOpen = false;
@@ -29,6 +30,7 @@ abstract class Controller implements HttpServerInterface
public function onClose(ConnectionInterface $connection)
{
unset($connection->laravelRequest);
unset($connection->requestBuffer);
unset($connection->contentLength);
unset($connection->request);

View File

@@ -26,4 +26,15 @@ class Configuration
{
return $this->port;
}
public function __isset($key)
{
return property_exists($this, $key) || ! is_null(config('expose.admin.'.$key));
}
public function __get($key)
{
dump(config('expose.admin'));
return $this->$key ?? config('expose.admin.'.$key);
}
}

View File

@@ -11,6 +11,8 @@ use App\Server\Http\Controllers\Admin\ListSitesController;
use App\Server\Http\Controllers\Admin\ListUsersController;
use App\Server\Http\Controllers\Admin\LoginController;
use App\Server\Http\Controllers\Admin\RedirectToUsersController;
use App\Server\Http\Controllers\Admin\SaveSettingsController;
use App\Server\Http\Controllers\Admin\ShowSettingsController;
use App\Server\Http\Controllers\Admin\StoreUsersController;
use App\Server\Http\Controllers\Admin\VerifyLoginController;
use App\Server\Http\Controllers\ControlMessageController;
@@ -114,6 +116,8 @@ class Factory
$this->router->get('/', RedirectToUsersController::class, $adminCondition);
$this->router->get('/users', ListUsersController::class, $adminCondition);
$this->router->get('/settings', ShowSettingsController::class, $adminCondition);
$this->router->post('/settings', SaveSettingsController::class, $adminCondition);
$this->router->post('/users', StoreUsersController::class, $adminCondition);
$this->router->delete('/users/delete/{id}', DeleteUsersController::class, $adminCondition);
$this->router->get('/sites', ListSitesController::class, $adminCondition);
@@ -183,7 +187,7 @@ class Factory
{
app()->singleton(DatabaseInterface::class, function() {
$factory = new \Clue\React\SQLite\Factory($this->loop);
return $factory->openLazy(base_path('database/expose.db'));
return $factory->openLazy(config('expose.admin.database', ':memory:'));
});
return $this;
@@ -210,7 +214,7 @@ class Factory
public function validateAuthTokens(bool $validate)
{
config()->set('expose.validate_auth_tokens', $validate);
config()->set('expose.admin.validate_auth_tokens', $validate);
return $this;
}

View File

@@ -31,7 +31,7 @@ class ListSitesController extends AdminController
public function handle(Request $request, ConnectionInterface $httpConnection)
{
try {
$sites = $this->getView('server.sites.index', [
$sites = $this->getView($httpConnection, 'server.sites.index', [
'scheme' => $this->configuration->port() === 443 ? 'https' : 'http',
'configuration' => $this->configuration,
'sites' => $this->connectionManager->getConnections()

View File

@@ -29,7 +29,7 @@ class ListUsersController extends AdminController
{
$this->database->query('SELECT * FROM users ORDER by created_at DESC')->then(function (Result $result) use ($httpConnection) {
$httpConnection->send(
respond_html($this->getView('server.users.index', ['users' => $result->rows]))
respond_html($this->getView($httpConnection, 'server.users.index', ['users' => $result->rows]))
);
$httpConnection->close();

View File

@@ -0,0 +1,40 @@
<?php
namespace App\Server\Http\Controllers\Admin;
use App\Contracts\ConnectionManager;
use App\Http\Controllers\Controller;
use App\Server\Configuration;
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 SaveSettingsController extends AdminController
{
/** @var ConnectionManager */
protected $connectionManager;
/** @var Configuration */
protected $configuration;
public function __construct(ConnectionManager $connectionManager, Configuration $configuration)
{
$this->connectionManager = $connectionManager;
$this->configuration = $configuration;
}
public function handle(Request $request, ConnectionInterface $httpConnection)
{
config()->set('expose.admin.validate_auth_tokens', $request->has('validate_auth_tokens'));
$httpConnection->send(str(new Response(301, [
'Location' => '/settings'
])));
}
}

View File

@@ -0,0 +1,40 @@
<?php
namespace App\Server\Http\Controllers\Admin;
use App\Contracts\ConnectionManager;
use App\Http\Controllers\Controller;
use App\Server\Configuration;
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 ShowSettingsController extends AdminController
{
/** @var ConnectionManager */
protected $connectionManager;
/** @var Configuration */
protected $configuration;
public function __construct(ConnectionManager $connectionManager, Configuration $configuration)
{
$this->connectionManager = $connectionManager;
$this->configuration = $configuration;
}
public function handle(Request $request, ConnectionInterface $httpConnection)
{
$httpConnection->send(
respond_html($this->getView($httpConnection, 'server.settings.index', [
'configuration' => $this->configuration,
]))
);
}
}

View File

@@ -74,7 +74,7 @@ class ControlMessageController implements MessageComponentInterface
protected function authenticate(ConnectionInterface $connection, $data)
{
if (config('expose.validate_auth_tokens') === true) {
if (config('expose.admin.validate_auth_tokens') === true) {
$this->verifyAuthToken($connection);
}

View File

@@ -44,7 +44,7 @@ class TunnelMessageController extends Controller
if (is_null($controlConnection)) {
$httpConnection->send(
respond_html($this->getView('server.errors.404', ['subdomain' => $subdomain]))
respond_html($this->getView($httpConnection, 'server.errors.404', ['subdomain' => $subdomain]), 404)
);
$httpConnection->close();
return;