mirror of
https://github.com/bitinflow/expose.git
synced 2026-03-13 13:35:54 +00:00
wip
This commit is contained in:
@@ -4,8 +4,10 @@ namespace App\Client;
|
|||||||
|
|
||||||
use App\Client\Connections\ControlConnection;
|
use App\Client\Connections\ControlConnection;
|
||||||
use App\Logger\CliRequestLogger;
|
use App\Logger\CliRequestLogger;
|
||||||
|
use Carbon\Carbon;
|
||||||
use Ratchet\Client\WebSocket;
|
use Ratchet\Client\WebSocket;
|
||||||
use React\EventLoop\LoopInterface;
|
use React\EventLoop\LoopInterface;
|
||||||
|
use React\Promise\Deferred;
|
||||||
use React\Promise\PromiseInterface;
|
use React\Promise\PromiseInterface;
|
||||||
use function Ratchet\Client\connect;
|
use function Ratchet\Client\connect;
|
||||||
|
|
||||||
@@ -20,6 +22,9 @@ class Client
|
|||||||
/** @var CliRequestLogger */
|
/** @var CliRequestLogger */
|
||||||
protected $logger;
|
protected $logger;
|
||||||
|
|
||||||
|
/** @var int */
|
||||||
|
protected $timeConnected = 0;
|
||||||
|
|
||||||
public static $subdomains = [];
|
public static $subdomains = [];
|
||||||
|
|
||||||
public function __construct(LoopInterface $loop, Configuration $configuration, CliRequestLogger $logger)
|
public function __construct(LoopInterface $loop, Configuration $configuration, CliRequestLogger $logger)
|
||||||
@@ -34,20 +39,18 @@ class Client
|
|||||||
$this->logger->info("Sharing http://{$sharedUrl}");
|
$this->logger->info("Sharing http://{$sharedUrl}");
|
||||||
|
|
||||||
foreach ($subdomains as $subdomain) {
|
foreach ($subdomains as $subdomain) {
|
||||||
$this->connectToServer($sharedUrl, $subdomain);
|
$this->connectToServer($sharedUrl, $subdomain, config('expose.auth_token'));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function connectToServer(string $sharedUrl, $subdomain): PromiseInterface
|
public function connectToServer(string $sharedUrl, $subdomain, $authToken = ''): PromiseInterface
|
||||||
{
|
{
|
||||||
$deferred = new \React\Promise\Deferred();
|
$deferred = new \React\Promise\Deferred();
|
||||||
$promise = $deferred->promise();
|
$promise = $deferred->promise();
|
||||||
|
|
||||||
$token = config('expose.auth_token');
|
|
||||||
|
|
||||||
$wsProtocol = $this->configuration->port() === 443 ? "wss" : "ws";
|
$wsProtocol = $this->configuration->port() === 443 ? "wss" : "ws";
|
||||||
|
|
||||||
connect($wsProtocol."://{$this->configuration->host()}:{$this->configuration->port()}/expose/control?authToken={$token}", [], [
|
connect($wsProtocol."://{$this->configuration->host()}:{$this->configuration->port()}/expose/control?authToken={$authToken}", [], [
|
||||||
'X-Expose-Control' => 'enabled',
|
'X-Expose-Control' => 'enabled',
|
||||||
], $this->loop)
|
], $this->loop)
|
||||||
->then(function (WebSocket $clientConnection) use ($sharedUrl, $subdomain, $deferred) {
|
->then(function (WebSocket $clientConnection) use ($sharedUrl, $subdomain, $deferred) {
|
||||||
@@ -55,19 +58,32 @@ class Client
|
|||||||
|
|
||||||
$connection->authenticate($sharedUrl, $subdomain);
|
$connection->authenticate($sharedUrl, $subdomain);
|
||||||
|
|
||||||
$clientConnection->on('close', function() {
|
$clientConnection->on('close', function() use ($deferred) {
|
||||||
$this->logger->error('Connection to server closed.');
|
$this->logger->error('Connection to server closed.');
|
||||||
exit(1);
|
|
||||||
|
$this->exit($deferred);
|
||||||
});
|
});
|
||||||
|
|
||||||
$connection->on('authenticationFailed', function ($data) {
|
$connection->on('authenticationFailed', function ($data) use ($deferred) {
|
||||||
$this->logger->error("Authentication failed. Please check your authentication token and try again.");
|
$this->logger->error("Authentication failed. Please check your authentication token and try again.");
|
||||||
exit(1);
|
|
||||||
|
$this->exit($deferred);
|
||||||
});
|
});
|
||||||
|
|
||||||
$connection->on('subdomainTaken', function ($data) {
|
$connection->on('subdomainTaken', function ($data) use ($deferred) {
|
||||||
$this->logger->error("The chosen subdomain \"{$data->data->subdomain}\" is already taken. Please choose a different subdomain.");
|
$this->logger->error("The chosen subdomain \"{$data->data->subdomain}\" is already taken. Please choose a different subdomain.");
|
||||||
exit(1);
|
|
||||||
|
$this->exit($deferred);
|
||||||
|
});
|
||||||
|
|
||||||
|
$connection->on('setMaximumConnectionLength', function ($data) {
|
||||||
|
$this->loop->addPeriodicTimer(1, function() use ($data) {
|
||||||
|
$this->timeConnected++;
|
||||||
|
|
||||||
|
$carbon = Carbon::createFromFormat('s', str_pad($data->length * 60 - $this->timeConnected, 2, 0, STR_PAD_LEFT));
|
||||||
|
|
||||||
|
$this->logger->info('Remaining time: '.$carbon->format('H:i:s'));
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
$connection->on('authenticated', function ($data) use ($deferred) {
|
$connection->on('authenticated', function ($data) use ($deferred) {
|
||||||
@@ -89,11 +105,18 @@ class Client
|
|||||||
$this->logger->error("Could not connect to the server.");
|
$this->logger->error("Could not connect to the server.");
|
||||||
$this->logger->error($e->getMessage());
|
$this->logger->error($e->getMessage());
|
||||||
|
|
||||||
$deferred->reject();
|
$this->exit($deferred);
|
||||||
|
|
||||||
exit(1);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
return $promise;
|
return $promise;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected function exit(Deferred $deferred)
|
||||||
|
{
|
||||||
|
$deferred->reject();
|
||||||
|
|
||||||
|
$this->loop->futureTick(function(){
|
||||||
|
exit(1);
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,6 +10,8 @@ interface ConnectionManager
|
|||||||
{
|
{
|
||||||
public function storeConnection(string $host, ?string $subdomain, ConnectionInterface $connection): ControlConnection;
|
public function storeConnection(string $host, ?string $subdomain, ConnectionInterface $connection): ControlConnection;
|
||||||
|
|
||||||
|
public function limitConnectionLength(ControlConnection $connection, int $maximumConnectionLength);
|
||||||
|
|
||||||
public function storeHttpConnection(ConnectionInterface $httpConnection, $requestId): HttpConnection;
|
public function storeHttpConnection(ConnectionInterface $httpConnection, $requestId): HttpConnection;
|
||||||
|
|
||||||
public function getHttpConnectionForRequestId(string $requestId): ?HttpConnection;
|
public function getHttpConnectionForRequestId(string $requestId): ?HttpConnection;
|
||||||
|
|||||||
18
app/Contracts/UserRepository.php
Normal file
18
app/Contracts/UserRepository.php
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Contracts;
|
||||||
|
|
||||||
|
use React\Promise\PromiseInterface;
|
||||||
|
|
||||||
|
interface UserRepository
|
||||||
|
{
|
||||||
|
public function getUsers(): PromiseInterface;
|
||||||
|
|
||||||
|
public function getUserById($id): PromiseInterface;
|
||||||
|
|
||||||
|
public function getUserByToken(string $authToken): PromiseInterface;
|
||||||
|
|
||||||
|
public function storeUser(array $data): PromiseInterface;
|
||||||
|
|
||||||
|
public function deleteUser($id): PromiseInterface;
|
||||||
|
}
|
||||||
@@ -5,6 +5,7 @@ namespace App\Server\Connections;
|
|||||||
use App\Contracts\ConnectionManager as ConnectionManagerContract;
|
use App\Contracts\ConnectionManager as ConnectionManagerContract;
|
||||||
use App\Contracts\SubdomainGenerator;
|
use App\Contracts\SubdomainGenerator;
|
||||||
use Ratchet\ConnectionInterface;
|
use Ratchet\ConnectionInterface;
|
||||||
|
use React\EventLoop\LoopInterface;
|
||||||
|
|
||||||
class ConnectionManager implements ConnectionManagerContract
|
class ConnectionManager implements ConnectionManagerContract
|
||||||
{
|
{
|
||||||
@@ -17,9 +18,26 @@ class ConnectionManager implements ConnectionManagerContract
|
|||||||
/** @var SubdomainGenerator */
|
/** @var SubdomainGenerator */
|
||||||
protected $subdomainGenerator;
|
protected $subdomainGenerator;
|
||||||
|
|
||||||
public function __construct(SubdomainGenerator $subdomainGenerator)
|
/** @var LoopInterface */
|
||||||
|
protected $loop;
|
||||||
|
|
||||||
|
public function __construct(SubdomainGenerator $subdomainGenerator, LoopInterface $loop)
|
||||||
{
|
{
|
||||||
$this->subdomainGenerator = $subdomainGenerator;
|
$this->subdomainGenerator = $subdomainGenerator;
|
||||||
|
$this->loop = $loop;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function limitConnectionLength(ControlConnection $connection, int $maximumConnectionLength)
|
||||||
|
{
|
||||||
|
if ($maximumConnectionLength === 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$connection->setMaximumConnectionLength($maximumConnectionLength);
|
||||||
|
|
||||||
|
$this->loop->addTimer($maximumConnectionLength * 60, function() use ($connection) {
|
||||||
|
$connection->socket->close();
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public function storeConnection(string $host, ?string $subdomain, ConnectionInterface $connection): ControlConnection
|
public function storeConnection(string $host, ?string $subdomain, ConnectionInterface $connection): ControlConnection
|
||||||
|
|||||||
@@ -33,6 +33,14 @@ class ControlConnection
|
|||||||
$this->shared_at = now()->toDateTimeString();
|
$this->shared_at = now()->toDateTimeString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function setMaximumConnectionLength(int $maximumConnectionLength)
|
||||||
|
{
|
||||||
|
$this->socket->send(json_encode([
|
||||||
|
'event' => 'setMaximumConnectionLength',
|
||||||
|
'length' => $maximumConnectionLength,
|
||||||
|
]));
|
||||||
|
}
|
||||||
|
|
||||||
public function registerProxy($requestId)
|
public function registerProxy($requestId)
|
||||||
{
|
{
|
||||||
$this->socket->send(json_encode([
|
$this->socket->send(json_encode([
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ namespace App\Server;
|
|||||||
|
|
||||||
use App\Contracts\ConnectionManager as ConnectionManagerContract;
|
use App\Contracts\ConnectionManager as ConnectionManagerContract;
|
||||||
use App\Contracts\SubdomainGenerator;
|
use App\Contracts\SubdomainGenerator;
|
||||||
|
use App\Contracts\UserRepository;
|
||||||
use App\Http\Server as HttpServer;
|
use App\Http\Server as HttpServer;
|
||||||
use App\Server\Connections\ConnectionManager;
|
use App\Server\Connections\ConnectionManager;
|
||||||
use App\Server\Http\Controllers\Admin\DeleteUsersController;
|
use App\Server\Http\Controllers\Admin\DeleteUsersController;
|
||||||
@@ -117,7 +118,7 @@ class Factory
|
|||||||
$this->router->get('/settings', ShowSettingsController::class, $adminCondition);
|
$this->router->get('/settings', ShowSettingsController::class, $adminCondition);
|
||||||
$this->router->post('/settings', SaveSettingsController::class, $adminCondition);
|
$this->router->post('/settings', SaveSettingsController::class, $adminCondition);
|
||||||
$this->router->post('/users', StoreUsersController::class, $adminCondition);
|
$this->router->post('/users', StoreUsersController::class, $adminCondition);
|
||||||
$this->router->delete('/users/delete/{id}', DeleteUsersController::class, $adminCondition);
|
$this->router->delete('/users/{id}', DeleteUsersController::class, $adminCondition);
|
||||||
$this->router->get('/sites', ListSitesController::class, $adminCondition);
|
$this->router->get('/sites', ListSitesController::class, $adminCondition);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -133,7 +134,7 @@ class Factory
|
|||||||
protected function bindSubdomainGenerator()
|
protected function bindSubdomainGenerator()
|
||||||
{
|
{
|
||||||
app()->singleton(SubdomainGenerator::class, function ($app) {
|
app()->singleton(SubdomainGenerator::class, function ($app) {
|
||||||
return $app->make(RandomSubdomainGenerator::class);
|
return $app->make(config('expose.admin.subdomain_generator'));
|
||||||
});
|
});
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
@@ -154,6 +155,7 @@ class Factory
|
|||||||
|
|
||||||
$this->bindConfiguration()
|
$this->bindConfiguration()
|
||||||
->bindSubdomainGenerator()
|
->bindSubdomainGenerator()
|
||||||
|
->bindUserRepository()
|
||||||
->bindDatabase()
|
->bindDatabase()
|
||||||
->ensureDatabaseIsInitialized()
|
->ensureDatabaseIsInitialized()
|
||||||
->bindConnectionManager()
|
->bindConnectionManager()
|
||||||
@@ -181,6 +183,15 @@ class Factory
|
|||||||
return $this->socket;
|
return $this->socket;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected function bindUserRepository()
|
||||||
|
{
|
||||||
|
app()->singleton(UserRepository::class, function() {
|
||||||
|
return app(config('expose.admin.user_repository'));
|
||||||
|
});
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
protected function bindDatabase()
|
protected function bindDatabase()
|
||||||
{
|
{
|
||||||
app()->singleton(DatabaseInterface::class, function() {
|
app()->singleton(DatabaseInterface::class, function() {
|
||||||
|
|||||||
@@ -2,9 +2,8 @@
|
|||||||
|
|
||||||
namespace App\Server\Http\Controllers\Admin;
|
namespace App\Server\Http\Controllers\Admin;
|
||||||
|
|
||||||
|
use App\Contracts\UserRepository;
|
||||||
use App\Http\Controllers\Controller;
|
use App\Http\Controllers\Controller;
|
||||||
use Clue\React\SQLite\DatabaseInterface;
|
|
||||||
use Clue\React\SQLite\Result;
|
|
||||||
use GuzzleHttp\Psr7\Response;
|
use GuzzleHttp\Psr7\Response;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use Illuminate\Support\Facades\Validator;
|
use Illuminate\Support\Facades\Validator;
|
||||||
@@ -19,20 +18,20 @@ class DeleteUsersController extends AdminController
|
|||||||
{
|
{
|
||||||
protected $keepConnectionOpen = true;
|
protected $keepConnectionOpen = true;
|
||||||
|
|
||||||
/** @var DatabaseInterface */
|
/** @var UserRepository */
|
||||||
protected $database;
|
protected $userRepository;
|
||||||
|
|
||||||
public function __construct(DatabaseInterface $database)
|
public function __construct(UserRepository $userRepository)
|
||||||
{
|
{
|
||||||
$this->database = $database;
|
$this->userRepository = $userRepository;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function handle(Request $request, ConnectionInterface $httpConnection)
|
public function handle(Request $request, ConnectionInterface $httpConnection)
|
||||||
{
|
{
|
||||||
$this->database->query("DELETE FROM users WHERE id = :id", ['id' => $request->id])
|
$this->userRepository->deleteUser($request->get('id'))
|
||||||
->then(function (Result $result) use ($httpConnection) {
|
->then(function() use ($httpConnection) {
|
||||||
$httpConnection->send(respond_json(['deleted' => true], 200));
|
$httpConnection->send(respond_json(['deleted' => true], 200));
|
||||||
$httpConnection->close();
|
$httpConnection->close();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,9 +5,6 @@ namespace App\Server\Http\Controllers\Admin;
|
|||||||
use App\Contracts\ConnectionManager;
|
use App\Contracts\ConnectionManager;
|
||||||
use App\Http\Controllers\Controller;
|
use App\Http\Controllers\Controller;
|
||||||
use App\Server\Configuration;
|
use App\Server\Configuration;
|
||||||
use Clue\React\SQLite\DatabaseInterface;
|
|
||||||
use Clue\React\SQLite\Result;
|
|
||||||
use GuzzleHttp\Psr7\Response;
|
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use Ratchet\ConnectionInterface;
|
use Ratchet\ConnectionInterface;
|
||||||
use Twig\Environment;
|
use Twig\Environment;
|
||||||
|
|||||||
@@ -2,8 +2,8 @@
|
|||||||
|
|
||||||
namespace App\Server\Http\Controllers\Admin;
|
namespace App\Server\Http\Controllers\Admin;
|
||||||
|
|
||||||
|
use App\Contracts\UserRepository;
|
||||||
use App\Http\Controllers\Controller;
|
use App\Http\Controllers\Controller;
|
||||||
use Clue\React\SQLite\DatabaseInterface;
|
|
||||||
use Clue\React\SQLite\Result;
|
use Clue\React\SQLite\Result;
|
||||||
use GuzzleHttp\Psr7\Response;
|
use GuzzleHttp\Psr7\Response;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
@@ -17,26 +17,24 @@ class ListUsersController extends AdminController
|
|||||||
{
|
{
|
||||||
protected $keepConnectionOpen = true;
|
protected $keepConnectionOpen = true;
|
||||||
|
|
||||||
/** @var DatabaseInterface */
|
/** @var UserRepository */
|
||||||
protected $database;
|
protected $userRepository;
|
||||||
|
|
||||||
public function __construct(DatabaseInterface $database)
|
public function __construct(UserRepository $userRepository)
|
||||||
{
|
{
|
||||||
$this->database = $database;
|
$this->userRepository = $userRepository;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function handle(Request $request, ConnectionInterface $httpConnection)
|
public function handle(Request $request, ConnectionInterface $httpConnection)
|
||||||
{
|
{
|
||||||
$this->database->query('SELECT * FROM users ORDER by created_at DESC')->then(function (Result $result) use ($httpConnection) {
|
$this->userRepository
|
||||||
$httpConnection->send(
|
->getUsers()
|
||||||
respond_html($this->getView($httpConnection, 'server.users.index', ['users' => $result->rows]))
|
->then(function ($users) use ($httpConnection) {
|
||||||
);
|
$httpConnection->send(
|
||||||
|
respond_html($this->getView($httpConnection, 'server.users.index', ['users' => $users]))
|
||||||
|
);
|
||||||
|
|
||||||
$httpConnection->close();
|
$httpConnection->close();
|
||||||
}, function (\Exception $exception) use ($httpConnection) {
|
});
|
||||||
$httpConnection->send(respond_html('Something went wrong: '.$exception->getMessage(), 500));
|
|
||||||
|
|
||||||
$httpConnection->close();
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ namespace App\Server\Http\Controllers\Admin;
|
|||||||
use App\Contracts\ConnectionManager;
|
use App\Contracts\ConnectionManager;
|
||||||
use App\Http\Controllers\Controller;
|
use App\Http\Controllers\Controller;
|
||||||
use App\Server\Configuration;
|
use App\Server\Configuration;
|
||||||
use Clue\React\SQLite\DatabaseInterface;
|
|
||||||
use Clue\React\SQLite\Result;
|
use Clue\React\SQLite\Result;
|
||||||
use GuzzleHttp\Psr7\Response;
|
use GuzzleHttp\Psr7\Response;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
|
|||||||
@@ -3,11 +3,7 @@
|
|||||||
namespace App\Server\Http\Controllers\Admin;
|
namespace App\Server\Http\Controllers\Admin;
|
||||||
|
|
||||||
use App\Contracts\ConnectionManager;
|
use App\Contracts\ConnectionManager;
|
||||||
use App\Http\Controllers\Controller;
|
|
||||||
use App\Server\Configuration;
|
use App\Server\Configuration;
|
||||||
use Clue\React\SQLite\DatabaseInterface;
|
|
||||||
use Clue\React\SQLite\Result;
|
|
||||||
use GuzzleHttp\Psr7\Response;
|
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use Ratchet\ConnectionInterface;
|
use Ratchet\ConnectionInterface;
|
||||||
use Twig\Environment;
|
use Twig\Environment;
|
||||||
|
|||||||
@@ -2,8 +2,8 @@
|
|||||||
|
|
||||||
namespace App\Server\Http\Controllers\Admin;
|
namespace App\Server\Http\Controllers\Admin;
|
||||||
|
|
||||||
|
use App\Contracts\UserRepository;
|
||||||
use App\Http\Controllers\Controller;
|
use App\Http\Controllers\Controller;
|
||||||
use Clue\React\SQLite\DatabaseInterface;
|
|
||||||
use Clue\React\SQLite\Result;
|
use Clue\React\SQLite\Result;
|
||||||
use GuzzleHttp\Psr7\Response;
|
use GuzzleHttp\Psr7\Response;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
@@ -19,12 +19,12 @@ class StoreUsersController extends AdminController
|
|||||||
{
|
{
|
||||||
protected $keepConnectionOpen = true;
|
protected $keepConnectionOpen = true;
|
||||||
|
|
||||||
/** @var DatabaseInterface */
|
/** @var UserRepository */
|
||||||
protected $database;
|
protected $userRepository;
|
||||||
|
|
||||||
public function __construct(DatabaseInterface $database)
|
public function __construct(UserRepository $userRepository)
|
||||||
{
|
{
|
||||||
$this->database = $database;
|
$this->userRepository = $userRepository;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function handle(Request $request, ConnectionInterface $httpConnection)
|
public function handle(Request $request, ConnectionInterface $httpConnection)
|
||||||
@@ -47,16 +47,11 @@ class StoreUsersController extends AdminController
|
|||||||
'auth_token' => (string)Str::uuid()
|
'auth_token' => (string)Str::uuid()
|
||||||
];
|
];
|
||||||
|
|
||||||
$this->database->query("
|
$this->userRepository
|
||||||
INSERT INTO users (name, auth_token, created_at)
|
->storeUser($insertData)
|
||||||
VALUES (:name, :auth_token, DATETIME('now'))
|
->then(function ($user) use ($httpConnection) {
|
||||||
", $insertData)
|
$httpConnection->send(respond_json(['user' => $user], 200));
|
||||||
->then(function (Result $result) use ($httpConnection) {
|
$httpConnection->close();
|
||||||
$this->database->query("SELECT * FROM users WHERE id = :id", ['id' => $result->insertId])
|
});
|
||||||
->then(function (Result $result) use ($httpConnection) {
|
|
||||||
$httpConnection->send(respond_json(['user' => $result->rows[0]], 200));
|
|
||||||
$httpConnection->close();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,10 +3,12 @@
|
|||||||
namespace App\Server\Http\Controllers;
|
namespace App\Server\Http\Controllers;
|
||||||
|
|
||||||
use App\Contracts\ConnectionManager;
|
use App\Contracts\ConnectionManager;
|
||||||
|
use App\Contracts\UserRepository;
|
||||||
use App\Http\QueryParameters;
|
use App\Http\QueryParameters;
|
||||||
use Clue\React\SQLite\DatabaseInterface;
|
|
||||||
use Clue\React\SQLite\Result;
|
|
||||||
use Ratchet\WebSocket\MessageComponentInterface;
|
use Ratchet\WebSocket\MessageComponentInterface;
|
||||||
|
use React\Promise\Deferred;
|
||||||
|
use React\Promise\FulfilledPromise;
|
||||||
|
use React\Promise\PromiseInterface;
|
||||||
use stdClass;
|
use stdClass;
|
||||||
use Ratchet\ConnectionInterface;
|
use Ratchet\ConnectionInterface;
|
||||||
|
|
||||||
@@ -16,13 +18,13 @@ class ControlMessageController implements MessageComponentInterface
|
|||||||
/** @var ConnectionManager */
|
/** @var ConnectionManager */
|
||||||
protected $connectionManager;
|
protected $connectionManager;
|
||||||
|
|
||||||
/** @var DatabaseInterface */
|
/** @var UserRepository */
|
||||||
protected $database;
|
protected $userRepository;
|
||||||
|
|
||||||
public function __construct(ConnectionManager $connectionManager, DatabaseInterface $database)
|
public function __construct(ConnectionManager $connectionManager, UserRepository $userRepository)
|
||||||
{
|
{
|
||||||
$this->connectionManager = $connectionManager;
|
$this->connectionManager = $connectionManager;
|
||||||
$this->database = $database;
|
$this->userRepository = $userRepository;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -75,21 +77,28 @@ class ControlMessageController implements MessageComponentInterface
|
|||||||
|
|
||||||
protected function authenticate(ConnectionInterface $connection, $data)
|
protected function authenticate(ConnectionInterface $connection, $data)
|
||||||
{
|
{
|
||||||
if (config('expose.admin.validate_auth_tokens') === true) {
|
$this->verifyAuthToken($connection)
|
||||||
$this->verifyAuthToken($connection);
|
->then(function () use ($connection, $data) {
|
||||||
}
|
if (! $this->hasValidSubdomain($connection, $data->subdomain)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (! $this->hasValidSubdomain($connection, $data->subdomain)) {
|
$connectionInfo = $this->connectionManager->storeConnection($data->host, $data->subdomain, $connection);
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
$connectionInfo = $this->connectionManager->storeConnection($data->host, $data->subdomain, $connection);
|
$this->connectionManager->limitConnectionLength($connectionInfo, config('expose.admin.maximum_session_length'));
|
||||||
|
|
||||||
$connection->send(json_encode([
|
$connection->send(json_encode([
|
||||||
'event' => 'authenticated',
|
'event' => 'authenticated',
|
||||||
'subdomain' => $connectionInfo->subdomain,
|
'subdomain' => $connectionInfo->subdomain,
|
||||||
'client_id' => $connectionInfo->client_id
|
'client_id' => $connectionInfo->client_id
|
||||||
]));
|
]));
|
||||||
|
}, function () use ($connection) {
|
||||||
|
$connection->send(json_encode([
|
||||||
|
'event' => 'authenticationFailed',
|
||||||
|
'data' => []
|
||||||
|
]));
|
||||||
|
$connection->close();
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function registerProxy(ConnectionInterface $connection, $data)
|
protected function registerProxy(ConnectionInterface $connection, $data)
|
||||||
@@ -111,28 +120,34 @@ class ControlMessageController implements MessageComponentInterface
|
|||||||
//
|
//
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function verifyAuthToken(ConnectionInterface $connection)
|
protected function verifyAuthToken(ConnectionInterface $connection): PromiseInterface
|
||||||
{
|
{
|
||||||
|
if (config('expose.admin.validate_auth_tokens') !== true) {
|
||||||
|
return new FulfilledPromise();
|
||||||
|
}
|
||||||
|
|
||||||
|
$deferred = new Deferred();
|
||||||
|
|
||||||
$authToken = QueryParameters::create($connection->httpRequest)->get('authToken');
|
$authToken = QueryParameters::create($connection->httpRequest)->get('authToken');
|
||||||
|
|
||||||
$this->database
|
$this->userRepository
|
||||||
->query("SELECT * FROM users WHERE auth_token = :token", ['token' => $authToken])
|
->getUserByToken($authToken)
|
||||||
->then(function (Result $result) use ($connection) {
|
->then(function ($user) use ($connection, $deferred) {
|
||||||
if (count($result->rows) === 0) {
|
if (is_null($user)) {
|
||||||
$connection->send(json_encode([
|
$deferred->reject();
|
||||||
'event' => 'authenticationFailed',
|
} else {
|
||||||
'data' => []
|
$deferred->resolve($user);
|
||||||
]));
|
|
||||||
$connection->close();
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
return $deferred->promise();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function hasValidSubdomain(ConnectionInterface $connection, ?string $subdomain): bool
|
protected function hasValidSubdomain(ConnectionInterface $connection, ?string $subdomain): bool
|
||||||
{
|
{
|
||||||
if (! is_null($subdomain)) {
|
if (!is_null($subdomain)) {
|
||||||
$controlConnection = $this->connectionManager->findControlConnectionForSubdomain($subdomain);
|
$controlConnection = $this->connectionManager->findControlConnectionForSubdomain($subdomain);
|
||||||
if (! is_null($controlConnection) || $subdomain === config('expose.admin.subdomain')) {
|
if (!is_null($controlConnection) || $subdomain === config('expose.admin.subdomain')) {
|
||||||
$connection->send(json_encode([
|
$connection->send(json_encode([
|
||||||
'event' => 'subdomainTaken',
|
'event' => 'subdomainTaken',
|
||||||
'data' => [
|
'data' => [
|
||||||
|
|||||||
89
app/Server/UserRepository/DatabaseUserRepository.php
Normal file
89
app/Server/UserRepository/DatabaseUserRepository.php
Normal file
@@ -0,0 +1,89 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Server\UserRepository;
|
||||||
|
|
||||||
|
use App\Contracts\UserRepository;
|
||||||
|
use Clue\React\SQLite\DatabaseInterface;
|
||||||
|
use Clue\React\SQLite\Result;
|
||||||
|
use React\Promise\Deferred;
|
||||||
|
use React\Promise\PromiseInterface;
|
||||||
|
|
||||||
|
class DatabaseUserRepository implements UserRepository
|
||||||
|
{
|
||||||
|
/** @var DatabaseInterface */
|
||||||
|
protected $database;
|
||||||
|
|
||||||
|
public function __construct(DatabaseInterface $database)
|
||||||
|
{
|
||||||
|
$this->database = $database;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getUsers(): PromiseInterface
|
||||||
|
{
|
||||||
|
$deferred = new Deferred();
|
||||||
|
|
||||||
|
$this->database
|
||||||
|
->query("SELECT * FROM users ORDER by created_at DESC")
|
||||||
|
->then(function (Result $result) use ($deferred) {
|
||||||
|
$deferred->resolve($result->rows);
|
||||||
|
});
|
||||||
|
|
||||||
|
return $deferred->promise();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getUserById($id): PromiseInterface
|
||||||
|
{
|
||||||
|
$deferred = new Deferred();
|
||||||
|
|
||||||
|
$this->database
|
||||||
|
->query("SELECT * FROM users WHERE id = :id", ['id' => $id])
|
||||||
|
->then(function (Result $result) use ($deferred) {
|
||||||
|
$deferred->resolve($result->rows[0] ?? null);
|
||||||
|
});
|
||||||
|
|
||||||
|
return $deferred->promise();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getUserByToken(string $authToken): PromiseInterface
|
||||||
|
{
|
||||||
|
$deferred = new Deferred();
|
||||||
|
|
||||||
|
$this->database
|
||||||
|
->query("SELECT * FROM users WHERE auth_token = :token", ['token' => $authToken])
|
||||||
|
->then(function (Result $result) use ($deferred) {
|
||||||
|
$deferred->resolve($result->rows[0] ?? null);
|
||||||
|
});
|
||||||
|
|
||||||
|
return $deferred->promise();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function storeUser(array $data): PromiseInterface
|
||||||
|
{
|
||||||
|
$deferred = new Deferred();
|
||||||
|
|
||||||
|
$this->database->query("
|
||||||
|
INSERT INTO users (name, auth_token, created_at)
|
||||||
|
VALUES (:name, :auth_token, DATETIME('now'))
|
||||||
|
", $data)
|
||||||
|
->then(function (Result $result) use ($deferred) {
|
||||||
|
$this->database->query("SELECT * FROM users WHERE id = :id", ['id' => $result->insertId])
|
||||||
|
->then(function (Result $result) use ($deferred) {
|
||||||
|
$deferred->resolve($result->rows[0]);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
return $deferred->promise();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function deleteUser($id): PromiseInterface
|
||||||
|
{
|
||||||
|
$deferred = new Deferred();
|
||||||
|
|
||||||
|
$this->database->query("DELETE FROM users WHERE id = :id", ['id' => $id])
|
||||||
|
->then(function (Result $result) use ($deferred) {
|
||||||
|
$deferred->resolve($result);
|
||||||
|
});
|
||||||
|
|
||||||
|
return $deferred->promise();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -11,6 +11,20 @@ return [
|
|||||||
|
|
||||||
'validate_auth_tokens' => false,
|
'validate_auth_tokens' => false,
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
| Maximum session length
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
|
|
||||||
|
| If you want to limit the amount of time that a single connection can
|
||||||
|
| stay connected to the expose server, you can specify the maximum
|
||||||
|
| session length in minutes here. A maximum length of 0 means that
|
||||||
|
| clients can stay connected as long as they want.
|
||||||
|
|
|
||||||
|
*/
|
||||||
|
'maximum_session_length' => 0,
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|--------------------------------------------------------------------------
|
|--------------------------------------------------------------------------
|
||||||
| Subdomain
|
| Subdomain
|
||||||
@@ -23,6 +37,18 @@ return [
|
|||||||
*/
|
*/
|
||||||
'subdomain' => 'expose',
|
'subdomain' => 'expose',
|
||||||
|
|
||||||
|
/*
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
| Subdomain Generator
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
|
|
||||||
|
| This is the subdomain generator that will be used, when no specific
|
||||||
|
| subdomain was provided. The default implementation simply generates
|
||||||
|
| a random string for you. Feel free to change this.
|
||||||
|
|
|
||||||
|
*/
|
||||||
|
'subdomain_generator' => \App\Server\SubdomainGenerator\RandomSubdomainGenerator::class,
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|--------------------------------------------------------------------------
|
|--------------------------------------------------------------------------
|
||||||
| Users
|
| Users
|
||||||
@@ -35,6 +61,18 @@ return [
|
|||||||
*/
|
*/
|
||||||
'users' => [
|
'users' => [
|
||||||
'username' => 'password'
|
'username' => 'password'
|
||||||
]
|
],
|
||||||
|
|
||||||
|
/*
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
| User Repository
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
|
|
||||||
|
| This is the user repository, which by default loads and saves all authorized
|
||||||
|
| users in a SQLite database. You can implement your own user repository
|
||||||
|
| if you want to store your users in a different store (Redis, MySQL, etc.)
|
||||||
|
|
|
||||||
|
*/
|
||||||
|
'user_repository' => \App\Server\UserRepository\DatabaseUserRepository::class,
|
||||||
]
|
]
|
||||||
];
|
];
|
||||||
|
|||||||
@@ -61,7 +61,7 @@
|
|||||||
|
|
||||||
methods: {
|
methods: {
|
||||||
deleteUser(user) {
|
deleteUser(user) {
|
||||||
fetch('/expose/users/delete/' + user.id, {
|
fetch('/expose/users/' + user.id, {
|
||||||
method: 'DELETE',
|
method: 'DELETE',
|
||||||
}).then((response) => {
|
}).then((response) => {
|
||||||
return response.json();
|
return response.json();
|
||||||
|
|||||||
@@ -84,6 +84,25 @@ class AdminTest extends TestCase
|
|||||||
/** @test */
|
/** @test */
|
||||||
public function it_can_create_users()
|
public function it_can_create_users()
|
||||||
{
|
{
|
||||||
|
/** @var Response $response */
|
||||||
|
$response = $this->await($this->browser->post('http://127.0.0.1:8080/users', [
|
||||||
|
'Host' => 'expose.localhost',
|
||||||
|
'Authorization' => base64_encode("username:secret"),
|
||||||
|
'Content-Type' => 'application/json'
|
||||||
|
], json_encode([
|
||||||
|
'name' => 'Marcel',
|
||||||
|
])));
|
||||||
|
|
||||||
|
$responseData = json_decode($response->getBody()->getContents());
|
||||||
|
$this->assertSame('Marcel', $responseData->user->name);
|
||||||
|
|
||||||
|
$this->assertDatabaseHasResults('SELECT * FROM users WHERE name = "Marcel"');
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @test */
|
||||||
|
public function it_can_delete_users()
|
||||||
|
{
|
||||||
|
/** @var Response $response */
|
||||||
$this->await($this->browser->post('http://127.0.0.1:8080/users', [
|
$this->await($this->browser->post('http://127.0.0.1:8080/users', [
|
||||||
'Host' => 'expose.localhost',
|
'Host' => 'expose.localhost',
|
||||||
'Authorization' => base64_encode("username:secret"),
|
'Authorization' => base64_encode("username:secret"),
|
||||||
@@ -92,7 +111,38 @@ class AdminTest extends TestCase
|
|||||||
'name' => 'Marcel',
|
'name' => 'Marcel',
|
||||||
])));
|
])));
|
||||||
|
|
||||||
$this->assertDatabaseHasResults('SELECT * FROM users WHERE name = "Marcel"');
|
|
||||||
|
$this->await($this->browser->delete('http://127.0.0.1:8080/users/1', [
|
||||||
|
'Host' => 'expose.localhost',
|
||||||
|
'Authorization' => base64_encode("username:secret"),
|
||||||
|
'Content-Type' => 'application/json'
|
||||||
|
]));
|
||||||
|
|
||||||
|
$this->assertDatabaseHasNoResults('SELECT * FROM users WHERE name = "Marcel"');
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @test */
|
||||||
|
public function it_can_list_all_users()
|
||||||
|
{
|
||||||
|
/** @var Response $response */
|
||||||
|
$this->await($this->browser->post('http://127.0.0.1:8080/users', [
|
||||||
|
'Host' => 'expose.localhost',
|
||||||
|
'Authorization' => base64_encode("username:secret"),
|
||||||
|
'Content-Type' => 'application/json'
|
||||||
|
], json_encode([
|
||||||
|
'name' => 'Marcel',
|
||||||
|
])));
|
||||||
|
|
||||||
|
/** @var Response $response */
|
||||||
|
$response = $this->await($this->browser->get('http://127.0.0.1:8080/users', [
|
||||||
|
'Host' => 'expose.localhost',
|
||||||
|
'Authorization' => base64_encode("username:secret"),
|
||||||
|
'Content-Type' => 'application/json'
|
||||||
|
]));
|
||||||
|
|
||||||
|
$body = $response->getBody()->getContents();
|
||||||
|
|
||||||
|
$this->assertTrue(Str::contains($body, 'Marcel'));
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @test */
|
/** @test */
|
||||||
|
|||||||
@@ -24,6 +24,9 @@ class TunnelTest extends TestCase
|
|||||||
/** @var Factory */
|
/** @var Factory */
|
||||||
protected $serverFactory;
|
protected $serverFactory;
|
||||||
|
|
||||||
|
/** @var \React\Socket\Server */
|
||||||
|
protected $testHttpServer;
|
||||||
|
|
||||||
public function setUp(): void
|
public function setUp(): void
|
||||||
{
|
{
|
||||||
parent::setUp();
|
parent::setUp();
|
||||||
@@ -37,6 +40,10 @@ class TunnelTest extends TestCase
|
|||||||
{
|
{
|
||||||
$this->serverFactory->getSocket()->close();
|
$this->serverFactory->getSocket()->close();
|
||||||
|
|
||||||
|
if (isset($this->testHttpServer)) {
|
||||||
|
$this->testHttpServer->close();
|
||||||
|
}
|
||||||
|
|
||||||
parent::tearDown();
|
parent::tearDown();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -74,6 +81,39 @@ class TunnelTest extends TestCase
|
|||||||
$this->assertSame('Hello World!', $response->getBody()->getContents());
|
$this->assertSame('Hello World!', $response->getBody()->getContents());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @test */
|
||||||
|
public function it_rejects_clients_with_invalid_auth_tokens()
|
||||||
|
{
|
||||||
|
$this->app['config']['expose.admin.validate_auth_tokens'] = true;
|
||||||
|
|
||||||
|
$this->createTestHttpServer();
|
||||||
|
|
||||||
|
$this->expectException(\UnexpectedValueException::class);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* We create an expose client that connects to our server and shares
|
||||||
|
* the created test HTTP server
|
||||||
|
*/
|
||||||
|
$client = $this->createClient();
|
||||||
|
$result = $this->await($client->connectToServer('127.0.0.1:8085', 'tunnel'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @test */
|
||||||
|
public function it_allows_clients_with_valid_auth_tokens()
|
||||||
|
{
|
||||||
|
$this->app['config']['expose.admin.validate_auth_tokens'] = true;
|
||||||
|
|
||||||
|
$this->createTestHttpServer();
|
||||||
|
|
||||||
|
$this->expectException(\UnexpectedValueException::class);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* We create an expose client that connects to our server and shares
|
||||||
|
* the created test HTTP server
|
||||||
|
*/
|
||||||
|
$client = $this->createClient();
|
||||||
|
$this->await($client->connectToServer('127.0.0.1:8085', 'tunnel'));
|
||||||
|
}
|
||||||
|
|
||||||
protected function startServer()
|
protected function startServer()
|
||||||
{
|
{
|
||||||
@@ -104,7 +144,7 @@ class TunnelTest extends TestCase
|
|||||||
return new Response(200, ['Content-Type' => 'text/plain'], "Hello World!");
|
return new Response(200, ['Content-Type' => 'text/plain'], "Hello World!");
|
||||||
});
|
});
|
||||||
|
|
||||||
$socket = new \React\Socket\Server(8085, $this->loop);
|
$this->testHttpServer = new \React\Socket\Server(8085, $this->loop);
|
||||||
$server->listen($socket);
|
$server->listen($this->testHttpServer);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -46,4 +46,13 @@ abstract class TestCase extends \Tests\TestCase
|
|||||||
|
|
||||||
$this->assertGreaterThanOrEqual(1, count($result->rows));
|
$this->assertGreaterThanOrEqual(1, count($result->rows));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected function assertDatabaseHasNoResults($query)
|
||||||
|
{
|
||||||
|
$database = app(DatabaseInterface::class);
|
||||||
|
|
||||||
|
$result = $this->await($database->query($query));
|
||||||
|
|
||||||
|
$this->assertEmpty($result->rows);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user