initial commit

This commit is contained in:
TheOnlyMace
2026-05-17 13:26:14 +02:00
commit 75299b723d
176 changed files with 20327 additions and 0 deletions

74
app/Models/IpPool.php Normal file
View File

@@ -0,0 +1,74 @@
<?php
namespace App\Models;
use App\Enums\IpPoolType;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
class IpPool extends Model
{
protected $fillable = [
'name',
'type',
'start_ip',
'end_ip',
'gateway',
'cidr',
'description',
'is_active',
];
protected function casts(): array
{
return [
'type' => IpPoolType::class,
'cidr' => 'integer',
'is_active' => 'boolean',
];
}
public function vms(): HasMany
{
return $this->hasMany(Customer::class, 'ip_pool_id');
}
public function totalIps(): int
{
$start = ip2long($this->start_ip);
$end = ip2long($this->end_ip);
if ($start === false || $end === false || $end < $start) {
return 0;
}
return (int) ($end - $start + 1);
}
public function usedIpsCount(): int
{
$query = Customer::query()
->where('ip_pool_id', $this->id)
->whereIn('status', ['pending', 'active']);
if ($this->type === IpPoolType::Public) {
return $query->whereNotNull('public_ip')->count();
}
return $query->whereNotNull('ip_address')->count();
}
public function freeIpsCount(): int
{
return max(0, $this->totalIps() - $this->usedIpsCount());
}
public function containsIp(string $ip): bool
{
$long = ip2long($ip);
$start = ip2long($this->start_ip);
$end = ip2long($this->end_ip);
return $long !== false && $long >= $start && $long <= $end;
}
}