<?php
namespace App\Controller\Cronjob\SafeRemove;
use App\Entity\VPN\OcServ\Server;
use App\Repository\VPN\Service\ServiceRepository;
use App\Repository\VPN\V2ray\ServerRepository;
use App\Service\Util\MarzbanNewAPI;
use App\Service\Util\Mikrotik;
use App\Service\Util\NotificationService;
use App\Service\Util\OcServSSHApi;
use App\Service\Util\Telegram;
use DateTime;
use Doctrine\ORM\EntityManagerInterface;
use Exception;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Annotation\Route;
class SafetyRemoveController extends AbstractController
{
#[Route('/cronjob/safe-remove', name: 'app_cronjob_safe_remove')]
public function app_cronjob_safe_remove(NotificationService $notificationService,\App\Repository\VPN\OpenVPN\ServerRepository $openServerRepository ,EntityManagerInterface $registry, ServiceRepository $serviceRepository, \App\Repository\VPN\OcServ\ServerRepository $ocServerRepository, ServerRepository $v2rayServerRepository, Telegram $telegram): JsonResponse
{
$now = new DateTime();
$currentHour = $now->format('H');
foreach ($v2rayServerRepository->findBy([
'deletedAt' => null, 'active' => 1]) as $server) {
// try {
$apiObject = new MarzbanNewAPI($server);
$users = $apiObject->getPanelUsers(0)['list'];
if (!isset($apiObject->getPanelUsers(0)['list'])){
var_dump($users);die;
}
foreach ($users as $item) {
$service = $serviceRepository->findOneBy(['clientId' => $item['username'], 'isEnabled' => 1, 'active' => 1]);
if (!$service) {
$apiObject = new MarzbanNewAPI($server);
$apiObject->removeUser($item['username']);
}else{
if (($service->getPlan()->getServiceType() === 'unlimited' && $service->getCountry() !== $server->getCountry()->getCode())){
$apiObject = new MarzbanNewAPI($server);
$apiObject->removeUser($item['username']);
}
}
}
// } catch (Exception $exception) {
// $notificationService->exception($exception);
// $telegram->exception($exception, 'V2rayMultiUsage', $server->getIp());
// }
}
$tempFileName = uniqid('temp-', false) . '.temp';
try {
foreach ($ocServerRepository->findBy(['active' => 1, 'deletedAt' => null
]) as $server) {
$ocServSShApi = new OcServSSHApi($server);
$host = $server->getIp();
$port = 22;
$username = $server->getSshUsername();
$password = $server->getSshPassword();
$remote_fille_path = '/etc/ocserv/ocpasswd';
$local_file_path = "./" . $tempFileName;
$connection = ssh2_connect($host, $port);
if (!$connection) {
throw new \Exception("Could not connect to $host on port $port");
}
$auth = ssh2_auth_password($connection, $username, $password);
if (!$auth) {
throw new \Exception("Could not authenticate with username $username and password ");
}
$sftp = ssh2_sftp($connection);
if (!$sftp) {
throw new \Exception("Could not initialize SFTP subsystem.");
}
if (ssh2_scp_recv($connection, $remote_fille_path, $local_file_path)) {
// echo "File successfully downloaded to $local_file_path";
} else {
throw new \Exception("Failed to download file from $remote_fille_path to $local_file_path.");
}
$lines = [];
$fh = fopen($local_file_path, 'r');
while ($line = fgets($fh)) {
$lines[] = str_replace('
', '', $line);
}
fclose($fh);
foreach ($lines as $line) {
$username = explode(':', $line)[0];
$clientID = substr($username, 1, -1);
$service = $serviceRepository->findOneBy(['clientId' => $clientID, 'isEnabled' => 1]);
if (!$service) {
$ocServSShApi->removeUser($username);
}else{
if ($service->getPlan()->getServiceType() === 'unlimited' && $service->getCountry() !== $server->getCountry()->getCode()) {
$ocServSShApi->removeUser($username);
}
}
}
exec('rm -rf ' . $tempFileName);
}
} catch (\Exception $exception) {
$notificationService->exception($exception);
$telegram->exception($exception);
}
try {
foreach ($openServerRepository->findBy(['active' => 1]) as $server) {
$serverContent = null;
$apiObject = new Mikrotik($server->getUMEndpoint(), $server->getUMUsername(), $server->getUMPassword(), $server->getUMProfileName(), $server->getUMSSHPort());
$serverContent = $apiObject->getUsersUsage()['data'];
if ($serverContent) {
foreach ($serverContent as $item) {
$service = $serviceRepository->findOneBy(['clientId' => $item['name'], 'isEnabled' => 1, 'active' => 1]);
if (!$service) {
$apiObject->removeService($item['name']);
}
}
}
}
} catch (\Exception $exception) {
$notificationService->exception($exception);
$telegram->exception($exception);
}
return $this->json(['res' => true]);
}
}