# 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
            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()
        if last_reload_request is not None:
            if self.restrictor.last_reload < last_reload_request:
                self.restrictor.reload_rules()


def get_setting(new_name, old_name, default=None):
    setting_name = new_name
    if hasattr(settings, old_name):
        setting_name = old_name

# 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

            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()
        if last_reload_request is not None:
            if self.restrictor.last_reload < last_reload_request:
                self.restrictor.reload_rules()


def get_setting(new_name, old_name, default=None):
    setting_name = new_name
    if hasattr(settings, old_name):
        setting_name = old_name

        warn_about_changed_setting(old_name, new_name)