# Path: iprestrict/models.py
# TYPE_LOCATION = 'location'
# TYPE_RANGE = 'range'
#     TYPE_CHOICES = ((TYPE_LOCATION, 'Location based'),
#                     (TYPE_RANGE, 'Range based'))
#     TYPE = None
#     TYPE = TYPE_RANGE
#     TYPE = TYPE_LOCATION
#     ACTION_CHOICES = (
#         ('A', 'ALLOW'),
#         ('D', 'DENY')
#     )
# class IPGroupManager(models.Manager):
# class IPGroup(models.Model):
#     class Meta:
# class RangeBasedIPGroup(IPGroup):
#     class Meta:
# class LocationBasedIPGroup(IPGroup):
#     class Meta:
# class IPRange(models.Model):
#     class Meta:
# class IPLocation(models.Model):
#     class Meta:
# class Rule(models.Model):
#     class Meta:
# class ReloadRulesRequest(models.Model):
#     def get_queryset(self):
#     def __init__(self, *args, **kwargs):
#     def load(self):
#     def save(self, *args, **kwargs):
#     def __str__(self):
# def typed_ip_group(ip_group):
#     def load_ranges(self):
#     def ranges(self, ip_type=None):
#     def matches(self, ip):
#     def details_str(self):
#     def load_locations(self):
#     def matches(self, ip):
#     def details_str(self):
#     def start(self):
#     def end(self):
#     def ip_type(self):
#     def __contains__(self, ip):
#     def __str__(self):
#     def __contains__(self, country_code):
#     def __str__(self):
#     def __init__(self, *args, **kwargs):
#     def regex(self):
#     def matches_url(self, url):
#     def matches_ip(self, ip):
#     def is_restricted(self):
#     def is_allowed(self):
#     def action_str(self):
#     def swap_with_rule(self, other):
#     def move_up(self):
#     def move_up_url(self):
#     def move_down_url(self):
#     def move_down(self):
#     def save(self, *args, **kwargs):
#     def request_reload(cls):
#     def last_request():
# 
# Path: iprestrict/restrictor.py
# class IPRestrictor(object):
#     def __init__(self):
#     def is_restricted(self, url, ip):
#     def load_rules(self):
# 
# Path: iprestrict/middleware.py
# class IPRestrictMiddleware(MiddlewareMixin):
#     restrictor = None
#     trusted_proxies = None
#     allow_proxies = None
#     reload_rules = None
# 
#     def __init__(self, *args, **kwargs):
#         super(IPRestrictMiddleware, self).__init__(*args, **kwargs)
#         self.restrictor = IPRestrictor()
#         self.trusted_proxies = tuple(get_setting('IPRESTRICT_TRUSTED_PROXIES', 'TRUSTED_PROXIES', []))
#         self.reload_rules = get_reload_rules_setting()
#         self.ignore_proxy_header = bool(get_setting('IPRESTRICT_IGNORE_PROXY_HEADER', 'IGNORE_PROXY_HEADER', False))
#         self.trust_all_proxies = bool(get_setting('IPRESTRICT_TRUST_ALL_PROXIES', 'TRUST_ALL_PROXIES', False))
# 
#     def process_request(self, request):
#         if self.reload_rules:
#             self.reload_rules_if_needed()
# 
#         url = request.path_info
#         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()
#         if last_reload_request is not None:
#             if self.restrictor.last_reload < last_reload_request:
#                 self.restrictor.reload_rules()


from django.test import TestCase
from django.test.client import RequestFactory
from django.test.utils import override_settings
from django.core import exceptions
from iprestrict import models
from iprestrict import restrictor
from iprestrict.middleware import IPRestrictMiddleware
        from django.core.management import call_command
        create_ip_allow_rule()

    def test_reload_with_custom_command(self):
        call_command('reload_rules', verbosity=0)

        response = self.client.get('', REMOTE_ADDR = LOCAL_IP)
        self.assertEqual(response.status_code, 404)


class MiddlewareExtractClientIpTest(TestCase):
    def setUp(self):
        self.middleware = IPRestrictMiddleware()
        self.factory = RequestFactory()
 
    def test_remote_addr_only(self):
        self.middleware = IPRestrictMiddleware()
        request = self.factory.get('', REMOTE_ADDR=LOCAL_IP)

        client_ip = self.middleware.extract_client_ip(request)
        self.assertEquals(client_ip, LOCAL_IP)

    def test_remote_addr_empty(self):
        self.middleware = IPRestrictMiddleware()
        request = self.factory.get('', REMOTE_ADDR='')

        client_ip = self.middleware.extract_client_ip(request)
        self.assertEquals(client_ip, '')

    @override_settings(IPRESTRICT_TRUSTED_PROXIES=(PROXY,))
    def test_single_proxy(self):

