|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Potelo\LaravelBlockBots\Jobs; |
| 4 | + |
| 5 | +use GuzzleHttp\Client; |
| 6 | +use Illuminate\Bus\Queueable; |
| 7 | +use Illuminate\Queue\SerializesModels; |
| 8 | +use Illuminate\Queue\InteractsWithQueue; |
| 9 | +use Illuminate\Contracts\Queue\ShouldQueue; |
| 10 | +use Illuminate\Foundation\Bus\Dispatchable; |
| 11 | +use Illuminate\Support\Facades\Redis; |
| 12 | +use Illuminate\Support\Facades\Log; |
| 13 | + |
| 14 | +class ProcessLogWithIpInfo implements ShouldQueue |
| 15 | +{ |
| 16 | + use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; |
| 17 | + |
| 18 | + protected $ip; |
| 19 | + protected $user_agent; |
| 20 | + protected $config; |
| 21 | + protected $attemped_url; |
| 22 | + protected $action; |
| 23 | + |
| 24 | + |
| 25 | + /** |
| 26 | + * Checks whether the given IP address really belongs to a valid host or not |
| 27 | + * |
| 28 | + * @param $ip the IP address to check |
| 29 | + * @return bool true if the given IP address belongs to any of the valid hosts, otherwise false |
| 30 | + */ |
| 31 | + public function __construct($ip, $user_agent, $action, $attemped_url=null) |
| 32 | + { |
| 33 | + $this->ip = $ip; |
| 34 | + $this->user_agent = $user_agent; |
| 35 | + $this->attemped_url = $attemped_url; |
| 36 | + $this->action = $action; |
| 37 | + $this->config = config('block-bots'); |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Execute the job. |
| 42 | + * |
| 43 | + * @return void |
| 44 | + */ |
| 45 | + public function handle() |
| 46 | + { |
| 47 | + // |
| 48 | + $user_agent = $this->user_agent; |
| 49 | + $ip = $this->ip; |
| 50 | + $key_access_count = "block_bot:{$ip}"; |
| 51 | + $number_of_requests = Redis::exists($key_access_count); |
| 52 | + |
| 53 | + $host = strtolower(gethostbyaddr($ip)); |
| 54 | + $messsage = "[Block-Bots] IP: {$ip}; After {$number_of_requests} requests , Host:{$host} \n with User agent: {$user_agent}; was {$this->action}"; |
| 55 | + |
| 56 | + $ip_info = $this->config['ip_info_key']; |
| 57 | + |
| 58 | + if ($ip_info) |
| 59 | + { |
| 60 | + $client = new Client(); |
| 61 | + $response = $client->get( |
| 62 | + "http://ipinfo.io/{$ip}" , |
| 63 | + [ |
| 64 | + 'query' => [ |
| 65 | + 'token' => $ip_info, |
| 66 | + ] |
| 67 | + ] |
| 68 | + ); |
| 69 | + $json_response = json_decode($response->getBody(), true); |
| 70 | + if(array_key_exists('org', $json_response) && array_key_exists('city', $json_response) && |
| 71 | + array_key_exists('region', $json_response) && array_key_exists('country', $json_response)) |
| 72 | + { |
| 73 | + $org = $json_response["org"]; |
| 74 | + $city = $json_response["city"]; |
| 75 | + $region = $json_response["region"]; |
| 76 | + $country = $json_response["country"]; |
| 77 | + |
| 78 | + $messsage .= "Org: {$org} | city: {$city} | region: {$region} | country: {$country} "; |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + if ($this->attemped_url){ |
| 83 | + $messsage .= " when accessing the URL:{$this->attemped_url} "; |
| 84 | + } |
| 85 | + |
| 86 | + if (($this->action == 'GOOD_CRAWLER') || ($this->action == 'WHITELISTED')) |
| 87 | + { |
| 88 | + Log::stack($this->config['channels_info'])->info($messsage); |
| 89 | + } |
| 90 | + else{ |
| 91 | + Log::stack($this->config['channels_info'])->error($messsage); |
| 92 | + } |
| 93 | + |
| 94 | + |
| 95 | + |
| 96 | + } |
| 97 | + |
| 98 | +} |
0 commit comments