Skip to content

add console commands to list [hits, notified], event BotBlockedEvent #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/BlockBotsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
namespace Potelo\LaravelBlockBots;

use Illuminate\Support\ServiceProvider;
use Potelo\LaravelBlockBots\Commands\ListWhitelist;
use Potelo\LaravelBlockBots\Commands\ClearWhitelist;
use Potelo\LaravelBlockBots\Commands\ListWhitelist;
use Potelo\LaravelBlockBots\Commands\ListHits;
use Potelo\LaravelBlockBots\Commands\ListNotified;

class BlockBotsServiceProvider extends ServiceProvider
{
Expand All @@ -18,8 +20,10 @@ public function boot()

if ($this->app->runningInConsole()) {
$this->commands([
ListWhitelist::class,
ClearWhitelist::class,
ListWhitelist::class,
ListHits::class,
ListNotified::class,
]);
}

Expand Down
57 changes: 57 additions & 0 deletions src/Commands/ListHits.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace Potelo\LaravelBlockBots\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Redis;
use Potelo\LaravelBlockBots\Contracts\Configuration;

class ListHits extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'block-bots:list-hits';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Show the list of IPs with hits count';

/**
* @var \Potelo\LaravelBlockBots\Contracts\Configuration
*/
private $options;

/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
$this->options = new Configuration();
}

/**
* Execute the console command.
*
* @return void
*/
public function handle()
{
$this->info("List of IPs hits:");

$keys = Redis::keys('block_bot:hits*');

foreach ($keys as $key) {
$key = str($key)->afterLast(':');
$this->info($key . " : " . Redis::get("block_bot:hits:{$key}"));
}
}
}
57 changes: 57 additions & 0 deletions src/Commands/ListNotified.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace Potelo\LaravelBlockBots\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Redis;
use Potelo\LaravelBlockBots\Contracts\Configuration;

class ListNotified extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'block-bots:list-notified';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Show the list of notified IPs with notified count';

/**
* @var \Potelo\LaravelBlockBots\Contracts\Configuration
*/
private $options;

/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
$this->options = new Configuration();
}

/**
* Execute the console command.
*
* @return void
*/
public function handle()
{
$this->info("List of notified IPs with notified count:");

$keys = Redis::keys('block_bot:notified*');

foreach ($keys as $key) {
$key = str($key)->afterLast(':');
$this->info($key . " : " . Redis::get("block_bot:notified:{$key}"));
}
}
}
2 changes: 1 addition & 1 deletion src/Contracts/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function __construct($request)
$this->ip = $request->getClientIp();
$this->id = Auth::check() ? Auth::id() : $this->ip;
$this->userAgent = $request->header('User-Agent');
$this->key = "block_bot:{$this->id}";
$this->key = "block_bot:hits:{$this->id}";
$this->logKey = "block_bot:notified:{$this->ip}";
$this->url = substr($request->fullUrl(), strlen($request->getScheme() . "://"));
$this->options = new Configuration();
Expand Down
35 changes: 35 additions & 0 deletions src/Events/BotBlockedEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Potelo\LaravelBlockBots\Events;

use Carbon\Carbon;
use Illuminate\Queue\SerializesModels;

class BotBlockedEvent
{
use SerializesModels;

public $ip;

/** @var integer */
public $number_of_hits;

/** @var Carbon */
public $block_date;

/**
* Create a new event instance.
*
* @param $ip
* @param integer $number_of_hits
* @param Carbon $block_date
*
* @return void
*/
public function __construct($ip, $number_of_hits, $block_date)
{
$this->ip = $ip;
$this->number_of_hits = $number_of_hits;
$this->block_date = $block_date;
}
}
5 changes: 4 additions & 1 deletion src/Middleware/BlockBots.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use Potelo\LaravelBlockBots\Events\UserBlockedEvent;
use Potelo\LaravelBlockBots\Jobs\ProcessLogWithIpInfo;
use Potelo\LaravelBlockBots\Abstracts\AbstractBlockBots;

use Potelo\LaravelBlockBots\Events\BotBlockedEvent;

class BlockBots extends AbstractBlockBots
{
Expand Down Expand Up @@ -55,6 +55,9 @@ protected function notAllowed()
event(new UserBlockedEvent(Auth::user(), $this->hits, Carbon::now()));
}

if (Auth::guest() && $this->isTheFirstOverflow()) {
event(new BotBlockedEvent($this->client->ip, $this->hits, Carbon::now()));
}

if ($this->request->expectsJson()) {
return response()->json($this->options->json_response, 429);
Expand Down