PHP code example of petecoop / protec

1. Go to this page and download the library: Download petecoop/protec library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

petecoop / protec example snippets


'policy' => ['attempts' => 10, 'within' => 60, 'block_for' => '1h'],

'policy' => ['attempts' => 1, 'block_for' => null],

use Petecoop\Protec\Facades\Protec;

Protec::block('203.0.113.10'); // Permanent until manually unblocked.
Protec::block('203.0.113.11', for: '2d', reason: 'manual-review');

use Petecoop\Protec\Facades\Protec;

Protec::unblock('203.0.113.10');

'allowed_ips' => [
    '203.0.113.10',
    '2001:db8::10',
    '198.51.100.0/24',
],

$summary = Protec::ip('203.0.113.10');

$summary->isBlocked;
$summary->isPermanentlyBlocked;
$summary->blockedUntil; // Latest active expiry; null if permanent or not blocked.
$summary->totalBlocks; // All retained block records for this IP.
$summary->activeBlocks;
$summary->latestBlock; // Most recent IpBlock record, which may have ended.
$summary->toArray(); // Also supports JSON serialization.

'escalation' => [
    'after' => 3, // number of times the offending detector can trigger
    'within' => '8h', // within this time period
    'block_for' => '7d', // null makes the escalated block permanent.
],

use Petecoop\Protec\Patterns;

// Inside 'detectors' in config/protec.php:
\Petecoop\Protec\Detectors\ScannerProbe::class => [
    'policy' => ['attempts' => 10, 'within' => '1m', 'block_for' => '1h'],
    'escalation' => ['after' => 5, 'within' => '8h', 'block_for' => '7d'],
    'patterns' => [
        'my-custom-pattern/*',
        Patterns::WORDPRESS,
        Patterns::CONFIGURATION,
        Patterns::VERSION_CONTROL,
    ],
    'except' => ['ignore-this-route'],
],

\Petecoop\Protec\Detectors\CorruptLivewirePayload::class => [
    'policy' => ['attempts' => 5, 'within' => '10m', 'block_for' => '10m'],
    'report' => false,
],

\Petecoop\Protec\Detectors\GuestLivewireComponents::class => [
    'enabled' => true,
    'policy' => ['attempts' => 20, 'within' => '10m', 'block_for' => '10m', 'reject_with' => 401],
    'components' => [\App\Livewire\Login::class, 'public-counter'],
],

use Petecoop\Protec\Middleware\ProtectRequests;

// In your PanelProvider's panel() method:
return $panel
    // ... existing panel configuration ...
    ->middleware([
        // ... keep the panel's existing middleware ...
        ProtectRequests::class,
    ]);

'components' => [
    \Petecoop\Protec\GuestComponents::FILAMENT,
    // Add your own guest components here.
],

Schedule::command('model:prune', [
    '--model' => [\Petecoop\Protec\Models\IpBlock::class],
])->daily();

'cache_store' => 'redis',
sh
composer n vendor:publish --tag=protec-migrations
php artisan migrate
sh
php artisan vendor:publish --tag=protec-config
sh
php artisan model:prune --model='Petecoop\Protec\Models\IpBlock'
sh
php artisan protec:cache