mirror of
https://github.com/bitinflow/expose.git
synced 2026-03-14 05:55:54 +00:00
wip
This commit is contained in:
@@ -5,6 +5,7 @@ namespace App\Server\Connections;
|
||||
use App\Contracts\ConnectionManager as ConnectionManagerContract;
|
||||
use App\Contracts\SubdomainGenerator;
|
||||
use Ratchet\ConnectionInterface;
|
||||
use React\EventLoop\LoopInterface;
|
||||
|
||||
class ConnectionManager implements ConnectionManagerContract
|
||||
{
|
||||
@@ -17,9 +18,26 @@ class ConnectionManager implements ConnectionManagerContract
|
||||
/** @var SubdomainGenerator */
|
||||
protected $subdomainGenerator;
|
||||
|
||||
public function __construct(SubdomainGenerator $subdomainGenerator)
|
||||
/** @var LoopInterface */
|
||||
protected $loop;
|
||||
|
||||
public function __construct(SubdomainGenerator $subdomainGenerator, LoopInterface $loop)
|
||||
{
|
||||
$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
|
||||
|
||||
@@ -33,6 +33,14 @@ class ControlConnection
|
||||
$this->shared_at = now()->toDateTimeString();
|
||||
}
|
||||
|
||||
public function setMaximumConnectionLength(int $maximumConnectionLength)
|
||||
{
|
||||
$this->socket->send(json_encode([
|
||||
'event' => 'setMaximumConnectionLength',
|
||||
'length' => $maximumConnectionLength,
|
||||
]));
|
||||
}
|
||||
|
||||
public function registerProxy($requestId)
|
||||
{
|
||||
$this->socket->send(json_encode([
|
||||
|
||||
@@ -4,6 +4,7 @@ namespace App\Server;
|
||||
|
||||
use App\Contracts\ConnectionManager as ConnectionManagerContract;
|
||||
use App\Contracts\SubdomainGenerator;
|
||||
use App\Contracts\UserRepository;
|
||||
use App\Http\Server as HttpServer;
|
||||
use App\Server\Connections\ConnectionManager;
|
||||
use App\Server\Http\Controllers\Admin\DeleteUsersController;
|
||||
@@ -117,7 +118,7 @@ class Factory
|
||||
$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->delete('/users/{id}', DeleteUsersController::class, $adminCondition);
|
||||
$this->router->get('/sites', ListSitesController::class, $adminCondition);
|
||||
}
|
||||
|
||||
@@ -133,7 +134,7 @@ class Factory
|
||||
protected function bindSubdomainGenerator()
|
||||
{
|
||||
app()->singleton(SubdomainGenerator::class, function ($app) {
|
||||
return $app->make(RandomSubdomainGenerator::class);
|
||||
return $app->make(config('expose.admin.subdomain_generator'));
|
||||
});
|
||||
|
||||
return $this;
|
||||
@@ -154,6 +155,7 @@ class Factory
|
||||
|
||||
$this->bindConfiguration()
|
||||
->bindSubdomainGenerator()
|
||||
->bindUserRepository()
|
||||
->bindDatabase()
|
||||
->ensureDatabaseIsInitialized()
|
||||
->bindConnectionManager()
|
||||
@@ -181,6 +183,15 @@ class Factory
|
||||
return $this->socket;
|
||||
}
|
||||
|
||||
protected function bindUserRepository()
|
||||
{
|
||||
app()->singleton(UserRepository::class, function() {
|
||||
return app(config('expose.admin.user_repository'));
|
||||
});
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
protected function bindDatabase()
|
||||
{
|
||||
app()->singleton(DatabaseInterface::class, function() {
|
||||
|
||||
@@ -2,9 +2,8 @@
|
||||
|
||||
namespace App\Server\Http\Controllers\Admin;
|
||||
|
||||
use App\Contracts\UserRepository;
|
||||
use App\Http\Controllers\Controller;
|
||||
use Clue\React\SQLite\DatabaseInterface;
|
||||
use Clue\React\SQLite\Result;
|
||||
use GuzzleHttp\Psr7\Response;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
@@ -19,20 +18,20 @@ class DeleteUsersController extends AdminController
|
||||
{
|
||||
protected $keepConnectionOpen = true;
|
||||
|
||||
/** @var DatabaseInterface */
|
||||
protected $database;
|
||||
/** @var UserRepository */
|
||||
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)
|
||||
{
|
||||
$this->database->query("DELETE FROM users WHERE id = :id", ['id' => $request->id])
|
||||
->then(function (Result $result) use ($httpConnection) {
|
||||
$httpConnection->send(respond_json(['deleted' => true], 200));
|
||||
$httpConnection->close();
|
||||
});
|
||||
$this->userRepository->deleteUser($request->get('id'))
|
||||
->then(function() use ($httpConnection) {
|
||||
$httpConnection->send(respond_json(['deleted' => true], 200));
|
||||
$httpConnection->close();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,9 +5,6 @@ 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;
|
||||
|
||||
@@ -2,8 +2,8 @@
|
||||
|
||||
namespace App\Server\Http\Controllers\Admin;
|
||||
|
||||
use App\Contracts\UserRepository;
|
||||
use App\Http\Controllers\Controller;
|
||||
use Clue\React\SQLite\DatabaseInterface;
|
||||
use Clue\React\SQLite\Result;
|
||||
use GuzzleHttp\Psr7\Response;
|
||||
use Illuminate\Http\Request;
|
||||
@@ -17,26 +17,24 @@ class ListUsersController extends AdminController
|
||||
{
|
||||
protected $keepConnectionOpen = true;
|
||||
|
||||
/** @var DatabaseInterface */
|
||||
protected $database;
|
||||
/** @var UserRepository */
|
||||
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)
|
||||
{
|
||||
$this->database->query('SELECT * FROM users ORDER by created_at DESC')->then(function (Result $result) use ($httpConnection) {
|
||||
$httpConnection->send(
|
||||
respond_html($this->getView($httpConnection, 'server.users.index', ['users' => $result->rows]))
|
||||
);
|
||||
$this->userRepository
|
||||
->getUsers()
|
||||
->then(function ($users) use ($httpConnection) {
|
||||
$httpConnection->send(
|
||||
respond_html($this->getView($httpConnection, 'server.users.index', ['users' => $users]))
|
||||
);
|
||||
|
||||
$httpConnection->close();
|
||||
}, function (\Exception $exception) use ($httpConnection) {
|
||||
$httpConnection->send(respond_html('Something went wrong: '.$exception->getMessage(), 500));
|
||||
|
||||
$httpConnection->close();
|
||||
});
|
||||
$httpConnection->close();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,7 +5,6 @@ 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;
|
||||
|
||||
@@ -3,11 +3,7 @@
|
||||
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;
|
||||
|
||||
@@ -2,8 +2,8 @@
|
||||
|
||||
namespace App\Server\Http\Controllers\Admin;
|
||||
|
||||
use App\Contracts\UserRepository;
|
||||
use App\Http\Controllers\Controller;
|
||||
use Clue\React\SQLite\DatabaseInterface;
|
||||
use Clue\React\SQLite\Result;
|
||||
use GuzzleHttp\Psr7\Response;
|
||||
use Illuminate\Http\Request;
|
||||
@@ -19,12 +19,12 @@ class StoreUsersController extends AdminController
|
||||
{
|
||||
protected $keepConnectionOpen = true;
|
||||
|
||||
/** @var DatabaseInterface */
|
||||
protected $database;
|
||||
/** @var UserRepository */
|
||||
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)
|
||||
@@ -47,16 +47,11 @@ class StoreUsersController extends AdminController
|
||||
'auth_token' => (string)Str::uuid()
|
||||
];
|
||||
|
||||
$this->database->query("
|
||||
INSERT INTO users (name, auth_token, created_at)
|
||||
VALUES (:name, :auth_token, DATETIME('now'))
|
||||
", $insertData)
|
||||
->then(function (Result $result) use ($httpConnection) {
|
||||
$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();
|
||||
});
|
||||
});
|
||||
$this->userRepository
|
||||
->storeUser($insertData)
|
||||
->then(function ($user) use ($httpConnection) {
|
||||
$httpConnection->send(respond_json(['user' => $user], 200));
|
||||
$httpConnection->close();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,10 +3,12 @@
|
||||
namespace App\Server\Http\Controllers;
|
||||
|
||||
use App\Contracts\ConnectionManager;
|
||||
use App\Contracts\UserRepository;
|
||||
use App\Http\QueryParameters;
|
||||
use Clue\React\SQLite\DatabaseInterface;
|
||||
use Clue\React\SQLite\Result;
|
||||
use Ratchet\WebSocket\MessageComponentInterface;
|
||||
use React\Promise\Deferred;
|
||||
use React\Promise\FulfilledPromise;
|
||||
use React\Promise\PromiseInterface;
|
||||
use stdClass;
|
||||
use Ratchet\ConnectionInterface;
|
||||
|
||||
@@ -16,13 +18,13 @@ class ControlMessageController implements MessageComponentInterface
|
||||
/** @var ConnectionManager */
|
||||
protected $connectionManager;
|
||||
|
||||
/** @var DatabaseInterface */
|
||||
protected $database;
|
||||
/** @var UserRepository */
|
||||
protected $userRepository;
|
||||
|
||||
public function __construct(ConnectionManager $connectionManager, DatabaseInterface $database)
|
||||
public function __construct(ConnectionManager $connectionManager, UserRepository $userRepository)
|
||||
{
|
||||
$this->connectionManager = $connectionManager;
|
||||
$this->database = $database;
|
||||
$this->userRepository = $userRepository;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -75,21 +77,28 @@ class ControlMessageController implements MessageComponentInterface
|
||||
|
||||
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)) {
|
||||
return;
|
||||
}
|
||||
$connectionInfo = $this->connectionManager->storeConnection($data->host, $data->subdomain, $connection);
|
||||
|
||||
$connectionInfo = $this->connectionManager->storeConnection($data->host, $data->subdomain, $connection);
|
||||
$this->connectionManager->limitConnectionLength($connectionInfo, config('expose.admin.maximum_session_length'));
|
||||
|
||||
$connection->send(json_encode([
|
||||
'event' => 'authenticated',
|
||||
'subdomain' => $connectionInfo->subdomain,
|
||||
'client_id' => $connectionInfo->client_id
|
||||
]));
|
||||
$connection->send(json_encode([
|
||||
'event' => 'authenticated',
|
||||
'subdomain' => $connectionInfo->subdomain,
|
||||
'client_id' => $connectionInfo->client_id
|
||||
]));
|
||||
}, function () use ($connection) {
|
||||
$connection->send(json_encode([
|
||||
'event' => 'authenticationFailed',
|
||||
'data' => []
|
||||
]));
|
||||
$connection->close();
|
||||
});
|
||||
}
|
||||
|
||||
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');
|
||||
|
||||
$this->database
|
||||
->query("SELECT * FROM users WHERE auth_token = :token", ['token' => $authToken])
|
||||
->then(function (Result $result) use ($connection) {
|
||||
if (count($result->rows) === 0) {
|
||||
$connection->send(json_encode([
|
||||
'event' => 'authenticationFailed',
|
||||
'data' => []
|
||||
]));
|
||||
$connection->close();
|
||||
$this->userRepository
|
||||
->getUserByToken($authToken)
|
||||
->then(function ($user) use ($connection, $deferred) {
|
||||
if (is_null($user)) {
|
||||
$deferred->reject();
|
||||
} else {
|
||||
$deferred->resolve($user);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
return $deferred->promise();
|
||||
}
|
||||
|
||||
protected function hasValidSubdomain(ConnectionInterface $connection, ?string $subdomain): bool
|
||||
{
|
||||
if (! is_null($subdomain)) {
|
||||
if (!is_null($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([
|
||||
'event' => 'subdomainTaken',
|
||||
'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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user