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

1
.gitignore vendored
View File

@@ -2,6 +2,7 @@
/.idea
/.vscode
/.vagrant
/composer.lock
.phpunit.result.cache
expose.php
database/expose.db

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([

View File

@@ -6,6 +6,7 @@
"config",
"vendor"
],
"exclude-dev-files": false,
"files": [
"composer.json"
],

View File

@@ -17,7 +17,11 @@
],
"require": {
"php": "^7.2.5",
"ext-json": "*",
"ext-json": "*"
},
"require-dev": {
"mockery/mockery": "^1.3.1",
"phpunit/phpunit": "^8.5",
"bfunky/http-parser": "^2.2",
"cboden/ratchet": "^0.4.2",
"clue/buzz-react": "^2.7",
@@ -39,10 +43,6 @@
"symfony/psr-http-message-bridge": "^2.0",
"twig/twig": "^3.0"
},
"require-dev": {
"mockery/mockery": "^1.3.1",
"phpunit/phpunit": "^8.5"
},
"autoload": {
"psr-4": {
"App\\": "app/"
@@ -68,5 +68,5 @@
},
"minimum-stability": "dev",
"prefer-stable": true,
"bin": ["expose"]
"bin": ["builds/expose"]
}

3053
composer.lock generated

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,194 @@
<html>
<head>
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.17/dist/vue.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@tailwindcss/ui@latest/dist/tailwind-ui.min.css">
</head>
<body>
<div class="min-h-screen bg-white" id="app">
<nav class="bg-white border-b border-gray-200">
<div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
<div class="flex justify-between h-16">
<div class="flex">
<div class="flex-shrink-0 flex items-center font-bold">
Expose
</div>
<div class="hidden sm:-my-px sm:ml-6 sm:flex">
<a href="/expose/users"
class="inline-flex items-center px-1 pt-1 border-b-2 border-transparent text-sm font-medium leading-5 text-gray-500 hover:text-gray-700 hover:border-gray-300 focus:outline-none focus:text-gray-700 focus:border-gray-300 transition duration-150 ease-in-out">
Users
</a>
<a href="#"
class="ml-8 inline-flex items-center px-1 pt-1 border-b-2 border-indigo-500 text-sm font-medium leading-5 text-gray-900 focus:outline-none focus:border-indigo-700 transition duration-150 ease-in-out">
Shared sites
</a>
<a href="#"
class="ml-8 inline-flex items-center px-1 pt-1 border-b-2 border-transparent text-sm font-medium leading-5 text-gray-500 hover:text-gray-700 hover:border-gray-300 focus:outline-none focus:text-gray-700 focus:border-gray-300 transition duration-150 ease-in-out">
Settings
</a>
</div>
</div>
<div class="-mr-2 flex items-center sm:hidden">
<!-- Mobile menu button -->
<button
class="inline-flex items-center justify-center p-2 rounded-md text-gray-400 hover:text-gray-500 hover:bg-gray-100 focus:outline-none focus:bg-gray-100 focus:text-gray-500 transition duration-150 ease-in-out">
<!-- Menu open: "hidden", Menu closed: "block" -->
<svg class="block h-6 w-6" stroke="currentColor" fill="none" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
d="M4 6h16M4 12h16M4 18h16"/>
</svg>
<!-- Menu open: "block", Menu closed: "hidden" -->
<svg class="hidden h-6 w-6" stroke="currentColor" fill="none" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
d="M6 18L18 6M6 6l12 12"/>
</svg>
</button>
</div>
</div>
</div>
<!--
Mobile menu, toggle classes based on menu state.
Open: "block", closed: "hidden"
-->
<div class="hidden sm:hidden">
<div class="pt-2 pb-3">
<a href="#"
class="block pl-3 pr-4 py-2 border-l-4 border-indigo-500 text-base font-medium text-indigo-700 bg-indigo-50 focus:outline-none focus:text-indigo-800 focus:bg-indigo-100 focus:border-indigo-700 transition duration-150 ease-in-out">Dashboard</a>
<a href="#"
class="mt-1 block pl-3 pr-4 py-2 border-l-4 border-transparent text-base font-medium text-gray-600 hover:text-gray-800 hover:bg-gray-50 hover:border-gray-300 focus:outline-none focus:text-gray-800 focus:bg-gray-50 focus:border-gray-300 transition duration-150 ease-in-out">Team</a>
<a href="#"
class="mt-1 block pl-3 pr-4 py-2 border-l-4 border-transparent text-base font-medium text-gray-600 hover:text-gray-800 hover:bg-gray-50 hover:border-gray-300 focus:outline-none focus:text-gray-800 focus:bg-gray-50 focus:border-gray-300 transition duration-150 ease-in-out">Projects</a>
<a href="#"
class="mt-1 block pl-3 pr-4 py-2 border-l-4 border-transparent text-base font-medium text-gray-600 hover:text-gray-800 hover:bg-gray-50 hover:border-gray-300 focus:outline-none focus:text-gray-800 focus:bg-gray-50 focus:border-gray-300 transition duration-150 ease-in-out">Calendar</a>
</div>
<div class="pt-4 pb-3 border-t border-gray-200">
<div class="flex items-center px-4">
<div class="flex-shrink-0">
<img class="h-10 w-10 rounded-full"
src="https://images.unsplash.com/photo-1472099645785-5658abf4ff4e?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=facearea&facepad=2&w=256&h=256&q=80"
alt=""/>
</div>
<div class="ml-3">
<div class="text-base font-medium leading-6 text-gray-800">Tom Cook</div>
<div class="text-sm font-medium leading-5 text-gray-500">tom@example.com</div>
</div>
</div>
<div class="mt-3" role="menu" aria-orientation="vertical" aria-labelledby="user-menu">
<a href="#"
class="block px-4 py-2 text-base font-medium text-gray-500 hover:text-gray-800 hover:bg-gray-100 focus:outline-none focus:text-gray-800 focus:bg-gray-100 transition duration-150 ease-in-out"
role="menuitem">Your Profile</a>
<a href="#"
class="mt-1 block px-4 py-2 text-base font-medium text-gray-500 hover:text-gray-800 hover:bg-gray-100 focus:outline-none focus:text-gray-800 focus:bg-gray-100 transition duration-150 ease-in-out"
role="menuitem">Settings</a>
<a href="#"
class="mt-1 block px-4 py-2 text-base font-medium text-gray-500 hover:text-gray-800 hover:bg-gray-100 focus:outline-none focus:text-gray-800 focus:bg-gray-100 transition duration-150 ease-in-out"
role="menuitem">Sign out</a>
</div>
</div>
</div>
</nav>
<div class="py-10">
<header>
<div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
<h1 class="text-3xl font-bold leading-tight text-gray-900">
Sites
</h1>
</div>
</header>
<main>
<div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
<div class="flex flex-col py-8">
<div class="-my-2 py-2 overflow-x-auto sm:-mx-6 sm:px-6 lg:-mx-8 lg:px-8">
<div
class="align-middle inline-block min-w-full shadow overflow-hidden sm:rounded-lg border-b border-gray-200">
<table class="min-w-full">
<thead>
<tr>
<th class="px-6 py-3 border-b border-gray-200 bg-gray-50 text-left text-xs leading-4 font-medium text-gray-500 uppercase tracking-wider">
Host
</th>
<th class="px-6 py-3 border-b border-gray-200 bg-gray-50 text-left text-xs leading-4 font-medium text-gray-500 uppercase tracking-wider">
Subdomain
</th>
<th class="px-6 py-3 border-b border-gray-200 bg-gray-50 text-left text-xs leading-4 font-medium text-gray-500 uppercase tracking-wider">
Shared At
</th>
<th class="px-6 py-3 border-b border-gray-200 bg-gray-50"></th>
</tr>
</thead>
<tbody class="bg-white">
{% for site in sites %}
<tr>
<td class="px-6 py-4 whitespace-no-wrap border-b border-gray-200 text-sm leading-5 font-medium text-gray-900">
{{ site.host }}
</td>
<td class="px-6 py-4 whitespace-no-wrap border-b border-gray-200 text-sm leading-5 text-gray-500">
{{ site.subdomain }}.localhost:8080
</td>
<td class="px-6 py-4 whitespace-no-wrap border-b border-gray-200 text-sm leading-5 text-gray-500">
{{ site.shared_at }}
</td>
<td class="px-6 py-4 whitespace-no-wrap text-right border-b border-gray-200 text-sm leading-5 font-medium">
<a href="http://{{ site.subdomain }}.localhost:8080" target="_blank" class="text-indigo-600 hover:text-indigo-900">Visit</a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
</div>
</div>
</main>
</div>
</div>
<script>
new Vue({
el: '#app',
delimiters: ['@{', '}'],
data: {
userForm: {
name: '',
errors: {},
},
users: {{ users|json_encode|raw }}
},
methods: {
deleteUser(user) {
fetch('/expose/users/delete/'+user.id, {
method: 'DELETE',
}).then((response) => {
return response.json();
}).then((data) => {
this.users = this.users.filter(u => u.id !== user.id);
});
},
saveUser() {
fetch('/expose/users', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(this.userForm)
}).then((response) => {
return response.json();
}).then((data) => {
if (data.user) {
this.userForm.errors = {};
this.users.unshift(data.user);
}
if (data.errors) {
this.userForm.errors = data.errors;
}
});
}
}
})
</script>
</body>
</html>

View File

@@ -17,7 +17,7 @@
class="inline-flex items-center px-1 pt-1 border-b-2 border-indigo-500 text-sm font-medium leading-5 text-gray-900 focus:outline-none focus:border-indigo-700 transition duration-150 ease-in-out">
Users
</a>
<a href="#"
<a href="/expose/sites"
class="ml-8 inline-flex items-center px-1 pt-1 border-b-2 border-transparent text-sm font-medium leading-5 text-gray-500 hover:text-gray-700 hover:border-gray-300 focus:outline-none focus:text-gray-700 focus:border-gray-300 transition duration-150 ease-in-out">
Shared sites
</a>