akaunting/laravel-firewall
Laravel Firewall adds an application-level firewall to block or whitelist IPs, detect suspicious requests, limit attempts, and prevent brute-force attacks. Includes logging, configurable rules, and easy middleware integration for protecting routes and admin areas.
Installation
composer require akaunting/laravel-firewall
php artisan vendor:publish --provider="Akaunting\Firewall\FirewallServiceProvider" --tag="config"
config/firewall.php).Basic Configuration
Edit config/firewall.php to define rules:
'rules' => [
'block' => [
'ip-ranges' => [
'192.168.1.0/24', // Block entire subnet
],
'user-agents' => [
'BadBot', // Block requests with this UA
],
],
'allow' => [
'ip-ranges' => [
'10.0.0.0/8', // Whitelist internal network
],
],
],
First Use Case
curl -A "BadBot" http://your-app.test
Should return a 403 Forbidden response if configured.Dynamic Rule Loading
Override config/firewall.php rules via environment variables or cached config:
'rules' => env('FIREWALL_RULES', []),
Middleware Integration Manually trigger checks in custom middleware:
use Akaunting\Firewall\Facades\Firewall;
public function handle($request, Closure $next) {
if (Firewall::isBlocked($request)) {
abort(403, 'Access denied by firewall.');
}
return $next($request);
}
Rate Limiting Combine with Laravel’s rate limiter:
'rules' => [
'rate-limit' => [
'max' => 100, // Requests per minute
'key' => 'ip', // 'ip' or 'user'
],
],
Geoblocking
Use geoip2 extension to block countries:
'rules' => [
'block' => [
'countries' => ['RU', 'CN'], // ISO codes
],
],
Requires geoip2/geoip2 and config:
'geoip' => [
'database' => database_path('GeoLite2-Country.mmdb'),
],
API-Specific Rules Apply rules only to API routes via route middleware:
Route::middleware(['firewall:api'])->group(function () {
// API routes here
});
Rule Precedence
allow rules override block rules. Order matters:
'rules' => [
'block' => ['ip-ranges' => ['192.168.1.0/24']],
'allow' => ['ip-ranges' => ['192.168.1.100']], // Whitelists 192.168.1.100
],
Performance Impact
'geoip' => [
'cache' => true, // Uses Laravel cache
],
Logging
'logging' => [
'enabled' => true,
'path' => storage_path('logs/firewall.log'),
],
tail -f storage/logs/firewall.log
tinker to inspect rules:
use Akaunting\Firewall\Facades\Firewall;
Firewall::getRules(); // Dump all active rules
Custom Rule Providers
Extend rule loading via FirewallServiceProvider:
public function register() {
$this->app->bind('firewall.rules', function () {
return [
'block' => ['custom' => ['your_logic_here']],
];
});
}
Event Hooks
Listen for firewall.blocked events:
Event::listen('firewall.blocked', function ($request, $rule) {
// Custom logic (e.g., notify admin)
});
Whitelisting Entire Routes
Use route middleware with except:
Route::middleware(['firewall:api'])->group(function () {
// ...
})->except(['admin.*']); // Skip firewall for admin routes
How can I help you explore Laravel packages today?