2022-02-21 01:17:59 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2022-05-14 17:21:51 +02:00
|
|
|
use Illuminate\Support\Facades\Cache;
|
2022-02-22 03:30:39 +01:00
|
|
|
use Illuminate\Support\Facades\DB;
|
2022-05-09 06:35:07 +02:00
|
|
|
use Illuminate\Support\Str;
|
2022-02-21 01:17:59 +01:00
|
|
|
|
|
|
|
class IPs extends Model
|
|
|
|
{
|
|
|
|
use HasFactory;
|
|
|
|
|
|
|
|
public $table = 'ips';
|
|
|
|
|
2022-07-22 15:49:18 +02:00
|
|
|
protected $keyType = 'string';
|
|
|
|
|
2022-02-21 01:17:59 +01:00
|
|
|
protected $fillable = ['id', 'active', 'service_id', 'address', 'is_ipv4'];
|
|
|
|
|
|
|
|
public $incrementing = false;
|
2022-02-22 03:30:39 +01:00
|
|
|
|
|
|
|
public static function deleteIPsAssignedTo($service_id)
|
|
|
|
{
|
|
|
|
DB::table('ips')->where('service_id', '=', $service_id)->delete();
|
|
|
|
}
|
|
|
|
|
2022-10-05 02:58:19 +02:00
|
|
|
public static function insertIP(string $service_id, string $address): IPs
|
2022-05-09 06:35:07 +02:00
|
|
|
{
|
2022-10-05 02:58:19 +02:00
|
|
|
return self::create(
|
2022-05-09 06:35:07 +02:00
|
|
|
[
|
|
|
|
'id' => Str::random(8),
|
|
|
|
'service_id' => $service_id,
|
|
|
|
'address' => $address,
|
|
|
|
'is_ipv4' => (filter_var($address, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) ? 0 : 1,
|
|
|
|
'active' => 1
|
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-05-14 17:21:51 +02:00
|
|
|
public static function ipsForServer(string $server_id)
|
|
|
|
{
|
2022-10-13 04:37:02 +02:00
|
|
|
return Cache::remember("ip_addresses.$server_id", now()->addHours(1), function () use ($server_id) {
|
2022-05-14 17:21:51 +02:00
|
|
|
return json_decode(DB::table('ips as i')
|
|
|
|
->where('i.service_id', '=', $server_id)
|
|
|
|
->get(), true);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-02-21 01:17:59 +01:00
|
|
|
}
|