erayaydin/fingerprint-laravel

Fingerprint Laravel 包装器

0.1.0 2024-09-25 16:49 UTC

This package is auto-updated.

Last update: 2024-09-25 17:16:59 UTC


README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads Test Coverage

Fingerprint Laravel 是一个将 Fingerprint Server API 集成到 Laravel 应用程序中的包,使用 Fingerprint Pro Server API 的 PHP SDK。它提供了基于 Fingerprint Server API 事件响应的 HTTP 中间件,以阻止机器人、VPN、Tor 等。

要求

  • Laravel 11
  • PHP ^8.2

功能

  • 使用 fingerprint 配置文件进行自定义实现。
  • 可注入的 EventFingerprint 类。当收到带有 requestId 的请求时,Event 数据类将自动绑定。
  • Fingerprint 类提供了一个流畅的接口来与 Fingerprint Server API 交互。
  • 基于 Fingerprint Server API 事件响应的现成 HTTP 中间件,用于阻止机器人、VPN、Tor 等。

安装

您可以通过 Composer 安装此包

$ composer require erayaydin/fingerprint-laravel

使用 about 命令检查安装

配置

发布配置文件

$ php artisan vendor:publish --tag=fingerprint-config

这将创建一个 config/fingerprint.php 文件,您可以在其中设置配置。

默认情况下,该包将使用 FINGERPRINT_PRO_SECRET_API_KEYFINGERPRINT_REGION 环境变量。您应该在您的 .env 文件中指定这些值。您可以在发布配置文件后更改环境变量名称。

配置选项

  • api_secret:您的 Fingerprint Server API 密钥。
  • region:Fingerprint Server API 的区域。可用选项:eu/europeap/asiaglobal
  • 中间件
    • bot_block:阻止好的和/或坏的机器人。
    • vpn_block:如果用户使用 VPN,则阻止请求。
    • tor_block:阻止使用 Tor 网络的用户。
    • min_confidence:所需的最小置信度分数。应在 0.0 到 1.0 范围内。如果为 null,则不会检查置信度分数。
    • incognito_block:阻止使用隐身模式的用户。
    • max_elapsed_time:请求和事件识别之间的最大经过时间。

用法

中间件

该包提供了多个中间件来阻止不同类型的流量

  • BlockBotsMiddleware (fingerprint.bots)
  • BlockIncognitoMiddleware (fingerprint.incognito)
  • BlockOldIdentificationMiddleware (fingerprint.old-identification)
  • BlockTorMiddleware (fingerprint.tor)
  • BlockVPNMiddleware (fingerprint.vpn)
  • MinConfidenceScoreMiddleware (fingerprint.confidence)

您可以在您的 bootstrap/app.php 文件中注册这些中间件,或在路由或控制器中使用提供的中间件组 fingerprint

在路由中使用中间件组

Route::middleware(['fingerprint'])->group(function () {
    // Request is valid!
});

或者,您可以使用特定的中间件

Route::middleware(['fingerprint.incognito', 'fingerprint.vpn'])->group(function () {
    // User is not in incognito mode and not using VPN!
});

事件数据访问

使用依赖注入在控制器或中间件中访问 Event 数据类

class ExampleController extends Controller
{
    public function store(Event $event)
    {
        ray($event->identification, $event->botD, $event->isTor, $event->isVPN);
    }
}

Fingerprint Server API 访问

使用依赖注入在控制器或中间件中访问 Fingerprint

class ExampleController extends Controller
{
    public function store(Fingerprint $fingerprint)
    {
        ray($fingerprint->getEvent('requestId'));
    }
}

About 命令支持

该包与 Laravel 的 AboutCommand 集成,以提供有关指纹配置的信息。这是自动注册的。

$ php artisan about
...
  Fingerprint Laravel .........................................................
  API Key ...................................................... Not Configured
  Bot Block ................................................ Enabled (Bad Bots)
  Incognito Block ..................................................... Enabled
  Min. Confidence ......................................................... 0.8
  Old Identification ............................................... 10 seconds
  Region ................................................................... eu
  TOR Block ............................................. Enabled (if signaled)
  VPN Block ........................................................... Enabled
  Version ................................................ 1.0.0+no-version-set

测试

您可以使用以下命令运行测试

composer test

路线图

  • 提高代码覆盖率到 ~95%。
  • 添加用于更改 requestId 键的配置选项。
  • 添加 IP 阻止中间件。
  • 添加使用 Visitor 模型和迁移的自动访客存储支持。
  • 添加 HasVisitorId 特性,可用于自定义 Eloquent 模型。

贡献

请参阅CONTRIBUTING获取详细信息。

许可协议

MIT 许可协议 (MIT)。请参阅许可文件以获取更多信息。