<?php
namespace App\Controller\Client;
use App\Entity\VPN\Service\Service;
use App\Entity\VPN\Service\ServiceUsage;
use App\Repository\VPN\OcServ\ServerRepository;
use App\Repository\VPN\Service\ServiceRepository;
use App\Repository\VPN\Service\ServiceUsageRepository;
use App\Service\Util\NotificationService;
use App\Service\Util\OcServSSHApi;
use App\Service\Util\ServersAnalyticsUsageService;
use App\Service\Util\Telegram;
use DateTime;
use DateTimeImmutable;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
class OcServController extends AbstractController
{
public ServerRepository $serversRepository;
public ServiceRepository $servicesRepository;
public EntityManagerInterface $entityManager;
public Telegram $telegram;
public function __construct(Telegram $telegram, EntityManagerInterface $entityManager, ServerRepository $serversRepository, ServiceRepository $servicesRepository)
{
$this->serversRepository = $serversRepository;
$this->servicesRepository = $servicesRepository;
$this->entityManager = $entityManager;
$this->telegram = $telegram;
}
#[Route('/client/oc-serv/dc-script/{username}', name: 'app_client_oc_serv_script')]
public function index($username,NotificationService $notificationService, Telegram $telegram,ServiceUsageRepository $serviceUsageRepository , Request $request, EntityManagerInterface $entityManager): JsonResponse
{
try {
// Request Get
// {"STATS_BYTES_IN":"12065","STATS_BYTES_OUT":"41501","STATS_DURATION":"59","REASON":"disconnect","I$":""}
// _SERVER
// {"USER":"www-data","HOME":"\/var\/www","HTTP_CONTENT_LENGTH":"80","HTTP_CONNECTION":"Keep-Alive","HTTP_CDN_LOOP":"cloudflare; loops=1","HTTP_USER_AGENT":"curl\/7.61.1","HTTP_CF_IPCOUNTRY":"FR","HTTP_CF_VISITOR":"{\"scheme\":\"https\"}","HTTP_ACCEPT":"*\/*","HTTP_CONTENT_TYPE":"application\/x-www-form-urlencoded","HTTP_X_FORWARDED_PROTO":"https","HTTP_ACCEPT_ENCODING":"gzip, br","HTTP_CF_CONNECTING_IP":"188.165.2.99","HTTP_HOST":"beta.tehrannet.org","HTTP_X_FORWARDED_FOR":"188.165.2.99","HTTP_CF_RAY":"92a921d59afb1cb0-AMS","SCRIPT_FILENAME":"\/var\/www\/html\/beta\/public\/index.php","REDIRECT_STATUS":"200","SERVER_NAME":"beta.tehrannet.org","SERVER_PORT":"80","SERVER_ADDR":"188.165.164.83","REMOTE_PORT":"40544","REMOTE_ADDR":"172.70.47.175","SERVER_SOFTWARE":"nginx\/1.18.0","GATEWAY_INTERFACE":"CGI\/1.1","REQUEST_SCHEME":"http","SERVER_PROTOCOL":"HTTP\/1.1","DOCUMENT_ROOT":"\/var\/www\/html\/beta\/public","DOCUMENT_URI":"\/index.php","REQUEST_URI":"\/client\/oc-serv\/dc-script\/hesam","SCRIPT_NAME":"\/index.php","CONTENT_LENGTH":"80","CONTENT_TYPE":"application\/x-www-form-urlencoded","REQUEST_METHOD":"POST","QUERY_STRING":"","FCGI_ROLE":"RESPONDER","PHP_SELF":"\/index.php","REQUEST_TIME_FLOAT":1743689245.082486,"REQUEST_TIME":1743689245,"APP_NAME":"TehranNetVPN","APP_ENV":"prod","APP_SECRET":"e852e3832856bcc5113b10163e6591df","APP_DOMAIN":"https:\/\/www.cheshmak.fun","MAILER_DSN":"gmail+smtp:\/\/tehrannetvpn:iuzaikwvqghktsqo@default","DATABASE_URL":"mysql:\/\/db:mast1391@localhost:3306\/appdev?serverVersion=mariadb-10.4.11","MAILER_ADDRESS":"","DATABASE_USERNAME":"db","DATABASE_PASSWORD":"mast1391","DATABASE_NAME":"app","APP_DOMAIN_V2":"https:\/\/tehrannet.org\/service\/login","APP_DOMAIN_V2UN":"https:\/\/tehrannet.org\/service\/login","APP_DOMAIN_OPEN":"https:\/\/tehrannet.org\/service\/login","APP_DOMAIN_OC":"https:\/\/tehrannet.org\/service\/login","APP_DOMAIN_STATIC":"https:\/\/tehrannet.org\/service\/login","APP_DOMAIN_ROUTER":"https:\/\/tehrannet.org\/service\/login","BOT_USERNAME":"http:\/\/t.me\/tehrannetbot","MAX_CHARGE":"50","APP_DEBUG":"1","SHELL_VERBOSITY":"3","SYMFONY_DOTENV_VARS":"APP_NAME,APP_ENV,APP_SECRET,APP_DOMAIN,MAILER_DSN,DATABASE_URL,MAILER_ADDRESS,DATABASE_USERNAME,DATABASE_PASSWORD,DATABASE_NAME,APP_DOMAIN_V2,APP_DOMAIN_V2UN,APP_DOMAIN_OPEN,APP_DOMAIN_OC,APP_DOMAIN_STATIC,APP_DOMAIN_ROUTER,BOT_USERNAME,MAX_CHARGE,APP_DEBUG,SHELL_VERBOSITY,APP_URL,V2RAY_RANGE,V2RAY_STATIC_RANGE,OC_RANGE,OC_VIP_RANGE,OPEN_RANGE,VIP_RANGE,CLIENTIDRANGE","APP_URL":"https:\/\/tehrannet.org","V2RAY_RANGE":"100000,200000","V2RAY_STATIC_RANGE":"300000000,400000000","OC_RANGE":"500000,600000","OC_VIP_RANGE":"700000,800000","OPEN_RANGE":"900000,1000000","VIP_RANGE":"2000000,3000000","CLIENTIDRANGE":"100000,1000000"}
$ciscoServerIp = $_SERVER['REMOTE_ADDR'] ?: null;
$calculationVolume = 1.5;
$analytics = new ServersAnalyticsUsageService($ciscoServerIp, $entityManager);
$in = $request->request->get('STATS_BYTES_IN');
$out = $request->request->get('STATS_BYTES_OUT');
$totalServerUsage = $in + $out;
$analytics->addServerUsage($ciscoServerIp, $totalServerUsage);
$server = $this->serversRepository->findOneBy(['ip' => $ciscoServerIp]);
/**
* @var OcServService $ocService
*/
$clientId = substr($username, 1, -1);
$service = $this->entityManager->getRepository(Service::class)->findOneBy(['clientId' => $clientId]);
if ($service) {
$in = $request->request->get('STATS_BYTES_IN');
$out = $request->request->get('STATS_BYTES_OUT');
$newSizeUsed = $out + $in;
if (!$service->getExpireAt())
{
$now = new \DateTimeImmutable();
$expiredAt = $now->add(new \DateInterval("P{$service->getPlan()->getDays()}D"));
$service->setExpireAt($expiredAt);
$entityManager->flush();
}
$service->setOcServUsage(
(int)$service->getOcServUsage()
+
(int)((float)$calculationVolume * (int)$newSizeUsed));
if ($service->getPlan()->getServiceType() === 'unlimited' && $service->getCountry() !== $server->getCountry()->getCode()){
// TODO Remove Service
}
$now = new \DateTime();
$todayUsageObject = $serviceUsageRepository->findOneBy(['service' => $service , 'date' => $now]);
if (!$todayUsageObject) {
$todayUsageObject = new ServiceUsage();
$todayUsageObject->setService($service);
$todayUsageObject->setDate(new \DateTime());
$entityManager->persist($todayUsageObject);
}
$todayUsageObject->setOcUsage(
$todayUsageObject->getOcUsage() +
(int)((float)$calculationVolume * (int)$newSizeUsed)
);
$this->entityManager->flush();
}
$this->entityManager->flush();
$this->entityManager->clear();
return $this->json(['res' => true], 200);
} catch (\Exception $exception) {
$notificationService->exception($exception);
// $telegram->exception($exception, 'Error On OcServ Disconnect Usage', $ciscoServerIp ?: null);
}
return $this->json(['res' => true]);
}
}