| 
									
										
										
										
											2022-02-21 11:17:59 +11:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace App\Models; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | use Illuminate\Database\Eloquent\Factories\HasFactory; | 
					
						
							|  |  |  | use Illuminate\Database\Eloquent\Model; | 
					
						
							| 
									
										
										
										
											2022-05-15 01:21:51 +10:00
										 |  |  | use Illuminate\Support\Facades\Cache; | 
					
						
							| 
									
										
										
										
											2022-02-22 13:30:39 +11:00
										 |  |  | use Illuminate\Support\Facades\DB; | 
					
						
							| 
									
										
										
										
											2024-12-09 18:58:02 +11:00
										 |  |  | use Illuminate\Support\Facades\Http; | 
					
						
							| 
									
										
										
										
											2022-05-09 14:35:07 +10:00
										 |  |  | use Illuminate\Support\Str; | 
					
						
							| 
									
										
										
										
											2022-02-21 11:17:59 +11:00
										 |  |  | 
 | 
					
						
							|  |  |  | class IPs extends Model | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     use HasFactory; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public $table = 'ips'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-22 23:49:18 +10:00
										 |  |  |     protected $keyType = 'string'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-09 18:58:02 +11:00
										 |  |  |     protected $fillable = ['id', 'service_id', 'address', 'is_ipv4', 'active', 'continent', 'country', 'region', 'city', 'org', 'isp', 'asn', 'timezone_gmt', 'fetched_at']; | 
					
						
							| 
									
										
										
										
											2022-02-21 11:17:59 +11:00
										 |  |  | 
 | 
					
						
							|  |  |  |     public $incrementing = false; | 
					
						
							| 
									
										
										
										
											2022-02-22 13:30:39 +11:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-19 23:46:18 +10:00
										 |  |  |     public static function deleteIPsAssignedTo($service_id): void | 
					
						
							| 
									
										
										
										
											2022-02-22 13:30:39 +11:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-08-18 19:33:26 +10:00
										 |  |  |         DB::table('ips')->where('service_id', $service_id)->delete(); | 
					
						
							| 
									
										
										
										
											2022-02-22 13:30:39 +11:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-05 11:58:19 +11:00
										 |  |  |     public static function insertIP(string $service_id, string $address): IPs | 
					
						
							| 
									
										
										
										
											2022-05-09 14:35:07 +10:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-10-05 11:58:19 +11:00
										 |  |  |         return self::create( | 
					
						
							| 
									
										
										
										
											2022-05-09 14:35:07 +10: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-15 01:21:51 +10:00
										 |  |  |     public static function ipsForServer(string $server_id) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-10-13 13:37:02 +11:00
										 |  |  |         return Cache::remember("ip_addresses.$server_id", now()->addHours(1), function () use ($server_id) { | 
					
						
							| 
									
										
										
										
											2022-05-15 01:21:51 +10:00
										 |  |  |             return json_decode(DB::table('ips as i') | 
					
						
							| 
									
										
										
										
											2023-08-18 19:33:26 +10:00
										 |  |  |                 ->where('i.service_id', $server_id) | 
					
						
							| 
									
										
										
										
											2022-05-15 01:21:51 +10:00
										 |  |  |                 ->get(), true); | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-02 13:57:28 +11:00
										 |  |  |     public function note(): \Illuminate\Database\Eloquent\Relations\HasOne | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return $this->hasOne(Note::class, 'service_id', 'id'); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-09 18:58:02 +11:00
										 |  |  |     public static function getUpdateIpInfo(IPs $IP): bool | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2024-12-09 22:37:13 +11:00
										 |  |  |         $response = Http::get("https://ipwhois.app/json/{$IP->address}"); | 
					
						
							| 
									
										
										
										
											2024-12-09 18:58:02 +11:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if ($response->ok()) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             $data = $response->json(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             $IP->update([ | 
					
						
							|  |  |  |                 'continent' => $data['continent'], | 
					
						
							|  |  |  |                 'country' => $data['country'], | 
					
						
							|  |  |  |                 'region' => $data['region'], | 
					
						
							|  |  |  |                 'city' => $data['city'], | 
					
						
							|  |  |  |                 'org' => $data['org'], | 
					
						
							|  |  |  |                 'isp' => $data['isp'], | 
					
						
							|  |  |  |                 'asn' => $data['asn'], | 
					
						
							|  |  |  |                 'timezone_gmt' => $data['timezone_gmt'], | 
					
						
							|  |  |  |                 'fetched_at' => now() | 
					
						
							|  |  |  |             ]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $response->ok(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-21 11:17:59 +11:00
										 |  |  | } |