Home.php 7.3 KB

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