AgentService.php 5.1 KB

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