NetworkService.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814
  1. <?php
  2. namespace ModulesGarden\ProxmoxAddon\App\Services\Cloud;
  3. use Illuminate\Database\Capsule\Manager as DB;
  4. use MGProvision\Proxmox\v2\Api;
  5. use MGProvision\Proxmox\v2\models\Config;
  6. use MGProvision\Proxmox\v2\models\IpConfig;
  7. use MGProvision\Proxmox\v2\models\Kvm;
  8. use MGProvision\Proxmox\v2\models\NetworkDeviceKvm;
  9. use MGProvision\Proxmox\v2\models\NetworkDeviceLxc;
  10. use ModulesGarden\ProxmoxAddon\App\Models\IpAddress;
  11. use ModulesGarden\ProxmoxAddon\App\Models\VirtualInterface;
  12. use ModulesGarden\ProxmoxAddon\App\Models\VirtualNetwork;
  13. use ModulesGarden\ProxmoxAddon\App\Models\VmIpAddress;
  14. use ModulesGarden\ProxmoxAddon\App\Services\ApiService;
  15. use ModulesGarden\ProxmoxAddon\App\Services\Utility;
  16. use ModulesGarden\ProxmoxAddon\Core\UI\Traits\WhmcsParams;
  17. use ModulesGarden\ProxmoxAddon\App\Enum\Cloud\ConfigurableOption;
  18. use ModulesGarden\ProxmoxAddon\App\Enum\Cloud\CustomField;
  19. use function ModulesGarden\ProxmoxAddon\Core\Helper\sl;
  20. class NetworkService
  21. {
  22. use WhmcsParams;
  23. use HostingService;
  24. use ApiService;
  25. use ProductService;
  26. /**
  27. * @param VmIpAddress[] $entries
  28. */
  29. public function create(array $entries)
  30. {
  31. //to do acl
  32. $this->setHostingId($this->getWhmcsParamByKey("serviceid"));
  33. DB::beginTransaction();
  34. try
  35. {
  36. foreach ($entries as &$ip)
  37. {
  38. //insert to data base
  39. $ip->save();
  40. $this->hosting()->ipAdd($ip->ip);
  41. //lock ip in proxmox addon
  42. if (!Utility::isIpManagerProxmoxVPSIntegration())
  43. {
  44. IpAddress::where('ip', $ip->ip)
  45. ->update(["hosting_id" => $ip->hosting_id]);
  46. }
  47. }
  48. //update hosting
  49. $this->hosting()->save();
  50. DB::commit();
  51. }
  52. catch (\Exception $ex)
  53. {
  54. DB::rollBack();
  55. throw $ex;
  56. }
  57. }
  58. /**
  59. * @deprecated
  60. */
  61. public function addPrivate( $entries)
  62. {
  63. $this->setHostingId($this->getWhmcsParamByKey("serviceid"));
  64. $vm = \ModulesGarden\ProxmoxAddon\Core\Helper\sl('Vm')->getVm();
  65. DB::beginTransaction();
  66. Api::beginTransaction();
  67. $container = [];
  68. $rate = null;
  69. if ($this->isWhmcsConfigOption(ConfigurableOption::NETWORK_RATE) && $this->getWhmcsConfigOption(ConfigurableOption::NETWORK_RATE) != "-1")
  70. {
  71. $rate = $this->getWhmcsConfigOption(ConfigurableOption::NETWORK_RATE);
  72. }
  73. else
  74. {
  75. if ($this->configuration()->getRate())
  76. {
  77. $rate = $this->configuration()->getRate();
  78. }
  79. }
  80. //cloud init
  81. $isCloudInit = $this->configuration()->isQemu() && $this->configuration()->isCloudInit();
  82. try
  83. {
  84. foreach ($entries as &$ip)
  85. {
  86. //public network
  87. if($ip->vn_id == 0){
  88. continue;
  89. }
  90. $tag = $ip->virtualNetwork->tag;
  91. //insert to data base
  92. $ip->save();
  93. $this->hosting()->ipAdd($ip->ip);
  94. $networkId = $vm->findFreeNetworDeviceId();
  95. if ($this->configuration()->isLxc())
  96. {
  97. $bridge = $this->configuration()->getPrivateBridge() ? $this->configuration()->getPrivateBridge() : $this->configuration()->getBridge();
  98. $networkDevice = new NetworkDeviceLxc('net' . $networkId);
  99. $networkDevice->setName('eth' . $networkId)
  100. ->setBridge($bridge)
  101. ->setFirewall($this->configuration()->isNetworkFirewall() ? 1 : 0);
  102. $networkDevice->setIp($ip->ip)
  103. ->setCidr($ip->virtualNetwork->cidr)
  104. ->setGw($ip->virtualNetwork->gateway)
  105. ->setTag($tag)
  106. ->setRate($rate);
  107. $container[$networkDevice->getId()] = $networkDevice->asConfig();
  108. }
  109. else
  110. {
  111. if ($this->configuration()->isQemu())
  112. {
  113. $networkDevice = new NetworkDeviceKvm('net' . $networkId);
  114. $networkDevice->setBridge($this->configuration()->getPrivateBridge())
  115. ->setModel($this->configuration()->getNetworkPrivateModel())
  116. ->setRate($rate)
  117. ->setTag($tag);
  118. $container[$networkDevice->getId()] = $networkDevice->asConfig();
  119. if ($isCloudInit)
  120. {
  121. $ipConfig = new IpConfig('ipconfig' . $networkId);
  122. $ipConfig->setIp(trim($ip->ip))
  123. ->setCidr(trim($ip->virtualNetwork->cidr))
  124. ->setGw(trim($ip->virtualNetwork->gateway));
  125. $container[$ipConfig->getId()] = $ipConfig->asConfig();
  126. }
  127. }
  128. }
  129. $ip->net = $networkDevice->getId();
  130. $ip->save();
  131. }
  132. //update hosting
  133. $this->hosting()->save();
  134. $vm->updateConfig($container);
  135. DB::commit();
  136. }
  137. catch (\Exception $ex)
  138. {
  139. DB::rollBack();
  140. Api::commit();
  141. throw $ex;
  142. }
  143. }
  144. protected function makeTag()
  145. {
  146. if (is_numeric($this->getWhmcsCustomField(CustomField::TAG)))
  147. {
  148. return $this->getWhmcsCustomField(CustomField::TAG);
  149. }
  150. if (!$this->configuration()->getTagFrom() && $this->configuration()->getTagTo())
  151. {
  152. return null;
  153. }
  154. $tag = $this->nextTag();
  155. if (!$tag)
  156. {
  157. throw new \Exception("Max VLAN tag have been reached, Please Configure product (Max VLAN tag)");
  158. }
  159. $this->customFieldUpdate("VLAN Tag", $tag);
  160. return $tag;
  161. }
  162. public function nextTag()
  163. {
  164. if(!$this->configuration()->getTagFrom() || !$this->configuration()->getTagTo() && $this->configuration()->isPermissionVirtualNetwork()){
  165. throw new \Exception("Product configuration validator error: VLAN TAG Range Number is empty.");
  166. }
  167. $h = 'tblhosting';
  168. $vn = (new VirtualNetwork)->getTable();
  169. for ($i = $this->configuration()->getTagFrom(); $i <= $this->configuration()->getTagTo(); $i++)
  170. {
  171. $query = VirtualNetwork::select("{$vn}.tag")
  172. ->leftJoin($h, "{$h}.id", "=", "{$vn}.hosting_id")
  173. ->where("{$h}.packageid", $this->getWhmcsParamByKey("packageid"))
  174. ->where("{$vn}.tag", $i);
  175. if ($query->count())
  176. {
  177. continue;
  178. }
  179. return $i;
  180. }
  181. return false;
  182. }
  183. /**
  184. * @return VmIpAddress
  185. */
  186. public function getPrivateIpAddress()
  187. {
  188. $ip = IpAddress::where('private', 1)
  189. ->where('type', 'IPv4')
  190. ->whereIn("sid", [$this->getWhmcsParamByKey('serverid'), "0"])
  191. ->where('hosting_id', '0')
  192. ->firstOrFail();
  193. $newIp = new VmIpAddress();
  194. $newIp->fill($ip->toArray());
  195. $newIp->hosting_id = $this->getWhmcsParamByKey("serviceid");
  196. $newIp->server_id = $this->getWhmcsParamByKey("serverid");
  197. return $newIp;
  198. }
  199. public function hasPrivateIpAddress()
  200. {
  201. $query = IpAddress::where('private', 1)
  202. ->where('type', 'IPv4')
  203. ->whereIn("sid", [$this->getWhmcsParamByKey('serverid'), "0"])
  204. ->where('hosting_id', '0');
  205. return $query->count();
  206. }
  207. public function deleteByNetworkId(array $networkIds)
  208. {
  209. if (is_null($networkIds))
  210. {
  211. throw new \InvalidArgumentException('The network ids cannot be empty');
  212. }
  213. $ipAddresses = VmIpAddress::whereIn("net", $networkIds)
  214. ->ofHostingId($this->getWhmcsParamByKey('serviceid'))
  215. ->ofVmId(\ModulesGarden\ProxmoxAddon\Core\Helper\sl('Vm')->getVmModel()->id)
  216. ->get()->all();
  217. return $this->deleteByIpAddresses($ipAddresses);
  218. }
  219. public function deleteByIpAddresses( $ipAddresses)
  220. {
  221. if (is_null($ipAddresses))
  222. {
  223. throw new \InvalidArgumentException('The IP Addresses cannot be empty');
  224. }
  225. $vm = \ModulesGarden\ProxmoxAddon\Core\Helper\sl('Vm')->getVm();
  226. $devices = $vm->getNetworkDevices();
  227. $configDelete = [];
  228. $this->setHostingId($this->getWhmcsParamByKey("serviceid"));
  229. DB::beginTransaction();
  230. Api::beginTransaction();
  231. try
  232. {
  233. foreach ($ipAddresses as &$ip)
  234. {
  235. if (is_numeric($ip))
  236. {
  237. $ip = VmIpAddress::where("id", $ip)->firstOrFail();
  238. }
  239. //vi
  240. $ip->delete();
  241. //Public IP
  242. if($ip instanceof VirtualInterface && $ip->vn_id == 0){
  243. VmIpAddress::ofHostingId($this->getWhmcsParamByKey("serviceid"))
  244. ->ofVmId($ip->vm_id)
  245. ->ofIp($ip->ip)
  246. ->update([ 'vm_id'=> null,'net'=> null]);
  247. }
  248. /**
  249. * @deprecated
  250. if ($ip instanceof VmIpAddress)
  251. {
  252. //Unlock ip in Proxmox Addon
  253. IpAddress::where('ip', $ip->ip)
  254. ->where('hosting_id', $ip->hosting_id)
  255. ->update(["hosting_id" => "0", 'last_check' => Utility::timeStamp()]);
  256. }
  257. */
  258. foreach ($devices as $deviceIndex => $device)
  259. {
  260. if ($this->configuration()->isOneNetworkDevice() && $device->getId() == "net0" && $ip instanceof VirtualInterface)
  261. {
  262. continue;
  263. }
  264. if($device instanceof NetworkDeviceLxc)
  265. {
  266. if ($ip->ip == $device->getIp())
  267. {
  268. $device->setIp(null);
  269. }
  270. if ($ip->ip == $device->getIp6())
  271. {
  272. $device->setIp6(null);
  273. }
  274. if(empty($device->getIp6()) && empty($device->getIp()))
  275. {
  276. unset($devices[$deviceIndex]);
  277. $configDelete[] = $device->getId();
  278. }
  279. }
  280. elseif($device instanceof NetworkDeviceKvm && $ip->net && $ip->net == $device->getId())
  281. {
  282. $configDelete[] = $device->getId();
  283. unset($devices[$deviceIndex]);
  284. }
  285. }
  286. //cloud init
  287. if ($vm instanceof Kvm)
  288. {
  289. foreach ($vm->getIpConfig()->fetch() as $ipConfig)
  290. {
  291. if ($ipConfig->getIp() == $ip->ip || $ipConfig->getIp6() == $ip->ip)
  292. {
  293. $configDelete[] = $ipConfig->getId();
  294. }
  295. }
  296. }
  297. }
  298. $this->hosting()->save();
  299. /**
  300. * Delete config. We need that when there is no more network interface
  301. */
  302. if (!empty($configDelete))
  303. {
  304. $vm->deleteConfig(implode(",", $configDelete));
  305. }
  306. /**
  307. * Update current config. We need that only if we remove only one IP and other IP is still on the same interface
  308. $config = new Config();
  309. $config->setNet($devices);
  310. $config = $config->toArray();
  311. if($config)
  312. {
  313. $vm->updateConfig($config);
  314. }
  315. *
  316. */
  317. DB::commit();
  318. }
  319. catch (\Exception $ex)
  320. {
  321. DB::rollBack();
  322. Api::commit();
  323. throw $ex;
  324. }
  325. }
  326. public function hasIp($totalIpv4, $totalIpv6, $nodeName)
  327. {
  328. $virtualization = $this->configuration()->isQemu() ? "KVM" : "LXC";
  329. if ((int)$totalIpv4 > 0)
  330. {
  331. /**
  332. * @var $ipCollections IpAddress
  333. */
  334. $ipCollections = IpAddress::where('hosting_id', '0')
  335. ->where('private', '0')
  336. ->where('type', 'IPv4')
  337. ->whereIn('sid', [$this->getWhmcsParamByKey('serverid'), '0'])
  338. ->whereIn('visualization', [$virtualization, 'Auto'])
  339. ->whereIn('node', [$nodeName, '0', ""]);
  340. if ($this->configuration()->getTags())
  341. {
  342. $ipCollections->ofTags($this->configuration()->getTags());
  343. }
  344. $count = $ipCollections->count();
  345. if ((int)$count < $totalIpv4)
  346. {
  347. throw new \Exception(sprintf("Unable to get %s of IPv4. IP Addresses available: %s", $totalIpv4, $count));
  348. }
  349. }
  350. if ((int)$totalIpv6 > 0)
  351. {
  352. $ipCollections = IpAddress::where('hosting_id', '0')
  353. ->where('private', '0')
  354. ->where('type', 'IPv6')
  355. ->whereIn('sid', [$this->getWhmcsParamByKey('serverid'), '0'])
  356. ->whereIn('visualization', [$virtualization, 'Auto'])
  357. ->whereIn('node', [$nodeName, '0', ""]);
  358. if ($this->configuration()->getTags())
  359. {
  360. $ipCollections->ofTags($this->configuration()->getTags());
  361. }
  362. $count = $ipCollections->count();
  363. if ((int)$count < $totalIpv6)
  364. {
  365. throw new \Exception(sprintf("Unable to get %s of IPv6. IP Addresses available: %s", $totalIpv6, $count));
  366. }
  367. }
  368. return $this;
  369. }
  370. public function addIp($totalIpv4, $totalIpv6, $nodeName)
  371. {
  372. try {
  373. DB::beginTransaction();
  374. $virtualization = $this->configuration()->isQemu() ? "KVM" : "LXC";
  375. $this->setHostingId($this->getWhmcsParamByKey('serviceid'));
  376. if ((int)$totalIpv4 > 0) {
  377. $query = IpAddress::where('hosting_id', '0')
  378. ->where('private', '0')
  379. ->where('type', 'IPv4')
  380. ->whereIn('sid', [$this->getWhmcsParamByKey('serverid'), '0'])
  381. ->whereIn('visualization', [$virtualization, 'Auto'])
  382. ->whereIn('node', [$nodeName, '0', ""]);
  383. if ($this->configuration()->getTags()) {
  384. $query->ofTags($this->configuration()->getTags());
  385. }
  386. $count = $query->limit($totalIpv4)
  387. ->orderBy('last_check', 'asc')
  388. ->update(["hosting_id" => $this->getWhmcsParamByKey('serviceid'), 'last_check' => Utility::timeStamp()]);
  389. if ((int)$count < $totalIpv4) {
  390. throw new \Exception(sprintf("Unable to get %s of IPv4. IP Addresses available: %s", $totalIpv4, $count));
  391. }
  392. }
  393. if ((int)$totalIpv6 > 0) {
  394. $query = IpAddress::where('hosting_id', '0')
  395. ->where('private', '0')
  396. ->where('type', 'IPv6')
  397. ->whereIn('sid', [$this->getWhmcsParamByKey('serverid'), '0'])
  398. ->whereIn('visualization', [$virtualization, 'Auto'])
  399. ->whereIn('node', [$nodeName, '0', ""]);
  400. if ($this->configuration()->getTags()) {
  401. $query->ofTags($this->configuration()->getTags());
  402. }
  403. $count = $query->limit($totalIpv6)
  404. ->orderBy('last_check', 'asc')
  405. ->update(["hosting_id" => $this->getWhmcsParamByKey('serviceid'), 'last_check' => Utility::timeStamp()]);
  406. if ((int)$count < $totalIpv6) {
  407. throw new \Exception(sprintf("Unable to get %s of IPv6. IP Addresses available: %s", $totalIpv6, $count));
  408. }
  409. }
  410. $vmIp = (new VmIpAddress())->getTable();
  411. $collection = IpAddress::where('hosting_id', $this->getWhmcsParamByKey('serviceid'))
  412. ->orderBy("ip", "desc")
  413. ->whereNotIn('ip', function ($query) use ($vmIp) {
  414. $query->select('ip')->from($vmIp);
  415. });
  416. foreach ($collection->get()->sortBy("type")->all() as $ip) {
  417. /*@var $ip IpAddress */
  418. $newIp = new VmIpAddress();
  419. $ipData = $ip->toArray();
  420. unset($ipData['id']);
  421. $ipData['hosting_id'] = $this->getWhmcsParamByKey('serviceid');
  422. $ipData['server_id'] = $this->getWhmcsParamByKey('serverid');
  423. $newIp->vm_id = null;
  424. if(sl('Vm')->hasVm()){
  425. $newIp->vm_id = sl('Vm')->getVmModel()->id;
  426. }
  427. $newIp->fill($ipData)->save();
  428. $this->hosting()->ipAdd($ip->ip);
  429. }
  430. $this->hosting()->save();
  431. DB::commit();
  432. } catch (\Exception $ex) {
  433. DB::rollBack();
  434. throw $ex;
  435. }
  436. }
  437. public function deleteIpAddresses()
  438. {
  439. $query = VmIpAddress::ofHostingId($this->getWhmcsParamByKey('serviceid'))
  440. ->ofVmId(sl('Vm')->getVmModel()->id)
  441. ->update(['vm_id'=>null,'net' => null]);
  442. /**
  443. * @deprecated
  444. foreach ($query->get() as $vmIp){
  445. //Unlock ip in Proxmox Addon
  446. IpAddress::where('hosting_id', $this->getWhmcsParamByKey('serviceid'))
  447. ->ofIp($vmIp->ip)
  448. ->update(["hosting_id" => "0", 'last_check' => Utility::timeStamp()]);
  449. $this->hosting()->ipDelete($vmIp->ip);
  450. $vmIp->delete();
  451. }
  452. */
  453. return $this;
  454. }
  455. /**
  456. * @deprecated
  457. */
  458. public function buildLxc()
  459. {
  460. $vmId = sl('Vm')->getVmModel()->id;
  461. $container = [];
  462. /**
  463. * @var VmIpAddress[] $ipv4
  464. */
  465. $ipv4 = VmIpAddress::ofHostingId($this->getWhmcsParamByKey("serviceid"))
  466. ->ofIp4()
  467. ->ofNetNull()
  468. ->ofVmId($vmId)
  469. ->get();
  470. //ip6
  471. $ipv6 = VmIpAddress::ofHostingId($this->getWhmcsParamByKey("serviceid"))
  472. ->ofIp6()
  473. ->ofNetNull()
  474. ->ofVmId($vmId)
  475. ->get();
  476. //Network
  477. $bridge = $this->configuration()->getBridge();
  478. $firewall = $this->configuration()->isNetworkFirewall() ? 1 : 0;
  479. $ip4Mode = $this->configuration()->getIpv4NetworkMode();
  480. $ip6Mode = $this->configuration()->getIpv6NetworkMode();
  481. $rate = null;
  482. if ($this->getWhmcsConfigOption(ConfigurableOption::NETWORK_RATE, $this->configuration()->getRate()) && $this->getWhmcsConfigOption(ConfigurableOption::NETWORK_RATE, $this->configuration()->getRate()) != "-1")
  483. {
  484. $rate = $this->getWhmcsConfigOption(ConfigurableOption::NETWORK_RATE, $this->configuration()->getRate());
  485. }
  486. $networkId = 0;
  487. if (sl('Vm')->hasVm())
  488. {
  489. $networkId = sl('Vm')->getVm()->findFreeNetworDeviceId();
  490. }
  491. for ($i = 0; $i <= 9; $i++)
  492. {
  493. //Empty IP Addresses
  494. if (!$ipv4->get($i) && !$ipv6->get($i))
  495. {
  496. break;
  497. }
  498. if ($networkId > 9)
  499. {
  500. break;
  501. }
  502. $mac = null;
  503. //Name
  504. $interface = new NetworkDeviceLxc('net' . $networkId);
  505. $interface->setName('eth' . $networkId)
  506. ->setBridge($bridge)
  507. ->setFirewall($firewall);
  508. //IP 4
  509. if ($ipv4->get($i))
  510. {
  511. if ($ipv4->get($i)->mac_address)
  512. {
  513. $mac = $ipv4->get($i)->mac_address;
  514. }
  515. if ($ipv4->get($i)->tag)
  516. {
  517. $interface->setTag($ipv4->get($i)->tag);
  518. }
  519. $interface->setTrunks($ipv4->get($i)->trunks);
  520. if ($ip4Mode == 'static')
  521. { //IP & CIRD
  522. $interface->setIp($ipv4->get($i)->ip);
  523. $interface->setCidr($ipv4->get($i)->cidr);
  524. //Gateway
  525. $interface->setGw($ipv4->get($i)->gateway);
  526. }
  527. $ip = trim($ipv4->get($i)->ip);
  528. $ipv4->get($i)->net = $interface->getId();
  529. $ipv4->get($i)->save();
  530. }
  531. if ($ip4Mode == "dhcp")
  532. {
  533. $interface->setIp("dhcp");
  534. }
  535. //IP 6
  536. if ($ip6Mode == "dhcp")
  537. {
  538. $interface->setIp6("dhcp");
  539. }
  540. else
  541. {
  542. if ($ip6Mode == "slaac")
  543. {
  544. $interface->setIp6("auto");
  545. }
  546. }
  547. if ($ipv6->get($i))
  548. { //IP & CIRD
  549. $ip = trim($ipv6->get($i)->ip);
  550. if ($ip6Mode == "static")
  551. {
  552. $interface->setIp6($ipv6->get($i)->ip);
  553. $interface->setCidr6($ipv6->get($i)->cidr);
  554. //Gateway
  555. $interface->setGw6($ipv6->get($i)->gateway);
  556. }
  557. if ($ipv6->get($i)->tag)
  558. {
  559. $interface->setTag($ipv6->get($i)->tag);
  560. }
  561. if ($ipv6->get($i)->trunks)
  562. {
  563. $interface->setTrunks($ipv6->get($i)->trunks);
  564. }
  565. if ($mac == null && $ipv6->get($i)->mac_address)
  566. {
  567. $mac = $ipv6->get($i)->mac_address;
  568. }
  569. $ipv6->get($i)->net = $interface->getId();
  570. $ipv6->get($i)->save();
  571. }
  572. if ($rate)
  573. {
  574. $interface->setRate($rate);
  575. }
  576. $interface->setHwaddr($mac);
  577. $container['net' . $networkId] = $interface->asConfig();
  578. $networkId++;
  579. VirtualInterface::ofHostingId($this->getWhmcsParamByKey("serviceid"))
  580. ->ofVmId($vmId )
  581. ->ofIp($ip)
  582. ->update(['net' =>$interface->getId()]);
  583. }
  584. return $container;
  585. }
  586. /**
  587. * @deprecated
  588. */
  589. public function buildQemu()
  590. {
  591. $container = [];
  592. $vmId = sl('Vm')->getVmModel()->id;
  593. /**
  594. * @var VmIpAddress[] $ipv4
  595. */
  596. $ipv4 = VmIpAddress::ofHostingId($this->getWhmcsParamByKey("serviceid"))
  597. ->ofIp4()
  598. ->ofNetNull()
  599. ->ofVmId($vmId)
  600. ->get();
  601. //ip6
  602. $ipv6 = VmIpAddress::ofHostingId($this->getWhmcsParamByKey("serviceid"))
  603. ->ofIp6()
  604. ->ofNetNull()
  605. ->ofVmId($vmId)
  606. ->get();
  607. //Network
  608. $model = $this->configuration()->getNetworkModel();
  609. $bridge = $this->configuration()->getBridge();
  610. $firewall = $this->configuration()->isNetworkFirewall() ? 1 : 0;
  611. $rate = null;
  612. if ($this->getWhmcsConfigOption(ConfigurableOption::NETWORK_RATE, $this->configuration()->getRate()) && $this->getWhmcsConfigOption(ConfigurableOption::NETWORK_RATE, $this->configuration()->getRate()) != "-1")
  613. {
  614. $rate = $this->getWhmcsConfigOption(ConfigurableOption::NETWORK_RATE, $this->configuration()->getRate());
  615. }
  616. $networkId = 0;
  617. $vmNetworkDevices = [];
  618. if (sl('Vm')->hasVm())
  619. {
  620. $networkId = sl('Vm')->getVm()->findFreeNetworDeviceId();
  621. $vmNetworkDevices = sl('Vm')->getVm()->getNetworkDevices();
  622. }
  623. $i4 = 0;
  624. $i6 = 0;
  625. $isApi52 = version_compare($this->api()->getVersion(), "5.2", '>=');
  626. for ($i = 0; $i <= 31; $i++)
  627. {
  628. //Empty IP Addresses
  629. if (!$ipv4->get($i4) && !$ipv6->get($i6))
  630. {
  631. break;
  632. }
  633. if ($networkId > 31)
  634. {
  635. break;
  636. }
  637. if ($this->configuration()->isOneNetworkDevice() && ($i > 0 || count($vmNetworkDevices)))
  638. {
  639. VmIpAddress::ofHostingId($this->getWhmcsParamByKey("serviceid"))
  640. ->ofNetNull()
  641. ->ofVmId($vmId )
  642. ->update(["net" => 'net0']);
  643. break;
  644. }
  645. $mac = null;
  646. $isIp4 = false;
  647. //Name
  648. $interface = new NetworkDeviceKvm('net' . $networkId);
  649. $interface->setModel($model)
  650. ->setBridge($bridge)
  651. ->setFirewall($firewall);
  652. $ipConfig = new IpConfig('ipconfig' . $networkId);
  653. //IP 4
  654. if ($ipv4->get($i4))
  655. {
  656. $isIp4 = true;
  657. $ip = trim($ipv4->get($i4)->ip);
  658. if (trim($ipv4->get($i4)->mac_address))
  659. {
  660. $mac = $ipv4->get($i4)->mac_address;
  661. }
  662. if ($ipv4->get($i4)->tag)
  663. {
  664. $interface->setTag(trim($ipv4->get($i4)->tag));
  665. }
  666. $interface->setTrunks(trim($ipv4->get($i4)->trunks));
  667. $ipConfig->setIp(trim($ipv4->get($i4)->ip));
  668. $ipConfig->setCidr(trim($ipv4->get($i4)->cidr));
  669. //Gateway
  670. $ipConfig->setGw(trim($ipv4->get($i4)->gateway));
  671. $ipv4->get($i4)->net = $interface->getId();
  672. $ipv4->get($i4)->save();
  673. $i4++;
  674. }
  675. //ipv6
  676. if ($ipv6->get($i6) && (!$isIp4 || $interface->getTag() == $ipv6->get($i6)->tag))
  677. { //IP & CIRD
  678. $ip = ttrim($ipv6->get($i6)->ip);
  679. $ipConfig->setIp6(trim($ipv6->get($i6)->ip));
  680. $ipConfig->setCidr6(trim($ipv6->get($i6)->cidr));
  681. //Gateway
  682. $ipConfig->setGw6(trim($ipv6->get($i6)->gateway));
  683. if ($ipv6->get($i6)->tag)
  684. {
  685. $tag = $ipv6->get($i6)->tag;
  686. $interface->setTag(trim($ipv6->get($i6)->tag));
  687. }
  688. if ($ipv6->get($i6)->trunks)
  689. {
  690. $interface->setTrunks(trim($ipv6->get($i6)->trunks));
  691. }
  692. if ($mac == null && $ipv6->get($i6)->mac_address)
  693. {
  694. $mac = $ipv6->get($i6)->mac_address;
  695. }
  696. $ipv6->get($i6)->net = $interface->getId();
  697. $ipv6->get($i6)->save();
  698. $i6++;
  699. }
  700. $interface->setRate(trim($rate));
  701. $interface->setMacAddress(trim($mac));
  702. $interface->setQueues(trim($this->configuration()->getQueues()));
  703. $container[$interface->getId()] = $interface->asConfig();
  704. if ($isApi52 && ($ipConfig->getIp() || $ipConfig->getIp6()))
  705. {
  706. $container[$ipConfig->getId()] = $ipConfig->asConfig();
  707. }
  708. VirtualInterface::ofHostingId($this->getWhmcsParamByKey("serviceid"))
  709. ->ofVmId($vmId )
  710. ->ofIp($ip)
  711. ->update(['net' =>$interface->getId()]);
  712. $networkId++;
  713. }
  714. return $container;
  715. }
  716. /**
  717. * @throws \Exception
  718. */
  719. public function unassignIpAddressesAndDeleteNetwork($requestIPv4, $requestIPv6)
  720. {
  721. $vmId = sl('Vm')->getVmModel()->id;
  722. $ip4 = [];
  723. $ip6 = [];
  724. if ($requestIPv4 < 0)
  725. {
  726. $ip4 = VmIpAddress::ofHostingId($this->getWhmcsParamByKey('serviceid'))
  727. ->ofIp4()
  728. ->ofNetNotNull()
  729. ->ofVmId($vmId)
  730. ->limit(abs($requestIPv4))
  731. ->orderByIdDesc()
  732. ->get()
  733. ->all();
  734. }
  735. if ($requestIPv6 < 0)
  736. {
  737. $ip6 = VmIpAddress::ofHostingId($this->getWhmcsParamByKey('serviceid'))
  738. ->ofIp6()
  739. ->ofNetNotNull()
  740. ->ofVmId($vmId)
  741. ->limit(abs($requestIPv6))
  742. ->orderByIdDesc()
  743. ->get()
  744. ->all();
  745. }
  746. $ipAddresses = array_merge($ip4, $ip6);
  747. if (!empty($ipAddresses))
  748. {
  749. $this->deleteByIpAddresses($ipAddresses);
  750. }
  751. }
  752. public function getIpAddressesRequest()
  753. {
  754. $ipv4Used = (int)VmIpAddress::ofHostingId($this->getWhmcsParamByKey('serviceid'))
  755. ->ofIp4()
  756. ->count();
  757. $ipv6Used = (int)VmIpAddress::ofHostingId($this->getWhmcsParamByKey('serviceid'))
  758. ->ofIp6()
  759. ->count();
  760. $requestIPv4 = (int)$this->configuration()->getIpv4();
  761. $requestIPv6 = (int)$this->configuration()->getIpv6();
  762. if ($this->isWhmcsConfigOption(ConfigurableOption::IPV4))
  763. {
  764. $requestIPv4 = (int)$this->getWhmcsConfigOption(ConfigurableOption::IPV4);
  765. }
  766. if ($this->isWhmcsConfigOption(ConfigurableOption::IPV6))
  767. {
  768. $requestIPv6 = (int)$this->getWhmcsConfigOption(ConfigurableOption::IPV6);
  769. }
  770. return [(int)$requestIPv4 -= $ipv4Used, (int)$requestIPv6 -= $ipv6Used];
  771. }
  772. }