Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a GET route for validate tunnel so we can use caddy on_demand #405

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion app/Server/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
use App\Server\Http\Controllers\Admin\StoreUsersController;
use App\Server\Http\Controllers\ControlMessageController;
use App\Server\Http\Controllers\TunnelMessageController;
use App\Server\Http\Controllers\ValidateTunnelController;
use App\Server\Http\Router;
use App\Server\LoggerRepository\NullLogger;
use App\Server\StatisticsCollector\DatabaseStatisticsCollector;
Expand Down Expand Up @@ -166,6 +167,17 @@ protected function addAdminRoutes()

$this->router->get('/api/tcp', GetTcpConnectionsController::class, $adminCondition);
$this->router->delete('/api/tcp/{id}', DisconnectTcpConnectionController::class, $adminCondition);

return $this;
}

protected function addValidateTunnel()
{
$localCondition = 'request.headers.get("Host") matches "/^'.$this->host.':'.$this->port.'$/i"';

$this->router->get('/validate-tunnel', ValidateTunnelController::class, $localCondition);

return $this;
}

protected function bindConfiguration()
Expand Down Expand Up @@ -209,7 +221,8 @@ public function createServer()
->ensureDatabaseIsInitialized()
->registerStatisticsCollector()
->bindConnectionManager()
->addAdminRoutes();
->addAdminRoutes()
->addValidateTunnel();

$controlConnection = $this->addControlConnectionRoute();

Expand Down
109 changes: 109 additions & 0 deletions app/Server/Http/Controllers/ValidateTunnelController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php

namespace App\Server\Http\Controllers;

use App\Contracts\ConnectionManager;
use App\Http\Controllers\Controller;
use App\Server\Configuration;
use App\Server\Connections\ControlConnection;
use Illuminate\Http\Request;
use Ratchet\ConnectionInterface;

class ValidateTunnelController extends Controller
{
private ConnectionManager $connectionManager;
private Configuration $configuration;

public function __construct(
Configuration $configuration,
ConnectionManager $connectionManager,
) {
$this->connectionManager = $connectionManager;
$this->configuration = $configuration;
}

public function handle(Request $request, ConnectionInterface $httpConnection)
{
$key = $request->get('key');

// Only allow requests with the correct key
if ($key !== $this->getAuthorizedKey()) {
$httpConnection->send(
respond_json(['exists' => false], 401),
);
$httpConnection->close();

return;
}

$domain = $request->get('domain');
if ($domain === null) {
$httpConnection->send(
respond_json(['exists' => false, 'error' => 'invalid_domain'], 404),
);
$httpConnection->close();

return;
}

// If the domain is the same as the hostname, then it requested the main domain
$hostname = $this->configuration->hostname();
if ($hostname === $domain) {
$this->isSuccessful($httpConnection);

return;
}

// Also allow the admin dashboard
$adminSubdomain = config('expose.admin.subdomain');
if ($domain === $adminSubdomain.'.'.$hostname) {
$this->isSuccessful($httpConnection);

return;
}

// Check if the domain is a tunnel
$sites = collect($this->connectionManager->getConnections())
->filter(function ($site) use ($domain) {
$isControlConnection = get_class($site) === ControlConnection::class;
if (! $isControlConnection) {
return false;
}

$fqdn = sprintf(
'%s.%s',
$site->subdomain,
$site->serverHost,
);

return $fqdn === $domain;
})
->map(function (ControlConnection $site) {
return sprintf(
'%s.%s',
$site->host,
$site->subdomain,
);
});

if ($sites->count() > 0) {
$this->isSuccessful($httpConnection);

return;
}

$httpConnection->send(respond_json(['exists' => false, 'error' => 'no_tunnel_found'], 404));
$httpConnection->close();
}

private function isSuccessful(ConnectionInterface $connection): void
{
$connection->send(respond_json(['exists' => true]));
$connection->close();
}

private function getAuthorizedKey(): string
{
return config('expose.validate_tunnel.authorized_key');
}
}
4 changes: 4 additions & 0 deletions config/expose.php
Original file line number Diff line number Diff line change
Expand Up @@ -390,4 +390,8 @@
'repository' => \App\Server\StatisticsRepository\DatabaseStatisticsRepository::class,
],
],

'validate_tunnel' => [
'authorized_key' => 'asHzMGp4y4fYmNzWAUmgsZZbcjSM5e',
],
];
Loading