palpalani/laravel-dns-deny-list-check
Installation:
composer require palpalani/laravel-dns-deny-list-check
Publish the config file:
php artisan vendor:publish --provider="Palpalani\DNSCheck\DNSCheckServiceProvider" --tag="config"
Configuration:
Edit config/dns-deny-list-check.php to define your blacklists (e.g., zen.spamhaus.org, bl.spamcop.net). Example:
'blacklists' => [
'zen.spamhaus.org',
'bl.spamcop.net',
],
First Use Case: Check an IP in a controller or middleware:
use Palpalani\DNSCheck\Facades\DNSCheck;
$ip = request()->ip();
$result = DNSCheck::check($ip);
if ($result->isBlacklisted()) {
// Handle blacklisted IP (e.g., log, block, or notify admin)
return response()->json(['error' => 'IP blacklisted'], 403);
}
Middleware Integration: Create a middleware to block blacklisted IPs globally:
namespace App\Http\Middleware;
use Closure;
use Palpalani\DNSCheck\Facades\DNSCheck;
class BlockBlacklistedIPs
{
public function handle($request, Closure $next)
{
$ip = $request->ip();
if (DNSCheck::check($ip)->isBlacklisted()) {
return response()->json(['error' => 'Access denied'], 403);
}
return $next($request);
}
}
Register in app/Http/Kernel.php:
protected $middleware = [
\App\Http\Middleware\BlockBlacklistedIPs::class,
];
Event-Based Checks: Trigger checks on user registration/login:
use Palpalani\DNSCheck\Facades\DNSCheck;
event(new Registered($user));
$ip = request()->ip();
if (DNSCheck::check($ip)->isBlacklisted()) {
event(new BlacklistedIPDetected($user, $ip));
}
Bulk Checking: Check multiple IPs (e.g., from a database):
$ips = User::pluck('ip_address');
$results = DNSCheck::checkMultiple($ips);
foreach ($results as $ip => $result) {
if ($result->isBlacklisted()) {
User::where('ip_address', $ip)->update(['status' => 'blacklisted']);
}
}
Caching: Enable caching in config to avoid repeated DNS lookups:
'cache' => [
'enabled' => true,
'ttl' => 3600, // 1 hour
],
Use DNSCheck::check($ip, true) to bypass cache if needed.
Custom Responses:
Extend the BlacklistResult class to add custom logic:
$result = DNSCheck::check($ip);
if ($result->isBlacklisted()) {
$blacklistDetails = $result->getBlacklistDetails();
// Log or notify with specific blacklist info
}
Queue Jobs: Offload checks to a queue for long-running processes:
CheckBlacklistedIPs::dispatch($ip)->onQueue('blacklist-checks');
DNS Resolution Delays:
'cache.enabled' => true) to mitigate.IPv6 Support:
zen.spamhaus.org) support it or pre-process IPs:$ip = filter_var(request()->ip(), FILTER_VALIDATE_IP, FILTER_FLAG_IPV4);
False Positives:
bl.spamcop.net) may flag legitimate IPs. Test thoroughly and whitelist known IPs if needed:'whitelist' => [
'192.168.1.1',
'10.0.0.0/8',
],
Rate Limiting:
Enable Logging:
Set 'debug' => true in config to log DNS queries and results to storage/logs/dns-check.log.
Manual Overrides: Temporarily disable checks for testing:
DNSCheck::setEnabled(false);
Blacklist-Specific Errors:
Check getBlacklistDetails() for granular errors (e.g., DNS resolution failures):
$result = DNSCheck::check($ip);
if ($result->hasErrors()) {
foreach ($result->getErrors() as $blacklist => $error) {
logger()->error("Blacklist {$blacklist} error: {$error}");
}
}
Custom Blacklists: Dynamically add blacklists at runtime:
DNSCheck::addBlacklist('custom.rbl.example.com');
Custom DNS Resolver: Override the default resolver (e.g., for private DNS):
DNSCheck::setResolver(function ($host) {
return dns_get_record($host, DNS_TXT);
});
Event Hooks:
Listen for blacklist events (e.g., BlacklistedIPDetected):
event(new BlacklistedIPDetected($user, $ip));
Register in EventServiceProvider:
protected $listen = [
BlacklistedIPDetected::class => [
HandleBlacklistedIP::class,
],
];
Testing: Mock DNS responses in tests:
DNSCheck::shouldReceive('resolve')
->with('zen.spamhaus.org')
->andReturn(['127.0.0.2']);
How can I help you explore Laravel packages today?