| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814 |
- <?php
- namespace ModulesGarden\ProxmoxAddon\App\Services\Cloud;
- use Illuminate\Database\Capsule\Manager as DB;
- use MGProvision\Proxmox\v2\Api;
- use MGProvision\Proxmox\v2\models\Config;
- use MGProvision\Proxmox\v2\models\IpConfig;
- use MGProvision\Proxmox\v2\models\Kvm;
- use MGProvision\Proxmox\v2\models\NetworkDeviceKvm;
- use MGProvision\Proxmox\v2\models\NetworkDeviceLxc;
- use ModulesGarden\ProxmoxAddon\App\Models\IpAddress;
- use ModulesGarden\ProxmoxAddon\App\Models\VirtualInterface;
- use ModulesGarden\ProxmoxAddon\App\Models\VirtualNetwork;
- use ModulesGarden\ProxmoxAddon\App\Models\VmIpAddress;
- use ModulesGarden\ProxmoxAddon\App\Services\ApiService;
- use ModulesGarden\ProxmoxAddon\App\Services\Utility;
- use ModulesGarden\ProxmoxAddon\Core\UI\Traits\WhmcsParams;
- use ModulesGarden\ProxmoxAddon\App\Enum\Cloud\ConfigurableOption;
- use ModulesGarden\ProxmoxAddon\App\Enum\Cloud\CustomField;
- use function ModulesGarden\ProxmoxAddon\Core\Helper\sl;
- class NetworkService
- {
- use WhmcsParams;
- use HostingService;
- use ApiService;
- use ProductService;
- /**
- * @param VmIpAddress[] $entries
- */
- public function create(array $entries)
- {
- //to do acl
- $this->setHostingId($this->getWhmcsParamByKey("serviceid"));
- DB::beginTransaction();
- try
- {
- foreach ($entries as &$ip)
- {
- //insert to data base
- $ip->save();
- $this->hosting()->ipAdd($ip->ip);
- //lock ip in proxmox addon
- if (!Utility::isIpManagerProxmoxVPSIntegration())
- {
- IpAddress::where('ip', $ip->ip)
- ->update(["hosting_id" => $ip->hosting_id]);
- }
- }
- //update hosting
- $this->hosting()->save();
- DB::commit();
- }
- catch (\Exception $ex)
- {
- DB::rollBack();
- throw $ex;
- }
- }
- /**
- * @deprecated
- */
- public function addPrivate( $entries)
- {
- $this->setHostingId($this->getWhmcsParamByKey("serviceid"));
- $vm = \ModulesGarden\ProxmoxAddon\Core\Helper\sl('Vm')->getVm();
- DB::beginTransaction();
- Api::beginTransaction();
- $container = [];
- $rate = null;
- if ($this->isWhmcsConfigOption(ConfigurableOption::NETWORK_RATE) && $this->getWhmcsConfigOption(ConfigurableOption::NETWORK_RATE) != "-1")
- {
- $rate = $this->getWhmcsConfigOption(ConfigurableOption::NETWORK_RATE);
- }
- else
- {
- if ($this->configuration()->getRate())
- {
- $rate = $this->configuration()->getRate();
- }
- }
- //cloud init
- $isCloudInit = $this->configuration()->isQemu() && $this->configuration()->isCloudInit();
- try
- {
- foreach ($entries as &$ip)
- {
- //public network
- if($ip->vn_id == 0){
- continue;
- }
- $tag = $ip->virtualNetwork->tag;
- //insert to data base
- $ip->save();
- $this->hosting()->ipAdd($ip->ip);
- $networkId = $vm->findFreeNetworDeviceId();
- if ($this->configuration()->isLxc())
- {
- $bridge = $this->configuration()->getPrivateBridge() ? $this->configuration()->getPrivateBridge() : $this->configuration()->getBridge();
- $networkDevice = new NetworkDeviceLxc('net' . $networkId);
- $networkDevice->setName('eth' . $networkId)
- ->setBridge($bridge)
- ->setFirewall($this->configuration()->isNetworkFirewall() ? 1 : 0);
- $networkDevice->setIp($ip->ip)
- ->setCidr($ip->virtualNetwork->cidr)
- ->setGw($ip->virtualNetwork->gateway)
- ->setTag($tag)
- ->setRate($rate);
- $container[$networkDevice->getId()] = $networkDevice->asConfig();
- }
- else
- {
- if ($this->configuration()->isQemu())
- {
- $networkDevice = new NetworkDeviceKvm('net' . $networkId);
- $networkDevice->setBridge($this->configuration()->getPrivateBridge())
- ->setModel($this->configuration()->getNetworkPrivateModel())
- ->setRate($rate)
- ->setTag($tag);
- $container[$networkDevice->getId()] = $networkDevice->asConfig();
- if ($isCloudInit)
- {
- $ipConfig = new IpConfig('ipconfig' . $networkId);
- $ipConfig->setIp(trim($ip->ip))
- ->setCidr(trim($ip->virtualNetwork->cidr))
- ->setGw(trim($ip->virtualNetwork->gateway));
- $container[$ipConfig->getId()] = $ipConfig->asConfig();
- }
- }
- }
- $ip->net = $networkDevice->getId();
- $ip->save();
- }
- //update hosting
- $this->hosting()->save();
- $vm->updateConfig($container);
- DB::commit();
- }
- catch (\Exception $ex)
- {
- DB::rollBack();
- Api::commit();
- throw $ex;
- }
- }
- protected function makeTag()
- {
- if (is_numeric($this->getWhmcsCustomField(CustomField::TAG)))
- {
- return $this->getWhmcsCustomField(CustomField::TAG);
- }
- if (!$this->configuration()->getTagFrom() && $this->configuration()->getTagTo())
- {
- return null;
- }
- $tag = $this->nextTag();
- if (!$tag)
- {
- throw new \Exception("Max VLAN tag have been reached, Please Configure product (Max VLAN tag)");
- }
- $this->customFieldUpdate("VLAN Tag", $tag);
- return $tag;
- }
- public function nextTag()
- {
- if(!$this->configuration()->getTagFrom() || !$this->configuration()->getTagTo() && $this->configuration()->isPermissionVirtualNetwork()){
- throw new \Exception("Product configuration validator error: VLAN TAG Range Number is empty.");
- }
- $h = 'tblhosting';
- $vn = (new VirtualNetwork)->getTable();
- for ($i = $this->configuration()->getTagFrom(); $i <= $this->configuration()->getTagTo(); $i++)
- {
- $query = VirtualNetwork::select("{$vn}.tag")
- ->leftJoin($h, "{$h}.id", "=", "{$vn}.hosting_id")
- ->where("{$h}.packageid", $this->getWhmcsParamByKey("packageid"))
- ->where("{$vn}.tag", $i);
- if ($query->count())
- {
- continue;
- }
- return $i;
- }
- return false;
- }
- /**
- * @return VmIpAddress
- */
- public function getPrivateIpAddress()
- {
- $ip = IpAddress::where('private', 1)
- ->where('type', 'IPv4')
- ->whereIn("sid", [$this->getWhmcsParamByKey('serverid'), "0"])
- ->where('hosting_id', '0')
- ->firstOrFail();
- $newIp = new VmIpAddress();
- $newIp->fill($ip->toArray());
- $newIp->hosting_id = $this->getWhmcsParamByKey("serviceid");
- $newIp->server_id = $this->getWhmcsParamByKey("serverid");
- return $newIp;
- }
- public function hasPrivateIpAddress()
- {
- $query = IpAddress::where('private', 1)
- ->where('type', 'IPv4')
- ->whereIn("sid", [$this->getWhmcsParamByKey('serverid'), "0"])
- ->where('hosting_id', '0');
- return $query->count();
- }
- public function deleteByNetworkId(array $networkIds)
- {
- if (is_null($networkIds))
- {
- throw new \InvalidArgumentException('The network ids cannot be empty');
- }
- $ipAddresses = VmIpAddress::whereIn("net", $networkIds)
- ->ofHostingId($this->getWhmcsParamByKey('serviceid'))
- ->ofVmId(\ModulesGarden\ProxmoxAddon\Core\Helper\sl('Vm')->getVmModel()->id)
- ->get()->all();
- return $this->deleteByIpAddresses($ipAddresses);
- }
- public function deleteByIpAddresses( $ipAddresses)
- {
- if (is_null($ipAddresses))
- {
- throw new \InvalidArgumentException('The IP Addresses cannot be empty');
- }
- $vm = \ModulesGarden\ProxmoxAddon\Core\Helper\sl('Vm')->getVm();
- $devices = $vm->getNetworkDevices();
- $configDelete = [];
- $this->setHostingId($this->getWhmcsParamByKey("serviceid"));
- DB::beginTransaction();
- Api::beginTransaction();
- try
- {
- foreach ($ipAddresses as &$ip)
- {
- if (is_numeric($ip))
- {
- $ip = VmIpAddress::where("id", $ip)->firstOrFail();
- }
- //vi
- $ip->delete();
- //Public IP
- if($ip instanceof VirtualInterface && $ip->vn_id == 0){
- VmIpAddress::ofHostingId($this->getWhmcsParamByKey("serviceid"))
- ->ofVmId($ip->vm_id)
- ->ofIp($ip->ip)
- ->update([ 'vm_id'=> null,'net'=> null]);
- }
- /**
- * @deprecated
- if ($ip instanceof VmIpAddress)
- {
- //Unlock ip in Proxmox Addon
- IpAddress::where('ip', $ip->ip)
- ->where('hosting_id', $ip->hosting_id)
- ->update(["hosting_id" => "0", 'last_check' => Utility::timeStamp()]);
- }
- */
- foreach ($devices as $deviceIndex => $device)
- {
- if ($this->configuration()->isOneNetworkDevice() && $device->getId() == "net0" && $ip instanceof VirtualInterface)
- {
- continue;
- }
- if($device instanceof NetworkDeviceLxc)
- {
- if ($ip->ip == $device->getIp())
- {
- $device->setIp(null);
- }
- if ($ip->ip == $device->getIp6())
- {
- $device->setIp6(null);
- }
- if(empty($device->getIp6()) && empty($device->getIp()))
- {
- unset($devices[$deviceIndex]);
- $configDelete[] = $device->getId();
- }
- }
- elseif($device instanceof NetworkDeviceKvm && $ip->net && $ip->net == $device->getId())
- {
- $configDelete[] = $device->getId();
- unset($devices[$deviceIndex]);
- }
- }
- //cloud init
- if ($vm instanceof Kvm)
- {
- foreach ($vm->getIpConfig()->fetch() as $ipConfig)
- {
- if ($ipConfig->getIp() == $ip->ip || $ipConfig->getIp6() == $ip->ip)
- {
- $configDelete[] = $ipConfig->getId();
- }
- }
- }
- }
- $this->hosting()->save();
- /**
- * Delete config. We need that when there is no more network interface
- */
- if (!empty($configDelete))
- {
- $vm->deleteConfig(implode(",", $configDelete));
- }
- /**
- * Update current config. We need that only if we remove only one IP and other IP is still on the same interface
- $config = new Config();
- $config->setNet($devices);
- $config = $config->toArray();
- if($config)
- {
- $vm->updateConfig($config);
- }
- *
- */
- DB::commit();
- }
- catch (\Exception $ex)
- {
- DB::rollBack();
- Api::commit();
- throw $ex;
- }
- }
- public function hasIp($totalIpv4, $totalIpv6, $nodeName)
- {
- $virtualization = $this->configuration()->isQemu() ? "KVM" : "LXC";
- if ((int)$totalIpv4 > 0)
- {
- /**
- * @var $ipCollections IpAddress
- */
- $ipCollections = IpAddress::where('hosting_id', '0')
- ->where('private', '0')
- ->where('type', 'IPv4')
- ->whereIn('sid', [$this->getWhmcsParamByKey('serverid'), '0'])
- ->whereIn('visualization', [$virtualization, 'Auto'])
- ->whereIn('node', [$nodeName, '0', ""]);
- if ($this->configuration()->getTags())
- {
- $ipCollections->ofTags($this->configuration()->getTags());
- }
- $count = $ipCollections->count();
- if ((int)$count < $totalIpv4)
- {
- throw new \Exception(sprintf("Unable to get %s of IPv4. IP Addresses available: %s", $totalIpv4, $count));
- }
- }
- if ((int)$totalIpv6 > 0)
- {
- $ipCollections = IpAddress::where('hosting_id', '0')
- ->where('private', '0')
- ->where('type', 'IPv6')
- ->whereIn('sid', [$this->getWhmcsParamByKey('serverid'), '0'])
- ->whereIn('visualization', [$virtualization, 'Auto'])
- ->whereIn('node', [$nodeName, '0', ""]);
- if ($this->configuration()->getTags())
- {
- $ipCollections->ofTags($this->configuration()->getTags());
- }
- $count = $ipCollections->count();
- if ((int)$count < $totalIpv6)
- {
- throw new \Exception(sprintf("Unable to get %s of IPv6. IP Addresses available: %s", $totalIpv6, $count));
- }
- }
- return $this;
- }
- public function addIp($totalIpv4, $totalIpv6, $nodeName)
- {
- try {
- DB::beginTransaction();
- $virtualization = $this->configuration()->isQemu() ? "KVM" : "LXC";
- $this->setHostingId($this->getWhmcsParamByKey('serviceid'));
- if ((int)$totalIpv4 > 0) {
- $query = IpAddress::where('hosting_id', '0')
- ->where('private', '0')
- ->where('type', 'IPv4')
- ->whereIn('sid', [$this->getWhmcsParamByKey('serverid'), '0'])
- ->whereIn('visualization', [$virtualization, 'Auto'])
- ->whereIn('node', [$nodeName, '0', ""]);
- if ($this->configuration()->getTags()) {
- $query->ofTags($this->configuration()->getTags());
- }
- $count = $query->limit($totalIpv4)
- ->orderBy('last_check', 'asc')
- ->update(["hosting_id" => $this->getWhmcsParamByKey('serviceid'), 'last_check' => Utility::timeStamp()]);
- if ((int)$count < $totalIpv4) {
- throw new \Exception(sprintf("Unable to get %s of IPv4. IP Addresses available: %s", $totalIpv4, $count));
- }
- }
- if ((int)$totalIpv6 > 0) {
- $query = IpAddress::where('hosting_id', '0')
- ->where('private', '0')
- ->where('type', 'IPv6')
- ->whereIn('sid', [$this->getWhmcsParamByKey('serverid'), '0'])
- ->whereIn('visualization', [$virtualization, 'Auto'])
- ->whereIn('node', [$nodeName, '0', ""]);
- if ($this->configuration()->getTags()) {
- $query->ofTags($this->configuration()->getTags());
- }
- $count = $query->limit($totalIpv6)
- ->orderBy('last_check', 'asc')
- ->update(["hosting_id" => $this->getWhmcsParamByKey('serviceid'), 'last_check' => Utility::timeStamp()]);
- if ((int)$count < $totalIpv6) {
- throw new \Exception(sprintf("Unable to get %s of IPv6. IP Addresses available: %s", $totalIpv6, $count));
- }
- }
- $vmIp = (new VmIpAddress())->getTable();
- $collection = IpAddress::where('hosting_id', $this->getWhmcsParamByKey('serviceid'))
- ->orderBy("ip", "desc")
- ->whereNotIn('ip', function ($query) use ($vmIp) {
- $query->select('ip')->from($vmIp);
- });
- foreach ($collection->get()->sortBy("type")->all() as $ip) {
- /*@var $ip IpAddress */
- $newIp = new VmIpAddress();
- $ipData = $ip->toArray();
- unset($ipData['id']);
- $ipData['hosting_id'] = $this->getWhmcsParamByKey('serviceid');
- $ipData['server_id'] = $this->getWhmcsParamByKey('serverid');
- $newIp->vm_id = null;
- if(sl('Vm')->hasVm()){
- $newIp->vm_id = sl('Vm')->getVmModel()->id;
- }
- $newIp->fill($ipData)->save();
- $this->hosting()->ipAdd($ip->ip);
- }
- $this->hosting()->save();
- DB::commit();
- } catch (\Exception $ex) {
- DB::rollBack();
- throw $ex;
- }
- }
- public function deleteIpAddresses()
- {
- $query = VmIpAddress::ofHostingId($this->getWhmcsParamByKey('serviceid'))
- ->ofVmId(sl('Vm')->getVmModel()->id)
- ->update(['vm_id'=>null,'net' => null]);
- /**
- * @deprecated
- foreach ($query->get() as $vmIp){
- //Unlock ip in Proxmox Addon
- IpAddress::where('hosting_id', $this->getWhmcsParamByKey('serviceid'))
- ->ofIp($vmIp->ip)
- ->update(["hosting_id" => "0", 'last_check' => Utility::timeStamp()]);
- $this->hosting()->ipDelete($vmIp->ip);
- $vmIp->delete();
- }
- */
- return $this;
- }
- /**
- * @deprecated
- */
- public function buildLxc()
- {
- $vmId = sl('Vm')->getVmModel()->id;
- $container = [];
- /**
- * @var VmIpAddress[] $ipv4
- */
- $ipv4 = VmIpAddress::ofHostingId($this->getWhmcsParamByKey("serviceid"))
- ->ofIp4()
- ->ofNetNull()
- ->ofVmId($vmId)
- ->get();
- //ip6
- $ipv6 = VmIpAddress::ofHostingId($this->getWhmcsParamByKey("serviceid"))
- ->ofIp6()
- ->ofNetNull()
- ->ofVmId($vmId)
- ->get();
- //Network
- $bridge = $this->configuration()->getBridge();
- $firewall = $this->configuration()->isNetworkFirewall() ? 1 : 0;
- $ip4Mode = $this->configuration()->getIpv4NetworkMode();
- $ip6Mode = $this->configuration()->getIpv6NetworkMode();
- $rate = null;
- if ($this->getWhmcsConfigOption(ConfigurableOption::NETWORK_RATE, $this->configuration()->getRate()) && $this->getWhmcsConfigOption(ConfigurableOption::NETWORK_RATE, $this->configuration()->getRate()) != "-1")
- {
- $rate = $this->getWhmcsConfigOption(ConfigurableOption::NETWORK_RATE, $this->configuration()->getRate());
- }
- $networkId = 0;
- if (sl('Vm')->hasVm())
- {
- $networkId = sl('Vm')->getVm()->findFreeNetworDeviceId();
- }
- for ($i = 0; $i <= 9; $i++)
- {
- //Empty IP Addresses
- if (!$ipv4->get($i) && !$ipv6->get($i))
- {
- break;
- }
- if ($networkId > 9)
- {
- break;
- }
- $mac = null;
- //Name
- $interface = new NetworkDeviceLxc('net' . $networkId);
- $interface->setName('eth' . $networkId)
- ->setBridge($bridge)
- ->setFirewall($firewall);
- //IP 4
- if ($ipv4->get($i))
- {
- if ($ipv4->get($i)->mac_address)
- {
- $mac = $ipv4->get($i)->mac_address;
- }
- if ($ipv4->get($i)->tag)
- {
- $interface->setTag($ipv4->get($i)->tag);
- }
- $interface->setTrunks($ipv4->get($i)->trunks);
- if ($ip4Mode == 'static')
- { //IP & CIRD
- $interface->setIp($ipv4->get($i)->ip);
- $interface->setCidr($ipv4->get($i)->cidr);
- //Gateway
- $interface->setGw($ipv4->get($i)->gateway);
- }
- $ip = trim($ipv4->get($i)->ip);
- $ipv4->get($i)->net = $interface->getId();
- $ipv4->get($i)->save();
- }
- if ($ip4Mode == "dhcp")
- {
- $interface->setIp("dhcp");
- }
- //IP 6
- if ($ip6Mode == "dhcp")
- {
- $interface->setIp6("dhcp");
- }
- else
- {
- if ($ip6Mode == "slaac")
- {
- $interface->setIp6("auto");
- }
- }
- if ($ipv6->get($i))
- { //IP & CIRD
- $ip = trim($ipv6->get($i)->ip);
- if ($ip6Mode == "static")
- {
- $interface->setIp6($ipv6->get($i)->ip);
- $interface->setCidr6($ipv6->get($i)->cidr);
- //Gateway
- $interface->setGw6($ipv6->get($i)->gateway);
- }
- if ($ipv6->get($i)->tag)
- {
- $interface->setTag($ipv6->get($i)->tag);
- }
- if ($ipv6->get($i)->trunks)
- {
- $interface->setTrunks($ipv6->get($i)->trunks);
- }
- if ($mac == null && $ipv6->get($i)->mac_address)
- {
- $mac = $ipv6->get($i)->mac_address;
- }
- $ipv6->get($i)->net = $interface->getId();
- $ipv6->get($i)->save();
- }
- if ($rate)
- {
- $interface->setRate($rate);
- }
- $interface->setHwaddr($mac);
- $container['net' . $networkId] = $interface->asConfig();
- $networkId++;
- VirtualInterface::ofHostingId($this->getWhmcsParamByKey("serviceid"))
- ->ofVmId($vmId )
- ->ofIp($ip)
- ->update(['net' =>$interface->getId()]);
- }
- return $container;
- }
- /**
- * @deprecated
- */
- public function buildQemu()
- {
- $container = [];
- $vmId = sl('Vm')->getVmModel()->id;
- /**
- * @var VmIpAddress[] $ipv4
- */
- $ipv4 = VmIpAddress::ofHostingId($this->getWhmcsParamByKey("serviceid"))
- ->ofIp4()
- ->ofNetNull()
- ->ofVmId($vmId)
- ->get();
- //ip6
- $ipv6 = VmIpAddress::ofHostingId($this->getWhmcsParamByKey("serviceid"))
- ->ofIp6()
- ->ofNetNull()
- ->ofVmId($vmId)
- ->get();
- //Network
- $model = $this->configuration()->getNetworkModel();
- $bridge = $this->configuration()->getBridge();
- $firewall = $this->configuration()->isNetworkFirewall() ? 1 : 0;
- $rate = null;
- if ($this->getWhmcsConfigOption(ConfigurableOption::NETWORK_RATE, $this->configuration()->getRate()) && $this->getWhmcsConfigOption(ConfigurableOption::NETWORK_RATE, $this->configuration()->getRate()) != "-1")
- {
- $rate = $this->getWhmcsConfigOption(ConfigurableOption::NETWORK_RATE, $this->configuration()->getRate());
- }
- $networkId = 0;
- $vmNetworkDevices = [];
- if (sl('Vm')->hasVm())
- {
- $networkId = sl('Vm')->getVm()->findFreeNetworDeviceId();
- $vmNetworkDevices = sl('Vm')->getVm()->getNetworkDevices();
- }
- $i4 = 0;
- $i6 = 0;
- $isApi52 = version_compare($this->api()->getVersion(), "5.2", '>=');
- for ($i = 0; $i <= 31; $i++)
- {
- //Empty IP Addresses
- if (!$ipv4->get($i4) && !$ipv6->get($i6))
- {
- break;
- }
- if ($networkId > 31)
- {
- break;
- }
- if ($this->configuration()->isOneNetworkDevice() && ($i > 0 || count($vmNetworkDevices)))
- {
- VmIpAddress::ofHostingId($this->getWhmcsParamByKey("serviceid"))
- ->ofNetNull()
- ->ofVmId($vmId )
- ->update(["net" => 'net0']);
- break;
- }
- $mac = null;
- $isIp4 = false;
- //Name
- $interface = new NetworkDeviceKvm('net' . $networkId);
- $interface->setModel($model)
- ->setBridge($bridge)
- ->setFirewall($firewall);
- $ipConfig = new IpConfig('ipconfig' . $networkId);
- //IP 4
- if ($ipv4->get($i4))
- {
- $isIp4 = true;
- $ip = trim($ipv4->get($i4)->ip);
- if (trim($ipv4->get($i4)->mac_address))
- {
- $mac = $ipv4->get($i4)->mac_address;
- }
- if ($ipv4->get($i4)->tag)
- {
- $interface->setTag(trim($ipv4->get($i4)->tag));
- }
- $interface->setTrunks(trim($ipv4->get($i4)->trunks));
- $ipConfig->setIp(trim($ipv4->get($i4)->ip));
- $ipConfig->setCidr(trim($ipv4->get($i4)->cidr));
- //Gateway
- $ipConfig->setGw(trim($ipv4->get($i4)->gateway));
- $ipv4->get($i4)->net = $interface->getId();
- $ipv4->get($i4)->save();
- $i4++;
- }
- //ipv6
- if ($ipv6->get($i6) && (!$isIp4 || $interface->getTag() == $ipv6->get($i6)->tag))
- { //IP & CIRD
- $ip = ttrim($ipv6->get($i6)->ip);
- $ipConfig->setIp6(trim($ipv6->get($i6)->ip));
- $ipConfig->setCidr6(trim($ipv6->get($i6)->cidr));
- //Gateway
- $ipConfig->setGw6(trim($ipv6->get($i6)->gateway));
- if ($ipv6->get($i6)->tag)
- {
- $tag = $ipv6->get($i6)->tag;
- $interface->setTag(trim($ipv6->get($i6)->tag));
- }
- if ($ipv6->get($i6)->trunks)
- {
- $interface->setTrunks(trim($ipv6->get($i6)->trunks));
- }
- if ($mac == null && $ipv6->get($i6)->mac_address)
- {
- $mac = $ipv6->get($i6)->mac_address;
- }
- $ipv6->get($i6)->net = $interface->getId();
- $ipv6->get($i6)->save();
- $i6++;
- }
- $interface->setRate(trim($rate));
- $interface->setMacAddress(trim($mac));
- $interface->setQueues(trim($this->configuration()->getQueues()));
- $container[$interface->getId()] = $interface->asConfig();
- if ($isApi52 && ($ipConfig->getIp() || $ipConfig->getIp6()))
- {
- $container[$ipConfig->getId()] = $ipConfig->asConfig();
- }
- VirtualInterface::ofHostingId($this->getWhmcsParamByKey("serviceid"))
- ->ofVmId($vmId )
- ->ofIp($ip)
- ->update(['net' =>$interface->getId()]);
- $networkId++;
- }
- return $container;
- }
- /**
- * @throws \Exception
- */
- public function unassignIpAddressesAndDeleteNetwork($requestIPv4, $requestIPv6)
- {
- $vmId = sl('Vm')->getVmModel()->id;
- $ip4 = [];
- $ip6 = [];
- if ($requestIPv4 < 0)
- {
- $ip4 = VmIpAddress::ofHostingId($this->getWhmcsParamByKey('serviceid'))
- ->ofIp4()
- ->ofNetNotNull()
- ->ofVmId($vmId)
- ->limit(abs($requestIPv4))
- ->orderByIdDesc()
- ->get()
- ->all();
- }
- if ($requestIPv6 < 0)
- {
- $ip6 = VmIpAddress::ofHostingId($this->getWhmcsParamByKey('serviceid'))
- ->ofIp6()
- ->ofNetNotNull()
- ->ofVmId($vmId)
- ->limit(abs($requestIPv6))
- ->orderByIdDesc()
- ->get()
- ->all();
- }
- $ipAddresses = array_merge($ip4, $ip6);
- if (!empty($ipAddresses))
- {
- $this->deleteByIpAddresses($ipAddresses);
- }
- }
- public function getIpAddressesRequest()
- {
- $ipv4Used = (int)VmIpAddress::ofHostingId($this->getWhmcsParamByKey('serviceid'))
- ->ofIp4()
- ->count();
- $ipv6Used = (int)VmIpAddress::ofHostingId($this->getWhmcsParamByKey('serviceid'))
- ->ofIp6()
- ->count();
- $requestIPv4 = (int)$this->configuration()->getIpv4();
- $requestIPv6 = (int)$this->configuration()->getIpv6();
- if ($this->isWhmcsConfigOption(ConfigurableOption::IPV4))
- {
- $requestIPv4 = (int)$this->getWhmcsConfigOption(ConfigurableOption::IPV4);
- }
- if ($this->isWhmcsConfigOption(ConfigurableOption::IPV6))
- {
- $requestIPv6 = (int)$this->getWhmcsConfigOption(ConfigurableOption::IPV6);
- }
- return [(int)$requestIPv4 -= $ipv4Used, (int)$requestIPv6 -= $ipv6Used];
- }
- }
|