|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Jobs; |
| 4 | + |
| 5 | +use App\Models\Star; |
| 6 | +use Illuminate\Bus\Queueable; |
| 7 | +use Illuminate\Contracts\Queue\ShouldQueue; |
| 8 | +use Illuminate\Foundation\Bus\Dispatchable; |
| 9 | +use Illuminate\Queue\InteractsWithQueue; |
| 10 | +use Illuminate\Queue\Middleware\RateLimited; |
| 11 | +use Illuminate\Queue\SerializesModels; |
| 12 | +use Illuminate\Support\Facades\Http; |
| 13 | + |
| 14 | +class CheckIfIpIsVpn implements ShouldQueue |
| 15 | +{ |
| 16 | + use Dispatchable; |
| 17 | + use InteractsWithQueue; |
| 18 | + use Queueable; |
| 19 | + use SerializesModels; |
| 20 | + |
| 21 | + /** |
| 22 | + * Create a new job instance. |
| 23 | + */ |
| 24 | + public function __construct( |
| 25 | + public string $ip, |
| 26 | + ) { |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * Execute the job. |
| 31 | + */ |
| 32 | + public function handle(): void |
| 33 | + { |
| 34 | + $existingIpStar = Star::query() |
| 35 | + ->where('ip', $this->ip) |
| 36 | + ->whereNotNull('is_vpn_ip') |
| 37 | + ->first(); |
| 38 | + |
| 39 | + if ($existingIpStar) { |
| 40 | + Star::query() |
| 41 | + ->where('ip', $this->ip) |
| 42 | + ->update(['is_vpn_ip' => $existingIpStar->is_vpn_ip]); |
| 43 | + |
| 44 | + return; |
| 45 | + } |
| 46 | + |
| 47 | + $isVpn = Http::retry(10) |
| 48 | + ->throw() |
| 49 | + ->get('https://vpnapi.io/api/' . request()->ip() . '?key=' . config('services.vpn_api.token')) |
| 50 | + ->json('security.vpn'); |
| 51 | + |
| 52 | + if (blank($isVpn)) { |
| 53 | + $this->fail("Failed to check if IP is VPN: {$this->ip}"); |
| 54 | + |
| 55 | + return; |
| 56 | + } |
| 57 | + |
| 58 | + Star::query() |
| 59 | + ->where('ip', $this->ip) |
| 60 | + ->update(['is_vpn_ip' => $isVpn]); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Get the middleware the job should pass through. |
| 65 | + * |
| 66 | + * @return array<int, object> |
| 67 | + */ |
| 68 | + public function middleware(): array |
| 69 | + { |
| 70 | + return [new RateLimited('vpn_api')]; |
| 71 | + } |
| 72 | +} |
0 commit comments