VmIpAddressConveter.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. namespace ModulesGarden\ProxmoxAddon\App\Services\Vps;
  3. use MGProvision\Proxmox\v2\models\IpConfig;
  4. use MGProvision\Proxmox\v2\models\Kvm;
  5. use MGProvision\Proxmox\v2\models\Lxc;
  6. use MGProvision\Proxmox\v2\models\NetworkDeviceKvm;
  7. use MGProvision\Proxmox\v2\models\NetworkDeviceLxc;
  8. use ModulesGarden\ProxmoxAddon\App\Enum\Vps\ConfigurableOption;
  9. use ModulesGarden\ProxmoxAddon\App\Models\VmIpAddress;
  10. use ModulesGarden\ProxmoxAddon\Core\UI\Traits\WhmcsParams;
  11. class VmIpAddressConveter
  12. {
  13. use WhmcsParams;
  14. use ProductService;
  15. /**
  16. * @var Kvm|Lxc|null
  17. */
  18. protected $vm;
  19. /**
  20. * @var NetworkDeviceKvm[]
  21. */
  22. protected $networkDevices;
  23. /**
  24. * @var IpConfig[]
  25. */
  26. protected $ipConfigs=[];
  27. /**
  28. * @var VmIpAddress[]
  29. */
  30. protected $vmIpAddress;
  31. /**
  32. * VmIpAddressConveter constructor.
  33. * @param Kvm|Lxc|null $vm
  34. * @param VmIpAddress[] $vmIpAddress
  35. */
  36. public function __construct( array $vmIpAddress, $vm=null)
  37. {
  38. $this->vmIpAddress = $vmIpAddress;
  39. $this->vm = $vm;
  40. }
  41. public function convert(){
  42. $rate = $this->getRate();
  43. $networkId = $this->freeNetworDeviceId();
  44. foreach ($this->vmIpAddress as $vmIpAddress){
  45. $bridge = $this->getBridge($vmIpAddress);
  46. if ($this->configuration()->isQemu())
  47. {
  48. $networkDevice = new NetworkDeviceKvm('net' . $networkId);
  49. $networkDevice->setBridge($bridge)
  50. ->setRate($rate)
  51. ->setFirewall($this->configuration()->isNetworkFirewall() ? 1 : 0);
  52. //ip config
  53. $ipConfig = new IpConfig('ipconfig' . $networkId);
  54. //ipv4
  55. if(!preg_match("/\:/",$vmIpAddress->ip)){
  56. $ipConfig->setIp(trim($vmIpAddress->ip))
  57. ->setCidr(trim($vmIpAddress->cidr))
  58. ->setGw(trim($vmIpAddress->gateway));
  59. }else{
  60. //ipv6
  61. $ipConfig->setIp6(trim($vmIpAddress->ip))
  62. ->setCidr6(trim($vmIpAddress->cidr))
  63. ->setGw6(trim($vmIpAddress->gateway));
  64. }
  65. $networkDevice->setTag($vmIpAddress->tag)
  66. ->setMacAddress($vmIpAddress->mac_address)
  67. ->setTrunks($vmIpAddress->trunks)
  68. ->setQueues(trim($this->configuration()->getQueues()));
  69. if(filter_var($vmIpAddress->ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE)){
  70. $networkDevice->setModel($this->configuration()->getNetworkModel());
  71. }else{
  72. //private
  73. $networkDevice->setModel($this->configuration()->getNetworkPrivateModel());
  74. }
  75. $this->networkDevices[] = $networkDevice;
  76. $this->ipConfigs[] = $ipConfig;
  77. }
  78. //lxc
  79. if ($this->configuration()->isLxc())
  80. {
  81. $networkDevice = new NetworkDeviceLxc('net' . $networkId);
  82. $networkDevice->setName('eth' . $networkId)
  83. ->setBridge($bridge)
  84. ->setFirewall($this->configuration()->isNetworkFirewall() ? 1 : 0)
  85. ->setIp($vmIpAddress->ip)
  86. ->setRate($rate)
  87. ->setCidr($vmIpAddress->cidr)
  88. ->setGw($vmIpAddress->gateway)
  89. ->setTag($vmIpAddress->tag)
  90. ->setHwaddr($vmIpAddress->mac_address)
  91. ->setTrunks($vmIpAddress->trunks);
  92. $this->networkDevices[] = $networkDevice;
  93. }
  94. $vmIpAddress->net = $networkDevice->getId();
  95. $vmIpAddress->save();
  96. $networkId++;
  97. unset($ip);
  98. }
  99. }
  100. public function asConfig(){
  101. $this->convert();
  102. return array_merge($this->getNetworkDevicesAsConfig(), $this->getIpConfigsAsConfig());
  103. }
  104. public function getNetworkDevicesAsConfig(){
  105. $container=[];
  106. foreach ($this->networkDevices as $networkDevice){
  107. $container[$networkDevice->getId()] = $networkDevice->asConfig();
  108. }
  109. return $container;
  110. }
  111. public function getIpConfigsAsConfig(){
  112. $container=[];
  113. foreach ($this->ipConfigs as $ipConfig){
  114. $container[$ipConfig->getId()] = $ipConfig->asConfig();
  115. }
  116. return $container;
  117. }
  118. private function getRate(){
  119. if ($this->isWhmcsConfigOption(ConfigurableOption::NETWORK_RATE) && $this->getWhmcsConfigOption(ConfigurableOption::NETWORK_RATE) != "-1")
  120. {
  121. return $this->getWhmcsConfigOption(ConfigurableOption::NETWORK_RATE);
  122. }
  123. return $this->configuration()->getRate();
  124. }
  125. private function getBridge(VmIpAddress $ipAddress){
  126. //public
  127. if(filter_var($ipAddress->ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE)){
  128. return $this->configuration()->getBridge();
  129. }
  130. //private
  131. return $this->configuration()->getPrivateBridge() ? $this->configuration()->getPrivateBridge() : $this->configuration()->getBridge();
  132. }
  133. private function freeNetworDeviceId(){
  134. if($this->vm){
  135. return $this->vm->findFreeNetworDeviceId();
  136. }
  137. return 0;
  138. }
  139. }