mirror of
https://github.com/bitinflow/expose.git
synced 2026-03-13 13:35:54 +00:00
Add site details api route
This commit is contained in:
@@ -19,6 +19,7 @@ use App\Server\Http\Controllers\Admin\DisconnectSiteController;
|
|||||||
use App\Server\Http\Controllers\Admin\DisconnectTcpConnectionController;
|
use App\Server\Http\Controllers\Admin\DisconnectTcpConnectionController;
|
||||||
use App\Server\Http\Controllers\Admin\GetSettingsController;
|
use App\Server\Http\Controllers\Admin\GetSettingsController;
|
||||||
use App\Server\Http\Controllers\Admin\GetSitesController;
|
use App\Server\Http\Controllers\Admin\GetSitesController;
|
||||||
|
use App\Server\Http\Controllers\Admin\GetSiteDetailsController;
|
||||||
use App\Server\Http\Controllers\Admin\GetStatisticsController;
|
use App\Server\Http\Controllers\Admin\GetStatisticsController;
|
||||||
use App\Server\Http\Controllers\Admin\GetTcpConnectionsController;
|
use App\Server\Http\Controllers\Admin\GetTcpConnectionsController;
|
||||||
use App\Server\Http\Controllers\Admin\GetUserDetailsController;
|
use App\Server\Http\Controllers\Admin\GetUserDetailsController;
|
||||||
@@ -149,6 +150,7 @@ class Factory
|
|||||||
$this->router->delete('/api/subdomains/{subdomain}', DeleteSubdomainController::class, $adminCondition);
|
$this->router->delete('/api/subdomains/{subdomain}', DeleteSubdomainController::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->get('/api/sites/{site}', GetSiteDetailsController::class, $adminCondition);
|
||||||
$this->router->delete('/api/sites/{id}', DisconnectSiteController::class, $adminCondition);
|
$this->router->delete('/api/sites/{id}', DisconnectSiteController::class, $adminCondition);
|
||||||
$this->router->get('/api/tcp', GetTcpConnectionsController::class, $adminCondition);
|
$this->router->get('/api/tcp', GetTcpConnectionsController::class, $adminCondition);
|
||||||
$this->router->delete('/api/tcp/{id}', DisconnectTcpConnectionController::class, $adminCondition);
|
$this->router->delete('/api/tcp/{id}', DisconnectTcpConnectionController::class, $adminCondition);
|
||||||
|
|||||||
@@ -0,0 +1,50 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Server\Http\Controllers\Admin;
|
||||||
|
|
||||||
|
use App\Contracts\ConnectionManager;
|
||||||
|
use App\Contracts\UserRepository;
|
||||||
|
use App\Server\Configuration;
|
||||||
|
use App\Server\Connections\ControlConnection;
|
||||||
|
use GuzzleHttp\Psr7\Message;
|
||||||
|
use GuzzleHttp\Psr7\Response;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use Ratchet\ConnectionInterface;
|
||||||
|
|
||||||
|
class GetSiteDetailsController extends AdminController
|
||||||
|
{
|
||||||
|
/** @var ConnectionManager */
|
||||||
|
protected $connectionManager;
|
||||||
|
|
||||||
|
/** @var Configuration */
|
||||||
|
protected $configuration;
|
||||||
|
|
||||||
|
public function __construct(ConnectionManager $connectionManager, Configuration $configuration)
|
||||||
|
{
|
||||||
|
$this->connectionManager = $connectionManager;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function handle(Request $request, ConnectionInterface $httpConnection)
|
||||||
|
{
|
||||||
|
$domain = $request->get('site');
|
||||||
|
|
||||||
|
$connectedSite = collect($this->connectionManager->getConnections())
|
||||||
|
->filter(function ($connection) {
|
||||||
|
return get_class($connection) === ControlConnection::class;
|
||||||
|
})
|
||||||
|
->first(function (ControlConnection $site) use ($domain) {
|
||||||
|
return "{$site->subdomain}.{$site->serverHost}" === $domain;
|
||||||
|
});
|
||||||
|
|
||||||
|
if (is_null($connectedSite)) {
|
||||||
|
$httpConnection->send(
|
||||||
|
Message::toString(new Response(404))
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$httpConnection->send(
|
||||||
|
respond_json($connectedSite->toArray())
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -156,7 +156,7 @@ class ControlMessageController implements MessageComponentInterface
|
|||||||
->then(function () use ($connection, $data, $user) {
|
->then(function () use ($connection, $data, $user) {
|
||||||
return $this->hasValidSubdomain($connection, $data->subdomain, $user, $data->server_host);
|
return $this->hasValidSubdomain($connection, $data->subdomain, $user, $data->server_host);
|
||||||
})
|
})
|
||||||
->then(function ($subdomain) use ($data, $connection) {
|
->then(function ($subdomain) use ($data, $connection, $user) {
|
||||||
if ($subdomain === false) {
|
if ($subdomain === false) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -170,7 +170,7 @@ class ControlMessageController implements MessageComponentInterface
|
|||||||
$connection->send(json_encode([
|
$connection->send(json_encode([
|
||||||
'event' => 'authenticated',
|
'event' => 'authenticated',
|
||||||
'data' => [
|
'data' => [
|
||||||
'message' => config('expose.admin.messages.message_of_the_day'),
|
'message' => config('expose.admin.messages.resolve_connection_message')($connectionInfo, $user),
|
||||||
'subdomain' => $connectionInfo->subdomain,
|
'subdomain' => $connectionInfo->subdomain,
|
||||||
'server_host' => $connectionInfo->serverHost,
|
'server_host' => $connectionInfo->serverHost,
|
||||||
'client_id' => $connectionInfo->client_id,
|
'client_id' => $connectionInfo->client_id,
|
||||||
|
|||||||
@@ -313,6 +313,10 @@ return [
|
|||||||
|
|
|
|
||||||
*/
|
*/
|
||||||
'messages' => [
|
'messages' => [
|
||||||
|
'resolve_connection_message' => function ($connectionInfo, $user) {
|
||||||
|
return config('expose.admin.messages.message_of_the_day');
|
||||||
|
},
|
||||||
|
|
||||||
'message_of_the_day' => 'Thank you for using expose.',
|
'message_of_the_day' => 'Thank you for using expose.',
|
||||||
|
|
||||||
'invalid_auth_token' => 'Authentication failed. Please check your authentication token and try again.',
|
'invalid_auth_token' => 'Authentication failed. Please check your authentication token and try again.',
|
||||||
|
|||||||
@@ -497,6 +497,53 @@ class ApiTest extends TestCase
|
|||||||
$this->assertSame('fixed-subdomain', $sites[0]->subdomain);
|
$this->assertSame('fixed-subdomain', $sites[0]->subdomain);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @test */
|
||||||
|
public function it_can_return_site_details()
|
||||||
|
{
|
||||||
|
/** @var ConnectionManager $connectionManager */
|
||||||
|
$connectionManager = app(ConnectionManager::class);
|
||||||
|
|
||||||
|
$connection = \Mockery::mock(IoConnection::class);
|
||||||
|
$connection->httpRequest = new Request('GET', '/?authToken=some-token');
|
||||||
|
|
||||||
|
$connectionManager->storeConnection('some-host.test', 'fixed-subdomain', 'localhost', $connection);
|
||||||
|
|
||||||
|
/** @var Response $response */
|
||||||
|
$response = $this->await($this->browser->get('http://127.0.0.1:8080/api/sites/fixed-subdomain.localhost', [
|
||||||
|
'Host' => 'expose.localhost',
|
||||||
|
'Authorization' => base64_encode('username:secret'),
|
||||||
|
'Content-Type' => 'application/json',
|
||||||
|
]));
|
||||||
|
|
||||||
|
$site = json_decode($response->getBody()->getContents());
|
||||||
|
|
||||||
|
$this->assertSame('some-host.test', $site->host);
|
||||||
|
$this->assertSame('some-token', $site->auth_token);
|
||||||
|
$this->assertSame('fixed-subdomain', $site->subdomain);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @test */
|
||||||
|
public function it_returns_404_for_invalid_site_details()
|
||||||
|
{
|
||||||
|
/** @var ConnectionManager $connectionManager */
|
||||||
|
$connectionManager = app(ConnectionManager::class);
|
||||||
|
|
||||||
|
$connection = \Mockery::mock(IoConnection::class);
|
||||||
|
$connection->httpRequest = new Request('GET', '/?authToken=some-token');
|
||||||
|
|
||||||
|
$connectionManager->storeConnection('some-host.test', 'fixed-subdomain', 'localhost', $connection);
|
||||||
|
|
||||||
|
$this->expectException(ResponseException::class);
|
||||||
|
$this->expectExceptionMessage('HTTP status code 404 (Not Found)');
|
||||||
|
|
||||||
|
/** @var Response $response */
|
||||||
|
$response = $this->await($this->browser->get('http://127.0.0.1:8080/api/sites/invalid-subdomain.localhost', [
|
||||||
|
'Host' => 'expose.localhost',
|
||||||
|
'Authorization' => base64_encode('username:secret'),
|
||||||
|
'Content-Type' => 'application/json',
|
||||||
|
]));
|
||||||
|
}
|
||||||
|
|
||||||
/** @test */
|
/** @test */
|
||||||
public function it_can_list_all_currently_connected_sites_without_auth_tokens()
|
public function it_can_list_all_currently_connected_sites_without_auth_tokens()
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user