src/Controller/Cronjob/SafeRemove/SafetyRemoveController.php line 23

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Cronjob\SafeRemove;
  3. use App\Entity\VPN\OcServ\Server;
  4. use App\Repository\VPN\Service\ServiceRepository;
  5. use App\Repository\VPN\V2ray\ServerRepository;
  6. use App\Service\Util\MarzbanNewAPI;
  7. use App\Service\Util\Mikrotik;
  8. use App\Service\Util\NotificationService;
  9. use App\Service\Util\OcServSSHApi;
  10. use App\Service\Util\Telegram;
  11. use DateTime;
  12. use Doctrine\ORM\EntityManagerInterface;
  13. use Exception;
  14. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  15. use Symfony\Component\HttpFoundation\JsonResponse;
  16. use Symfony\Component\Routing\Annotation\Route;
  17. class SafetyRemoveController extends AbstractController
  18. {
  19.     #[Route('/cronjob/safe-remove'name'app_cronjob_safe_remove')]
  20.     public function app_cronjob_safe_remove(NotificationService $notificationService,\App\Repository\VPN\OpenVPN\ServerRepository $openServerRepository ,EntityManagerInterface $registryServiceRepository $serviceRepository\App\Repository\VPN\OcServ\ServerRepository $ocServerRepository,  ServerRepository $v2rayServerRepositoryTelegram $telegram): JsonResponse
  21.     {
  22.         $now = new DateTime();
  23.         $currentHour $now->format('H');
  24.         foreach ($v2rayServerRepository->findBy([
  25.             'deletedAt' => null'active' => 1]) as $server) {
  26. //            try {
  27.                 $apiObject = new MarzbanNewAPI($server);
  28.                 $users $apiObject->getPanelUsers(0)['list'];
  29.                 if (!isset($apiObject->getPanelUsers(0)['list'])){
  30.                     var_dump($users);die;
  31.                 }
  32.             foreach ($users as $item) {
  33.                     $service $serviceRepository->findOneBy(['clientId' => $item['username'], 'isEnabled' => 1'active' => 1]);
  34.                     if (!$service) {
  35.                         $apiObject = new MarzbanNewAPI($server);
  36.                         $apiObject->removeUser($item['username']);
  37.                     }else{
  38.                         if (($service->getPlan()->getServiceType() === 'unlimited' && $service->getCountry() !== $server->getCountry()->getCode())){
  39.                             $apiObject = new MarzbanNewAPI($server);
  40.                             $apiObject->removeUser($item['username']);
  41.                         }
  42.                     }
  43.                 }
  44. //            } catch (Exception $exception) {
  45. //                $notificationService->exception($exception);
  46. //                $telegram->exception($exception, 'V2rayMultiUsage', $server->getIp());
  47. //            }
  48.         }
  49.         $tempFileName uniqid('temp-'false) . '.temp';
  50.         try {
  51.             foreach ($ocServerRepository->findBy(['active' => 1'deletedAt' => null
  52.             ]) as $server) {
  53.                 $ocServSShApi = new OcServSSHApi($server);
  54.                 $host $server->getIp();
  55.                 $port 22;
  56.                 $username $server->getSshUsername();
  57.                 $password $server->getSshPassword();
  58.                 $remote_fille_path '/etc/ocserv/ocpasswd';
  59.                 $local_file_path "./" $tempFileName;
  60.                 $connection ssh2_connect($host$port);
  61.                 if (!$connection) {
  62.                     throw new \Exception("Could not connect to $host on port $port");
  63.                 }
  64.                 $auth ssh2_auth_password($connection$username$password);
  65.                 if (!$auth) {
  66.                     throw new \Exception("Could not authenticate with username $username and password ");
  67.                 }
  68.                 $sftp ssh2_sftp($connection);
  69.                 if (!$sftp) {
  70.                     throw new \Exception("Could not initialize SFTP subsystem.");
  71.                 }
  72.                 if (ssh2_scp_recv($connection$remote_fille_path$local_file_path)) {
  73. //                        echo "File successfully downloaded to $local_file_path";
  74.                 } else {
  75.                     throw new \Exception("Failed to download file from $remote_fille_path to $local_file_path.");
  76.                 }
  77.                 $lines = [];
  78.                 $fh fopen($local_file_path'r');
  79.                 while ($line fgets($fh)) {
  80.                     $lines[] = str_replace('
  81. '''$line);
  82.                 }
  83.                 fclose($fh);
  84.                 foreach ($lines as $line) {
  85.                     $username explode(':'$line)[0];
  86.                     $clientID substr($username1, -1);
  87.                     $service $serviceRepository->findOneBy(['clientId' => $clientID'isEnabled' => 1]);
  88.                     if (!$service) {
  89.                         $ocServSShApi->removeUser($username);
  90.                     }else{
  91.                         if ($service->getPlan()->getServiceType() === 'unlimited' && $service->getCountry() !== $server->getCountry()->getCode()) {
  92.                             $ocServSShApi->removeUser($username);
  93.                         }
  94.                     }
  95.                 }
  96.                 exec('rm -rf ' $tempFileName);
  97.             }
  98.         } catch (\Exception $exception) {
  99.             $notificationService->exception($exception);
  100.             $telegram->exception($exception);
  101.         }
  102.         try {
  103.             foreach ($openServerRepository->findBy(['active' => 1]) as $server) {
  104.                 $serverContent null;
  105.                 $apiObject = new Mikrotik($server->getUMEndpoint(), $server->getUMUsername(), $server->getUMPassword(), $server->getUMProfileName(), $server->getUMSSHPort());
  106.                 $serverContent $apiObject->getUsersUsage()['data'];
  107.                 if ($serverContent) {
  108.                     foreach ($serverContent as $item) {
  109.                         $service $serviceRepository->findOneBy(['clientId' => $item['name'], 'isEnabled' => 1'active' => 1]);
  110.                         if (!$service) {
  111.                             $apiObject->removeService($item['name']);
  112.                         }
  113.                     }
  114.                 }
  115.             }
  116.         } catch (\Exception $exception) {
  117.             $notificationService->exception($exception);
  118.             $telegram->exception($exception);
  119.         }
  120.         return $this->json(['res' => true]);
  121.     }
  122. }