Home.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. namespace ModulesGarden\Servers\ProxmoxVps\App\Http\Admin;
  3. use MGProvision\Proxmox\v2\models\Kvm;
  4. use ModulesGarden\ProxmoxAddon\App\Models\ModuleSettings;
  5. use ModulesGarden\ProxmoxAddon\App\Services\ApiService;
  6. use ModulesGarden\ProxmoxAddon\App\Services\Vps\ProductService;
  7. use ModulesGarden\Servers\ProxmoxVps\App\UI\Admin\IpSet\Pages\IpSetDataTable;
  8. use ModulesGarden\Servers\ProxmoxVps\App\UI\Admin\QemuGuestAgent\Pages\NetworkInterfacesDataTable;
  9. use ModulesGarden\Servers\ProxmoxVps\App\UI\Admin\User\Pages\UserDataTable;
  10. use ModulesGarden\Servers\ProxmoxVps\App\UI\Graph\Pages\CpuGraph;
  11. use ModulesGarden\Servers\ProxmoxVps\App\UI\Graph\Pages\DiskGraph;
  12. use ModulesGarden\Servers\ProxmoxVps\App\UI\Graph\Pages\MemoryGraph;
  13. use ModulesGarden\Servers\ProxmoxVps\App\UI\Graph\Pages\NetworkGraph;
  14. use ModulesGarden\Servers\ProxmoxVps\App\UI\Home\Pages\ServiceActions;
  15. use ModulesGarden\Servers\ProxmoxVps\App\UI\IpAddress\Pages\IpAddressDataTable;
  16. use ModulesGarden\Servers\ProxmoxVps\App\UI\Reinstall\Pages\ReinstallTab;
  17. use ModulesGarden\Servers\ProxmoxVps\App\UI\Reinstall\Pages\TemplateDataTable;
  18. use ModulesGarden\Servers\ProxmoxVps\App\UI\ServiceInformation\Pages\ServiceInformation;
  19. use ModulesGarden\Servers\ProxmoxVps\Core\Helper;
  20. use ModulesGarden\Servers\ProxmoxVps\Core\Http\AbstractController;
  21. use ModulesGarden\Servers\ProxmoxVps\Core\Traits\OutputBuffer;
  22. use ModulesGarden\Servers\ProxmoxVps\Core\UI\Traits\WhmcsParams;
  23. use ModulesGarden\ProxmoxAddon\App\Services\Vps\UserService;
  24. use Symfony\Component\HttpFoundation\RedirectResponse;
  25. use MGProvision\Proxmox\v2\Api;
  26. /**
  27. * Example admin home page controler
  28. * @author Sławomir Miśkowicz <slawomir@modulesgarden.com>
  29. */
  30. class Home extends AbstractController
  31. {
  32. use WhmcsParams;
  33. use ProductService;
  34. use ApiService;
  35. use OutputBuffer;
  36. use UserService;
  37. public function index()
  38. {
  39. $view = Helper\viewIntegrationAddon();
  40. $view->initCustomAssetFiles();
  41. //serviceActions
  42. $view->addElement(ServiceActions::class);
  43. // serviceInformationDataTable
  44. $view->addElement(ServiceInformation::class);
  45. $view->addElement(IpAddressDataTable::class);
  46. $view->addElement(IpSetDataTable::class);
  47. if ($this->vm() instanceof Kvm) {
  48. if ($this->vm()->config()['agent'] == 1) {
  49. try {
  50. $this->vm()->agent()->getHostname();
  51. $view->addElement(NetworkInterfacesDataTable::class);
  52. } catch (\Exception $ex) {
  53. }
  54. }
  55. $view->addElement(ReinstallTab::class);
  56. } else {
  57. $view->addElement(TemplateDataTable::class);
  58. }
  59. $view->addElement(CpuGraph::class)
  60. ->addElement(MemoryGraph::class)
  61. ->addElement(NetworkGraph::class)
  62. ->addElement(DiskGraph::class)
  63. ->addElement(UserDataTable::class);
  64. return $view;
  65. }
  66. public function novnc()
  67. {
  68. $this->cleanOutputBuffer();
  69. $this->acl()->novnc();
  70. $this->console(['novnc' => 1]);
  71. }
  72. public function xtermjs()
  73. {
  74. $this->cleanOutputBuffer();
  75. $this->acl()->novnc();
  76. $this->console(['xtermjs' => 1]);
  77. }
  78. private function console($request = [])
  79. {
  80. $serverHost = $this->getWhmcsParamByKey('serverip') ? $this->getWhmcsParamByKey('serverip') : $this->getWhmcsParamByKey('serverhostname');
  81. //Host
  82. $consoleHost = $this->getWhmcsParamByKey('serverhostname') ? $this->getWhmcsParamByKey('serverhostname') : $this->getWhmcsParamByKey('serverip');
  83. if ($this->configuration()->getConsoleHost())
  84. {
  85. $consoleHost = $this->configuration()->getConsoleHost();
  86. }
  87. //Port
  88. $consolePort = 8006;
  89. if(is_numeric($this->getWhmcsParamByKey('serverport'))){
  90. $consolePort = $this->getWhmcsParamByKey('serverport');
  91. }
  92. if (preg_match('/\:/', $consoleHost))
  93. {
  94. $ex = explode(":", $consoleHost);
  95. $consoleHost = $ex['0'];
  96. $consolePort = $ex['1'];
  97. }
  98. try
  99. {
  100. //User
  101. $vpsUser = $this->getUser();
  102. if (empty($vpsUser->username))
  103. {
  104. throw new \Exception('User does not have permissions to access noVNC console');
  105. }
  106. //Load Users API
  107. if(!preg_match('/\:/', $serverHost) && $this->getWhmcsParamByKey('serverport') ){
  108. $serverHost .=":".$this->getWhmcsParamByKey('serverport');
  109. }
  110. $api = new Api($serverHost, $vpsUser->username, $vpsUser->realm, $vpsUser->getPassword());
  111. $api->debug(ModuleSettings::isDebug());
  112. //User Authorization
  113. $ticket = $api->getLoginTicket();
  114. $request['token'] = $ticket['ticket'];
  115. $request['CSRFPreventionToken'] = $ticket['CSRFPreventionToken'];
  116. $request['console'] = $this->vm()->getVirtualization();
  117. /* @deprecated $vars['virtualization'] since version 2.6.0 */
  118. $request['virtualization'] = $this->vm()->getVirtualization();
  119. $request['node'] = $this->vm()->getNode();
  120. $request['vmid'] = $this->vm()->getVmid();
  121. }
  122. catch (\Exception $ex)
  123. {
  124. $request['error'] = $ex->getMessage();
  125. }
  126. $response = new RedirectResponse("https://{$consoleHost}:{$consolePort}/novnc/mgnovnc.html?" . http_build_query($request));
  127. $response->send();
  128. }
  129. }