diff --git a/.gitignore b/.gitignore index 703812b..2cffa58 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ .phpunit.result.cache expose.php database/expose.db +.expose.php diff --git a/app/Client/Client.php b/app/Client/Client.php index 5bfc13f..a3b385f 100644 --- a/app/Client/Client.php +++ b/app/Client/Client.php @@ -41,7 +41,9 @@ class Client { $token = config('expose.auth_token'); - connect("ws://{$this->configuration->host()}:{$this->configuration->port()}/__expose_control__?authToken={$token}", [], [ + $protocol = $this->configuration->port() === 443 ? "wss" : "ws"; + + connect($protocol."://{$this->configuration->host()}:{$this->configuration->port()}/__expose_control__?authToken={$token}", [], [ 'X-Expose-Control' => 'enabled', ], $this->loop) ->then(function (WebSocket $clientConnection) use ($sharedUrl, $subdomain) { @@ -65,8 +67,10 @@ class Client static::$subdomains[] = "$data->subdomain.{$this->configuration->host()}:{$this->configuration->port()}"; }); - }, function ($e) { - echo "Could not connect: {$e->getMessage()}\n"; + }, function (\Exception $e) { + $this->logger->error("Could not connect to the server."); + $this->logger->error($e->getMessage()); + exit(1); }); } } diff --git a/app/Client/Configuration.php b/app/Client/Configuration.php index 423fdc0..6d78fc5 100644 --- a/app/Client/Configuration.php +++ b/app/Client/Configuration.php @@ -34,6 +34,6 @@ class Configuration public function port(): int { - return $this->port; + return intval($this->port); } } diff --git a/app/Client/ProxyManager.php b/app/Client/ProxyManager.php index a583e5c..d291f2c 100644 --- a/app/Client/ProxyManager.php +++ b/app/Client/ProxyManager.php @@ -24,7 +24,9 @@ class ProxyManager public function createProxy(string $clientId, $connectionData) { - connect("ws://{$this->configuration->host()}:{$this->configuration->port()}/__expose_control__", [], [ + $protocol = $this->configuration->port() === 443 ? "wss" : "ws"; + + connect($protocol."://{$this->configuration->host()}:{$this->configuration->port()}/__expose_control__", [], [ 'X-Expose-Control' => 'enabled', ], $this->loop) ->then(function (WebSocket $proxyConnection) use ($clientId, $connectionData) { diff --git a/app/Commands/ShareCommand.php b/app/Commands/ShareCommand.php index ed6c1c0..010103c 100644 --- a/app/Commands/ShareCommand.php +++ b/app/Commands/ShareCommand.php @@ -30,8 +30,8 @@ class ShareCommand extends Command (new Factory()) ->setLoop(app(LoopInterface::class)) -// ->setHost('beyond.sh') // TODO: Read from (local/global) config file -// ->setPort(8080) // TODO: Read from (local/global) config file + ->setHost(config('expose.host', 'localhost')) + ->setPort(config('expose.port', 8080)) ->setAuth($this->option('auth')) ->createClient($this->argument('host'), explode(',', $this->option('subdomain'))) ->createHttpServer() diff --git a/app/Commands/ShareCurrentWorkingDirectoryCommand.php b/app/Commands/ShareCurrentWorkingDirectoryCommand.php index 6a6b4d9..ffc7e1b 100644 --- a/app/Commands/ShareCurrentWorkingDirectoryCommand.php +++ b/app/Commands/ShareCurrentWorkingDirectoryCommand.php @@ -12,6 +12,8 @@ class ShareCurrentWorkingDirectoryCommand extends ShareCommand { $this->input->setArgument('host', basename(getcwd()).'.test'); + $this->input->setOption('subdomain', basename(getcwd())); + parent::handle(); } } diff --git a/app/HttpServer/Controllers/Controller.php b/app/HttpServer/Controllers/Controller.php index 052c08a..de8de21 100644 --- a/app/HttpServer/Controllers/Controller.php +++ b/app/HttpServer/Controllers/Controller.php @@ -5,6 +5,9 @@ namespace App\HttpServer\Controllers; use Exception; use Ratchet\ConnectionInterface; use Ratchet\Http\HttpServerInterface; +use Twig\Environment; +use Twig\Loader\ArrayLoader; +use function GuzzleHttp\Psr7\stream_for; abstract class Controller implements HttpServerInterface { @@ -22,4 +25,17 @@ abstract class Controller implements HttpServerInterface public function onMessage(ConnectionInterface $from, $msg) { } + + protected function getView(string $view, array $data) + { + $templatePath = implode(DIRECTORY_SEPARATOR, explode('.', $view)); + + $twig = new Environment( + new ArrayLoader([ + 'template' => file_get_contents(base_path('resources/views/'.$templatePath.'.twig')), + ]) + ); + + return stream_for($twig->render('template', $data)); + } } diff --git a/app/HttpServer/Controllers/DashboardController.php b/app/HttpServer/Controllers/DashboardController.php index 5ccc930..57ce024 100644 --- a/app/HttpServer/Controllers/DashboardController.php +++ b/app/HttpServer/Controllers/DashboardController.php @@ -16,19 +16,12 @@ class DashboardController extends Controller str(new Response( 200, ['Content-Type' => 'text/html'], - $this->getView() + $this->getView('client.dashboard', [ + 'subdomains' => Client::$subdomains, + ]) )) ); $connection->close(); } - - protected function getView(): string - { - $view = file_get_contents(base_path('resources/views/index.html')); - - $view = str_replace('%subdomains%', implode(' ', Client::$subdomains), $view); - - return $view; - } } diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index dfd25d8..28278f1 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -31,6 +31,14 @@ class AppServiceProvider extends ServiceProvider protected function loadConfigurationFile() { + $localConfigFile = getcwd() . DIRECTORY_SEPARATOR . '.expose.php'; + + if (file_exists($localConfigFile)) { + config()->set('expose', require_once $localConfigFile); + return; + } + + $configFile = implode(DIRECTORY_SEPARATOR, [ $_SERVER['HOME'], '.expose', @@ -39,12 +47,6 @@ class AppServiceProvider extends ServiceProvider 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); } } } diff --git a/app/Server/Factory.php b/app/Server/Factory.php index 0dd1f4d..0c14a5f 100644 --- a/app/Server/Factory.php +++ b/app/Server/Factory.php @@ -80,9 +80,12 @@ class Factory protected function addExposeRoutes() { + $wsServer = new WsServer(app(ControlMessageController::class)); + $wsServer->enableKeepAlive($this->loop); + $this->routes->add('control', new Route('/__expose_control__', [ - '_controller' => new WsServer(app(ControlMessageController::class)) + '_controller' => $wsServer ], [], [], null, [], [] ) ); diff --git a/app/Server/Http/Controllers/Admin/ListSitesController.php b/app/Server/Http/Controllers/Admin/ListSitesController.php index 42782b4..a3ab69e 100644 --- a/app/Server/Http/Controllers/Admin/ListSitesController.php +++ b/app/Server/Http/Controllers/Admin/ListSitesController.php @@ -27,18 +27,7 @@ class ListSitesController extends PostController public function handle(Request $request, ConnectionInterface $httpConnection) { $httpConnection->send( - respond_html($this->getView(['sites' => $this->connectionManager->getConnections()])) + respond_html($this->getView('server.sites.index', ['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)); - } } diff --git a/app/Server/Http/Controllers/Admin/ListUsersController.php b/app/Server/Http/Controllers/Admin/ListUsersController.php index ec73f2a..bd201db 100644 --- a/app/Server/Http/Controllers/Admin/ListUsersController.php +++ b/app/Server/Http/Controllers/Admin/ListUsersController.php @@ -29,7 +29,7 @@ class ListUsersController extends PostController { $this->database->query('SELECT * FROM users ORDER by created_at DESC')->then(function (Result $result) use ($httpConnection) { $httpConnection->send( - respond_html($this->getView(['users' => $result->rows])) + respond_html($this->getView('server.users.index', ['users' => $result->rows])) ); $httpConnection->close(); @@ -39,15 +39,4 @@ class ListUsersController extends PostController $httpConnection->close(); }); } - - protected function getView(array $data) - { - $twig = new Environment( - new ArrayLoader([ - 'template' => file_get_contents(base_path('resources/views/admin/users/index.twig')), - ]) - ); - - return stream_for($twig->render('template', $data)); - } } diff --git a/app/helpers.php b/app/helpers.php index c9227d7..827bb1d 100644 --- a/app/helpers.php +++ b/app/helpers.php @@ -3,11 +3,6 @@ use GuzzleHttp\Psr7\Response; use function GuzzleHttp\Psr7\str; -function expose_view_path(string $path = '') -{ - return base_path('resources/views/' . $path); -} - function respond_json($responseData, int $statusCode = 200) { return str(new Response( diff --git a/resources/views/index.html b/resources/views/client/dashboard.twig similarity index 91% rename from resources/views/index.html rename to resources/views/client/dashboard.twig index 892d790..c32bcf5 100644 --- a/resources/views/index.html +++ b/resources/views/client/dashboard.twig @@ -91,8 +91,11 @@
- Waiting for requests on: %subdomains% + Waiting for requests on: + {% for subdomain in subdomains %} + {{ subdomain }} + {% endfor %} +
- {{ log.request.method }} - {{ log.request.uri }} + @{ log.request.method } + @{ log.request.uri }
- {{ log.subdomain }} + @{ log.subdomain }- Status code: {{ currentLog.response?.status}} + Status code: @{ currentLog.response?.status}
{{ currentLog.request.body }}
+ @{ currentLog.request.body }
{{ currentLog.response.body }}
+ @{ currentLog.response.body }