src/Controller/Client/OcServController.php line 38

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Client;
  3. use App\Entity\VPN\Service\Service;
  4. use App\Entity\VPN\Service\ServiceUsage;
  5. use App\Repository\VPN\OcServ\ServerRepository;
  6. use App\Repository\VPN\Service\ServiceRepository;
  7. use App\Repository\VPN\Service\ServiceUsageRepository;
  8. use App\Service\Util\NotificationService;
  9. use App\Service\Util\OcServSSHApi;
  10. use App\Service\Util\ServersAnalyticsUsageService;
  11. use App\Service\Util\Telegram;
  12. use DateTime;
  13. use DateTimeImmutable;
  14. use Doctrine\ORM\EntityManagerInterface;
  15. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  16. use Symfony\Component\HttpFoundation\JsonResponse;
  17. use Symfony\Component\HttpFoundation\Request;
  18. use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
  19. use Symfony\Component\Routing\Annotation\Route;
  20. class OcServController extends AbstractController
  21. {
  22.     public ServerRepository $serversRepository;
  23.     public ServiceRepository $servicesRepository;
  24.     public EntityManagerInterface $entityManager;
  25.     public Telegram $telegram;
  26.     public function __construct(Telegram $telegramEntityManagerInterface $entityManagerServerRepository $serversRepositoryServiceRepository $servicesRepository)
  27.     {
  28.         $this->serversRepository $serversRepository;
  29.         $this->servicesRepository $servicesRepository;
  30.         $this->entityManager $entityManager;
  31.         $this->telegram $telegram;
  32.     }
  33.     #[Route('/client/oc-serv/dc-script/{username}'name'app_client_oc_serv_script')]
  34.     public function index($username,NotificationService $notificationServiceTelegram $telegram,ServiceUsageRepository $serviceUsageRepository Request $requestEntityManagerInterface $entityManager): JsonResponse
  35.     {
  36.         try {
  37.             $ciscoServerIp $_SERVER['REMOTE_ADDR'] ?: null;
  38.             $server $this->serversRepository->findOneBy(['ip' => $ciscoServerIp]);
  39.             if (!$server && $ciscoServerIp!== '20.171.207.48') {
  40. //                $this->telegram->sendRawMessage($ciscoServerIp);
  41.                 return $this->json(['res' => false]  , 403);
  42.             }
  43.             $calculationVolume 1.5;
  44.             $analytics = new ServersAnalyticsUsageService($ciscoServerIp$entityManager);
  45.             $in $request->request->get('STATS_BYTES_IN');
  46.             $out $request->request->get('STATS_BYTES_OUT');
  47.             $totalServerUsage $in $out;
  48.             $analytics->addServerUsage($ciscoServerIp$totalServerUsage);
  49.             /**
  50.              * @var OcServService $ocService
  51.              */
  52.             $clientId substr($username1, -1);
  53.             $service $this->entityManager->getRepository(Service::class)->findOneBy(['clientId' => $clientId]);
  54.             if ($service) {
  55.                 $in $request->request->get('STATS_BYTES_IN');
  56.                 $out $request->request->get('STATS_BYTES_OUT');
  57.                 $newSizeUsed $out $in;
  58.                 if (!$service->getExpireAt())
  59.                 {
  60.                     $now = new \DateTimeImmutable();
  61.                     $expiredAt =  $now->add(new \DateInterval("P{$service->getPlan()->getDays()}D"));
  62.                     $service->setExpireAt($expiredAt);
  63.                     $entityManager->flush();
  64.                 }
  65.                 $service->setOcServUsage(
  66.                     (int)$service->getOcServUsage()
  67.                     +
  68.                     (int)((float)$calculationVolume * (int)$newSizeUsed));
  69.                 if ($service->getPlan()->getServiceType() === 'unlimited' &&
  70.                     $service->getCountry() !==
  71.                     $server->getCountry()->getCode()){
  72. //                    TODO Remove Service
  73.                 }
  74.                 $now = new \DateTime();
  75.                 $todayUsageObject $serviceUsageRepository->findOneBy(['service' => $service 'date' => $now]);
  76.                 if (!$todayUsageObject) {
  77.                     $todayUsageObject = new ServiceUsage();
  78.                     $todayUsageObject->setService($service);
  79.                     $todayUsageObject->setDate(new \DateTime());
  80.                     $entityManager->persist($todayUsageObject);
  81.                 }
  82.                 $todayUsageObject->setOcUsage(
  83.                     $todayUsageObject->getOcUsage() +
  84.                     (int)((float)$calculationVolume * (int)$newSizeUsed)
  85.                 );
  86.                 $this->entityManager->flush();
  87.             }
  88.             $this->entityManager->flush();
  89.             $this->entityManager->clear();
  90.             return $this->json(['res' => true], 200);
  91.         } catch (\Exception $exception) {
  92.             $notificationService->exception($exception);
  93. //            $telegram->exception($exception, 'Error On OcServ Disconnect Usage', $ciscoServerIp ?: null);
  94.         }
  95.         return $this->json(['res' => true]);
  96.     }
  97. }