<commit_before>import ipaddress
import logging

from django.conf import settings
from django import http

logger = logging.getLogger(__name__)


class IpRestrictionMiddleware(object):
    """
    Simple middlware to allow and/or block IP addresses via settings variables ALLOWED_IPS, ALLOWED_IP_RANGES,
    BLOCKED_IPS, and BLOCKED_IP_RANGES.  Blocked IPs or ranges trump allowed ones.

    The settings must have RESTRICT_IPS = True for IP checking to perform, else the middlware does nothing.
    """

    def get_client_ip(self, request):
        x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')

        if x_forwarded_for:
            ip = x_forwarded_for.split(',')[-1].strip()
        else:
            ip = request.META.get('REMOTE_ADDR')

        return ip

    def process_request(self, request):
        if getattr(settings, 'RESTRICT_IPS', False):
            block_request = True

            request_ip_str = self.get_client_ip(request)
            request_ip = ipaddress.ip_address(request_ip_str)

            if request_ip_str in settings.ALLOWED_IPS:
                block_request = False

            for allowed_range in settings.ALLOWED_IP_RANGES:
                if request_ip in ipaddress.ip_network(allowed_range):
                    block_request = False
                    break

            if block_request:
                return http.HttpResponseForbidden('<h1>Forbidden</h1>')

        return None<commit_msg>allow access to the admin interface without ip restriction<commit_after>import ipaddress
import logging

from django.conf import settings
from django import http
from django.core.urlresolvers import resolve

logger = logging.getLogger(__name__)


class IpRestrictionMiddleware(object):
    """
    Simple middlware to allow and/or block IP addresses via settings variables ALLOWED_IPS, ALLOWED_IP_RANGES,
    BLOCKED_IPS, and BLOCKED_IP_RANGES.  Blocked IPs or ranges trump allowed ones.

    The settings must have RESTRICT_IPS = True for IP checking to perform, else the middlware does nothing.
    """

    def get_client_ip(self, request):
        x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')

        if x_forwarded_for:
            ip = x_forwarded_for.split(',')[-1].strip()
        else:
            ip = request.META.get('REMOTE_ADDR')

        return ip

    def process_request(self, request):
        if getattr(settings, 'RESTRICT_IPS', False):
            # Get the app name
            app_name = resolve(request.path).app_name

            # Allow access to the admin
            if app_name == 'admin':
                return None

            block_request = True
            # Get the incoming IP address
            request_ip_str = self.get_client_ip(request)
            request_ip = ipaddress.ip_address(request_ip_str)

            # If it's in the ALLOWED_IPS, don't block it
            if request_ip_str in settings.ALLOWED_IPS:
                block_request = False

            # If it's within a ALLOWED_IP_RANGE, don't block it
            for allowed_range in settings.ALLOWED_IP_RANGES:
                if request_ip in ipaddress.ip_network(allowed_range):
                    block_request = False
                    break

            # Otherwise, 403 Forbidden
            if block_request:
                return http.HttpResponseForbidden('<h1>Forbidden</h1>')

        return None
{'fraction_non_alphanumeric': 0.06645008860011814, 'fraction_numerical': 0.0026580035440047253, 'mean_word_length': 2.387, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2711930', 'n_tokens_mistral': 994, 'n_tokens_neox': 946, 'n_words': 261}