diff --git a/src/Controller/OgAgent/OgAgentController.php b/src/Controller/OgAgent/OgAgentController.php new file mode 100644 index 0000000..b402993 --- /dev/null +++ b/src/Controller/OgAgent/OgAgentController.php @@ -0,0 +1,224 @@ +toArray(); + $requiredFields = ['mac', 'ip', 'secret', 'ostype', 'osversion', 'agent_version']; + + foreach ($requiredFields as $field) { + if (!isset($data[$field])) { + return new JsonResponse(['message' => "Missing parameter: $field"], Response::HTTP_BAD_REQUEST); + } + } + + // Procesar los datos recibidos si es necesario + + return new JsonResponse([], Response::HTTP_OK); + } + + #[Route('/opengnsys/rest/ogagent/stopped', methods: ['POST'])] + public function agentStopped(Request $request): JsonResponse + { + $data = $request->toArray(); + $requiredFields = ['mac', 'ip', 'ostype', 'osversion']; + + foreach ($requiredFields as $field) { + if (!isset($data[$field])) { + return new JsonResponse(['message' => "Missing parameter: $field"], Response::HTTP_BAD_REQUEST); + } + } + + // Procesar los datos recibidos si es necesario + + return new JsonResponse([], Response::HTTP_OK); + } + + #[Route('/opengnsys/rest/ogagent/loggedin', methods: ['POST'])] + public function agentLoggedIn(Request $request): JsonResponse + { + $data = $request->toArray(); + $requiredFields = ['ip', 'user', 'language', 'session', 'ostype', 'osversion']; + + foreach ($requiredFields as $field) { + if (!isset($data[$field])) { + return new JsonResponse(['message' => "Missing parameter: $field"], Response::HTTP_BAD_REQUEST); + } + } + + // Procesar los datos recibidos si es necesario + + return new JsonResponse([], Response::HTTP_OK); + } + + #[Route('/opengnsys/rest/ogagent/loggedout', methods: ['POST'])] + public function agentLoggedOut(Request $request): JsonResponse + { + $data = $request->toArray(); + $requiredFields = ['ip', 'user']; + + foreach ($requiredFields as $field) { + if (!isset($data[$field])) { + return new JsonResponse(['message' => "Missing parameter: $field"], Response::HTTP_BAD_REQUEST); + } + } + + // Procesar los datos recibidos si es necesario + + return new JsonResponse([], Response::HTTP_OK); + } + + + #[Route('/opengnsys/rest/__ogAdmClient/InclusionCliente', methods: ['POST'])] + public function inclusionCliente(Request $request): JsonResponse + { + $data = $request->toArray(); + $requiredFields = ['iph', 'ido', 'npc', 'idc', 'ida', 'cfg']; + + foreach ($requiredFields as $field) { + if (!isset($data[$field])) { + return new JsonResponse(['message' => "Missing parameter: $field"], Response::HTTP_BAD_REQUEST); + } + } + + // Suponemos que el proceso para incluir al cliente es exitoso + + $responseData = [ + 'res' => 1, + 'ido' => $data['ido'], + 'npc' => $data['npc'], + 'che' => 42, + 'exe' => 42, + 'ida' => $data['ida'], + 'idc' => $data['idc'] + ]; + + return new JsonResponse($responseData, Response::HTTP_OK); + } + + + #[Route('/opengnsys/rest/__ogAdmClient/AutoexecCliente', methods: ['POST'])] + public function autoexecCliente(Request $request): JsonResponse + { + $data = $request->toArray(); + $requiredFields = ['iph', 'ido', 'npc', 'idc', 'ida', 'exe']; + + foreach ($requiredFields as $field) { + if (!isset($data[$field])) { + return new JsonResponse(['message' => "Missing parameter: $field"], Response::HTTP_BAD_REQUEST); + } + } + + // Crear archivo autoexec + $fileAutoExec = '/tmp/Sautoexec-' . $data['iph']; + try { + $fileExe = fopen($fileAutoExec, 'w'); + fwrite($fileExe, json_encode(['param' => '@'.join(["nfn=popup\rtitle=my title\rmessage=my message"])])); + fclose($fileExe); + } catch (\Exception $e) { + return new JsonResponse([], Response::HTTP_INTERNAL_SERVER_ERROR); + } + + $responseData = [ + 'res' => 1, + 'nfl' => $fileAutoExec + ]; + + return new JsonResponse($responseData, Response::HTTP_OK); + } + + #[Route('/opengnsys/rest/__ogAdmClient/enviaArchivo', methods: ['POST'])] + public function enviaArchivo(Request $request): JsonResponse + { + $data = $request->toArray(); + $requiredFields = ['iph', 'ido', 'npc', 'idc', 'ida', 'nfl']; + + foreach ($requiredFields as $field) { + if (!isset($data[$field])) { + return new JsonResponse(['message' => "Missing parameter: $field"], Response::HTTP_BAD_REQUEST); + } + } + + $nfl = $data['nfl']; + if (!file_exists($nfl)) { + return new JsonResponse(['message' => 'File not found'], Response::HTTP_NOT_FOUND); + } + + $contents = file_get_contents($nfl); + + return new JsonResponse(['contents' => $contents], Response::HTTP_OK); + } + + #[Route('/opengnsys/rest/__ogAdmClient/ComandosPendientes', methods: ['POST'])] + public function comandosPendientes(Request $request): JsonResponse + { + $data = $request->toArray(); + $requiredFields = ['iph', 'ido', 'npc', 'idc', 'ida']; + + foreach ($requiredFields as $field) { + if (!isset($data[$field])) { + return new JsonResponse(['message' => "Missing parameter: $field"], Response::HTTP_BAD_REQUEST); + } + } + + // Simula que el cliente existe + if (!$this->clienteExistente($data['iph'])) { + return new JsonResponse(['message' => 'Client not found'], Response::HTTP_NOT_FOUND); + } + + // Simula obtener comandos pendientes + $param = $this->buscaComandos($data['ido']); + + if (is_null($param)) { + return new JsonResponse(['nfn' => 'NoComandosPtes'], Response::HTTP_OK); + } + + return new JsonResponse($param, Response::HTTP_OK); + } + + #[Route('/opengnsys/rest/__ogAdmClient/DisponibilidadComandos', methods: ['POST'])] + public function disponibilidadComandos(Request $request): JsonResponse + { + $data = $request->toArray(); + $requiredFields = ['iph', 'ido', 'npc', 'idc', 'ida', 'tpc']; + + foreach ($requiredFields as $field) { + if (!isset($data[$field])) { + return new JsonResponse(['message' => "Missing parameter: $field"], Response::HTTP_BAD_REQUEST); + } + } + + // Simula que el cliente existe + if (!$this->clienteExistente($data['iph'])) { + return new JsonResponse(['message' => 'Client not found'], Response::HTTP_NOT_FOUND); + } + + return new JsonResponse([], Response::HTTP_OK); + } + + private function clienteExistente($iph) + { + // Simula que el cliente existe + return true; + } + + private function buscaComandos($ido) + { + // Simula obtener comandos pendientes + return ['nfn' => 'popup', 'title' => 'my title', 'message' => 'my message', 'ids' => 42]; + } +} diff --git a/src/OpenApi/OpenApiFactory.php b/src/OpenApi/OpenApiFactory.php index 9e0ae6f..ae73b2b 100644 --- a/src/OpenApi/OpenApiFactory.php +++ b/src/OpenApi/OpenApiFactory.php @@ -9,7 +9,6 @@ use Symfony\Component\HttpFoundation\Response; final readonly class OpenApiFactory implements OpenApiFactoryInterface { - public function __construct(private OpenApiFactoryInterface $decorated) { } @@ -18,18 +17,17 @@ final readonly class OpenApiFactory implements OpenApiFactoryInterface { $openApi = $this->decorated->__invoke($context); - $this->addRefreshToken($openApi); - $this->addSearchEndpoint($openApi); - + $this->addAuthEndpoints($openApi); + $this->addOgAgentEndpoints($openApi); return $openApi; } - private function addRefreshToken(OpenApi $openApi): void + private function addAuthEndpoints(OpenApi $openApi): void { $openApi ->getPaths() - ->addPath( '/auth/refresh', (new Model\PathItem())->withPost( + ->addPath('/auth/refresh', (new Model\PathItem())->withPost( (new Model\Operation('postRefreshToken')) ->withTags(['Login check']) ->withResponses([ @@ -61,7 +59,7 @@ final readonly class OpenApiFactory implements OpenApiFactoryInterface ->withRequestBody( (new Model\RequestBody()) ->withDescription('The refresh token data') - ->withContent( new \ArrayObject([ + ->withContent(new \ArrayObject([ 'application/json' => new Model\MediaType(new \ArrayObject(new \ArrayObject([ 'type' => 'object', 'properties' => [ @@ -77,36 +75,406 @@ final readonly class OpenApiFactory implements OpenApiFactoryInterface ) )); } - private function addSearchEndpoint(OpenApi $openApi): void + + private function addOgAgentEndpoints(OpenApi $openApi): void { $openApi ->getPaths() - ->addPath('/search', (new Model\PathItem())->withGet( - (new Model\Operation('getSearch')) - ->withTags(['Search']) + ->addPath('/opengnsys/rest/ogagent/started', (new Model\PathItem())->withPost( + (new Model\Operation('postAgentStarted')) + ->withTags(['OS agent']) + ->withSummary('Receive notification that an agent has started') + ->withRequestBody( + (new Model\RequestBody()) + ->withDescription('Notification of agent start') + ->withContent(new \ArrayObject([ + 'application/json' => new Model\MediaType(new \ArrayObject(new \ArrayObject([ + 'type' => 'object', + 'properties' => [ + 'mac' => [ + 'type' => 'string', + 'example' => '00:01:02:03:04:05', + ], + 'ip' => [ + 'type' => 'string', + 'example' => '192.168.2.11', + ], + 'secret' => [ + 'type' => 'string', + 'example' => 'nug05l0ipc0lzl578uu5ohm1a074v4t2', + ], + 'ostype' => [ + 'type' => 'string', + 'enum' => ['Linux', 'MacOS', 'Windows'], + ], + 'osversion' => [ + 'type' => 'string', + 'example' => 'Debian GNU/Linux 12 (bookworm)', + ], + 'agent_version' => [ + 'type' => 'string', + 'example' => '1.3.0', + ], + ], + 'required' => ['mac', 'ip', 'secret', 'ostype', 'osversion', 'agent_version'], + ]))) + ])) + ->withRequired(true) + ) ->withResponses([ Response::HTTP_OK => [ - 'description' => 'Search results', + 'description' => 'Success', + 'content' => [ + 'application/json' => [ + 'schema' => ['$ref' => '#/components/schemas/EmptyObj'], + ], + ], + ], + ]) + )); + $openApi + ->getPaths() + ->addPath('/opengnsys/rest/ogagent/stopped', (new Model\PathItem())->withPost( + (new Model\Operation('postAgentStopped')) + ->withTags(['OS agent']) + ->withSummary('Receive notification that an agent has stopped') + ->withRequestBody( + (new Model\RequestBody()) + ->withDescription('Notification of agent stop') + ->withContent(new \ArrayObject([ + 'application/json' => new Model\MediaType(new \ArrayObject(new \ArrayObject([ + 'type' => 'object', + 'properties' => [ + 'mac' => [ + 'type' => 'string', + 'example' => '00:01:02:03:04:05', + ], + 'ip' => [ + 'type' => 'string', + 'example' => '192.168.2.11', + ], + 'ostype' => [ + 'type' => 'string', + 'enum' => ['Linux', 'MacOS', 'Windows'], + ], + 'osversion' => [ + 'type' => 'string', + 'example' => 'Debian GNU/Linux 12 (bookworm)', + ], + ], + 'required' => ['mac', 'ip', 'ostype', 'osversion'], + ]))) + ])) + ->withRequired(true) + ) + ->withResponses([ + Response::HTTP_OK => [ + 'description' => 'Success', + 'content' => [ + 'application/json' => [ + 'schema' => ['$ref' => '#/components/schemas/EmptyObj'], + ], + ], + ], + ]) + )); + $openApi + ->getPaths() + ->addPath('/opengnsys/rest/ogagent/loggedin', (new Model\PathItem())->withPost( + (new Model\Operation('postAgentLoggedIn')) + ->withTags(['OS agent']) + ->withSummary('Receive notification that a user has logged in') + ->withRequestBody( + (new Model\RequestBody()) + ->withDescription('Notification of user login') + ->withContent(new \ArrayObject([ + 'application/json' => new Model\MediaType(new \ArrayObject(new \ArrayObject([ + 'type' => 'object', + 'properties' => [ + 'ip' => [ + 'type' => 'string', + 'example' => '192.168.2.11', + ], + 'user' => [ + 'type' => 'string', + 'example' => 'student', + ], + 'language' => [ + 'type' => 'string', + 'example' => 'en_GB', + ], + 'session' => [ + 'type' => 'string', + 'example' => 'x11', + ], + 'ostype' => [ + 'type' => 'string', + 'enum' => ['Linux', 'MacOS', 'Windows'], + ], + 'osversion' => [ + 'type' => 'string', + 'example' => 'Debian GNU/Linux 12 (bookworm)', + ], + ], + 'required' => ['ip', 'user', 'language', 'session', 'ostype', 'osversion'], + ]))) + ])) + ->withRequired(true) + ) + ->withResponses([ + Response::HTTP_OK => [ + 'description' => 'Success', + 'content' => [ + 'application/json' => [ + 'schema' => ['$ref' => '#/components/schemas/EmptyObj'], + ], + ], + ], + ]) + )); + $openApi + ->getPaths() + ->addPath('/opengnsys/rest/ogagent/loggedout', (new Model\PathItem())->withPost( + (new Model\Operation('postAgentLoggedOut')) + ->withTags(['OS agent']) + ->withSummary('Receive notification that a user has logged out') + ->withRequestBody( + (new Model\RequestBody()) + ->withDescription('Notification of user logout') + ->withContent(new \ArrayObject([ + 'application/json' => new Model\MediaType(new \ArrayObject(new \ArrayObject([ + 'type' => 'object', + 'properties' => [ + 'ip' => [ + 'type' => 'string', + 'example' => '192.168.2.11', + ], + 'user' => [ + 'type' => 'string', + 'example' => 'student', + ], + ], + 'required' => ['ip', 'user'], + ]))) + ])) + ->withRequired(true) + ) + ->withResponses([ + Response::HTTP_OK => [ + 'description' => 'Success', + 'content' => [ + 'application/json' => [ + 'schema' => ['$ref' => '#/components/schemas/EmptyObj'], + ], + ], + ], + ]) + )); + $openApi + ->getPaths() + ->addPath('/opengnsys/rest/__ogAdmClient/InclusionCliente', (new Model\PathItem())->withPost( + (new Model\Operation('postInclusionCliente')) + ->withTags(['OgLive agent']) + ->withSummary('Add client to the list of known ones') + ->withRequestBody( + (new Model\RequestBody()) + ->withDescription('Inclusion cliente request') + ->withContent(new \ArrayObject([ + 'application/json' => new Model\MediaType(new \ArrayObject(new \ArrayObject([ + 'type' => 'object', + 'properties' => [ + 'iph' => [ + 'type' => 'string', + ], + 'ido' => [ + 'type' => 'integer', + ], + 'npc' => [ + 'type' => 'string', + ], + 'idc' => [ + 'type' => 'integer', + ], + 'ida' => [ + 'type' => 'integer', + ], + 'cfg' => [ + 'type' => 'string', + ], + ], + 'required' => ['iph', 'ido', 'npc', 'idc', 'ida', 'cfg'], + ]))) + ])) + ->withRequired(true) + ) + ->withResponses([ + Response::HTTP_OK => [ + 'description' => 'Success', + 'content' => [ + 'application/json' => [ + 'schema' => ['$ref' => '#/components/schemas/InclusionClienteRes'], + ], + ], + ], + ]) + )); + $openApi + ->getPaths() + ->addPath('/opengnsys/rest/__ogAdmClient/AutoexecCliente', (new Model\PathItem())->withPost( + (new Model\Operation('postAutoexecCliente')) + ->withTags(['OgLive agent']) + ->withSummary('Create and return autoexec file for client') + ->withRequestBody( + (new Model\RequestBody()) + ->withDescription('AutoexecCliente request') + ->withContent(new \ArrayObject([ + 'application/json' => new Model\MediaType(new \ArrayObject(new \ArrayObject([ + 'type' => 'object', + 'properties' => [ + 'iph' => [ + 'type' => 'string', + ], + 'ido' => [ + 'type' => 'integer', + ], + 'npc' => [ + 'type' => 'string', + ], + 'idc' => [ + 'type' => 'integer', + ], + 'ida' => [ + 'type' => 'integer', + ], + 'exe' => [ + 'type' => 'string', + ], + ], + 'required' => ['iph', 'ido', 'npc', 'idc', 'ida', 'exe'], + ]))) + ])) + ->withRequired(true) + ) + ->withResponses([ + Response::HTTP_OK => [ + 'description' => 'Success, autoexec file created', 'content' => [ 'application/json' => [ 'schema' => [ 'type' => 'object', 'properties' => [ - 'results' => [ - 'type' => 'array', - 'items' => [ - 'type' => 'object', - 'properties' => [ - 'id' => [ - 'type' => 'integer', - 'example' => 1, - ], - 'name' => [ - 'type' => 'string', - 'example' => 'Item name', - ], - ], - ], + 'res' => [ + 'type' => 'integer', + 'example' => 1, + ], + 'nfl' => [ + 'type' => 'string', + 'example' => '/tmp/Sautoexec-00:01:02:03:04:05', + ], + ], + 'required' => ['res', 'nfl'], + ], + ], + ], + ], + Response::HTTP_BAD_REQUEST => [ + 'description' => 'Missing parameter', + 'content' => [ + 'application/json' => [ + 'schema' => [ + 'type' => 'object', + 'properties' => [ + 'message' => [ + 'type' => 'string', + ], + ], + ], + ], + ], + ], + Response::HTTP_INTERNAL_SERVER_ERROR => [ + 'description' => 'Error creating file', + ], + ]) + )); + $openApi + ->getPaths() + ->addPath('/opengnsys/rest/__ogAdmClient/enviaArchivo', (new Model\PathItem())->withPost( + (new Model\Operation('postEnviaArchivo')) + ->withTags(['OgLive agent']) + ->withSummary('Send the contents of a file') + ->withRequestBody( + (new Model\RequestBody()) + ->withDescription('EnviaArchivo request') + ->withContent(new \ArrayObject([ + 'application/json' => new Model\MediaType(new \ArrayObject(new \ArrayObject([ + 'type' => 'object', + 'properties' => [ + 'iph' => [ + 'type' => 'string', + ], + 'ido' => [ + 'type' => 'integer', + ], + 'npc' => [ + 'type' => 'string', + ], + 'idc' => [ + 'type' => 'integer', + ], + 'ida' => [ + 'type' => 'integer', + ], + 'nfl' => [ + 'type' => 'string', + ], + ], + 'required' => ['iph', 'ido', 'npc', 'idc', 'ida', 'nfl'], + ]))) + ])) + ->withRequired(true) + ) + ->withResponses([ + Response::HTTP_OK => [ + 'description' => 'Success, file content returned', + 'content' => [ + 'application/json' => [ + 'schema' => [ + 'type' => 'object', + 'properties' => [ + 'contents' => [ + 'type' => 'string', + ], + ], + 'required' => ['contents'], + ], + ], + ], + ], + Response::HTTP_BAD_REQUEST => [ + 'description' => 'Missing parameter', + 'content' => [ + 'application/json' => [ + 'schema' => [ + 'type' => 'object', + 'properties' => [ + 'message' => [ + 'type' => 'string', + ], + ], + ], + ], + ], + ], + Response::HTTP_NOT_FOUND => [ + 'description' => 'File not found', + 'content' => [ + 'application/json' => [ + 'schema' => [ + 'type' => 'object', + 'properties' => [ + 'message' => [ + 'type' => 'string', ], ], ], @@ -114,16 +482,173 @@ final readonly class OpenApiFactory implements OpenApiFactoryInterface ], ], ]) - ->withSummary('Search for items') - ->withParameters([ - new Model\Parameter( - 'query', - 'query', - 'Search query parameter', - true, - false, - ), + )); + $openApi + ->getPaths() + ->addPath('/opengnsys/rest/__ogAdmClient/ComandosPendientes', (new Model\PathItem())->withPost( + (new Model\Operation('postComandosPendientes')) + ->withTags(['OgLive agent']) + ->withSummary('Retrieve pending commands for a client') + ->withRequestBody( + (new Model\RequestBody()) + ->withDescription('ComandosPendientes request') + ->withContent(new \ArrayObject([ + 'application/json' => new Model\MediaType(new \ArrayObject(new \ArrayObject([ + 'type' => 'object', + 'properties' => [ + 'iph' => [ + 'type' => 'string', + ], + 'ido' => [ + 'type' => 'integer', + ], + 'npc' => [ + 'type' => 'string', + ], + 'idc' => [ + 'type' => 'integer', + ], + 'ida' => [ + 'type' => 'integer', + ], + ], + 'required' => ['iph', 'ido', 'npc', 'idc', 'ida'], + ]))) + ])) + ->withRequired(true) + ) + ->withResponses([ + Response::HTTP_OK => [ + 'description' => 'Success, pending commands returned', + 'content' => [ + 'application/json' => [ + 'schema' => [ + 'type' => 'object', + 'properties' => [ + 'nfn' => [ + 'type' => 'string', + 'example' => 'NoComandosPtes', + ], + 'comandos' => [ + 'type' => 'array', + 'items' => [ + 'type' => 'string', + ], + ], + ], + ], + ], + ], + ], + Response::HTTP_BAD_REQUEST => [ + 'description' => 'Missing parameter', + 'content' => [ + 'application/json' => [ + 'schema' => [ + 'type' => 'object', + 'properties' => [ + 'message' => [ + 'type' => 'string', + ], + ], + ], + ], + ], + ], + Response::HTTP_NOT_FOUND => [ + 'description' => 'Client not found', + 'content' => [ + 'application/json' => [ + 'schema' => [ + 'type' => 'object', + 'properties' => [ + 'message' => [ + 'type' => 'string', + ], + ], + ], + ], + ], + ], + ]) + )); + $openApi + ->getPaths() + ->addPath('/opengnsys/rest/__ogAdmClient/DisponibilidadComandos', (new Model\PathItem())->withPost( + (new Model\Operation('postDisponibilidadComandos')) + ->withTags(['OgLive agent']) + ->withSummary('Check command availability for a client') + ->withRequestBody( + (new Model\RequestBody()) + ->withDescription('DisponibilidadComandos request') + ->withContent(new \ArrayObject([ + 'application/json' => new Model\MediaType(new \ArrayObject(new \ArrayObject([ + 'type' => 'object', + 'properties' => [ + 'iph' => [ + 'type' => 'string', + ], + 'ido' => [ + 'type' => 'integer', + ], + 'npc' => [ + 'type' => 'string', + ], + 'idc' => [ + 'type' => 'integer', + ], + 'ida' => [ + 'type' => 'integer', + ], + 'tpc' => [ + 'type' => 'integer', + ], + ], + 'required' => ['iph', 'ido', 'npc', 'idc', 'ida', 'tpc'], + ]))) + ])) + ->withRequired(true) + ) + ->withResponses([ + Response::HTTP_OK => [ + 'description' => 'Success, command availability checked', + 'content' => [ + 'application/json' => [ + 'schema' => ['$ref' => '#/components/schemas/EmptyObj'], + ], + ], + ], + Response::HTTP_BAD_REQUEST => [ + 'description' => 'Missing parameter', + 'content' => [ + 'application/json' => [ + 'schema' => [ + 'type' => 'object', + 'properties' => [ + 'message' => [ + 'type' => 'string', + ], + ], + ], + ], + ], + ], + Response::HTTP_NOT_FOUND => [ + 'description' => 'Client not found', + 'content' => [ + 'application/json' => [ + 'schema' => [ + 'type' => 'object', + 'properties' => [ + 'message' => [ + 'type' => 'string', + ], + ], + ], + ], + ], + ], ]) )); } -} \ No newline at end of file +}