5 Commits

Author SHA1 Message Date
Marcel Pociot
548c29772a Merge branch 'share-files' of github.com:beyondcode/phunnel into share-files 2020-11-01 20:34:06 +01:00
Marcel Pociot
844a3cd15a Rename page title 2020-11-01 20:33:56 +01:00
Marcel Pociot
e773dfa689 Merge pull request #151 from beyondcode/analysis-64ov5W
Apply fixes from StyleCI
2020-11-01 17:44:14 +01:00
Marcel Pociot
c56f05c030 Apply fixes from StyleCI 2020-11-01 16:44:06 +00:00
Marcel Pociot
ce945e1326 Add fileserver support 2020-11-01 17:43:42 +01:00
37 changed files with 529 additions and 930 deletions

View File

@@ -21,4 +21,3 @@ ENV password=password
ENV exposeConfigPath=/src/config/expose.php ENV exposeConfigPath=/src/config/expose.php
CMD sed -i "s|username|${username}|g" ${exposeConfigPath} && sed -i "s|password|${password}|g" ${exposeConfigPath} && php expose serve ${domain} --port ${port} --validateAuthTokens CMD sed -i "s|username|${username}|g" ${exposeConfigPath} && sed -i "s|password|${password}|g" ${exposeConfigPath} && php expose serve ${domain} --port ${port} --validateAuthTokens
ENTRYPOINT ["/src/expose"]

View File

@@ -40,12 +40,12 @@ class Client
$this->logger = $logger; $this->logger = $logger;
} }
public function share(string $sharedUrl, array $subdomains = [], string $hostname = '') public function share(string $sharedUrl, array $subdomains = [])
{ {
$sharedUrl = $this->prepareSharedUrl($sharedUrl); $sharedUrl = $this->prepareSharedUrl($sharedUrl);
foreach ($subdomains as $subdomain) { foreach ($subdomains as $subdomain) {
$this->connectToServer($sharedUrl, $subdomain, $hostname, config('expose.auth_token')); $this->connectToServer($sharedUrl, $subdomain, config('expose.auth_token'));
} }
} }
@@ -72,7 +72,7 @@ class Client
return $url; return $url;
} }
public function connectToServer(string $sharedUrl, $subdomain, $hostname = '', $authToken = ''): PromiseInterface public function connectToServer(string $sharedUrl, $subdomain, $authToken = ''): PromiseInterface
{ {
$deferred = new Deferred(); $deferred = new Deferred();
$promise = $deferred->promise(); $promise = $deferred->promise();
@@ -82,18 +82,18 @@ class Client
connect($wsProtocol."://{$this->configuration->host()}:{$this->configuration->port()}/expose/control?authToken={$authToken}", [], [ 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, $hostname, $deferred, $authToken) { ->then(function (WebSocket $clientConnection) use ($sharedUrl, $subdomain, $deferred, $authToken) {
$this->connectionRetries = 0; $this->connectionRetries = 0;
$connection = ControlConnection::create($clientConnection); $connection = ControlConnection::create($clientConnection);
$connection->authenticate($sharedUrl, $subdomain, $hostname); $connection->authenticate($sharedUrl, $subdomain);
$clientConnection->on('close', function () use ($sharedUrl, $subdomain, $hostname, $authToken) { $clientConnection->on('close', function () use ($sharedUrl, $subdomain, $authToken) {
$this->logger->error('Connection to server closed.'); $this->logger->error('Connection to server closed.');
$this->retryConnectionOrExit(function () use ($sharedUrl, $subdomain, $hostname, $authToken) { $this->retryConnectionOrExit(function () use ($sharedUrl, $subdomain, $authToken) {
$this->connectToServer($sharedUrl, $subdomain, $hostname, $authToken); $this->connectToServer($sharedUrl, $subdomain, $authToken);
}); });
}); });
@@ -113,16 +113,10 @@ class Client
$host .= ":{$this->configuration->port()}"; $host .= ":{$this->configuration->port()}";
} }
if ($data->hostname !== '' && ! is_null($data->hostname)) {
$exposeUrl = "{$httpProtocol}://{$data->hostname}";
} else {
$exposeUrl = "{$httpProtocol}://{$data->subdomain}.{$host}";
}
$this->logger->info($data->message); $this->logger->info($data->message);
$this->logger->info("Local-URL:\t\t{$sharedUrl}"); $this->logger->info("Local-URL:\t\t{$sharedUrl}");
$this->logger->info("Dashboard-URL:\t\thttp://127.0.0.1:".config()->get('expose.dashboard_port')); $this->logger->info("Dashboard-URL:\t\thttp://127.0.0.1:".config()->get('expose.dashboard_port'));
$this->logger->info("Expose-URL:\t\t{$exposeUrl}"); $this->logger->info("Expose-URL:\t\t{$httpProtocol}://{$data->subdomain}.{$host}");
$this->logger->line(''); $this->logger->line('');
static::$subdomains[] = "{$httpProtocol}://{$data->subdomain}.{$host}"; static::$subdomains[] = "{$httpProtocol}://{$data->subdomain}.{$host}";

View File

@@ -57,7 +57,7 @@ class ControlConnection
$this->proxyManager->createTcpProxy($this->clientId, $data); $this->proxyManager->createTcpProxy($this->clientId, $data);
} }
public function authenticate(string $sharedHost, ?string $subdomain, ?string $hostname) public function authenticate(string $sharedHost, string $subdomain)
{ {
$this->socket->send(json_encode([ $this->socket->send(json_encode([
'event' => 'authenticate', 'event' => 'authenticate',
@@ -65,7 +65,6 @@ class ControlConnection
'type' => 'http', 'type' => 'http',
'host' => $sharedHost, 'host' => $sharedHost,
'subdomain' => empty($subdomain) ? null : $subdomain, 'subdomain' => empty($subdomain) ? null : $subdomain,
'hostname' => empty($hostname) ? null : $hostname,
], ],
])); ]));
} }

View File

