AgentService.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. namespace ModulesGarden\ProxmoxAddon\App\Services\Vps;
  3. use MGProvision\Proxmox\v2\ProxmoxApiException;
  4. use ModulesGarden\ProxmoxAddon\App\Models\VmIpAddress;
  5. use ModulesGarden\ProxmoxAddon\App\Services\ApiService;
  6. use ModulesGarden\ProxmoxAddon\App\UI\IpManagement\Providers\IpAddressProvider;
  7. use ModulesGarden\ProxmoxAddon\Core\UI\Traits\WhmcsParams;
  8. class AgentService
  9. {
  10. use WhmcsParams;
  11. use HostingService;
  12. use ApiService;
  13. use ProductService;
  14. private $networkInterfaces=[];
  15. private $assignedIpAddresses=[];
  16. public function isEnabled()
  17. {
  18. $configurationIsOn = $this->configuration()->isAgent() && ( $this->configuration()->isAgentConfigureNetwork() || $this->configuration()->isAgentTemplateUser() || $this->configuration()->isAgentServicePassword());
  19. $ostype = $this->vm()->config()['ostype'];
  20. return $configurationIsOn && preg_match('/w/', $ostype);
  21. }
  22. public function getUserAndUpdate()
  23. {
  24. if(!$this->configuration()->isAgentTemplateUser()){
  25. return;
  26. }
  27. $response = $this->vm()->agent()->getUsers();
  28. if(!$response['result']['0']['user']){
  29. throw new ProxmoxApiException("OS user not found");
  30. }
  31. $user = $response['result']['0']['user'];
  32. $this->hosting()->username = $user;
  33. $this->hosting()->save();
  34. }
  35. public function passwordUpdate()
  36. {
  37. if(!$this->configuration()->isAgentServicePassword()){
  38. return;
  39. }
  40. $this->vm()->agent()->setUserPassword($this->hosting()->username, $this->getWhmcsParamByKey("password"));
  41. }
  42. public function configureNetwork()
  43. {
  44. if(!$this->configuration()->isAgentConfigureNetwork()){
  45. return;
  46. }
  47. $this->networkInterfaces = $this->vm()->agent()->networkGetInterfaces();
  48. //remove
  49. $this->deleteIpAddresses();
  50. $ips = VmIpAddress::ofHostingId($this->getWhmcsParamByKey("serviceid"))
  51. ->orderByIdAsc();
  52. foreach ($this->networkInterfaces['result'] as $net){
  53. $networkName = $net['name'];
  54. if(!$net['hardware-address'] || preg_match("/loopback/", strtolower($networkName) ) ){ //Loopback
  55. continue;
  56. }
  57. /**
  58. * Add IP Addresses
  59. * @var $ip VmIpAddress
  60. */
  61. foreach ($ips->get() as $ip){
  62. //exist?
  63. if(in_array($ip->ip, (array) $this->assignedIpAddresses)){
  64. continue;
  65. }
  66. //IPV6
  67. if(preg_match("/\:/", $ip->ip )){
  68. $command = sprintf("netsh interface ipv6 add address \"%s\" %s", $networkName, $ip->ip);
  69. //IPV4
  70. }else {
  71. $subnet = $ip->subnet_mask ? $ip->subnet_mask : IpAddressProvider::translateBitmaskToNetmask($ip->cidr);
  72. if($ip->ip == $this->hosting()->dedicatedip){
  73. $command = sprintf("netsh interface ipv4 set address name=\"%s\" static %s %s %s",$networkName, $ip->ip, $subnet, $ip->gateway);
  74. }else{
  75. $command = sprintf("netsh interface ipv4 add address name=\"%s\" %s %s %s",$networkName, $ip->ip, $subnet, $ip->gateway);
  76. }
  77. }
  78. $this->vm()->agent()->exec($command);
  79. $this->assignedIpAddresses[] = $ip->ip;
  80. sleep(1);
  81. if(!$this->configuration()->isOneNetworkDevice()){
  82. break;
  83. }
  84. }
  85. //configure DNS
  86. $ns1 = trim($this->hosting()->ns1);
  87. $ns2 = trim($this->hosting()->ns2);
  88. if (!empty($ns1) && filter_var($ns1, FILTER_VALIDATE_IP))
  89. {
  90. $command = sprintf("netsh interface ip set dns name=\"%s\" static %s",$networkName, $ns1);
  91. $this->vm()->agent()->exec($command);
  92. sleep(1);
  93. }
  94. if (!empty($ns2) && filter_var($ns2, FILTER_VALIDATE_IP))
  95. {
  96. $command = sprintf("netsh interface ip add dns name=\"%s\" %s index=2",$networkName, $ns2);
  97. $this->vm()->agent()->exec($command);
  98. sleep(1);
  99. }
  100. }
  101. }
  102. private function deleteIpAddresses(){
  103. foreach ($this->networkInterfaces['result'] as $net){
  104. foreach ($net['ip-addresses'] as $ipAddress)
  105. {
  106. if(!$net['hardware-address'] || preg_match("/loopback/", strtolower($net['name']) ) ){ //Loopback
  107. continue;
  108. }
  109. $ipAddress = $ipAddress['ip-address'];
  110. $this->assignedIpAddresses[] = $ipAddress;
  111. if(!$ipAddress || VmIpAddress::ofHostingId($this->getWhmcsParamByKey("serviceid"))->ofIp($ipAddress)->count()){
  112. continue;
  113. }
  114. //IPV6
  115. if(preg_match("/\:/", $ipAddress )){
  116. $command = sprintf("netsh interface ipv6 delete address %s", $ipAddress);
  117. //IPV4
  118. }else{
  119. $command = sprintf("netsh interface ipv4 delete address name=\"%s\" %s", $net['name'], $ipAddress);
  120. }
  121. $this->vm()->agent()->exec($command);
  122. sleep(1);
  123. }
  124. }
  125. }
  126. }