Skip to content

IP Banning

DjangoAPI Guard provides powerful IP banning capabilities through the IPBanManager.


Automatic IP Banning

Configure automatic IP banning based on suspicious activity:

GUARD_SECURITY_CONFIG = SecurityConfig(
    auto_ban_threshold=5,  # Ban after 5 suspicious requests
    auto_ban_duration=3600,  # Ban duration in seconds (1 hour)
)

Manual IP Banning

You can also manually ban IPs using the IPBanManager:

from djangoapi_guard import ip_ban_manager
from django.http import JsonResponse

def ban_ip(request, ip):
    duration = int(request.GET.get("duration", 3600))
    ip_ban_manager.ban_ip(ip, duration)
    return JsonResponse({"message": f"IP {ip} banned for {duration} seconds"})

Checking Ban Status

def check_ban(request, ip):
    is_banned = ip_ban_manager.is_ip_banned(ip)
    return JsonResponse({"ip": ip, "banned": is_banned})

Reset All Bans

def reset_bans(request):
    ip_ban_manager.reset()
    return JsonResponse({"message": "All IP bans cleared"})