neelkanthk / laravel-surveillance
将用户、IP地址和匿名浏览器指纹置于监控之下,并阻止恶意访问。
1.3.0
2020-10-21 19:08 UTC
Requires
- php: >=7.2
- laravel/framework: >=6.0
This package is auto-updated.
Last update: 2024-08-29 05:19:12 UTC
README
Laravel Surveillance 
Laravel Surveillance 是一个用于将恶意用户、IP地址和匿名浏览器指纹置于监控之下、写入监控日志并阻止恶意访问应用的包。
在使用此包之前,请阅读以下重要信息
此包收集和处理各种可能属于个人可识别信息的属性,因此在采用此包之前应予以披露和审查。该包的作者不对用户可能遇到的相关合规性问题承担责任。请咨询法律专家以负责任地使用。
此包提供以下功能:
1. 一个用于路由的中间件。
2. 命令行界面,用于启用/禁用监控以及阻止/解除阻止访问。
3. 一个流畅的API,用于在运行时程序化地启用/禁用监控、阻止/解除阻止访问并记录请求。
4. 默认情况下,此包使用MySQL数据库作为存储,但该包可以被扩展以使用任何虚拟存储技术。
注意:此包不提供用于浏览器指纹的客户端库。 FingerprintJS 开源库 是用于客户端浏览器指纹的一个很好的库。
重要公告!
介绍 Laravel Surveillance UI:一个提供图形UI的包,可与现有应用程序集成。
最低要求
1. Laravel 6.0
2. PHP 7.2
安装
1. 通过composer安装此包
composer require neelkanthk/laravel-surveillance
2.1. 发布迁移文件
php artisan vendor:publish --provider="Neelkanth\Laravel\Surveillance\Providers\SurveillanceServiceProvider" --tag="migrations"
2.2. 发布语言文件
php artisan vendor:publish --provider="Neelkanth\Laravel\Surveillance\Providers\SurveillanceServiceProvider" --tag="lang"
3. 运行迁移
php artisan migrate
4. 运行迁移后,数据库中将创建两个表,分别是 surveillance_managers
和 surveillance_logs
5. 您可以使用以下命令发布配置文件
php artisan vendor:publish --provider="Neelkanth\Laravel\Surveillance\Providers\SurveillanceServiceProvider" --tag="config"
这是将发布到 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"] ];
命令行使用
为IP地址启用监控
php artisan surveillance:enable ip 192.1.2.4
为IP地址禁用监控
php artisan surveillance:disable ip 192.1.2.4
为用户ID启用监控
php artisan surveillance:enable userid 1234
为用户ID禁用监控
php artisan surveillance:disable userid 1234
为浏览器指纹启用监控
php artisan surveillance:enable fingerprint hjP0tLyIUy7SXaSY6gyb
为浏览器指纹禁用监控
php artisan surveillance:disable fingerprint hjP0tLyIUy7SXaSY6gyb
阻止IP地址
php artisan surveillance:block ip 192.1.2.4
解除阻止IP地址
php artisan surveillance:unblock ip 192.1.2.4
阻止用户ID
php artisan surveillance:block userid 1234
解除阻止用户ID
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
中间件使用
您可以在任何路由或路由组中使用 'surveillance' 中间件,就像使用任何其他中间件一样。
注意:中间件在 config/surveillance.php
中的 fingerprint-header-key
设置的头部名称中查找浏览器指纹。
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();
记录请求(当在用户ID、IP地址或浏览器指纹上启用监视时生效)
use Neelkanth\Laravel\Surveillance\Services\Surveillance; Surveillance::logger()->writeLog();
允许的类型
目前仅允许用户ID、IP和指纹类型。
自定义和覆盖默认设置
要覆盖默认的监视管理功能
步骤1:扩展 SurveillanceManagerRepository
类并覆盖所有其方法
//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; } }
步骤2:在 config/surveillance.php
文件的 manager-repository
键中提供自定义类
/* * 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',
要覆盖默认的日志功能
步骤1:扩展 SurveillanceLogRepository
类并覆盖所有其方法
//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); } } }
步骤2:在 config/surveillance.php
文件的 log-repository
键中提供自定义类
/* * 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',
贡献
欢迎提交拉取请求。对于重大更改,请先创建一个问题来讨论您想要更改的内容。
安全
如果您发现任何与安全相关的问题,请通过电子邮件 me.neelkanth@gmail.com 联系,而不是使用问题跟踪器。