Stars: 216
Forks: 15
Pull Requests: 15
Issues: 1
Watchers: 9
Last Updated: 2021-10-30 17:32:46
Put malicious users, IP addresses and anonymous browser fingerprints under surveillance, log the URLs they visit and block malicious ones from accessing the Laravel app.
License: MIT License
Languages: PHP
Laravel Surveillance is a package to put malicious users, IP addresses and anonymous browser fingerprints under surveillance, write surveillance logs and block malicious ones from accessing the app.
Please read the IMPORTANT INFORMATION below before using this package
This package collects and processes various attributes that may be Personal Identifiable Information and this should therefore be disclosed and screened before adopting this package. This packages author does not take responsibility for any compliance issues users may face. Please consult legal expertise to use responsibly.
This package provides:
1. A middleware to be used on routes.
2. A command line interface to enable/disable surveillance and block/unblock access.
3. A fluent API to programmatically enable/disable surveillance, block/unblock access and log the requests at runtime.
4. By default the package used MySQL database as storage but the package can be extended to use virtually any storage technology.
NOTE: This package does not provide a client side library for browser fingerprinting. FingerprintJS Open Source is a good library to use for client side browser fingerprinting.
Introducing Laravel Surveillance UI : A package which provides Graphical UI for Laravel Surveillance and integrates within your existing application.
composer require neelkanthk/laravel-surveillance
php artisan vendor:publish --provider="Neelkanth\Laravel\Surveillance\Providers\SurveillanceServiceProvider" --tag="migrations"
php artisan vendor:publish --provider="Neelkanth\Laravel\Surveillance\Providers\SurveillanceServiceProvider" --tag="lang"
php artisan migrate
surveillance_managers
and surveillance_logs
php artisan vendor:publish --provider="Neelkanth\Laravel\Surveillance\Providers\SurveillanceServiceProvider" --tag="config"
This is the contents of the file that will be published at config/surveillance.php
:
return [
/*
* The name of the header to be used for browser fingerprint
*/
"fingerprint-header-key" => "fingerprint",
/*
* This class is responsible enabling, disabling, blocking and unblocking.
* To override the default functionality extend the below class and provide its name here.
*/
"manager-repository" => 'Neelkanth\Laravel\Surveillance\Implementations\SurveillanceManagerRepository',
/*
* This class is responsible for logging the surveillance enabled requests
* To override the default functionality extend the below class and provide its name here.
*/
"log-repository" => 'Neelkanth\Laravel\Surveillance\Implementations\SurveillanceLogRepository',
/*
* The types which are allowed currently.
* DO NOT MODIFY THESE
*/
"allowed-types" => ["userid", "ip", "fingerprint"]
];
php artisan surveillance:enable ip 192.1.2.4
php artisan surveillance:disable ip 192.1.2.4
php artisan surveillance:enable userid 1234
php artisan surveillance:disable userid 1234
php artisan surveillance:enable fingerprint hjP0tLyIUy7SXaSY6gyb
php artisan surveillance:disable fingerprint hjP0tLyIUy7SXaSY6gyb
php artisan surveillance:block ip 192.1.2.4
php artisan surveillance:unblock ip 192.1.2.4
php artisan surveillance:block userid 1234
php artisan surveillance:unblock userid 1234
php artisan surveillance:block fingerprint hjP0tLyIUy7SXaSY6gyb
php artisan surveillance:unblock fingerprint hjP0tLyIUy7SXaSY6gyb
php artisan surveillance:remove ip 192.5.4.3
fingerprint-header-key
inside config/surveillance.php
Route::middleware(["surveillance"])->get('/', function () {
});
use Neelkanth\Laravel\Surveillance\Services\Surveillance;
Surveillance::manager()->type("ip")->value("192.5.4.1")->enableSurveillance();
use Neelkanth\Laravel\Surveillance\Services\Surveillance;
Surveillance::manager()->type("userid")->value(2121)->blockAccess();
use Neelkanth\Laravel\Surveillance\Services\Surveillance;
Surveillance::logger()->writeLog();
SurveillanceManagerRepository
Class and override all of its methods//Example repository to use MongoDB instead of MySQL
namespace App;
use Neelkanth\Laravel\Surveillance\Implementations\SurveillanceManagerRepository;
use Illuminate\Support\Carbon;
class SurveillanceManagerMongoDbRepository extends SurveillanceManagerRepository
{
public function enableSurveillance()
{
$surveillance = $this->getRecord();
if (is_null($surveillance)) {
$surveillance["type"] = $this->getType();
$surveillance["value"] = $this->getValue();
}
$surveillance["surveillance_enabled"] = 1;
$surveillance["surveillance_enabled_at"] = Carbon::now()->toDateTimeString();
$collection = (new \MongoDB\Client)->surveillance->manager;
$insertOneResult = $collection->insertOne($surveillance);
return $insertOneResult;
}
}
config/surveillance.php
file's manager-repository
key/*
* This class is responsible enabling, disabling, blocking and unblocking.
* To override the default functionality extend the below class and provide its name here.
*/
"manager-repository" => 'App\SurveillanceManagerMongoDbRepository',
SurveillanceLogRepository
Class and override all of its methods//Example repository to write Logs in MongoDB instead of MySQL
namespace App;
use Neelkanth\Laravel\Surveillance\Implementations\SurveillanceLogRepository;
class SurveillanceLogMongoDbRepository extends SurveillanceLogRepository
{
public function writeLog($dataToLog = null)
{
if (!is_null($dataToLog)) {
$this->setLogToWrite($dataToLog);
}
$log = $this->getLogToWrite();
if (!empty($log) && is_array($log)) {
$collection = (new \MongoDB\Client)->surveillance->logs;
$insertOneResult = $collection->insertOne($log);
}
}
}
config/surveillance.php
file's log-repository
key/*
* This class is responsible for logging the surveillance enabled requests
* To override the default functionality extend the below class and provide its name here.
*/
"log-repository" => 'App\SurveillanceLogMongoDbRepository',
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
If you discover any security-related issues, please email [email protected] instead of using the issue tracker.