# Path: iprestrict/models.py
# class ReloadRulesRequest(models.Model):
#     at = models.DateTimeField(auto_now_add=True)
# 
#     @classmethod
#     def request_reload(cls):
#         rrs = ReloadRulesRequest.objects.all()
#         if len(rrs) > 0:
#             obj = rrs[0]
#             obj.at = timezone.now()
#             obj.save()
#         else:
#             cls.objects.create()
# 
#     @staticmethod
#     def last_request():
#         result = None
#         rrs = ReloadRulesRequest.objects.all()
#         if len(rrs) > 0:
#             result = rrs[0].at
#         return result
# 
# Path: iprestrict/restrictor.py
# class IPRestrictor(object):
#     rules = None
#     last_reload = None
# 
#     def __init__(self):
#         self.load_rules()
# 
#     def is_restricted(self, url, ip):
#         for rule in self.rules:
#             if rule.matches_url(url) and rule.matches_ip(ip):
#                 return rule.is_restricted()
#         return False
# 
#     def load_rules(self):
#         # We are caching the rules, to avoid DB lookup on each request
#         from .models import Rule
#         self.rules = list(Rule.objects.all())
#         self.last_reload = timezone.now()
# 
#     reload_rules = load_rules


from django.core import exceptions
from django.conf import settings
from .models import ReloadRulesRequest
from .restrictor import IPRestrictor
    from django.utils.deprecation import MiddlewareMixin
import logging
import warnings
        client_ip = self.extract_client_ip(request)

        if self.restrictor.is_restricted(url, client_ip):
            logger.warn("Denying access of %s to %s" % (url, client_ip))
            raise exceptions.PermissionDenied

    def extract_client_ip(self, request):
        client_ip = request.META['REMOTE_ADDR']
        if not self.ignore_proxy_header:
            forwarded_for = self.get_forwarded_for(request)
            if forwarded_for:
                closest_proxy = client_ip
                client_ip = forwarded_for.pop(0)
                if self.trust_all_proxies:
                    return client_ip
                proxies = [closest_proxy] + forwarded_for
                for proxy in proxies:
                    if proxy not in self.trusted_proxies:
                        logger.warn("Client IP %s forwarded by untrusted proxy %s" % (client_ip, proxy))
                        raise exceptions.PermissionDenied
        return client_ip

    def get_forwarded_for(self, request):
        hdr = request.META.get('HTTP_X_FORWARDED_FOR')
        if hdr is not None:
            return [ip.strip() for ip in hdr.split(',')]
        else:
            return []

    def reload_rules_if_needed(self):

# Path: iprestrict/models.py
# class ReloadRulesRequest(models.Model):
#     at = models.DateTimeField(auto_now_add=True)
# 
#     @classmethod
#     def request_reload(cls):
#         rrs = ReloadRulesRequest.objects.all()
#         if len(rrs) > 0:
#             obj = rrs[0]
#             obj.at = timezone.now()
#             obj.save()
#         else:
#             cls.objects.create()
# 
#     @staticmethod
#     def last_request():
#         result = None
#         rrs = ReloadRulesRequest.objects.all()
#         if len(rrs) > 0:
#             result = rrs[0].at
#         return result
# 
# Path: iprestrict/restrictor.py
# class IPRestrictor(object):
#     rules = None
#     last_reload = None
# 
#     def __init__(self):
#         self.load_rules()
# 
#     def is_restricted(self, url, ip):
#         for rule in self.rules:
#             if rule.matches_url(url) and rule.matches_ip(ip):
#                 return rule.is_restricted()
#         return False
# 
#     def load_rules(self):
#         # We are caching the rules, to avoid DB lookup on each request
#         from .models import Rule
#         self.rules = list(Rule.objects.all())
#         self.last_reload = timezone.now()
# 
#     reload_rules = load_rules


# Path: iprestrict/middleware.py
from django.core import exceptions
from django.conf import settings
from .models import ReloadRulesRequest
from .restrictor import IPRestrictor
    from django.utils.deprecation import MiddlewareMixin
import logging
import warnings

        client_ip = self.extract_client_ip(request)

        if self.restrictor.is_restricted(url, client_ip):
            logger.warn("Denying access of %s to %s" % (url, client_ip))
            raise exceptions.PermissionDenied

    def extract_client_ip(self, request):
        client_ip = request.META['REMOTE_ADDR']
        if not self.ignore_proxy_header:
            forwarded_for = self.get_forwarded_for(request)
            if forwarded_for:
                closest_proxy = client_ip
                client_ip = forwarded_for.pop(0)
                if self.trust_all_proxies:
                    return client_ip
                proxies = [closest_proxy] + forwarded_for
                for proxy in proxies:
                    if proxy not in self.trusted_proxies:
                        logger.warn("Client IP %s forwarded by untrusted proxy %s" % (client_ip, proxy))
                        raise exceptions.PermissionDenied
        return client_ip

    def get_forwarded_for(self, request):
        hdr = request.META.get('HTTP_X_FORWARDED_FOR')
        if hdr is not None:
            return [ip.strip() for ip in hdr.split(',')]
        else:
            return []

    def reload_rules_if_needed(self):

        last_reload_request = ReloadRulesRequest.last_request()