Console.php 5.1 KB

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