From 0cce7ff52de9c2d26cb0b58578f6e3294a584151 Mon Sep 17 00:00:00 2001 From: Manuel Aranda Date: Fri, 11 Apr 2025 08:14:07 +0200 Subject: [PATCH] Some improvements. Sync multi clients ogBoot. Added base64 encode run script --- src/Controller/OgAgent/RunScriptAction.php | 6 +-- .../Webhook/AgentSessionController.php | 40 ++++++++++++++++++- src/EventListener/ClientStatusNotifier.php | 15 ++++++- .../OrganizationalUnitSubscriber.php | 35 +++++++++++++++- 4 files changed, 89 insertions(+), 7 deletions(-) diff --git a/src/Controller/OgAgent/RunScriptAction.php b/src/Controller/OgAgent/RunScriptAction.php index 170c91b..0282f3a 100644 --- a/src/Controller/OgAgent/RunScriptAction.php +++ b/src/Controller/OgAgent/RunScriptAction.php @@ -39,15 +39,14 @@ class RunScriptAction extends AbstractController throw new ValidatorException('IP is required'); } - //TODO: base64 script content $data = [ 'nfn' => 'EjecutarScript', - 'scp' => $input->script, + 'scp' => base64_encode($input->script), 'ids' => '0' ]; try { - $response = $this->httpClient->request('POST', 'https://' . $client->getIp() . ':8000/ogAdmClient/EjecutarScript', [ + $response = $this->httpClient->request('POST', 'https://' . $client->getIp() . ':8000/opengnsys/EjecutarScript', [ 'verify_peer' => false, 'verify_host' => false, 'headers' => [ @@ -74,7 +73,6 @@ class RunScriptAction extends AbstractController 'script' => $input->script, ]; - $this->createService->__invoke($client, CommandTypes::RUN_SCRIPT, TraceStatus::SUCCESS, $jobId, $inputData); } diff --git a/src/Controller/OgAgent/Webhook/AgentSessionController.php b/src/Controller/OgAgent/Webhook/AgentSessionController.php index 67d6a6e..6723ea8 100644 --- a/src/Controller/OgAgent/Webhook/AgentSessionController.php +++ b/src/Controller/OgAgent/Webhook/AgentSessionController.php @@ -7,6 +7,7 @@ namespace App\Controller\OgAgent\Webhook; use App\Entity\Client; use App\Model\ClientStatus; use Doctrine\ORM\EntityManagerInterface; +use Psr\Log\LoggerInterface; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Request; @@ -18,7 +19,8 @@ use Symfony\Component\Routing\Annotation\Route; class AgentSessionController extends AbstractController { public function __construct( - protected readonly EntityManagerInterface $entityManager + protected readonly EntityManagerInterface $entityManager, + protected readonly LoggerInterface $logger ) { } @@ -58,6 +60,14 @@ class AgentSessionController extends AbstractController $this->entityManager->persist($client); $this->entityManager->flush(); + $this->logger->info('Client started', [ + 'mac' => $data['mac'], + 'ip' => $data['ip'], + 'ostype' => $data['ostype'], + 'osversion' => $data['osversion'], + 'agent_version' => $data['agent_version'], + ]); + return new JsonResponse([], Response::HTTP_OK); } @@ -82,6 +92,13 @@ class AgentSessionController extends AbstractController $this->entityManager->persist($client); $this->entityManager->flush(); + $this->logger->info('Client stopped', [ + 'mac' => $data['mac'], + 'ip' => $data['ip'], + 'ostype' => $data['ostype'], + 'osversion' => $data['osversion'], + ]); + return new JsonResponse([], Response::HTTP_OK); } @@ -117,6 +134,18 @@ class AgentSessionController extends AbstractController return new JsonResponse(['message' => 'Invalid status'], Response::HTTP_BAD_REQUEST); } + $this->entityManager->persist($client); + $this->entityManager->flush(); + + $this->logger->info('Client logged in', [ + 'ip' => $data['ip'], + 'user' => $data['user'], + 'language' => $data['language'], + 'session' => $data['session'], + 'ostype' => $data['ostype'], + 'osversion' => $data['osversion'], + ]); + return new JsonResponse([], Response::HTTP_OK); } @@ -152,6 +181,15 @@ class AgentSessionController extends AbstractController return new JsonResponse(['message' => 'Invalid status'], Response::HTTP_BAD_REQUEST); } + $this->entityManager->persist($client); + $this->entityManager->flush(); + + $this->logger->info('Client logged out', [ + 'ip' => $data['ip'], + 'user' => $data['user'], + 'ostype' => $data['ostype'], + ]); + return new JsonResponse([], Response::HTTP_OK); } } diff --git a/src/EventListener/ClientStatusNotifier.php b/src/EventListener/ClientStatusNotifier.php index 8ec0ff9..b4ab843 100644 --- a/src/EventListener/ClientStatusNotifier.php +++ b/src/EventListener/ClientStatusNotifier.php @@ -25,6 +25,14 @@ class ClientStatusNotifier public function postUpdate(Client $client, PostUpdateEventArgs $event): void { + $em = $event->getObjectManager(); + $uow = $em->getUnitOfWork(); + $changeSet = $uow->getEntityChangeSet($client); + + if (!array_key_exists('status', $changeSet)) { + return; + } + try { $this->notifyClientStatusChange($client); } catch (\Exception $e) { @@ -38,9 +46,14 @@ class ClientStatusNotifier private function notifyClientStatusChange(Client $client): void { + $data[] = [ + '@id' => '/clients/' . $client->getUuid(), + 'status' => $client->getStatus(), + ]; + $update = new Update( 'clients', - json_encode(['@id' => '/clients/'.$client->getUuid(), 'status' => $client->getStatus()]) + json_encode($data) ); $this->hub->publish($update); diff --git a/src/EventSubscriber/OrganizationalUnitSubscriber.php b/src/EventSubscriber/OrganizationalUnitSubscriber.php index 57f0adb..a25eae3 100644 --- a/src/EventSubscriber/OrganizationalUnitSubscriber.php +++ b/src/EventSubscriber/OrganizationalUnitSubscriber.php @@ -5,6 +5,7 @@ namespace App\EventSubscriber; use ApiPlatform\Symfony\EventListener\EventPriorities; use App\Controller\OgBoot\PxeBootFile\PostAction; use App\Dto\Output\OrganizationalUnitOutput; +use App\Entity\Client; use App\Entity\NetworkSettings; use App\Entity\OrganizationalUnit; use Doctrine\ORM\EntityManagerInterface; @@ -12,11 +13,16 @@ use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpKernel\Event\ViewEvent; use Symfony\Component\HttpKernel\KernelEvents; +use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface; +use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface; +use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface; +use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface; final readonly class OrganizationalUnitSubscriber implements EventSubscriberInterface { public function __construct( private EntityManagerInterface $entityManager, + private readonly PostAction $postAction, ) { @@ -29,6 +35,12 @@ final readonly class OrganizationalUnitSubscriber implements EventSubscriberInte ]; } + /** + * @throws TransportExceptionInterface + * @throws ServerExceptionInterface + * @throws RedirectionExceptionInterface + * @throws ClientExceptionInterface + */ public function updateNetworkSettings(ViewEvent $event): void { $organizationalUnitOutput = $event->getControllerResult(); @@ -49,8 +61,9 @@ final readonly class OrganizationalUnitSubscriber implements EventSubscriberInte $newNetworkSettings = $this->buildNetworkSettings($organizationalUnitEntity); $this->updateChildrenNetworkSettings($organizationalUnitEntity, $newNetworkSettings); - $this->entityManager->flush(); + + $this->syncOgBoot($organizationalUnitEntity); } @@ -103,4 +116,24 @@ final readonly class OrganizationalUnitSubscriber implements EventSubscriberInte return $newNetworkSettings; } + + /** + * @throws TransportExceptionInterface + * @throws ServerExceptionInterface + * @throws RedirectionExceptionInterface + * @throws ClientExceptionInterface + */ + private function syncOgBoot (OrganizationalUnit $organizationalUnitEntity): void + { + $clients = $this->entityManager->getRepository(Client::class)->findClientsByOrganizationalUnitAndDescendants($organizationalUnitEntity->getId(), []); + + /** @var Client $client */ + foreach ($clients as $client) { + if ($client->getTemplate() === null) { + continue; + } + + $this->postAction->__invoke($client, $client->getTemplate()); + } + } } \ No newline at end of file