mirror of
https://github.com/bitinflow/expose.git
synced 2026-03-13 13:35:54 +00:00
Custom domain support
This commit is contained in:
135
app/Server/DomainRepository/DatabaseDomainRepository.php
Normal file
135
app/Server/DomainRepository/DatabaseDomainRepository.php
Normal file
@@ -0,0 +1,135 @@
|
||||
<?php
|
||||
|
||||
namespace App\Server\DomainRepository;
|
||||
|
||||
use App\Contracts\DomainRepository;
|
||||
use Clue\React\SQLite\DatabaseInterface;
|
||||
use Clue\React\SQLite\Result;
|
||||
use React\Promise\Deferred;
|
||||
use React\Promise\PromiseInterface;
|
||||
|
||||
class DatabaseDomainRepository implements DomainRepository
|
||||
{
|
||||
/** @var DatabaseInterface */
|
||||
protected $database;
|
||||
|
||||
public function __construct(DatabaseInterface $database)
|
||||
{
|
||||
$this->database = $database;
|
||||
}
|
||||
|
||||
public function getDomains(): PromiseInterface
|
||||
{
|
||||
$deferred = new Deferred();
|
||||
|
||||
$this->database
|
||||
->query('SELECT * FROM domains ORDER by created_at DESC')
|
||||
->then(function (Result $result) use ($deferred) {
|
||||
$deferred->resolve($result->rows);
|
||||
});
|
||||
|
||||
return $deferred->promise();
|
||||
}
|
||||
|
||||
public function getDomainById($id): PromiseInterface
|
||||
{
|
||||
$deferred = new Deferred();
|
||||
|
||||
$this->database
|
||||
->query('SELECT * FROM domains WHERE id = :id', ['id' => $id])
|
||||
->then(function (Result $result) use ($deferred) {
|
||||
$deferred->resolve($result->rows[0] ?? null);
|
||||
});
|
||||
|
||||
return $deferred->promise();
|
||||
}
|
||||
|
||||
public function getDomainByName(string $name): PromiseInterface
|
||||
{
|
||||
$deferred = new Deferred();
|
||||
|
||||
$this->database
|
||||
->query('SELECT * FROM domains WHERE domain = :name', ['name' => $name])
|
||||
->then(function (Result $result) use ($deferred) {
|
||||
$deferred->resolve($result->rows[0] ?? null);
|
||||
});
|
||||
|
||||
return $deferred->promise();
|
||||
}
|
||||
|
||||
public function getDomainsByUserId($id): PromiseInterface
|
||||
{
|
||||
$deferred = new Deferred();
|
||||
|
||||
$this->database
|
||||
->query('SELECT * FROM domains WHERE user_id = :user_id ORDER by created_at DESC', [
|
||||
'user_id' => $id,
|
||||
])
|
||||
->then(function (Result $result) use ($deferred) {
|
||||
$deferred->resolve($result->rows);
|
||||
});
|
||||
|
||||
return $deferred->promise();
|
||||
}
|
||||
|
||||
public function storeDomain(array $data): PromiseInterface
|
||||
{
|
||||
$deferred = new Deferred();
|
||||
|
||||
$this->getDomainByName($data['domain'])
|
||||
->then(function ($registeredDomain) use ($data, $deferred) {
|
||||
$this->database->query("
|
||||
INSERT INTO domains (user_id, domain, created_at)
|
||||
VALUES (:user_id, :domain, DATETIME('now'))
|
||||
", $data)
|
||||
->then(function (Result $result) use ($deferred) {
|
||||
$this->database->query('SELECT * FROM domains WHERE id = :id', ['id' => $result->insertId])
|
||||
->then(function (Result $result) use ($deferred) {
|
||||
$deferred->resolve($result->rows[0]);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
return $deferred->promise();
|
||||
}
|
||||
|
||||
public function getDomainsByUserIdAndName($id, $name): PromiseInterface
|
||||
{
|
||||
$deferred = new Deferred();
|
||||
|
||||
$this->database
|
||||
->query('SELECT * FROM domains WHERE user_id = :user_id AND domain = :name ORDER by created_at DESC', [
|
||||
'user_id' => $id,
|
||||
'name' => $name,
|
||||
])
|
||||
->then(function (Result $result) use ($deferred) {
|
||||
$deferred->resolve($result->rows);
|
||||
});
|
||||
|
||||
return $deferred->promise();
|
||||
}
|
||||
|
||||
public function deleteDomainForUserId($userId, $domainId): PromiseInterface
|
||||
{
|
||||
$deferred = new Deferred();
|
||||
|
||||
$this->database->query('DELETE FROM domains WHERE id = :id AND user_id = :user_id', [
|
||||
'id' => $domainId,
|
||||
'user_id' => $userId,
|
||||
])
|
||||
->then(function (Result $result) use ($deferred) {
|
||||
$deferred->resolve($result);
|
||||
});
|
||||
|
||||
return $deferred->promise();
|
||||
}
|
||||
|
||||
public function updateDomain($id, array $data): PromiseInterface
|
||||
{
|
||||
$deferred = new Deferred();
|
||||
|
||||
// TODO
|
||||
|
||||
return $deferred->promise();
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@
|
||||
namespace App\Server;
|
||||
|
||||
use App\Contracts\ConnectionManager as ConnectionManagerContract;
|
||||
use App\Contracts\DomainRepository;
|
||||
use App\Contracts\StatisticsCollector;
|
||||
use App\Contracts\StatisticsRepository;
|
||||
use App\Contracts\SubdomainGenerator;
|
||||
@@ -11,6 +12,7 @@ use App\Contracts\UserRepository;
|
||||
use App\Http\RouteGenerator;
|
||||
use App\Http\Server as HttpServer;
|
||||
use App\Server\Connections\ConnectionManager;
|
||||
use App\Server\DomainRepository\DatabaseDomainRepository;
|
||||
use App\Server\Http\Controllers\Admin\DeleteSubdomainController;
|
||||
use App\Server\Http\Controllers\Admin\DeleteUsersController;
|
||||
use App\Server\Http\Controllers\Admin\DisconnectSiteController;
|
||||
@@ -26,6 +28,7 @@ use App\Server\Http\Controllers\Admin\ListTcpConnectionsController;
|
||||
use App\Server\Http\Controllers\Admin\ListUsersController;
|
||||
use App\Server\Http\Controllers\Admin\RedirectToUsersController;
|
||||
use App\Server\Http\Controllers\Admin\ShowSettingsController;
|
||||
use App\Server\Http\Controllers\Admin\StoreDomainController;
|
||||
use App\Server\Http\Controllers\Admin\StoreSettingsController;
|
||||
use App\Server\Http\Controllers\Admin\StoreSubdomainController;
|
||||
use App\Server\Http\Controllers\Admin\StoreUsersController;
|
||||
@@ -34,6 +37,7 @@ use App\Server\Http\Controllers\TunnelMessageController;
|
||||
use App\Server\Http\Router;
|
||||
use App\Server\StatisticsCollector\DatabaseStatisticsCollector;
|
||||
use App\Server\StatisticsRepository\DatabaseStatisticsRepository;
|
||||
use App\Server\SubdomainRepository\DatabaseSubdomainRepository;
|
||||
use Clue\React\SQLite\DatabaseInterface;
|
||||
use Phar;
|
||||
use Ratchet\Server\IoServer;
|
||||
@@ -139,6 +143,8 @@ class Factory
|
||||
$this->router->get('/api/users', GetUsersController::class, $adminCondition);
|
||||
$this->router->post('/api/users', StoreUsersController::class, $adminCondition);
|
||||
$this->router->get('/api/users/{id}', GetUserDetailsController::class, $adminCondition);
|
||||
$this->router->post('/api/domains', StoreDomainController::class, $adminCondition);
|
||||
$this->router->delete('/api/domains/{domain}', DeleteSubdomainController::class, $adminCondition);
|
||||
$this->router->post('/api/subdomains', StoreSubdomainController::class, $adminCondition);
|
||||
$this->router->delete('/api/subdomains/{subdomain}', DeleteSubdomainController::class, $adminCondition);
|
||||
$this->router->delete('/api/users/{id}', DeleteUsersController::class, $adminCondition);
|
||||
@@ -183,6 +189,7 @@ class Factory
|
||||
->bindSubdomainGenerator()
|
||||
->bindUserRepository()
|
||||
->bindSubdomainRepository()
|
||||
->bindDomainRepository()
|
||||
->bindDatabase()
|
||||
->ensureDatabaseIsInitialized()
|
||||
->registerStatisticsCollector()
|
||||
@@ -223,7 +230,16 @@ class Factory
|
||||
protected function bindSubdomainRepository()
|
||||
{
|
||||
app()->singleton(SubdomainRepository::class, function () {
|
||||
return app(config('expose.admin.subdomain_repository'));
|
||||
return app(config('expose.admin.subdomain_repository', DatabaseSubdomainRepository::class));
|
||||
});
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
protected function bindDomainRepository()
|
||||
{
|
||||
app()->singleton(DomainRepository::class, function () {
|
||||
return app(config('expose.admin.domain_repository', DatabaseDomainRepository::class));
|
||||
});
|
||||
|
||||
return $this;
|
||||
|
||||
72
app/Server/Http/Controllers/Admin/StoreDomainController.php
Normal file
72
app/Server/Http/Controllers/Admin/StoreDomainController.php
Normal file
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
|
||||
namespace App\Server\Http\Controllers\Admin;
|
||||
|
||||
use App\Contracts\DomainRepository;
|
||||
use App\Contracts\UserRepository;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
use Ratchet\ConnectionInterface;
|
||||
|
||||
class StoreDomainController extends AdminController
|
||||
{
|
||||
protected $keepConnectionOpen = true;
|
||||
|
||||
/** @var DomainRepository */
|
||||
protected $domainRepository;
|
||||
|
||||
/** @var UserRepository */
|
||||
protected $userRepository;
|
||||
|
||||
public function __construct(UserRepository $userRepository, DomainRepository $domainRepository)
|
||||
{
|
||||
$this->userRepository = $userRepository;
|
||||
$this->domainRepository = $domainRepository;
|
||||
}
|
||||
|
||||
public function handle(Request $request, ConnectionInterface $httpConnection)
|
||||
{
|
||||
$validator = Validator::make($request->all(), [
|
||||
'domain' => 'required',
|
||||
], [
|
||||
'required' => 'The :attribute field is required.',
|
||||
]);
|
||||
|
||||
if ($validator->fails()) {
|
||||
$httpConnection->send(respond_json(['errors' => $validator->getMessageBag()], 401));
|
||||
$httpConnection->close();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$this->userRepository
|
||||
->getUserByToken($request->get('auth_token', ''))
|
||||
->then(function ($user) use ($httpConnection, $request) {
|
||||
if (is_null($user)) {
|
||||
$httpConnection->send(respond_json(['error' => 'The user does not exist'], 404));
|
||||
$httpConnection->close();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if ($user['can_specify_domains'] === 0) {
|
||||
$httpConnection->send(respond_json(['error' => 'The user is not allowed to reserve custom domains.'], 401));
|
||||
$httpConnection->close();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$insertData = [
|
||||
'user_id' => $user['id'],
|
||||
'domain' => $request->get('domain'),
|
||||
];
|
||||
|
||||
$this->domainRepository
|
||||
->storeDomain($insertData)
|
||||
->then(function ($domain) use ($httpConnection) {
|
||||
$httpConnection->send(respond_json(['domain' => $domain], 200));
|
||||
$httpConnection->close();
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@ namespace App\Server\Http\Controllers\Admin;
|
||||
|
||||
use App\Contracts\SubdomainRepository;
|
||||
use App\Contracts\UserRepository;
|
||||
use App\Server\Configuration;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
use Ratchet\ConnectionInterface;
|
||||
@@ -18,10 +19,14 @@ class StoreSubdomainController extends AdminController
|
||||
/** @var UserRepository */
|
||||
protected $userRepository;
|
||||
|
||||
public function __construct(UserRepository $userRepository, SubdomainRepository $subdomainRepository)
|
||||
/** @var Configuration */
|
||||
protected $configuration;
|
||||
|
||||
public function __construct(UserRepository $userRepository, SubdomainRepository $subdomainRepository, Configuration $configuration)
|
||||
{
|
||||
$this->userRepository = $userRepository;
|
||||
$this->subdomainRepository = $subdomainRepository;
|
||||
$this->configuration = $configuration;
|
||||
}
|
||||
|
||||
public function handle(Request $request, ConnectionInterface $httpConnection)
|
||||
@@ -66,6 +71,7 @@ class StoreSubdomainController extends AdminController
|
||||
$insertData = [
|
||||
'user_id' => $user['id'],
|
||||
'subdomain' => $request->get('subdomain'),
|
||||
'domain' => $request->get('domain', $this->configuration->hostname()),
|
||||
];
|
||||
|
||||
$this->subdomainRepository
|
||||
|
||||
@@ -39,6 +39,7 @@ class StoreUsersController extends AdminController
|
||||
'name' => $request->get('name'),
|
||||
'auth_token' => (string) Str::uuid(),
|
||||
'can_specify_subdomains' => (int) $request->get('can_specify_subdomains'),
|
||||
'can_specify_domains' => (int) $request->get('can_specify_domains'),
|
||||
'can_share_tcp_ports' => (int) $request->get('can_share_tcp_ports'),
|
||||
'max_connections' => (int) $request->get('max_connections'),
|
||||
];
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
namespace App\Server\Http\Controllers;
|
||||
|
||||
use App\Contracts\ConnectionManager;
|
||||
use App\Contracts\DomainRepository;
|
||||
use App\Contracts\SubdomainRepository;
|
||||
use App\Contracts\UserRepository;
|
||||
use App\Http\QueryParameters;
|
||||
@@ -27,14 +28,18 @@ class ControlMessageController implements MessageComponentInterface
|
||||
/** @var SubdomainRepository */
|
||||
protected $subdomainRepository;
|
||||
|
||||
/** @var DomainRepository */
|
||||
protected $domainRepository;
|
||||
|
||||
/** @var Configuration */
|
||||
protected $configuration;
|
||||
|
||||
public function __construct(ConnectionManager $connectionManager, UserRepository $userRepository, SubdomainRepository $subdomainRepository, Configuration $configuration)
|
||||
public function __construct(ConnectionManager $connectionManager, UserRepository $userRepository, SubdomainRepository $subdomainRepository, Configuration $configuration, DomainRepository $domainRepository)
|
||||
{
|
||||
$this->connectionManager = $connectionManager;
|
||||
$this->userRepository = $userRepository;
|
||||
$this->subdomainRepository = $subdomainRepository;
|
||||
$this->domainRepository = $domainRepository;
|
||||
$this->configuration = $configuration;
|
||||
}
|
||||
|
||||
@@ -147,27 +152,31 @@ class ControlMessageController implements MessageComponentInterface
|
||||
|
||||
protected function handleHttpConnection(ConnectionInterface $connection, $data, $user = null)
|
||||
{
|
||||
$this->hasValidSubdomain($connection, $data->subdomain, $user, $data->server_host)->then(function ($subdomain) use ($data, $connection) {
|
||||
if ($subdomain === false) {
|
||||
return;
|
||||
}
|
||||
$this->hasValidDomain($connection, $data->server_host, $user)
|
||||
->then(function () use ($connection, $data, $user) {
|
||||
return $this->hasValidSubdomain($connection, $data->subdomain, $user, $data->server_host);
|
||||
})
|
||||
->then(function ($subdomain) use ($data, $connection) {
|
||||
if ($subdomain === false) {
|
||||
return;
|
||||
}
|
||||
|
||||
$data->subdomain = $subdomain;
|
||||
$data->subdomain = $subdomain;
|
||||
|
||||
$connectionInfo = $this->connectionManager->storeConnection($data->host, $data->subdomain, $data->server_host, $connection);
|
||||
$connectionInfo = $this->connectionManager->storeConnection($data->host, $data->subdomain, $data->server_host, $connection);
|
||||
|
||||
$this->connectionManager->limitConnectionLength($connectionInfo, config('expose.admin.maximum_connection_length'));
|
||||
$this->connectionManager->limitConnectionLength($connectionInfo, config('expose.admin.maximum_connection_length'));
|
||||
|
||||
$connection->send(json_encode([
|
||||
'event' => 'authenticated',
|
||||
'data' => [
|
||||
'message' => config('expose.admin.messages.message_of_the_day'),
|
||||
'subdomain' => $connectionInfo->subdomain,
|
||||
'server_host' => $connectionInfo->serverHost,
|
||||
'client_id' => $connectionInfo->client_id,
|
||||
],
|
||||
]));
|
||||
});
|
||||
$connection->send(json_encode([
|
||||
'event' => 'authenticated',
|
||||
'data' => [
|
||||
'message' => config('expose.admin.messages.message_of_the_day'),
|
||||
'subdomain' => $connectionInfo->subdomain,
|
||||
'server_host' => $connectionInfo->serverHost,
|
||||
'client_id' => $connectionInfo->client_id,
|
||||
],
|
||||
]));
|
||||
});
|
||||
}
|
||||
|
||||
protected function handleTcpConnection(ConnectionInterface $connection, $data, $user = null)
|
||||
@@ -259,6 +268,40 @@ class ControlMessageController implements MessageComponentInterface
|
||||
return $deferred->promise();
|
||||
}
|
||||
|
||||
protected function hasValidDomain(ConnectionInterface $connection, ?string $serverHost, ?array $user): PromiseInterface
|
||||
{
|
||||
if (! is_null($user) && $serverHost !== $this->configuration->hostname()) {
|
||||
$deferred = new Deferred();
|
||||
|
||||
$this->domainRepository
|
||||
->getDomainsByUserId($user['id'])
|
||||
->then(function ($domains) use ($connection, $deferred, $user, $serverHost) {
|
||||
$userDomain = collect($domains)->first(function ($domain) use ($serverHost) {
|
||||
return strtolower($domain['domain']) === strtolower($serverHost);
|
||||
});
|
||||
|
||||
if (is_null($userDomain)) {
|
||||
$connection->send(json_encode([
|
||||
'event' => 'authenticationFailed',
|
||||
'data' => [
|
||||
'message' => config('expose.admin.messages.custom_domain_unauthorized').PHP_EOL,
|
||||
],
|
||||
]));
|
||||
$connection->close();
|
||||
|
||||
$deferred->reject(null);
|
||||
return;
|
||||
}
|
||||
|
||||
$deferred->resolve(null);
|
||||
});
|
||||
|
||||
return $deferred->promise();
|
||||
} else {
|
||||
return \React\Promise\resolve(null);
|
||||
}
|
||||
}
|
||||
|
||||
protected function hasValidSubdomain(ConnectionInterface $connection, ?string $subdomain, ?array $user, string $serverHost): PromiseInterface
|
||||
{
|
||||
/**
|
||||
@@ -279,9 +322,9 @@ class ControlMessageController implements MessageComponentInterface
|
||||
* Check if the given subdomain is reserved for a different user.
|
||||
*/
|
||||
if (! is_null($subdomain)) {
|
||||
return $this->subdomainRepository->getSubdomainByName($subdomain)
|
||||
return $this->subdomainRepository->getSubdomainByNameAndDomain($subdomain, $serverHost)
|
||||
->then(function ($foundSubdomain) use ($connection, $subdomain, $user, $serverHost) {
|
||||
if (! is_null($foundSubdomain) && ! is_null($user) && $foundSubdomain['user_id'] !== $user['id']) {
|
||||
if (! is_null($foundSubdomain) && ! is_null($user) && $foundSubdomain['user_id'] !== $user['id']){
|
||||
$message = config('expose.admin.messages.subdomain_reserved');
|
||||
$message = str_replace(':subdomain', $subdomain, $message);
|
||||
|
||||
|
||||
@@ -57,6 +57,22 @@ class DatabaseSubdomainRepository implements SubdomainRepository
|
||||
return $deferred->promise();
|
||||
}
|
||||
|
||||
public function getSubdomainByNameAndDomain(string $name, string $domain): PromiseInterface
|
||||
{
|
||||
$deferred = new Deferred();
|
||||
|
||||
$this->database
|
||||
->query('SELECT * FROM subdomains WHERE subdomain = :name AND domain = :domain', [
|
||||
'name' => $name,
|
||||
'domain' => $domain
|
||||
])
|
||||
->then(function (Result $result) use ($deferred) {
|
||||
$deferred->resolve($result->rows[0] ?? null);
|
||||
});
|
||||
|
||||
return $deferred->promise();
|
||||
}
|
||||
|
||||
public function getSubdomainsByUserId($id): PromiseInterface
|
||||
{
|
||||
$deferred = new Deferred();
|
||||
@@ -85,8 +101,8 @@ class DatabaseSubdomainRepository implements SubdomainRepository
|
||||
}
|
||||
|
||||
$this->database->query("
|
||||
INSERT INTO subdomains (user_id, subdomain, created_at)
|
||||
VALUES (:user_id, :subdomain, DATETIME('now'))
|
||||
INSERT INTO subdomains (user_id, subdomain, domain, created_at)
|
||||
VALUES (:user_id, :subdomain, :domain, DATETIME('now'))
|
||||
", $data)
|
||||
->then(function (Result $result) use ($deferred) {
|
||||
$this->database->query('SELECT * FROM subdomains WHERE id = :id', ['id' => $result->insertId])
|
||||
|
||||
@@ -151,8 +151,8 @@ class DatabaseUserRepository implements UserRepository
|
||||
$deferred = new Deferred();
|
||||
|
||||
$this->database->query("
|
||||
INSERT INTO users (name, auth_token, can_specify_subdomains, can_share_tcp_ports, max_connections, created_at)
|
||||
VALUES (:name, :auth_token, :can_specify_subdomains, :can_share_tcp_ports, :max_connections, DATETIME('now'))
|
||||
INSERT INTO users (name, auth_token, can_specify_subdomains, can_specify_domains, can_share_tcp_ports, max_connections, created_at)
|
||||
VALUES (:name, :auth_token, :can_specify_subdomains, :can_specify_domains, :can_share_tcp_ports, :max_connections, DATETIME('now'))
|
||||
", $data)
|
||||
->then(function (Result $result) use ($deferred) {
|
||||
$this->database->query('SELECT * FROM users WHERE id = :id', ['id' => $result->insertId])
|
||||
|
||||
Reference in New Issue
Block a user