This commit is contained in:
Marcel Pociot
2020-04-27 15:52:28 +02:00
parent 7867c10524
commit 873207a655
3 changed files with 29 additions and 18 deletions

View File

@@ -106,7 +106,7 @@ class Factory
protected function addAdminRoutes() protected function addAdminRoutes()
{ {
$adminCondition = 'request.headers.get("Host") matches "/'.config('expose.dashboard_subdomain').'./i"'; $adminCondition = 'request.headers.get("Host") matches "/'.config('expose.dashboard_subdomain').'\./i"';
$this->router->get('/', LoginController::class, $adminCondition); $this->router->get('/', LoginController::class, $adminCondition);
$this->router->post('/', VerifyLoginController::class, $adminCondition); $this->router->post('/', VerifyLoginController::class, $adminCondition);
@@ -121,6 +121,8 @@ class Factory
app()->singleton(Configuration::class, function ($app) { app()->singleton(Configuration::class, function ($app) {
return new Configuration($this->hostname, $this->port); return new Configuration($this->hostname, $this->port);
}); });
return $this;
} }
protected function bindSubdomainGenerator() protected function bindSubdomainGenerator()
@@ -128,6 +130,8 @@ class Factory
app()->singleton(SubdomainGenerator::class, function ($app) { app()->singleton(SubdomainGenerator::class, function ($app) {
return $app->make(RandomSubdomainGenerator::class); return $app->make(RandomSubdomainGenerator::class);
}); });
return $this;
} }
protected function bindConnectionManager() protected function bindConnectionManager()
@@ -135,23 +139,20 @@ class Factory
app()->singleton(ConnectionManagerContract::class, function ($app) { app()->singleton(ConnectionManagerContract::class, function ($app) {
return $app->make(ConnectionManager::class); return $app->make(ConnectionManager::class);
}); });
return $this;
} }
public function createServer() public function createServer()
{ {
$socket = new Server("{$this->host}:{$this->port}", $this->loop); $socket = new Server("{$this->host}:{$this->port}", $this->loop);
$this->bindConfiguration(); $this->bindConfiguration()
->bindSubdomainGenerator()
$this->bindSubdomainGenerator(); ->bindDatabase()
->ensureDatabaseIsInitialized()
$this->bindDatabase(); ->bindConnectionManager()
->addAdminRoutes();
$this->ensureDatabaseIsInitialized();
$this->bindConnectionManager();
$this->addAdminRoutes();
$controlConnection = $this->addControlConnectionRoute(); $controlConnection = $this->addControlConnectionRoute();
@@ -176,6 +177,8 @@ class Factory
$factory = new \Clue\React\SQLite\Factory($this->loop); $factory = new \Clue\React\SQLite\Factory($this->loop);
return $factory->openLazy(base_path('database/expose.db')); return $factory->openLazy(base_path('database/expose.db'));
}); });
return $this;
} }
protected function ensureDatabaseIsInitialized() protected function ensureDatabaseIsInitialized()
@@ -193,6 +196,8 @@ class Factory
foreach ($migrations as $migration) { foreach ($migrations as $migration) {
$db->exec($migration->getContents()); $db->exec($migration->getContents());
} }
return $this;
} }
public function validateAuthTokens(bool $validate) public function validateAuthTokens(bool $validate)

View File

@@ -9,6 +9,7 @@ use App\Server\Connections\ControlConnection;
use GuzzleHttp\Psr7\Response; use GuzzleHttp\Psr7\Response;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Pipeline\Pipeline; use Illuminate\Pipeline\Pipeline;
use Illuminate\Support\Str;
use Nyholm\Psr7\Factory\Psr17Factory; use Nyholm\Psr7\Factory\Psr17Factory;
use Ratchet\ConnectionInterface; use Ratchet\ConnectionInterface;
use Ratchet\RFC6455\Messaging\Frame; use Ratchet\RFC6455\Messaging\Frame;
@@ -37,10 +38,14 @@ class TunnelMessageController extends PostController
public function handle(Request $request, ConnectionInterface $httpConnection) public function handle(Request $request, ConnectionInterface $httpConnection)
{ {
$controlConnection = $this->connectionManager->findControlConnectionForSubdomain($this->detectSubdomain($request)); $subdomain = $this->detectSubdomain($request);
$controlConnection = $this->connectionManager->findControlConnectionForSubdomain($subdomain);
if (is_null($controlConnection)) { if (is_null($controlConnection)) {
$httpConnection->send(str(new Response(404, [], 'Not found'))); $httpConnection->send(
respond_html($this->getView('server.errors.404', ['subdomain' => $subdomain]))
);
$httpConnection->close(); $httpConnection->close();
return; return;
} }
@@ -50,9 +55,9 @@ class TunnelMessageController extends PostController
protected function detectSubdomain(Request $request): ?string protected function detectSubdomain(Request $request): ?string
{ {
$domainParts = explode('.', $request->getHost()); $subdomain = Str::before($request->getHost(), '.'.$this->configuration->hostname());
return trim($domainParts[0]); return $subdomain === $request->getHost() ? null : $subdomain;
} }
protected function sendRequestToClient(Request $request, ControlConnection $controlConnection, ConnectionInterface $httpConnection) protected function sendRequestToClient(Request $request, ControlConnection $controlConnection, ConnectionInterface $httpConnection)
@@ -85,7 +90,7 @@ class TunnelMessageController extends PostController
$host = $this->configuration->hostname(); $host = $this->configuration->hostname();
if (! $request->isSecure()) { if (!$request->isSecure()) {
$host .= ":{$this->configuration->port()}"; $host .= ":{$this->configuration->port()}";
} }
@@ -93,7 +98,7 @@ class TunnelMessageController extends PostController
$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', "{$controlConnection->subdomain}.{$host}"); $request->headers->set('X-Original-Host', "{$controlConnection->subdomain}.{$host}");
return $request; return $request;

View File

@@ -0,0 +1 @@
The tunnel {{ subdomain }} was not found.