Skip to content

Commit

Permalink
Add API for caddy domain check
Browse files Browse the repository at this point in the history
  • Loading branch information
SiebeVE committed Mar 13, 2024
1 parent f82dd88 commit b3dcfbc
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 1 deletion.
13 changes: 12 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,15 @@ 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()
{
$this->router->get('/validate-tunnel', ValidateTunnelController::class);

return $this;
}

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

$controlConnection = $this->addControlConnectionRoute();

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

namespace App\Server\Http\Controllers;

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

class ValidateTunnelController extends Controller
{
private ConnectionManager $connectionManager;


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

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

return;
}

/** @var Collection $sites */
$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,
);
});

$response = $sites->count() === 0
? respond_json(['exists' => false, 'error' => 'no_tunnel_found'], 404)
: respond_json(['exists' => true, 'sites' => $sites->toArray()]);

$httpConnection->send($response);
}
}

0 comments on commit b3dcfbc

Please sign in to comment.