# Path: iprestrict/models.py
# TYPE_LOCATION = 'location'
# TYPE_RANGE = 'range'
#     TYPE_CHOICES = ((TYPE_LOCATION, 'Location based'),
#                     (TYPE_RANGE, 'Range based'))
#     TYPE = None
#     TYPE = TYPE_RANGE
#     TYPE = TYPE_LOCATION
#     ACTION_CHOICES = (
#         ('A', 'ALLOW'),
#         ('D', 'DENY')
#     )
# class IPGroupManager(models.Manager):
# class IPGroup(models.Model):
#     class Meta:
# class RangeBasedIPGroup(IPGroup):
#     class Meta:
# class LocationBasedIPGroup(IPGroup):
#     class Meta:
# class IPRange(models.Model):
#     class Meta:
# class IPLocation(models.Model):
#     class Meta:
# class Rule(models.Model):
#     class Meta:
# class ReloadRulesRequest(models.Model):
#     def get_queryset(self):
#     def __init__(self, *args, **kwargs):
#     def load(self):
#     def save(self, *args, **kwargs):
#     def __str__(self):
# def typed_ip_group(ip_group):
#     def load_ranges(self):
#     def ranges(self, ip_type=None):
#     def matches(self, ip):
#     def details_str(self):
#     def load_locations(self):
#     def matches(self, ip):
#     def details_str(self):
#     def start(self):
#     def end(self):
#     def ip_type(self):
#     def __contains__(self, ip):
#     def __str__(self):
#     def __contains__(self, country_code):
#     def __str__(self):
#     def __init__(self, *args, **kwargs):
#     def regex(self):
#     def matches_url(self, url):
#     def matches_ip(self, ip):
#     def is_restricted(self):
#     def is_allowed(self):
#     def action_str(self):
#     def swap_with_rule(self, other):
#     def move_up(self):
#     def move_up_url(self):
#     def move_down_url(self):
#     def move_down(self):
#     def save(self, *args, **kwargs):
#     def request_reload(cls):
#     def last_request():
# 
# Path: iprestrict/restrictor.py
# class IPRestrictor(object):
#     def __init__(self):
#     def is_restricted(self, url, ip):
#     def load_rules(self):
# 
# Path: iprestrict/middleware.py
# class IPRestrictMiddleware(MiddlewareMixin):
#     restrictor = None
#     trusted_proxies = None
#     allow_proxies = None
#     reload_rules = None
# 
#     def __init__(self, *args, **kwargs):
#         super(IPRestrictMiddleware, self).__init__(*args, **kwargs)
#         self.restrictor = IPRestrictor()
#         self.trusted_proxies = tuple(get_setting('IPRESTRICT_TRUSTED_PROXIES', 'TRUSTED_PROXIES', []))
#         self.reload_rules = get_reload_rules_setting()
#         self.ignore_proxy_header = bool(get_setting('IPRESTRICT_IGNORE_PROXY_HEADER', 'IGNORE_PROXY_HEADER', False))
#         self.trust_all_proxies = bool(get_setting('IPRESTRICT_TRUST_ALL_PROXIES', 'TRUST_ALL_PROXIES', False))
# 
#     def process_request(self, request):
#         if self.reload_rules:
#             self.reload_rules_if_needed()
# 
#         url = request.path_info
#         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()
#         if last_reload_request is not None:
#             if self.restrictor.last_reload < last_reload_request:
#                 self.restrictor.reload_rules()


# Path: tests/test_middleware.py
from django.test import TestCase
from django.test.client import RequestFactory
from django.test.utils import override_settings
from django.core import exceptions
from iprestrict import models
from iprestrict import restrictor
from iprestrict.middleware import IPRestrictMiddleware
        from django.core.management import call_command

        create_ip_allow_rule()

    def test_reload_with_custom_command(self):
        call_command('reload_rules', verbosity=0)

        response = self.client.get('', REMOTE_ADDR = LOCAL_IP)
        self.assertEqual(response.status_code, 404)


class MiddlewareExtractClientIpTest(TestCase):
    def setUp(self):
        self.middleware = IPRestrictMiddleware()
        self.factory = RequestFactory()
 
    def test_remote_addr_only(self):
        self.middleware = IPRestrictMiddleware()
        request = self.factory.get('', REMOTE_ADDR=LOCAL_IP)

        client_ip = self.middleware.extract_client_ip(request)
        self.assertEquals(client_ip, LOCAL_IP)

    def test_remote_addr_empty(self):
        self.middleware = IPRestrictMiddleware()
        request = self.factory.get('', REMOTE_ADDR='')

        client_ip = self.middleware.extract_client_ip(request)
        self.assertEquals(client_ip, '')

    @override_settings(IPRESTRICT_TRUSTED_PROXIES=(PROXY,))
    def test_single_proxy(self):

        self.middleware = IPRestrictMiddleware()