2024-02-16 20:56:38 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Api;
|
|
|
|
|
|
|
|
use App\Http\Controllers\Controller;
|
2024-06-21 19:35:02 +00:00
|
|
|
use App\Models\Application;
|
|
|
|
use App\Models\InstanceSettings;
|
|
|
|
use App\Models\Project;
|
2024-02-16 20:56:38 +00:00
|
|
|
use App\Models\Server as ModelsServer;
|
|
|
|
use Illuminate\Http\Request;
|
2024-07-01 14:26:50 +00:00
|
|
|
use Stringable;
|
2024-02-16 20:56:38 +00:00
|
|
|
|
2024-07-01 14:26:50 +00:00
|
|
|
class ServersController extends Controller
|
2024-02-16 20:56:38 +00:00
|
|
|
{
|
|
|
|
public function servers(Request $request)
|
|
|
|
{
|
2024-07-01 14:26:50 +00:00
|
|
|
$teamId = getTeamIdFromToken();
|
2024-02-16 20:56:38 +00:00
|
|
|
if (is_null($teamId)) {
|
2024-07-01 14:26:50 +00:00
|
|
|
return invalidTokenResponse();
|
2024-02-16 20:56:38 +00:00
|
|
|
}
|
|
|
|
$servers = ModelsServer::whereTeamId($teamId)->select('id', 'name', 'uuid', 'ip', 'user', 'port')->get()->load(['settings'])->map(function ($server) {
|
|
|
|
$server['is_reachable'] = $server->settings->is_reachable;
|
|
|
|
$server['is_usable'] = $server->settings->is_usable;
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2024-02-16 20:56:38 +00:00
|
|
|
return $server;
|
|
|
|
});
|
2024-07-01 14:26:50 +00:00
|
|
|
$servers = $servers->map(function ($server) {
|
|
|
|
return serializeApiResponse($server);
|
|
|
|
});
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2024-07-01 14:26:50 +00:00
|
|
|
return response()->json([
|
|
|
|
'success' => true,
|
|
|
|
'data' => $servers,
|
|
|
|
]);
|
2024-02-16 20:56:38 +00:00
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2024-02-16 20:56:38 +00:00
|
|
|
public function server_by_uuid(Request $request)
|
|
|
|
{
|
2024-03-05 14:47:43 +00:00
|
|
|
$with_resources = $request->query('resources');
|
2024-07-01 14:26:50 +00:00
|
|
|
$teamId = getTeamIdFromToken();
|
2024-02-16 20:56:38 +00:00
|
|
|
if (is_null($teamId)) {
|
2024-07-01 14:26:50 +00:00
|
|
|
return invalidTokenResponse();
|
2024-02-16 20:56:38 +00:00
|
|
|
}
|
|
|
|
$server = ModelsServer::whereTeamId($teamId)->whereUuid(request()->uuid)->first();
|
|
|
|
if (is_null($server)) {
|
2024-06-26 11:00:36 +00:00
|
|
|
return response()->json(['message' => 'Server not found.'], 404);
|
2024-02-16 20:56:38 +00:00
|
|
|
}
|
2024-03-05 14:47:43 +00:00
|
|
|
if ($with_resources) {
|
|
|
|
$server['resources'] = $server->definedResources()->map(function ($resource) {
|
|
|
|
$payload = [
|
|
|
|
'id' => $resource->id,
|
|
|
|
'uuid' => $resource->uuid,
|
|
|
|
'name' => $resource->name,
|
|
|
|
'type' => $resource->type(),
|
|
|
|
'created_at' => $resource->created_at,
|
|
|
|
'updated_at' => $resource->updated_at,
|
|
|
|
];
|
|
|
|
if ($resource->type() === 'service') {
|
|
|
|
$payload['status'] = $resource->status();
|
|
|
|
} else {
|
|
|
|
$payload['status'] = $resource->status;
|
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2024-03-05 14:47:43 +00:00
|
|
|
return $payload;
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
$server->load(['settings']);
|
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2024-07-01 14:26:50 +00:00
|
|
|
return response()->json([
|
|
|
|
'success' => true,
|
|
|
|
'data' => serializeApiResponse($server),
|
|
|
|
]);
|
2024-02-16 20:56:38 +00:00
|
|
|
}
|
2024-06-21 19:35:02 +00:00
|
|
|
|
|
|
|
public function get_domains_by_server(Request $request)
|
|
|
|
{
|
2024-07-01 14:26:50 +00:00
|
|
|
$teamId = getTeamIdFromToken();
|
2024-06-21 19:35:02 +00:00
|
|
|
if (is_null($teamId)) {
|
2024-07-01 14:26:50 +00:00
|
|
|
return invalidTokenResponse();
|
2024-06-21 19:35:02 +00:00
|
|
|
}
|
2024-07-01 14:26:50 +00:00
|
|
|
$uuid = $request->get('uuid');
|
2024-06-21 19:35:02 +00:00
|
|
|
if ($uuid) {
|
|
|
|
$domains = Application::getDomainsByUuid($uuid);
|
|
|
|
|
|
|
|
return response()->json([
|
2024-07-01 14:26:50 +00:00
|
|
|
'success' => true,
|
|
|
|
'data' => serializeApiResponse($domains),
|
2024-06-21 19:35:02 +00:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
$projects = Project::where('team_id', $teamId)->get();
|
|
|
|
$domains = collect();
|
|
|
|
$applications = $projects->pluck('applications')->flatten();
|
|
|
|
$settings = InstanceSettings::get();
|
|
|
|
if ($applications->count() > 0) {
|
|
|
|
foreach ($applications as $application) {
|
|
|
|
$ip = $application->destination->server->ip;
|
|
|
|
$fqdn = str($application->fqdn)->explode(',')->map(function ($fqdn) {
|
2024-07-01 14:26:50 +00:00
|
|
|
$f = str($fqdn)->replace('http://', '')->replace('https://', '')->explode('/');
|
|
|
|
|
|
|
|
return str(str($f[0])->explode(':')[0]);
|
|
|
|
})->filter(function (Stringable $fqdn) {
|
|
|
|
return $fqdn->isNotEmpty();
|
2024-06-21 19:35:02 +00:00
|
|
|
});
|
2024-07-01 14:26:50 +00:00
|
|
|
|
2024-06-21 19:35:02 +00:00
|
|
|
if ($ip === 'host.docker.internal') {
|
|
|
|
if ($settings->public_ipv4) {
|
|
|
|
$domains->push([
|
|
|
|
'domain' => $fqdn,
|
|
|
|
'ip' => $settings->public_ipv4,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
if ($settings->public_ipv6) {
|
|
|
|
$domains->push([
|
|
|
|
'domain' => $fqdn,
|
|
|
|
'ip' => $settings->public_ipv6,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
if (! $settings->public_ipv4 && ! $settings->public_ipv6) {
|
|
|
|
$domains->push([
|
|
|
|
'domain' => $fqdn,
|
|
|
|
'ip' => $ip,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$domains->push([
|
|
|
|
'domain' => $fqdn,
|
|
|
|
'ip' => $ip,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$services = $projects->pluck('services')->flatten();
|
|
|
|
if ($services->count() > 0) {
|
|
|
|
foreach ($services as $service) {
|
|
|
|
$service_applications = $service->applications;
|
|
|
|
if ($service_applications->count() > 0) {
|
|
|
|
foreach ($service_applications as $application) {
|
|
|
|
$fqdn = str($application->fqdn)->explode(',')->map(function ($fqdn) {
|
2024-07-01 14:26:50 +00:00
|
|
|
$f = str($fqdn)->replace('http://', '')->replace('https://', '')->explode('/');
|
|
|
|
|
|
|
|
return str(str($f[0])->explode(':')[0]);
|
|
|
|
})->filter(function (Stringable $fqdn) {
|
|
|
|
return $fqdn->isNotEmpty();
|
2024-06-21 19:35:02 +00:00
|
|
|
});
|
|
|
|
if ($ip === 'host.docker.internal') {
|
|
|
|
if ($settings->public_ipv4) {
|
|
|
|
$domains->push([
|
|
|
|
'domain' => $fqdn,
|
|
|
|
'ip' => $settings->public_ipv4,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
if ($settings->public_ipv6) {
|
|
|
|
$domains->push([
|
|
|
|
'domain' => $fqdn,
|
|
|
|
'ip' => $settings->public_ipv6,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
if (! $settings->public_ipv4 && ! $settings->public_ipv6) {
|
|
|
|
$domains->push([
|
|
|
|
'domain' => $fqdn,
|
|
|
|
'ip' => $ip,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$domains->push([
|
|
|
|
'domain' => $fqdn,
|
|
|
|
'ip' => $ip,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$domains = $domains->groupBy('ip')->map(function ($domain) {
|
|
|
|
return $domain->pluck('domain')->flatten();
|
|
|
|
})->map(function ($domain, $ip) {
|
|
|
|
return [
|
|
|
|
'ip' => $ip,
|
|
|
|
'domains' => $domain,
|
|
|
|
];
|
|
|
|
})->values();
|
|
|
|
|
2024-07-01 14:26:50 +00:00
|
|
|
return response()->json([
|
|
|
|
'success' => true,
|
|
|
|
'data' => serializeApiResponse($domains),
|
|
|
|
]);
|
2024-06-21 19:35:02 +00:00
|
|
|
}
|
2024-02-16 20:56:38 +00:00
|
|
|
}
|