Console.php 5.9 KB

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