@@ -2,6 +2,7 @@
namespace App\Client; namespace App\Client;
use App\Client\Fileserver\Fileserver;
use App\Client\Http\Controllers\AttachDataToLogController; use App\Client\Http\Controllers\AttachDataToLogController;
use App\Client\Http\Controllers\ClearLogsController; use App\Client\Http\Controllers\ClearLogsController;
use App\Client\Http\Controllers\CreateTunnelController; use App\Client\Http\Controllers\CreateTunnelController;
@@ -33,6 +34,9 @@ class Factory
/** @var App */ /** @var App */
protected $app; protected $app;
/** @var Fileserver */
protected $fileserver;
/** @var RouteGenerator */ /** @var RouteGenerator */
protected $router; protected $router;
@@ -102,9 +106,9 @@ class Factory
return $this; return $this;
} }
public function share($sharedUrl, $subdomain = null, $hostname = null) public function share($sharedUrl, $subdomain = null)
{ {
app('expose.client')->share($sharedUrl, $subdomain, $hostname); app('expose.client')->share($sharedUrl, $subdomain);
return $this; return $this;
} }
@@ -116,6 +120,15 @@ class Factory
return $this; return $this;
} }
public function shareFolder(string $folder, string $name, $subdomain = null)
{
$host = $this->createFileServer($folder, $name);
$this->share($host, $subdomain);
return $this;
}
protected function addRoutes() protected function addRoutes()
{ {
$this->router->get('/', DashboardController::class); $this->router->get('/', DashboardController::class);
@@ -134,18 +147,18 @@ class Factory
} }
} }
protected function detectNextFreeDashboardPort($port = 4040): int protected function detectNextAvailablePort($startPort = 4040): int
{ {
while (is_resource(@fsockopen('127.0.0.1', $port))) { while (is_resource(@fsockopen('127.0.0.1', $startPort))) {
$port++; $startPort++;
} }
return $port; return $startPort;
} }
public function createHttpServer() public function createHttpServer()
{ {
$dashboardPort = $this->detectNextFreeDashboardPort(); $dashboardPort = $this->detectNextAvailablePort();
config()->set('expose.dashboard_port', $dashboardPort); config()->set('expose.dashboard_port', $dashboardPort);
@@ -156,11 +169,25 @@ class Factory
return $this; return $this;
} }
public function createFileServer(string $folder, string $name)
{
$port = $this->detectNextAvailablePort(8090);
$this->fileserver = new Fileserver($folder, $name, $port, '0.0.0.0', $this->loop);
return "127.0.0.1:{$port}";
}
public function getApp(): App public function getApp(): App
{ {
return $this->app; return $this->app;
} }
public function getFileserver(): Fileserver
{
return $this->fileserver;
}
public function run() public function run()
{ {
$this->loop->run(); $this->loop->run();

View File

@@ -0,0 +1,135 @@
<?php
namespace App\Client\Fileserver;
use App\Http\Controllers\Concerns\LoadsViews;
use App\Http\QueryParameters;
use GuzzleHttp\Psr7\ServerRequest;
use Illuminate\Http\Request;
use Psr\Http\Message\ServerRequestInterface;
use React\EventLoop\LoopInterface;
use React\Http\Response;
use React\Stream\ReadableResourceStream;
use Symfony\Bridge\PsrHttpMessage\Factory\HttpFoundationFactory;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Finder\Iterator\FilenameFilterIterator;
class ConnectionHandler
{
use LoadsViews;
/** @var string */
protected $rootFolder;
/** @var string */
protected $name;
/** @var LoopInterface */
protected $loop;
public function __construct(string $rootFolder, string $name, LoopInterface $loop)
{
$this->rootFolder = $rootFolder;
$this->name = $name;
$this->loop = $loop;
}
public function handle(ServerRequestInterface $request)
{
$request = $this->createLaravelRequest($request);
$targetPath = realpath($this->rootFolder.DIRECTORY_SEPARATOR.$request->path());
if (! $this->isValidTarget($targetPath)) {
return new Response(404);
}
if (is_dir($targetPath)) {
// Directory listing
$directoryContent = Finder::create()
->depth(0)
->sort(function ($a, $b) {
return strcmp(strtolower($a->getRealpath()), strtolower($b->getRealpath()));
})
->in($targetPath);
if ($this->name !== '') {
$directoryContent->name($this->name);
}
$parentPath = explode('/', $request->path());
array_pop($parentPath);
$parentPath = implode('/', $parentPath);
return new Response(
200,
['Content-Type' => 'text/html'],
$this->getView(null, 'client.fileserver', [
'currentPath' => $request->path(),
'parentPath' => $parentPath,
'directory' => $targetPath,
'directoryContent' => $directoryContent,
])
);
}
if (is_file($targetPath)) {
return new Response(
200,
['Content-Type' => mime_content_type($targetPath)],
new ReadableResourceStream(fopen($targetPath, 'r'), $this->loop)
);
}
}
protected function isValidTarget(string $targetPath): bool
{
if (! file_exists($targetPath)) {
return false;
}
if ($this->name !== '') {
$filter = new class(basename($targetPath), [$this->name]) extends FilenameFilterIterator {
protected $filename;
public function __construct(string $filename, array $matchPatterns)
{
$this->filename = $filename;
foreach ($matchPatterns as $pattern) {
$this->matchRegexps[] = $this->toRegex($pattern);
}
}
public function accept()
{
return $this->isAccepted($this->filename);
}
};
return $filter->accept();
}
return true;
}
protected function createLaravelRequest(ServerRequestInterface $request): Request
{
try {
parse_str($request->getBody(), $bodyParameters);
} catch (\Throwable $e) {
$bodyParameters = [];
}
$serverRequest = (new ServerRequest(
$request->getMethod(),
$request->getUri(),
$request->getHeaders(),
$request->getBody(),
$request->getProtocolVersion(),
))
->withQueryParams(QueryParameters::create($request)->all())
->withParsedBody($bodyParameters);
return Request::createFromBase((new HttpFoundationFactory)->createRequest($serverRequest));
}
}

View File

@@ -0,0 +1,30 @@
<?php
namespace App\Client\Fileserver;
use Psr\Http\Message\ServerRequestInterface;
use React\EventLoop\LoopInterface;
use React\Http\Server;
use React\Socket\Server as SocketServer;
class Fileserver
{
/** @var SocketServer */
protected $socket;
public function __construct($rootFolder, $name, $port, $address, LoopInterface $loop)
{
$server = new Server(function (ServerRequestInterface $request) use ($rootFolder, $name, $loop) {
return (new ConnectionHandler($rootFolder, $name, $loop))->handle($request);
});
$this->socket = new SocketServer("{$address}:{$port}", $loop);
$server->listen($this->socket);
}
public function getSocket(): SocketServer
{
return $this->socket;
}
}

View File

@@ -10,7 +10,7 @@ use Symfony\Component\Console\Output\ConsoleOutput;
class ShareCommand extends Command class ShareCommand extends Command
{ {
protected $signature = 'share {host} {--hostname=} {--subdomain=} {--auth=}'; protected $signature = 'share {host} {--subdomain=} {--auth=}';
protected $description = 'Share a local url with a remote expose server'; protected $description = 'Share a local url with a remote expose server';
@@ -25,12 +25,6 @@ class ShareCommand extends Command
public function handle() public function handle()
{ {
if (! empty($this->option('hostname')) && ! empty($this->option('subdomain'))) {
$this->error('You can only specify one. Either a custom hostname or a subdomain.');
return;
}
$this->configureConnectionLogger(); $this->configureConnectionLogger();
(new Factory()) (new Factory())
@@ -39,11 +33,7 @@ class ShareCommand extends Command
->setPort(config('expose.port', 8080)) ->setPort(config('expose.port', 8080))
->setAuth($this->option('auth')) ->setAuth($this->option('auth'))
->createClient() ->createClient()
->share( ->share($this->argument('host'), explode(',', $this->option('subdomain')))
$this->argument('host'),
explode(',', $this->option('subdomain')),
$this->option('hostname')
)
->createHttpServer() ->createHttpServer()
->run(); ->run();
} }

View File

@@ -4,7 +4,7 @@ namespace App\Commands;
class ShareCurrentWorkingDirectoryCommand extends ShareCommand class ShareCurrentWorkingDirectoryCommand extends ShareCommand
{ {
protected $signature = 'share-cwd {host?} {--hostname=} {--subdomain=} {--auth=}'; protected $signature = 'share-cwd {host?} {--subdomain=} {--auth=}';
public function handle() public function handle()
{ {
@@ -13,7 +13,7 @@ class ShareCurrentWorkingDirectoryCommand extends ShareCommand
$this->input->setArgument('host', $host); $this->input->setArgument('host', $host);
if (! $this->option('subdomain') && ! $this->option('hostname')) { if (! $this->option('subdomain')) {
$this->input->setOption('subdomain', $subdomain); $this->input->setOption('subdomain', $subdomain);
} }

View File

@@ -0,0 +1,48 @@
<?php
namespace App\Commands;
use App\Client\Factory;
use App\Logger\CliRequestLogger;
use LaravelZero\Framework\Commands\Command;
use React\EventLoop\LoopInterface;
use Symfony\Component\Console\Output\ConsoleOutput;
class ShareFilesCommand extends Command
{
protected $signature = 'share-files {folder=.} {--name=} {--subdomain=} {--auth=}';
protected $description = 'Share a local folder with a remote expose server';
protected function configureConnectionLogger()
{
app()->bind(CliRequestLogger::class, function () {
return new CliRequestLogger(new ConsoleOutput());
});
return $this;
}
public function handle()
{
if (! is_dir($this->argument('folder'))) {
throw new \InvalidArgumentException('The folder '.$this->argument('folder').' does not exist.');
}
$this->configureConnectionLogger();
(new Factory())
->setLoop(app(LoopInterface::class))
->setHost(config('expose.host', 'localhost'))
->setPort(config('expose.port', 8080))
->setAuth($this->option('auth'))
->createClient()
->shareFolder(
$this->argument('folder'),
$this->option('name') ?? '',
explode(',', $this->option('subdomain'))
)
->createHttpServer()
->run();
}
}

View File

@@ -8,7 +8,7 @@ use Ratchet\ConnectionInterface;
interface ConnectionManager interface ConnectionManager
{ {
public function storeConnection(string $host, ?string $subdomain, ?string $hostname, ConnectionInterface $connection): ControlConnection; public function storeConnection(string $host, ?string $subdomain, ConnectionInterface $connection): ControlConnection;
public function storeTcpConnection(int $port, ConnectionInterface $connection): ControlConnection; public function storeTcpConnection(int $port, ConnectionInterface $connection): ControlConnection;
@@ -22,8 +22,6 @@ interface ConnectionManager
public function findControlConnectionForSubdomain($subdomain): ?ControlConnection; public function findControlConnectionForSubdomain($subdomain): ?ControlConnection;
public function findControlConnectionForHostname(string $hostname): ?ControlConnection;
public function findControlConnectionForClientId(string $clientId): ?ControlConnection; public function findControlConnectionForClientId(string $clientId): ?ControlConnection;
public function getConnections(): array; public function getConnections(): array;

View File

@@ -1,22 +0,0 @@
<?php
namespace App\Contracts;
use React\Promise\PromiseInterface;
interface HostnameRepository
{
public function getHostnames(): PromiseInterface;
public function getHostnameById($id): PromiseInterface;
public function getHostnameByName(string $name): PromiseInterface;
public function getHostnamesByUserId($id): PromiseInterface;
public function getHostnamesByUserIdAndName($id, $name): PromiseInterface;
public function deleteHostnameForUserId($userId, $hostnameId): PromiseInterface;
public function storeHostname(array $data): PromiseInterface;
}

View File

@@ -9,7 +9,7 @@ use Twig\Loader\ArrayLoader;
trait LoadsViews trait LoadsViews
{ {
protected function getView(ConnectionInterface $connection, string $view, array $data = []) protected function getView(?ConnectionInterface $connection, string $view, array $data = [])
{ {
$templatePath = implode(DIRECTORY_SEPARATOR, explode('.', $view)); $templatePath = implode(DIRECTORY_SEPARATOR, explode('.', $view));
@@ -23,7 +23,10 @@ trait LoadsViews
$data = array_merge($data, [ $data = array_merge($data, [
'request' => $connection->laravelRequest ?? null, 'request' => $connection->laravelRequest ?? null,
]); ]);
try {
return stream_for($twig->render('template', $data)); return stream_for($twig->render('template', $data));
} catch (\Throwable $e) {
var_dump($e->getMessage());
}
} }
} }

View File

@@ -1,35 +0,0 @@
<?php
namespace App\Server\Connections;
class ConnectionConfiguration
{
protected $hostname;
protected $subdomain;
private function __construct($subdomain, $hostname)
{
$this->subdomain = $subdomain;
$this->hostname = $hostname;
}
public static function withSubdomain($subdomain)
{
return new static($subdomain, null);
}
public static function withHostname($hostname)
{
return new static(null, $hostname);
}
public function getSubdomain()
{
return $this->subdomain;
}
public function getHostname()
{
return $this->hostname;
}
}

View File

@@ -43,23 +43,16 @@ class ConnectionManager implements ConnectionManagerContract
}); });
} }
public function storeConnection(string $host, ?string $subdomain, ?string $hostname, ConnectionInterface $connection): ControlConnection public function storeConnection(string $host, ?string $subdomain, ConnectionInterface $connection): ControlConnection
{ {
$clientId = (string) uniqid(); $clientId = (string) uniqid();
$connection->client_id = $clientId; $connection->client_id = $clientId;
if (! is_null($hostname) && $hostname !== '') {
$subdomain = '';
} else {
$subdomain = $subdomain ?? $this->subdomainGenerator->generateSubdomain();
}
$storedConnection = new ControlConnection( $storedConnection = new ControlConnection(
$connection, $connection,
$host, $host,
$subdomain, $subdomain ?? $this->subdomainGenerator->generateSubdomain(),
$hostname,
$clientId, $clientId,
$this->getAuthTokenFromConnection($connection) $this->getAuthTokenFromConnection($connection)
); );
@@ -157,13 +150,6 @@ class ConnectionManager implements ConnectionManagerContract
}); });
} }
public function findControlConnectionForHostname($hostname): ?ControlConnection
{
return collect($this->connections)->last(function ($connection) use ($hostname) {
return $connection->hostname == $hostname;
});
}
public function findControlConnectionForClientId(string $clientId): ?ControlConnection public function findControlConnectionForClientId(string $clientId): ?ControlConnection
{ {
return collect($this->connections)->last(function ($connection) use ($clientId) { return collect($this->connections)->last(function ($connection) use ($clientId) {

View File

@@ -14,17 +14,15 @@ class ControlConnection
public $host; public $host;
public $authToken; public $authToken;
public $subdomain; public $subdomain;
public $hostname;
public $client_id; public $client_id;
public $proxies = []; public $proxies = [];
protected $shared_at; protected $shared_at;
public function __construct(ConnectionInterface $socket, string $host, string $subdomain, ?string $hostname, string $clientId, string $authToken = '') public function __construct(ConnectionInterface $socket, string $host, string $subdomain, string $clientId, string $authToken = '')
{ {
$this->socket = $socket; $this->socket = $socket;
$this->host = $host; $this->host = $host;
$this->subdomain = $subdomain; $this->subdomain = $subdomain;
$this->hostname = $hostname;
$this->client_id = $clientId; $this->client_id = $clientId;
$this->authToken = $authToken; $this->authToken = $authToken;
$this->shared_at = now()->toDateTimeString(); $this->shared_at = now()->toDateTimeString();
@@ -66,7 +64,6 @@ class ControlConnection
'client_id' => $this->client_id, 'client_id' => $this->client_id,
'auth_token' => $this->authToken, 'auth_token' => $this->authToken,
'subdomain' => $this->subdomain, 'subdomain' => $this->subdomain,
'hostname' => $this->hostname,
'shared_at' => $this->shared_at, 'shared_at' => $this->shared_at,
]; ];
} }

View File

@@ -3,14 +3,12 @@
namespace App\Server; namespace App\Server;
use App\Contracts\ConnectionManager as ConnectionManagerContract; use App\Contracts\ConnectionManager as ConnectionManagerContract;
use App\Contracts\HostnameRepository;
use App\Contracts\SubdomainGenerator; use App\Contracts\SubdomainGenerator;
use App\Contracts\SubdomainRepository; use App\Contracts\SubdomainRepository;
use App\Contracts\UserRepository; use App\Contracts\UserRepository;
use App\Http\RouteGenerator; use App\Http\RouteGenerator;
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\DeleteHostnameController;
use App\Server\Http\Controllers\Admin\DeleteSubdomainController; use App\Server\Http\Controllers\Admin\DeleteSubdomainController;
use App\Server\Http\Controllers\Admin\DeleteUsersController; use App\Server\Http\Controllers\Admin\DeleteUsersController;
use App\Server\Http\Controllers\Admin\DisconnectSiteController; use App\Server\Http\Controllers\Admin\DisconnectSiteController;
@@ -25,7 +23,6 @@ use App\Server\Http\Controllers\Admin\ListTcpConnectionsController;
use App\Server\Http\Controllers\Admin\ListUsersController; use App\Server\Http\Controllers\Admin\ListUsersController;
use App\Server\Http\Controllers\Admin\RedirectToUsersController; use App\Server\Http\Controllers\Admin\RedirectToUsersController;
use App\Server\Http\Controllers\Admin\ShowSettingsController; use App\Server\Http\Controllers\Admin\ShowSettingsController;
use App\Server\Http\Controllers\Admin\StoreHostnameController;
use App\Server\Http\Controllers\Admin\StoreSettingsController; use App\Server\Http\Controllers\Admin\StoreSettingsController;
use App\Server\Http\Controllers\Admin\StoreSubdomainController; use App\Server\Http\Controllers\Admin\StoreSubdomainController;
use App\Server\Http\Controllers\Admin\StoreUsersController; use App\Server\Http\Controllers\Admin\StoreUsersController;
@@ -138,8 +135,6 @@ class Factory
$this->router->get('/api/users/{id}', GetUserDetailsController::class, $adminCondition); $this->router->get('/api/users/{id}', GetUserDetailsController::class, $adminCondition);
$this->router->post('/api/subdomains', StoreSubdomainController::class, $adminCondition); $this->router->post('/api/subdomains', StoreSubdomainController::class, $adminCondition);
$this->router->delete('/api/subdomains/{subdomain}', DeleteSubdomainController::class, $adminCondition); $this->router->delete('/api/subdomains/{subdomain}', DeleteSubdomainController::class, $adminCondition);
$this->router->post('/api/hostnames', StoreHostnameController::class, $adminCondition);
$this->router->delete('/api/hostnames/{hostname}', DeleteHostnameController::class, $adminCondition);
$this->router->delete('/api/users/{id}', DeleteUsersController::class, $adminCondition); $this->router->delete('/api/users/{id}', DeleteUsersController::class, $adminCondition);
$this->router->get('/api/sites', GetSitesController::class, $adminCondition); $this->router->get('/api/sites', GetSitesController::class, $adminCondition);
$this->router->delete('/api/sites/{id}', DisconnectSiteController::class, $adminCondition); $this->router->delete('/api/sites/{id}', DisconnectSiteController::class, $adminCondition);
@@ -182,7 +177,6 @@ class Factory
->bindSubdomainGenerator() ->bindSubdomainGenerator()
->bindUserRepository() ->bindUserRepository()
->bindSubdomainRepository() ->bindSubdomainRepository()
->bindHostnameRepository()
->bindDatabase() ->bindDatabase()
->ensureDatabaseIsInitialized() ->ensureDatabaseIsInitialized()
->bindConnectionManager() ->bindConnectionManager()
@@ -228,15 +222,6 @@ class Factory
return $this; return $this;
} }
protected function bindHostnameRepository()
{
app()->singleton(HostnameRepository::class, function () {
return app(config('expose.admin.hostname_repository'));
});
return $this;
}
protected function bindDatabase() protected function bindDatabase()
{ {
app()->singleton(DatabaseInterface::class, function () { app()->singleton(DatabaseInterface::class, function () {
@@ -263,7 +248,6 @@ class Factory
->files() ->files()
->ignoreDotFiles(true) ->ignoreDotFiles(true)
->in(database_path('migrations')) ->in(database_path('migrations'))
->sortByName()
->name('*.sql'); ->name('*.sql');
/** @var SplFileInfo $migration */ /** @var SplFileInfo $migration */

View File

@@ -1,132 +0,0 @@
<?php
namespace App\Server\HostnameRepository;
use App\Contracts\HostnameRepository;
use Clue\React\SQLite\DatabaseInterface;
use Clue\React\SQLite\Result;
use React\Promise\Deferred;
use React\Promise\PromiseInterface;
class DatabaseHostnameRepository implements HostnameRepository
{
/** @var DatabaseInterface */
protected $database;
public function __construct(DatabaseInterface $database)
{
$this->database = $database;
}
public function getHostnames(): PromiseInterface
{
$deferred = new Deferred();
$this->database
->query('SELECT * FROM hostnames ORDER by created_at DESC')
->then(function (Result $result) use ($deferred) {
$deferred->resolve($result->rows);
});
return $deferred->promise();
}
public function getHostnameById($id): PromiseInterface
{
$deferred = new Deferred();
$this->database
->query('SELECT * FROM hostnames WHERE id = :id', ['id' => $id])
->then(function (Result $result) use ($deferred) {
$deferred->resolve($result->rows[0] ?? null);
});
return $deferred->promise();
}
public function getHostnameByName(string $name): PromiseInterface
{
$deferred = new Deferred();
$this->database
->query('SELECT * FROM hostnames WHERE hostname = :name', ['name' => $name])
->then(function (Result $result) use ($deferred) {
$deferred->resolve($result->rows[0] ?? null);
});
return $deferred->promise();
}
public function getHostnamesByUserId($id): PromiseInterface
{
$deferred = new Deferred();
$this->database
->query('SELECT * FROM hostnames 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 storeHostname(array $data): PromiseInterface
{
$deferred = new Deferred();
$this->getHostnameByName($data['hostname'])
->then(function ($registeredHostname) use ($data, $deferred) {
if (! is_null($registeredHostname)) {
$deferred->resolve(null);
return;
}
$this->database->query("
INSERT INTO hostnames (user_id, hostname, created_at)
VALUES (:user_id, :hostname, DATETIME('now'))
", $data)
->then(function (Result $result) use ($deferred) {
$this->database->query('SELECT * FROM hostnames WHERE id = :id', ['id' => $result->insertId])
->then(function (Result $result) use ($deferred) {
$deferred->resolve($result->rows[0]);
});
});
});
return $deferred->promise();
}
public function getHostnamesByUserIdAndName($id, $name): PromiseInterface
{
$deferred = new Deferred();
$this->database
->query('SELECT * FROM hostnames WHERE user_id = :user_id AND hostname = :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 deleteHostnameForUserId($userId, $hostnameId): PromiseInterface
{
$deferred = new Deferred();
$this->database->query('DELETE FROM hostnames WHERE id = :id AND user_id = :user_id', [
'id' => $hostnameId,
'user_id' => $userId,
])
->then(function (Result $result) use ($deferred) {
$deferred->resolve($result);
});
return $deferred->promise();
}
}

View File

@@ -1,44 +0,0 @@
<?php
namespace App\Server\Http\Controllers\Admin;
use App\Contracts\HostnameRepository;
use App\Contracts\UserRepository;
use Illuminate\Http\Request;
use Ratchet\ConnectionInterface;
class DeleteHostnameController extends AdminController
{
protected $keepConnectionOpen = true;
/** @var HostnameRepository */
protected $hostnameRepository;
/** @var UserRepository */
protected $userRepository;
public function __construct(UserRepository $userRepository, HostnameRepository $hostnameRepository)
{
$this->userRepository = $userRepository;
$this->hostnameRepository = $hostnameRepository;
}
public function handle(Request $request, ConnectionInterface $httpConnection)
{
$this->userRepository->getUserByToken($request->get('auth_token', ''))
->then(function ($user) use ($request, $httpConnection) {
if (is_null($user)) {
$httpConnection->send(respond_json(['error' => 'The user does not exist'], 404));
$httpConnection->close();
return;
}
$this->hostnameRepository->deleteHostnameForUserId($user['id'], $request->get('hostname'))
->then(function ($deleted) use ($httpConnection) {
$httpConnection->send(respond_json(['deleted' => $deleted], 200));
$httpConnection->close();
});
});
}
}

View File

@@ -2,7 +2,6 @@
namespace App\Server\Http\Controllers\Admin; namespace App\Server\Http\Controllers\Admin;
use App\Contracts\HostnameRepository;
use App\Contracts\SubdomainRepository; use App\Contracts\SubdomainRepository;
use App\Contracts\UserRepository; use App\Contracts\UserRepository;
use Illuminate\Http\Request; use Illuminate\Http\Request;
@@ -18,14 +17,10 @@ class GetUserDetailsController extends AdminController
/** @var SubdomainRepository */ /** @var SubdomainRepository */
protected $subdomainRepository; protected $subdomainRepository;
/** @var HostnameRepository */ public function __construct(UserRepository $userRepository, SubdomainRepository $subdomainRepository)
protected $hostnameRepository;
public function __construct(UserRepository $userRepository, SubdomainRepository $subdomainRepository, HostnameRepository $hostnameRepository)
{ {
$this->userRepository = $userRepository; $this->userRepository = $userRepository;
$this->subdomainRepository = $subdomainRepository; $this->subdomainRepository = $subdomainRepository;
$this->hostnameRepository = $hostnameRepository;
} }
public function handle(Request $request, ConnectionInterface $httpConnection) public function handle(Request $request, ConnectionInterface $httpConnection)
@@ -34,19 +29,15 @@ class GetUserDetailsController extends AdminController
->getUserById($request->get('id')) ->getUserById($request->get('id'))
->then(function ($user) use ($httpConnection, $request) { ->then(function ($user) use ($httpConnection, $request) {
$this->subdomainRepository->getSubdomainsByUserId($request->get('id')) $this->subdomainRepository->getSubdomainsByUserId($request->get('id'))
->then(function ($subdomains) use ($httpConnection, $user, $request) { ->then(function ($subdomains) use ($httpConnection, $user) {
$this->hostnameRepository->getHostnamesByUserId($request->get('id')) $httpConnection->send(
->then(function ($hostnames) use ($httpConnection, $user, $subdomains) { respond_json([
$httpConnection->send( 'user' => $user,
respond_json([ 'subdomains' => $subdomains,
'user' => $user, ])
'subdomains' => $subdomains, );
'hostnames' => $hostnames,
])
);
$httpConnection->close(); $httpConnection->close();
});
}); });
}); });
} }

View File

@@ -1,77 +0,0 @@
<?php
namespace App\Server\Http\Controllers\Admin;
use App\Contracts\HostnameRepository;
use App\Contracts\UserRepository;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Ratchet\ConnectionInterface;
class StoreHostnameController extends AdminController
{
protected $keepConnectionOpen = true;
/** @var HostnameRepository */
protected $hostnameRepository;
/** @var UserRepository */
protected $userRepository;
public function __construct(UserRepository $userRepository, HostnameRepository $hostnameRepository)
{
$this->userRepository = $userRepository;
$this->hostnameRepository = $hostnameRepository;
}
public function handle(Request $request, ConnectionInterface $httpConnection)
{
$validator = Validator::make($request->all(), [
'hostname' => '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_hostnames'] === 0) {
$httpConnection->send(respond_json(['error' => 'The user is not allowed to reserve hostnames.'], 401));
$httpConnection->close();
return;
}
$insertData = [
'user_id' => $user['id'],
'hostname' => $request->get('hostname'),
];
$this->hostnameRepository
->storeHostname($insertData)
->then(function ($hostname) use ($httpConnection) {
if (is_null($hostname)) {
$httpConnection->send(respond_json(['error' => 'The hostname is already taken.'], 422));
$httpConnection->close();
return;
}
$httpConnection->send(respond_json(['hostname' => $hostname], 200));
$httpConnection->close();
});
});
}
}

View File

@@ -39,7 +39,6 @@ class StoreUsersController extends AdminController
$insertData = [ $insertData = [
'name' => $request->get('name'), 'name' => $request->get('name'),
'auth_token' => (string) Str::uuid(), 'auth_token' => (string) Str::uuid(),
'can_specify_hostnames' => (int) $request->get('can_specify_hostnames'),
'can_specify_subdomains' => (int) $request->get('can_specify_subdomains'), 'can_specify_subdomains' => (int) $request->get('can_specify_subdomains'),
'can_share_tcp_ports' => (int) $request->get('can_share_tcp_ports'), 'can_share_tcp_ports' => (int) $request->get('can_share_tcp_ports'),
]; ];

View File

@@ -3,19 +3,14 @@
namespace App\Server\Http\Controllers; namespace App\Server\Http\Controllers;
use App\Contracts\ConnectionManager; use App\Contracts\ConnectionManager;
use App\Contracts\HostnameRepository;
use App\Contracts\SubdomainRepository; use App\Contracts\SubdomainRepository;
use App\Contracts\UserRepository; use App\Contracts\UserRepository;
use App\Http\QueryParameters; use App\Http\QueryParameters;
use App\Server\Connections\ConnectionConfiguration;
use App\Server\Exceptions\NoFreePortAvailable; use App\Server\Exceptions\NoFreePortAvailable;
use Illuminate\Support\Str;
use Ratchet\ConnectionInterface; use Ratchet\ConnectionInterface;
use Ratchet\WebSocket\MessageComponentInterface; use Ratchet\WebSocket\MessageComponentInterface;
use React\Promise\Deferred; use React\Promise\Deferred;
use React\Promise\PromiseInterface; use React\Promise\PromiseInterface;
use function React\Promise\reject;
use function React\Promise\resolve as resolvePromise;
use stdClass; use stdClass;
class ControlMessageController implements MessageComponentInterface class ControlMessageController implements MessageComponentInterface
@@ -29,15 +24,11 @@ class ControlMessageController implements MessageComponentInterface
/** @var SubdomainRepository */ /** @var SubdomainRepository */
protected $subdomainRepository; protected $subdomainRepository;
/** @var HostnameRepository */ public function __construct(ConnectionManager $connectionManager, UserRepository $userRepository, SubdomainRepository $subdomainRepository)
protected $hostnameRepository;
public function __construct(ConnectionManager $connectionManager, UserRepository $userRepository, SubdomainRepository $subdomainRepository, HostnameRepository $hostnameRepository)
{ {
$this->connectionManager = $connectionManager; $this->connectionManager = $connectionManager;
$this->userRepository = $userRepository; $this->userRepository = $userRepository;
$this->subdomainRepository = $subdomainRepository; $this->subdomainRepository = $subdomainRepository;
$this->hostnameRepository = $hostnameRepository;
} }
/** /**
@@ -114,25 +105,26 @@ class ControlMessageController implements MessageComponentInterface
protected function handleHttpConnection(ConnectionInterface $connection, $data, $user = null) protected function handleHttpConnection(ConnectionInterface $connection, $data, $user = null)
{ {
$this->hasValidConfiguration($connection, $data, $user) $this->hasValidSubdomain($connection, $data->subdomain, $user)->then(function ($subdomain) use ($data, $connection) {
->then(function (ConnectionConfiguration $configuration) use ($data, $connection) { if ($subdomain === false) {
$data->subdomain = $configuration->getSubdomain(); return;
$data->hostname = $configuration->getHostname(); }
$connectionInfo = $this->connectionManager->storeConnection($data->host, $data->subdomain, $data->hostname, $connection); $data->subdomain = $subdomain;
$this->connectionManager->limitConnectionLength($connectionInfo, config('expose.admin.maximum_connection_length')); $connectionInfo = $this->connectionManager->storeConnection($data->host, $data->subdomain, $connection);
$connection->send(json_encode([ $this->connectionManager->limitConnectionLength($connectionInfo, config('expose.admin.maximum_connection_length'));
'event' => 'authenticated',
'data' => [ $connection->send(json_encode([
'message' => config('expose.admin.messages.message_of_the_day'), 'event' => 'authenticated',
'subdomain' => $connectionInfo->subdomain, 'data' => [
'hostname' => $connectionInfo->hostname, 'message' => config('expose.admin.messages.message_of_the_day'),
'client_id' => $connectionInfo->client_id, 'subdomain' => $connectionInfo->subdomain,
], 'client_id' => $connectionInfo->client_id,
],
])); ]));
}); });
} }
protected function handleTcpConnection(ConnectionInterface $connection, $data, $user = null) protected function handleTcpConnection(ConnectionInterface $connection, $data, $user = null)
@@ -200,7 +192,7 @@ class ControlMessageController implements MessageComponentInterface
protected function verifyAuthToken(ConnectionInterface $connection): PromiseInterface protected function verifyAuthToken(ConnectionInterface $connection): PromiseInterface
{ {
if (config('expose.admin.validate_auth_tokens') !== true) { if (config('expose.admin.validate_auth_tokens') !== true) {
return resolvePromise(null); return \React\Promise\resolve(null);
} }
$deferred = new Deferred(); $deferred = new Deferred();
@@ -233,7 +225,7 @@ class ControlMessageController implements MessageComponentInterface
], ],
])); ]));
return resolvePromise(ConnectionConfiguration::withSubdomain(null)); return \React\Promise\resolve(null);
} }
/** /**
@@ -254,7 +246,7 @@ class ControlMessageController implements MessageComponentInterface
])); ]));
$connection->close(); $connection->close();
return reject(false); return \React\Promise\resolve(false);
} }
$controlConnection = $this->connectionManager->findControlConnectionForSubdomain($subdomain); $controlConnection = $this->connectionManager->findControlConnectionForSubdomain($subdomain);
@@ -271,75 +263,14 @@ class ControlMessageController implements MessageComponentInterface
])); ]));
$connection->close(); $connection->close();
return reject(false); return \React\Promise\resolve(false);
} }
return resolvePromise(ConnectionConfiguration::withSubdomain($subdomain)); return \React\Promise\resolve($subdomain);
}); });
} }
return resolvePromise(ConnectionConfiguration::withSubdomain($subdomain)); return \React\Promise\resolve($subdomain);
}
protected function hasValidHostname(ConnectionInterface $connection, string $hostname, ?array $user): PromiseInterface
{
/**
* Check if the user can specify a custom hostname in the first place.
*/
if (! is_null($user) && $user['can_specify_hostnames'] === 0) {
$connection->send(json_encode([
'event' => 'info',
'data' => [
'message' => config('expose.admin.messages.custom_hostname_unauthorized').PHP_EOL,
],
]));
return reject();
}
/**
* Check if the given hostname is reserved for a different user.
*/
return $this->hostnameRepository->getHostnamesByUserId($user['id'])
->then(function ($foundHostnames) use ($connection, $hostname) {
$foundHostname = collect($foundHostnames)->first(function ($foundHostname) use ($hostname) {
return Str::is($foundHostname['hostname'], $hostname);
});
if (is_null($foundHostname)) {
$message = config('expose.admin.messages.hostname_invalid');
$message = str_replace(':hostname', $hostname, $message);
$connection->send(json_encode([
'event' => 'hostnameTaken',
'data' => [
'message' => $message,
],
]));
$connection->close();
return reject(false);
}
$controlConnection = $this->connectionManager->findControlConnectionForHostname($hostname);
if (! is_null($controlConnection)) {
$message = config('expose.admin.messages.hostname_taken');
$message = str_replace(':hostname', $hostname, $message);
$connection->send(json_encode([
'event' => 'hostnameTaken',
'data' => [
'message' => $message,
],
]));
$connection->close();
return reject(false);
}
return resolvePromise(ConnectionConfiguration::withHostname($hostname));
});
} }
protected function canShareTcpPorts(ConnectionInterface $connection, $data, $user) protected function canShareTcpPorts(ConnectionInterface $connection, $data, $user)
@@ -358,13 +289,4 @@ class ControlMessageController implements MessageComponentInterface
return true; return true;
} }
protected function hasValidConfiguration(ConnectionInterface $connection, $data, $user)
{
if (isset($data->hostname) && ! is_null($data->hostname)) {
return $this->hasValidHostname($connection, $data->hostname, $user);
}
return $this->hasValidSubdomain($connection, $data->subdomain, $user);
}
} }

View File

@@ -36,9 +36,8 @@ class TunnelMessageController extends Controller
public function handle(Request $request, ConnectionInterface $httpConnection) public function handle(Request $request, ConnectionInterface $httpConnection)
{ {
$subdomain = $this->detectSubdomain($request); $subdomain = $this->detectSubdomain($request);
$hostname = $request->getHost();
if (is_null($subdomain) && $hostname === $this->configuration->hostname()) { if (is_null($subdomain)) {
$httpConnection->send( $httpConnection->send(
respond_html($this->getView($httpConnection, 'server.homepage'), 200) respond_html($this->getView($httpConnection, 'server.homepage'), 200)
); );
@@ -47,11 +46,7 @@ class TunnelMessageController extends Controller
return; return;
} }
if (! is_null($subdomain)) { $controlConnection = $this->connectionManager->findControlConnectionForSubdomain($subdomain);
$controlConnection = $this->connectionManager->findControlConnectionForSubdomain($subdomain);
} else {
$controlConnection = $this->connectionManager->findControlConnectionForHostname($hostname);
}
if (is_null($controlConnection)) { if (is_null($controlConnection)) {
$httpConnection->send( $httpConnection->send(
@@ -118,19 +113,13 @@ class TunnelMessageController extends Controller
$host .= ":{$this->configuration->port()}"; $host .= ":{$this->configuration->port()}";
} }
if (empty($controlConnection->subdomain)) {
$originalHost = $controlConnection->hostname;
} else {
$originalHost = "{$controlConnection->subdomain}.{$host}";
}
$request->headers->set('Host', $controlConnection->host); $request->headers->set('Host', $controlConnection->host);
$request->headers->set('X-Forwarded-Proto', $request->isSecure() ? 'https' : 'http'); $request->headers->set('X-Forwarded-Proto', $request->isSecure() ? 'https' : 'http');
$request->headers->set('X-Expose-Request-ID', uniqid()); $request->headers->set('X-Expose-Request-ID', uniqid());
$request->headers->set('Upgrade-Insecure-Requests', 1); $request->headers->set('Upgrade-Insecure-Requests', 1);
$request->headers->set('X-Exposed-By', config('app.name').' '.config('app.version')); $request->headers->set('X-Exposed-By', config('app.name').' '.config('app.version'));
$request->headers->set('X-Original-Host', $originalHost); $request->headers->set('X-Original-Host', "{$controlConnection->subdomain}.{$host}");
$request->headers->set('X-Forwarded-Host', $originalHost); $request->headers->set('X-Forwarded-Host', "{$controlConnection->subdomain}.{$host}");
return $request; return $request;
} }

View File

@@ -114,8 +114,8 @@ class DatabaseUserRepository implements UserRepository
$deferred = new Deferred(); $deferred = new Deferred();
$this->database->query(" $this->database->query("
INSERT INTO users (name, auth_token, can_specify_subdomains, can_specify_hostnames, can_share_tcp_ports, created_at) INSERT INTO users (name, auth_token, can_specify_subdomains, can_share_tcp_ports, created_at)
VALUES (:name, :auth_token, :can_specify_subdomains, :can_specify_hostnames, :can_share_tcp_ports, DATETIME('now')) VALUES (:name, :auth_token, :can_specify_subdomains, :can_share_tcp_ports, DATETIME('now'))
", $data) ", $data)
->then(function (Result $result) use ($deferred) { ->then(function (Result $result) use ($deferred) {
$this->database->query('SELECT * FROM users WHERE id = :id', ['id' => $result->insertId]) $this->database->query('SELECT * FROM users WHERE id = :id', ['id' => $result->insertId])

View File

@@ -234,8 +234,6 @@ return [
'subdomain_repository' => \App\Server\SubdomainRepository\DatabaseSubdomainRepository::class, 'subdomain_repository' => \App\Server\SubdomainRepository\DatabaseSubdomainRepository::class,
'hostname_repository' => \App\Server\HostnameRepository\DatabaseHostnameRepository::class,
/* /*
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
| Messages | Messages

View File

@@ -1 +0,0 @@
ALTER TABLE users ADD can_specify_hostnames BOOLEAN DEFAULT 1;

View File

@@ -1,7 +0,0 @@
CREATE TABLE IF NOT EXISTS hostnames (
id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id INTEGER NOT NULL,
hostname STRING NOT NULL,
created_at DATETIME,
updated_at DATETIME
)

View File

@@ -0,0 +1,93 @@
<html lang="en">
<head>
<title>Expose Fileserver</title>
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.17/dist/vue.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@tailwindcss/ui@latest/dist/tailwind-ui.min.css">
<script src="https://cdn.jsdelivr.net/npm/clipboard@2/dist/clipboard.min.js"></script>
<link rel="stylesheet" href="//cdn.jsdelivr.net/gh/highlightjs/cdn-release@10.1.0/build/styles/github.min.css">
<script src="//cdn.jsdelivr.net/gh/highlightjs/cdn-release@10.1.0/build/highlight.min.js" async></script>
</head>
<body>
<div id="app" class="">
<div class="relative bg-indigo-600" style="marign-left: -1px">
<div class="max-w-screen-xl mx-auto py-3 px-3 sm:px-6 lg:px-8">
<div class="pr-16 sm:text-center sm:px-16">
<p class="font-medium text-white flex justify-center">
<span class="inline-block font-mono">{{ directory }}</span>
</p>
</div>
</div>
</div>
{% macro bytesToSize(bytes) %}
{% set kilobyte = 1024 %}
{% set megabyte = kilobyte * 1024 %}
{% set gigabyte = megabyte * 1024 %}
{% set terabyte = gigabyte * 1024 %}
{% if bytes < kilobyte %}
{{ bytes ~ ' B' }}
{% elseif bytes < megabyte %}
{{ (bytes / kilobyte)|number_format(2, '.') ~ ' KiB' }}
{% elseif bytes < gigabyte %}
{{ (bytes / megabyte)|number_format(2, '.') ~ ' MiB' }}
{% elseif bytes < terabyte %}
{{ (bytes / gigabyte)|number_format(2, '.') ~ ' GiB' }}
{% else %}
{{ (bytes / terabyte)|number_format(2, '.') ~ ' TiB' }}
{% endif %}
{% endmacro %}
<div class="flex flex-col px-6 py-4">
<div class="-my-2 overflow-x-auto sm:-mx-6 lg:-mx-8">
<div class="py-2 align-middle inline-block min-w-full sm:px-6 lg:px-8">
<div class="shadow overflow-hidden border-b border-gray-200 sm:rounded-lg">
<table class="min-w-full divide-y divide-gray-200">
<thead>
<tr>
<th class="px-6 py-3 bg-gray-50 text-left text-xs leading-4 font-medium text-gray-500 uppercase tracking-wider">
Name
</th>
<th class="px-6 py-3 bg-gray-50 text-left text-xs leading-4 font-medium text-gray-500 uppercase tracking-wider">
Date Modified
</th>
<th class="px-6 py-3 bg-gray-50 text-left text-xs leading-4 font-medium text-gray-500 uppercase tracking-wider">
Size
</th>
</tr>
</thead>
<tbody class="bg-white">
{% if currentPath != '/' %}
<tr class="border-b">
<td colspan="3" class="px-6 py-4 whitespace-no-wrap text-sm leading-5 font-mono text-gray-900">
<a href="/{{ parentPath }}" class="text-indigo-600 font-bold hover:text-indigo-900">Back</a>
</td>
</tr>
{% endif %}
{% for item in directoryContent %}
<tr class="{% if loop.index % 2 == 0 %} bg-gray-50 {% endif %}">
<td class="px-6 py-4 whitespace-no-wrap text-sm leading-5 font-mono text-gray-900">
{% if currentPath != '/' %}
<a href="/{{ currentPath }}/{{ item.getFilename() }}" class="text-indigo-600 hover:text-indigo-900">{{ item.filename }}</a>
{% else %}
<a href="/{{ item.getFilename() }}" class="text-indigo-600 hover:text-indigo-900">{{ item.filename }}</a>
{% endif %}
</td>
<td class="px-6 py-4 whitespace-no-wrap text-sm leading-5 text-gray-500">
{{ item.getMTime() | date("m/d/Y H:i:s") }}
</td>
<td class="px-6 py-4 whitespace-no-wrap text-sm leading-5 text-gray-500">
{% if item.isDir() %}
-
{% else %}
{{ _self.bytesToSize(item.getSize()) }}
{% endif %}
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
</div>
</div>
</body>
</html>

View File

@@ -9,10 +9,10 @@
<thead> <thead>
<tr> <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"> <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">
Local Host Host
</th> </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"> <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">
Expose Host Subdomain
</th> </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"> <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 Shared At
@@ -26,13 +26,13 @@
@{ site.host } @{ site.host }
</td> </td>
<td class="px-6 py-4 whitespace-no-wrap border-b border-gray-200 text-sm leading-5 text-gray-500"> <td class="px-6 py-4 whitespace-no-wrap border-b border-gray-200 text-sm leading-5 text-gray-500">
@{ getUrl(site) } @{ site.subdomain }.{{ configuration.hostname()}}:{{ configuration.port() }}
</td> </td>
<td class="px-6 py-4 whitespace-no-wrap border-b border-gray-200 text-sm leading-5 text-gray-500"> <td class="px-6 py-4 whitespace-no-wrap border-b border-gray-200 text-sm leading-5 text-gray-500">
@{ site.shared_at } @{ site.shared_at }
</td> </td>
<td class="px-6 py-4 whitespace-no-wrap text-right border-b border-gray-200 text-sm leading-5 font-medium"> <td class="px-6 py-4 whitespace-no-wrap text-right border-b border-gray-200 text-sm leading-5 font-medium">
<a :href="'{{ scheme|raw }}://'+getUrl(site)" target="_blank" <a :href="'{{ scheme|raw }}://'+site.subdomain+'.{{ configuration.hostname()}}:{{ configuration.port() }}'" target="_blank"
class="text-indigo-600 hover:text-indigo-900">Visit</a> class="text-indigo-600 hover:text-indigo-900">Visit</a>
<a href="#" <a href="#"
@click.prevent="disconnectSite(site.client_id)" @click.prevent="disconnectSite(site.client_id)"
@@ -60,12 +60,6 @@
}, },
methods: { methods: {
getUrl(site) {
if (site.hostname !== '' && site.hostname !== null) {
return site.hostname;
}
return `${site.subdomain}.{{ configuration.hostname()}}:{{ configuration.port() }}`;
},
disconnectSite(id) { disconnectSite(id) {
fetch('/api/sites/' + id, { fetch('/api/sites/' + id, {
method: 'DELETE', method: 'DELETE',

View File

@@ -43,25 +43,6 @@
</div> </div>
</div> </div>
</div> </div>
<div class="sm:grid sm:grid-cols-3 sm:gap-4 sm:items-start sm:pt-5">
<label for="can_specify_hostnames"
class="block text-sm font-medium leading-5 text-gray-700 sm:mt-px sm:pt-2">
Can specify custom hostnames
</label>
<div class="mt-1 sm:mt-0 sm:col-span-2">
<div class="mt-2 flex items-center justify-between">
<div class="flex items-center">
<input id="can_specify_hostnames"
v-model="userForm.can_specify_hostnames"
name="can_specify_hostnames"
value="1" type="checkbox" class="form-checkbox h-4 w-4 text-indigo-600 transition duration-150 ease-in-out" />
<label for="can_specify_hostnames" class="ml-2 block text-sm leading-5 text-gray-900">
Yes
</label>
</div>
</div>
</div>
</div>
<div class="sm:grid sm:grid-cols-3 sm:gap-4 sm:items-start sm:pt-5"> <div class="sm:grid sm:grid-cols-3 sm:gap-4 sm:items-start sm:pt-5">
<label for="can_share_tcp_ports" <label for="can_share_tcp_ports"
class="block text-sm font-medium leading-5 text-gray-700 sm:mt-px sm:pt-2"> class="block text-sm font-medium leading-5 text-gray-700 sm:mt-px sm:pt-2">
@@ -111,9 +92,6 @@
<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"> <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">
Custom Subdomains Custom Subdomains
</th> </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">
Custom Hostnames
</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"> <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">
TCP ports TCP ports
</th> </th>
@@ -139,14 +117,6 @@
Yes Yes
</span> </span>
</td> </td>
<td class="px-6 py-4 whitespace-no-wrap border-b border-gray-200 text-sm leading-5 text-gray-500">
<span v-if="user.can_specify_hostnames === 0">
No
</span>
<span v-else>
Yes
</span>
</td>
<td class="px-6 py-4 whitespace-no-wrap border-b border-gray-200 text-sm leading-5 text-gray-500"> <td class="px-6 py-4 whitespace-no-wrap border-b border-gray-200 text-sm leading-5 text-gray-500">
<span v-if="user.can_share_tcp_ports === 0"> <span v-if="user.can_share_tcp_ports === 0">
No No
@@ -204,7 +174,6 @@
userForm: { userForm: {
name: '', name: '',
can_specify_subdomains: true, can_specify_subdomains: true,
can_specify_hostnames: true,
can_share_tcp_ports: true, can_share_tcp_ports: true,
errors: {}, errors: {},
}, },
@@ -249,7 +218,6 @@
if (data.user) { if (data.user) {
this.userForm.name = ''; this.userForm.name = '';
this.userForm.can_specify_subdomains = true; this.userForm.can_specify_subdomains = true;
this.userForm.can_specify_hostnames = true;
this.userForm.can_share_tcp_ports = true; this.userForm.can_share_tcp_ports = true;
this.userForm.errors = {}; this.userForm.errors = {};
this.users.unshift(data.user); this.users.unshift(data.user);

View File

@@ -0,0 +1,111 @@
<?php
namespace Tests\Feature\Client;
use App\Client\Configuration;
use App\Client\Factory;
use Clue\React\Buzz\Browser;
use Clue\React\Buzz\Message\ResponseException;
use Psr\Http\Message\ResponseInterface;
use Tests\Feature\TestCase;
class FileserverTest extends TestCase
{
/** @var Browser */
protected $browser;
/** @var Factory */
protected $clientFactory;
/** @var string */
protected $fileserverUrl;
public function setUp(): void
{
parent::setUp();
$this->browser = new Browser($this->loop);
}
public function tearDown(): void
{
parent::tearDown();
$this->clientFactory->getFileserver()->getSocket()->close();
}
/** @test */
public function accessing_the_fileserver_works()
{
$this->shareFolder(__DIR__);
/** @var ResponseInterface $response */
$response = $this->await($this->browser->get('http://'.$this->fileserverUrl));
$this->assertSame(200, $response->getStatusCode());
}
/** @test */
public function accessing_invalid_files_returns_404()
{
$this->shareFolder(__DIR__);
$this->expectException(ResponseException::class);
$this->expectExceptionMessage(404);
/** @var ResponseInterface $response */
$response = $this->await($this->browser->get('http://'.$this->fileserverUrl.'/invalid-file'));
$this->assertSame(404, $response->getStatusCode());
}
/** @test */
public function it_can_return_filtered_responses()
{
$this->shareFolder(__DIR__.'/../../fixtures', '*.md');
$this->expectException(ResponseException::class);
$this->expectExceptionMessage(404);
/** @var ResponseInterface $response */
$response = $this->await($this->browser->get('http://'.$this->fileserverUrl.'/test.txt'));
$this->assertSame(200, $response->getStatusCode());
$this->assertSame('test-file'.PHP_EOL, $response->getBody()->getContents());
}
/** @test */
public function it_can_return_file_responses()
{
$this->shareFolder(__DIR__.'/../../fixtures');
/** @var ResponseInterface $response */
$response = $this->await($this->browser->get('http://'.$this->fileserverUrl.'/test.txt'));
$this->assertSame(200, $response->getStatusCode());
$this->assertSame('test-file'.PHP_EOL, $response->getBody()->getContents());
}
/** @test */
public function it_can_return_file_responses_for_valid_filtered_files()
{
$this->shareFolder(__DIR__.'/../../fixtures', '*.txt');
/** @var ResponseInterface $response */
$response = $this->await($this->browser->get('http://'.$this->fileserverUrl.'/test.txt'));
$this->assertSame(200, $response->getStatusCode());
$this->assertSame('test-file'.PHP_EOL, $response->getBody()->getContents());
}
protected function shareFolder(string $folder, string $name = '')
{
app()->singleton(Configuration::class, function ($app) {
return new Configuration('localhost', '8080', false);
});
$factory = (new Factory())->setLoop($this->loop);
$this->fileserverUrl = $factory->createFileServer($folder, $name);
$this->clientFactory = $factory;
}
}

View File

@@ -152,7 +152,7 @@ class AdminTest extends TestCase
$connection = \Mockery::mock(IoConnection::class); $connection = \Mockery::mock(IoConnection::class);
$connection->httpRequest = new Request('GET', '/?authToken=some-token'); $connection->httpRequest = new Request('GET', '/?authToken=some-token');
$connectionManager->storeConnection('some-host.text', 'fixed-subdomain', '', $connection); $connectionManager->storeConnection('some-host.text', 'fixed-subdomain', $connection);
/** @var Response $response */ /** @var Response $response */
$response = $this->await($this->browser->get('http://127.0.0.1:8080/sites', [ $response = $this->await($this->browser->get('http://127.0.0.1:8080/sites', [

View File

@@ -119,78 +119,6 @@ class ApiTest extends TestCase
$this->assertSame(200, $response->getStatusCode()); $this->assertSame(200, $response->getStatusCode());
} }
/** @test */
public function it_allows__hostname_reservation_for_users_with_the_right_flag()
{
/** @var Response $response */
$response = $this->await($this->browser->post('http://127.0.0.1:8080/api/users', [
'Host' => 'expose.localhost',
'Authorization' => base64_encode('username:secret'),
'Content-Type' => 'application/json',
], json_encode([
'name' => 'Marcel',
'can_specify_hostnames' => 1,
])));
$user = json_decode($response->getBody()->getContents())->user;
$response = $this->await($this->browser->post('http://127.0.0.1:8080/api/hostnames', [
'Host' => 'expose.localhost',
'Authorization' => base64_encode('username:secret'),
'Content-Type' => 'application/json',
], json_encode([
'auth_token' => $user->auth_token,
'hostname' => 'reserved.beyondco.de',
])));
$this->assertSame(200, $response->getStatusCode());
}
/** @test */
public function it_can_delete_hostnames()
{
/** @var Response $response */
$response = $this->await($this->browser->post('http://127.0.0.1:8080/api/users', [
'Host' => 'expose.localhost',
'Authorization' => base64_encode('username:secret'),
'Content-Type' => 'application/json',
], json_encode([
'name' => 'Marcel',
'can_specify_hostnames' => 1,
])));
$user = json_decode($response->getBody()->getContents())->user;
$response = $this->await($this->browser->post('http://127.0.0.1:8080/api/hostnames', [
'Host' => 'expose.localhost',
'Authorization' => base64_encode('username:secret'),
'Content-Type' => 'application/json',
], json_encode([
'hostname' => 'reserved.beyondco.de',
'auth_token' => $user->auth_token,
])));
$this->await($this->browser->delete('http://127.0.0.1:8080/api/hostnames/1', [
'Host' => 'expose.localhost',
'Authorization' => base64_encode('username:secret'),
'Content-Type' => 'application/json',
], json_encode([
'auth_token' => $user->auth_token,
])));
/** @var Response $response */
$response = $this->await($this->browser->get('http://127.0.0.1:8080/api/users/1', [
'Host' => 'expose.localhost',
'Authorization' => base64_encode('username:secret'),
'Content-Type' => 'application/json',
]));
$body = json_decode($response->getBody()->getContents());
$hostnames = $body->hostnames;
$this->assertCount(0, $hostnames);
}
/** @test */ /** @test */
public function it_can_get_user_details() public function it_can_get_user_details()
{ {
@@ -201,7 +129,6 @@ class ApiTest extends TestCase
'Content-Type' => 'application/json', 'Content-Type' => 'application/json',
], json_encode([ ], json_encode([
'name' => 'Marcel', 'name' => 'Marcel',
'can_specify_hostnames' => 1,
'can_specify_subdomains' => 1, 'can_specify_subdomains' => 1,
]))); ])));
@@ -216,15 +143,6 @@ class ApiTest extends TestCase
'subdomain' => 'reserved', 'subdomain' => 'reserved',
]))); ])));
$this->await($this->browser->post('http://127.0.0.1:8080/api/hostnames', [
'Host' => 'expose.localhost',
'Authorization' => base64_encode('username:secret'),
'Content-Type' => 'application/json',
], json_encode([
'auth_token' => $user->auth_token,
'hostname' => 'reserved.beyondco.de',
])));
/** @var Response $response */ /** @var Response $response */
$response = $this->await($this->browser->get('http://127.0.0.1:8080/api/users/1', [ $response = $this->await($this->browser->get('http://127.0.0.1:8080/api/users/1', [
'Host' => 'expose.localhost', 'Host' => 'expose.localhost',
@@ -235,14 +153,12 @@ class ApiTest extends TestCase
$body = json_decode($response->getBody()->getContents()); $body = json_decode($response->getBody()->getContents());
$user = $body->user; $user = $body->user;
$subdomains = $body->subdomains; $subdomains = $body->subdomains;
$hostnames = $body->hostnames;
$this->assertSame('Marcel', $user->name); $this->assertSame('Marcel', $user->name);
$this->assertSame([], $user->sites); $this->assertSame([], $user->sites);
$this->assertSame([], $user->tcp_connections); $this->assertSame([], $user->tcp_connections);
$this->assertCount(1, $subdomains); $this->assertCount(1, $subdomains);
$this->assertCount(1, $hostnames);
} }
/** @test */ /** @test */
@@ -368,11 +284,11 @@ class ApiTest extends TestCase
$connection = \Mockery::mock(IoConnection::class); $connection = \Mockery::mock(IoConnection::class);
$connection->httpRequest = new Request('GET', '/?authToken='.$createdUser->auth_token); $connection->httpRequest = new Request('GET', '/?authToken='.$createdUser->auth_token);
$connectionManager->storeConnection('some-host.test', 'fixed-subdomain', '', $connection); $connectionManager->storeConnection('some-host.test', 'fixed-subdomain', $connection);
$connection = \Mockery::mock(IoConnection::class); $connection = \Mockery::mock(IoConnection::class);
$connection->httpRequest = new Request('GET', '/?authToken=some-other-token'); $connection->httpRequest = new Request('GET', '/?authToken=some-other-token');
$connectionManager->storeConnection('some-different-host.test', 'different-subdomain', '', $connection); $connectionManager->storeConnection('some-different-host.test', 'different-subdomain', $connection);
$connection = \Mockery::mock(IoConnection::class); $connection = \Mockery::mock(IoConnection::class);
$connection->httpRequest = new Request('GET', '/?authToken='.$createdUser->auth_token); $connection->httpRequest = new Request('GET', '/?authToken='.$createdUser->auth_token);
@@ -403,7 +319,7 @@ class ApiTest extends TestCase
$connection = \Mockery::mock(IoConnection::class); $connection = \Mockery::mock(IoConnection::class);
$connection->httpRequest = new Request('GET', '/?authToken=some-token'); $connection->httpRequest = new Request('GET', '/?authToken=some-token');
$connectionManager->storeConnection('some-host.test', 'fixed-subdomain', '', $connection); $connectionManager->storeConnection('some-host.test', 'fixed-subdomain', $connection);
/** @var Response $response */ /** @var Response $response */
$response = $this->await($this->browser->get('http://127.0.0.1:8080/api/sites', [ $response = $this->await($this->browser->get('http://127.0.0.1:8080/api/sites', [
@@ -430,7 +346,7 @@ class ApiTest extends TestCase
$connection = \Mockery::mock(IoConnection::class); $connection = \Mockery::mock(IoConnection::class);
$connection->httpRequest = new Request('GET', '/'); $connection->httpRequest = new Request('GET', '/');
$connectionManager->storeConnection('some-host.test', 'fixed-subdomain', '', $connection); $connectionManager->storeConnection('some-host.test', 'fixed-subdomain', $connection);
/** @var Response $response */ /** @var Response $response */
$response = $this->await($this->browser->get('http://127.0.0.1:8080/api/sites', [ $response = $this->await($this->browser->get('http://127.0.0.1:8080/api/sites', [

View File

@@ -9,7 +9,6 @@ use Clue\React\Buzz\Message\ResponseException;
use GuzzleHttp\Psr7\Response; use GuzzleHttp\Psr7\Response;
use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\ServerRequestInterface;
use React\Http\Server; use React\Http\Server;
use React\Promise\Timer\TimeoutException;
use React\Socket\Connection; use React\Socket\Connection;
use Tests\Feature\TestCase; use Tests\Feature\TestCase;
@@ -90,76 +89,6 @@ class TunnelTest extends TestCase
$this->assertSame('Hello World!', $response->getBody()->getContents()); $this->assertSame('Hello World!', $response->getBody()->getContents());
} }
/** @test */
public function it_sends_incoming_requests_to_the_connected_client_with_random_subdomain()
{
$this->createTestHttpServer();
$this->app['config']['expose.admin.validate_auth_tokens'] = false;
/**
* We create an expose client that connects to our server and shares
* the created test HTTP server.
*/
$client = $this->createClient();
$data = $this->await($client->connectToServer('127.0.0.1:8085', null));
/**
* Once the client is connected, we perform a GET request on the
* created tunnel.
*/
$response = $this->await($this->browser->get('http://127.0.0.1:8080/', [
'Host' => $data->subdomain.'.localhost',
]));
$this->assertSame('Hello World!', $response->getBody()->getContents());
}
/** @test */
public function it_sends_incoming_requests_to_the_connected_client_with_specific_hostname()
{
$this->createTestHttpServer();
$this->app['config']['expose.admin.validate_auth_tokens'] = true;
$response = $this->await($this->browser->post('http://127.0.0.1:8080/api/users', [
'Host' => 'expose.localhost',
'Authorization' => base64_encode('username:secret'),
'Content-Type' => 'application/json',
], json_encode([
'name' => 'Marcel',
'can_specify_hostnames' => 1,
])));
$user = json_decode($response->getBody()->getContents())->user;
$this->await($this->browser->post('http://127.0.0.1:8080/api/hostnames', [
'Host' => 'expose.localhost',
'Authorization' => base64_encode('username:secret'),
'Content-Type' => 'application/json',
], json_encode([
'hostname' => 'reserved.beyondco.de',
'auth_token' => $user->auth_token,
])));
/**
* We create an expose client that connects to our server and shares
* the created test HTTP server.
*/
$client = $this->createClient();
$data = $this->await($client->connectToServer('127.0.0.1:8085', null, 'reserved.beyondco.de', $user->auth_token));
/**
* Once the client is connected, we perform a GET request on the
* created tunnel.
*/
$response = $this->await($this->browser->get('http://127.0.0.1:8080/', [
'Host' => 'reserved.beyondco.de',
]));
$this->assertSame('Hello World!', $response->getBody()->getContents());
}
/** @test */ /** @test */
public function it_sends_incoming_requests_to_the_connected_client_via_tcp() public function it_sends_incoming_requests_to_the_connected_client_via_tcp()
{ {
@@ -287,7 +216,7 @@ class TunnelTest extends TestCase
* the created test HTTP server. * the created test HTTP server.
*/ */
$client = $this->createClient(); $client = $this->createClient();
$response = $this->await($client->connectToServer('127.0.0.1:8085', 'tunnel', null, $user->auth_token)); $response = $this->await($client->connectToServer('127.0.0.1:8085', 'tunnel', $user->auth_token));
$this->assertSame('tunnel', $response->subdomain); $this->assertSame('tunnel', $response->subdomain);
} }
@@ -315,7 +244,7 @@ class TunnelTest extends TestCase
* the created test HTTP server. * the created test HTTP server.
*/ */
$client = $this->createClient(); $client = $this->createClient();
$response = $this->await($client->connectToServer('127.0.0.1:8085', 'tunnel', null, $user->auth_token)); $response = $this->await($client->connectToServer('127.0.0.1:8085', 'tunnel', $user->auth_token));
$this->assertNotSame('tunnel', $response->subdomain); $this->assertNotSame('tunnel', $response->subdomain);
} }
@@ -400,196 +329,11 @@ class TunnelTest extends TestCase
* the created test HTTP server. * the created test HTTP server.
*/ */
$client = $this->createClient(); $client = $this->createClient();
$response = $this->await($client->connectToServer('127.0.0.1:8085', 'reserved', null, $user->auth_token)); $response = $this->await($client->connectToServer('127.0.0.1:8085', 'reserved', $user->auth_token));
$this->assertSame('reserved', $response->subdomain); $this->assertSame('reserved', $response->subdomain);
} }
/** @test */
public function it_allows_users_to_use_their_own_reserved_hostnames()
{
$this->app['config']['expose.admin.validate_auth_tokens'] = true;
$response = $this->await($this->browser->post('http://127.0.0.1:8080/api/users', [
'Host' => 'expose.localhost',
'Authorization' => base64_encode('username:secret'),
'Content-Type' => 'application/json',
], json_encode([
'name' => 'Marcel',
'can_specify_hostnames' => 1,
])));
$user = json_decode($response->getBody()->getContents())->user;
$response = $this->await($this->browser->post('http://127.0.0.1:8080/api/hostnames', [
'Host' => 'expose.localhost',
'Authorization' => base64_encode('username:secret'),
'Content-Type' => 'application/json',
], json_encode([
'hostname' => 'reserved.beyondco.de',
'auth_token' => $user->auth_token,
])));
$this->createTestHttpServer();
/**
* We create an expose client that connects to our server and shares
* the created test HTTP server.
*/
$client = $this->createClient();
$response = $this->await($client->connectToServer('127.0.0.1:8085', null, 'reserved.beyondco.de', $user->auth_token));
$this->assertSame('reserved.beyondco.de', $response->hostname);
}
/** @test */
public function it_allows_users_to_use_their_own_reserved_hostnames_with_wildcards()
{
$this->app['config']['expose.admin.validate_auth_tokens'] = true;
$response = $this->await($this->browser->post('http://127.0.0.1:8080/api/users', [
'Host' => 'expose.localhost',
'Authorization' => base64_encode('username:secret'),
'Content-Type' => 'application/json',
], json_encode([
'name' => 'Marcel',
'can_specify_hostnames' => 1,
])));
$user = json_decode($response->getBody()->getContents())->user;
$response = $this->await($this->browser->post('http://127.0.0.1:8080/api/hostnames', [
'Host' => 'expose.localhost',
'Authorization' => base64_encode('username:secret'),
'Content-Type' => 'application/json',
], json_encode([
'hostname' => '*.share.beyondco.de',
'auth_token' => $user->auth_token,
])));
$this->createTestHttpServer();
/**
* We create an expose client that connects to our server and shares
* the created test HTTP server.
*/
$client = $this->createClient();
$response = $this->await($client->connectToServer('127.0.0.1:8085', null, 'foo.share.beyondco.de', $user->auth_token));
$this->assertSame('foo.share.beyondco.de', $response->hostname);
}
/** @test */
public function it_rejects_users_trying_to_use_non_registered_hostnames()
{
$this->app['config']['expose.admin.validate_auth_tokens'] = true;
$response = $this->await($this->browser->post('http://127.0.0.1:8080/api/users', [
'Host' => 'expose.localhost',
'Authorization' => base64_encode('username:secret'),
'Content-Type' => 'application/json',
], json_encode([
'name' => 'Marcel',
'can_specify_hostnames' => 1,
])));
$user = json_decode($response->getBody()->getContents())->user;
$response = $this->await($this->browser->post('http://127.0.0.1:8080/api/hostnames', [
'Host' => 'expose.localhost',
'Authorization' => base64_encode('username:secret'),
'Content-Type' => 'application/json',
], json_encode([
'hostname' => 'share.beyondco.de',
'auth_token' => $user->auth_token,
])));
$this->createTestHttpServer();
$this->expectException(TimeoutException::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', null, 'foo.beyondco.de', $user->auth_token));
}
/** @test */
public function it_rejects_users_trying_to_use_other_peoples_registered_hostnames()
{
$this->app['config']['expose.admin.validate_auth_tokens'] = true;
$response = $this->await($this->browser->post('http://127.0.0.1:8080/api/users', [
'Host' => 'expose.localhost',
'Authorization' => base64_encode('username:secret'),
'Content-Type' => 'application/json',
], json_encode([
'name' => 'Marcel',
'can_specify_hostnames' => 1,
])));
$user = json_decode($response->getBody()->getContents())->user;
$response = $this->await($this->browser->post('http://127.0.0.1:8080/api/hostnames', [
'Host' => 'expose.localhost',
'Authorization' => base64_encode('username:secret'),
'Content-Type' => 'application/json',
], json_encode([
'hostname' => '*.share.beyondco.de',
'auth_token' => $user->auth_token,
])));
$response = $this->await($this->browser->post('http://127.0.0.1:8080/api/users', [
'Host' => 'expose.localhost',
'Authorization' => base64_encode('username:secret'),
'Content-Type' => 'application/json',
], json_encode([
'name' => 'Marcel',
'can_specify_hostnames' => 1,
])));
$user = json_decode($response->getBody()->getContents())->user;
$this->createTestHttpServer();
$this->expectException(TimeoutException::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', null, 'foo.share.beyondco.de', $user->auth_token));
}
/** @test */
public function it_rejects_clients_to_specify_custom_hostnames()
{
$this->app['config']['expose.admin.validate_auth_tokens'] = true;
$response = $this->await($this->browser->post('http://127.0.0.1:8080/api/users', [
'Host' => 'expose.localhost',
'Authorization' => base64_encode('username:secret'),
'Content-Type' => 'application/json',
], json_encode([
'name' => 'Marcel',
'can_specify_hostnames' => 0,
])));
$user = json_decode($response->getBody()->getContents())->user;
$this->createTestHttpServer();
$this->expectException(TimeoutException::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', null, 'reserved.beyondco.de', $user->auth_token));
}
/** @test */ /** @test */
public function it_allows_clients_to_use_random_subdomains_if_custom_subdomains_are_forbidden() public function it_allows_clients_to_use_random_subdomains_if_custom_subdomains_are_forbidden()
{ {
@@ -613,7 +357,7 @@ class TunnelTest extends TestCase
* the created test HTTP server. * the created test HTTP server.
*/ */
$client = $this->createClient(); $client = $this->createClient();
$response = $this->await($client->connectToServer('127.0.0.1:8085', '', null, $user->auth_token)); $response = $this->await($client->connectToServer('127.0.0.1:8085', '', $user->auth_token));
$this->assertInstanceOf(\stdClass::class, $response); $this->assertInstanceOf(\stdClass::class, $response);
} }

View File

@@ -11,7 +11,7 @@ use Symfony\Component\Console\Output\ConsoleOutputInterface;
abstract class TestCase extends \Tests\TestCase abstract class TestCase extends \Tests\TestCase
{ {
const AWAIT_TIMEOUT = 0.2; const AWAIT_TIMEOUT = 5.0;
/** @var LoopInterface */ /** @var LoopInterface */
protected $loop; protected $loop;

1
tests/fixtures/test.md vendored Normal file
View File

@@ -0,0 +1 @@
# Markdown

1
tests/fixtures/test.txt vendored Normal file
View File

@@ -0,0 +1 @@
test-file