Console.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. namespace ModulesGarden\Servers\ProxmoxCloudVps\App\Http\Client;
  3. use MGProvision\Proxmox\v2\Api;
  4. use ModulesGarden\ProxmoxAddon\App\Models\ModuleSettings;
  5. use ModulesGarden\ProxmoxAddon\App\Services\ApiService;
  6. use ModulesGarden\ProxmoxAddon\App\Services\Cloud\ProductService;
  7. use ModulesGarden\ProxmoxAddon\App\Services\Cloud\UserService;
  8. use ModulesGarden\ProxmoxAddon\App\Services\Utility;
  9. use ModulesGarden\Servers\ProxmoxCloudVps\App\Helpers\AppParams;
  10. use ModulesGarden\Servers\ProxmoxCloudVps\App\UI\Home\Pages\DetailsContainer;
  11. use ModulesGarden\Servers\ProxmoxCloudVps\Core\Http\AbstractClientController;
  12. use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Traits\WhmcsParams;
  13. use Symfony\Component\HttpFoundation\RedirectResponse;
  14. use Symfony\Component\HttpFoundation\StreamedResponse;
  15. class Console extends AbstractClientController
  16. {
  17. use WhmcsParams;
  18. use UserService;
  19. use ApiService;
  20. use ProductService;
  21. use BaseClientController;
  22. /**
  23. * Console constructor.
  24. */
  25. public function __construct()
  26. {
  27. $this->initVm();;
  28. (new AppParams())->initFromWhmcsParams();
  29. }
  30. public function novnc()
  31. {
  32. $this->acl()->novnc();
  33. $this->console(['novnc' => 1]);
  34. }
  35. public function xtermjs()
  36. {
  37. $this->acl()->xtermjs();
  38. $this->console(['xtermjs' => 1]);
  39. }
  40. private function console($request = [])
  41. {
  42. $vm = \ModulesGarden\ProxmoxAddon\Core\Helper\sl('Vm')->getVm();
  43. $serverHost = $this->getWhmcsParamByKey('serverip') ? $this->getWhmcsParamByKey('serverip') : $this->getWhmcsParamByKey('serverhostname');
  44. //Host
  45. $consoleHost = $this->getWhmcsParamByKey('serverhostname') ? $this->getWhmcsParamByKey('serverhostname') : $this->getWhmcsParamByKey('serverip');
  46. if ($this->configuration()->getConsoleHost())
  47. {
  48. $consoleHost = $this->configuration()->getConsoleHost();
  49. }
  50. //Port
  51. $consolePort = 8006;
  52. if(is_numeric($this->getWhmcsParamByKey('serverport'))){
  53. $consolePort = $this->getWhmcsParamByKey('serverport');
  54. }
  55. if (preg_match('/\:/', $consoleHost))
  56. {
  57. $ex = explode(":", $consoleHost);
  58. $consoleHost = $ex['0'];
  59. $consolePort = $ex['1'];
  60. }
  61. try
  62. {
  63. //User
  64. $vpsUser = $this->getUser();
  65. if (empty($vpsUser->username))
  66. {
  67. throw new \Exception('User does not have permissions to access noVNC console');
  68. }
  69. //Load Users API
  70. if(!preg_match('/\:/', $serverHost) && $this->getWhmcsParamByKey('serverport') ){
  71. $serverHost .=":".$this->getWhmcsParamByKey('serverport');
  72. }
  73. $api = new Api($serverHost, $vpsUser->username, $vpsUser->realm, $vpsUser->getPassword());
  74. $api->debug(ModuleSettings::isDebug());
  75. //User Authorization
  76. $ticket = $api->getLoginTicket();
  77. $request['token'] = $ticket['ticket'];
  78. $request['CSRFPreventionToken'] = $ticket['CSRFPreventionToken'];
  79. $request['console'] = $vm->getVirtualization();
  80. /* @deprecated $vars['virtualization'] since version 2.6.0 */
  81. $request['virtualization'] = $vm->getVirtualization();
  82. $request['node'] = $vm->getNode();
  83. $request['vmid'] = $vm->getVmid();
  84. }
  85. catch (\Exception $ex)
  86. {
  87. $request['error'] = $ex->getMessage();
  88. }
  89. $response = new RedirectResponse("https://{$consoleHost}:{$consolePort}/novnc/mgnovnc.html?" . http_build_query($request));
  90. $response->send();
  91. }
  92. public function spice()
  93. {
  94. $this->acl()->spice();
  95. $vm = \ModulesGarden\ProxmoxAddon\Core\Helper\sl('Vm')->getVm();
  96. $this->api();
  97. $proxy = $vm->spiceproxy();
  98. $consoleHost = $this->getWhmcsParamByKey('serverhostname') ? $this->getWhmcsParamByKey('serverhostname') : $this->getWhmcsParamByKey('serverip');
  99. if ($this->configuration()->getConsoleHost())
  100. {
  101. $consoleHost = $this->configuration()->getConsoleHost();
  102. }
  103. if ($consoleHost)
  104. {
  105. $ex = explode(":", $proxy['proxy']);
  106. $port = end($ex);
  107. $proxy['proxy'] = "http://{$consoleHost}:{$port}";
  108. }
  109. $content = "[virt-viewer]\r\n";
  110. foreach ($proxy as $k => $v)
  111. {
  112. $content .= "{$k}={$v}\r\n";
  113. }
  114. //Response
  115. $response = new StreamedResponse();
  116. $response->setStatusCode(200);
  117. $response->headers->set('Cache-Control', 'public');
  118. $response->headers->set('Content-Type', 'application/x-virt-viewer');
  119. $response->headers->set('Content-Transfer-Encoding', 'Binary');
  120. $response->setCallback(function () use ($content)
  121. {
  122. echo $content;
  123. });
  124. $fileName = Utility::generatePassword(8, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");
  125. $fileName .= ".vv";
  126. $response->headers->set(
  127. 'Content-Disposition', "attachment; filename=\"{$fileName}\""
  128. );
  129. $response->send();
  130. exit();
  131. }
  132. }