NetworkService.php 31 KB

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