Beispiel #1
0
    def _check_query(self,
                     query,
                     country=False,
                     city=False,
                     city_or_country=False):
        "Helper routine for checking the query and database availability."
        # Making sure a string was passed in for the query.
        if not isinstance(query, six.string_types):
            raise TypeError('GeoIP query must be a string, not type %s' %
                            type(query).__name__)

        # Extra checks for the existence of country and city databases.
        if city_or_country and not (self._country or self._city):
            raise GeoIP2Exception('Invalid GeoIP country and city data files.')
        elif country and not self._country:
            raise GeoIP2Exception('Invalid GeoIP country data file: %s' %
                                  self._country_file)
        elif city and not self._city:
            raise GeoIP2Exception('Invalid GeoIP city data file: %s' %
                                  self._city_file)

        # Return the query string back to the caller. GeoIP2 only takes IP addresses.
        if not (ipv4_re.match(query) or is_valid_ipv6_address(query)):
            query = socket.gethostbyname(query)

        return query
Beispiel #2
0
def host_represents_ip_address(host):
    """
    Returns True if the host represents an IP address, possibly with a port
    number

    >>> host_represents_ip_address('example.com')
    False
    >>> host_represents_ip_address('10.0.0.1')
    True
    >>> host_represents_ip_address('10.0.0.1:80')
    True
    >>> host_represents_ip_address('[2001:db8:85a3:8d3:1319:8a2e:370:7348]')
    True
    >>> host_represents_ip_address('[2001:db8:85a3:8d3:1319:8a2e:370::::7348]')
    False
    >>> host_represents_ip_address('[2001:db8:85a3:8d3:1319:8a2e:370:7348]:')
    False
    >>> host_represents_ip_address('[2001:db8:85a3:8d3:1319:8a2e:370:7348]:443')
    True
    """
    host_split = host.split(':')
    if len(host_split) <= 2:
        # it might be an ipv4 address with optional port number
        if ipv4_re.match(host_split[0]):
            return True
    else:
        # it might be an ipv6 address
        match = ipv6_possible_re.match(host)
        if match and is_valid_ipv6_address(match.group(1)):
            return True
    return False
Beispiel #3
0
def host_represents_ip_address(host):
    """
    Returns True if the host represents an IP address, possibly with a port
    number

    >>> host_represents_ip_address('example.com')
    False
    >>> host_represents_ip_address('10.0.0.1')
    True
    >>> host_represents_ip_address('10.0.0.1:80')
    True
    >>> host_represents_ip_address('[2001:db8:85a3:8d3:1319:8a2e:370:7348]')
    True
    >>> host_represents_ip_address('[2001:db8:85a3:8d3:1319:8a2e:370::::7348]')
    False
    >>> host_represents_ip_address('[2001:db8:85a3:8d3:1319:8a2e:370:7348]:')
    False
    >>> host_represents_ip_address('[2001:db8:85a3:8d3:1319:8a2e:370:7348]:443')
    True
    """
    host_split = host.split(':')
    if len(host_split) <= 2:
        # it might be an ipv4 address with optional port number
        if ipv4_re.match(host_split[0]):
            return True
    else:
        # it might be an ipv6 address
        match = ipv6_possible_re.match(host)
        if match and is_valid_ipv6_address(match.group(1)):
            return True
    return False
Beispiel #4
0
 def country_name(self, query):
     "Returns the country name for the given IP Address or FQDN."
     enc_query = self._check_query(query, city_or_country=True)
     if self._country:
         if ipv4_re.match(query):
             return GeoIP_country_name_by_addr(self._country, enc_query)
         else:
             return GeoIP_country_name_by_name(self._country, enc_query)
     else:
         return self.city(query)['country_name']
Beispiel #5
0
 def country_name(self, query):
     "Returns the country name for the given IP Address or FQDN."
     enc_query = self._check_query(query, city_or_country=True)
     if self._country:
         if ipv4_re.match(query):
             return GeoIP_country_name_by_addr(self._country, enc_query)
         else:
             return GeoIP_country_name_by_name(self._country, enc_query)
     else:
         return self.city(query)['country_name']
Beispiel #6
0
 def city(self, query):
     """
     Returns a dictionary of city information for the given IP address or
     Fully Qualified Domain Name (FQDN).  Some information in the dictionary
     may be undefined (None).
     """
     enc_query = self._check_query(query, city=True)
     if ipv4_re.match(query):
         # If an IP address was passed in
         return GeoIP_record_by_addr(self._city, c_char_p(enc_query))
     else:
         # If a FQDN was passed in.
         return GeoIP_record_by_name(self._city, c_char_p(enc_query))
Beispiel #7
0
 def city(self, query):
     """
     Returns a dictionary of city information for the given IP address or
     Fully Qualified Domain Name (FQDN).  Some information in the dictionary
     may be undefined (None).
     """
     enc_query = self._check_query(query, city=True)
     if ipv4_re.match(query):
         # If an IP address was passed in
         return GeoIP_record_by_addr(self._city, c_char_p(enc_query))
     else:
         # If a FQDN was passed in.
         return GeoIP_record_by_name(self._city, c_char_p(enc_query))
Beispiel #8
0
def get_ip(request):
    """
    Retrieves the remote IP address from the request data.  If the user is
    behind a proxy, they may have a comma-separated list of IP addresses, so
    we need to account for that.  In such a case, only the first IP in the
    list will be retrieved.  Also, some hosts that use a proxy will put the
    REMOTE_ADDR into HTTP_X_FORWARDED_FOR.  This will handle pulling back the
    IP from the proper place.

    """
    ip = request.META.get('HTTP_X_FORWARDED_FOR',
                          request.META.get('REMOTE_ADDR', '127.0.0.1'))
    ip = ipv4_re.match(ip)
    if not ip:
        return None
    return ip.group(0)
Beispiel #9
0
    def _check_query(self, query, country=False, city=False, city_or_country=False):
        "Helper routine for checking the query and database availability."
        # Making sure a string was passed in for the query.
        if not isinstance(query, six.string_types):
            raise TypeError("GeoIP query must be a string, not type %s" % type(query).__name__)

        # Extra checks for the existence of country and city databases.
        if city_or_country and not (self._country or self._city):
            raise GeoIP2Exception("Invalid GeoIP country and city data files.")
        elif country and not self._country:
            raise GeoIP2Exception("Invalid GeoIP country data file: %s" % self._country_file)
        elif city and not self._city:
            raise GeoIP2Exception("Invalid GeoIP city data file: %s" % self._city_file)

        # Return the query string back to the caller. GeoIP2 only takes IP addresses.
        if not (ipv4_re.match(query) or is_valid_ipv6_address(query)):
            query = socket.gethostbyname(query)

        return query
Beispiel #10
0
    def from_request(self, request, video):
        """
        Creates a Watch based on an HTTP request.  If the request came
        from localhost, check to see if it was forwarded to (hopefully) get the
        right IP address.

        """
        user_agent = request.META.get('HTTP_USER_AGENT', '')

        ip = request.META.get('REMOTE_ADDR', '0.0.0.0')
        if not ipv4_re.match(ip):
            ip = '0.0.0.0'

        if hasattr(request, 'user') and request.user.is_authenticated():
            user = request.user
        else:
            user = None

        self.create(video=video,
                    user=user,
                    ip_address=ip,
                    user_agent=user_agent)
Beispiel #11
0
        # Making sure a string was passed in for the query.
        if not isinstance(query, str):
>>>>>>> 37c99181c9a6b95433d60f8c8ef9af5731096435
            raise TypeError('GeoIP query must be a string, not type %s' % type(query).__name__)

        # Extra checks for the existence of country and city databases.
        if city_or_country and not (self._country or self._city):
            raise GeoIP2Exception('Invalid GeoIP country and city data files.')
        elif country and not self._country:
            raise GeoIP2Exception('Invalid GeoIP country data file: %s' % self._country_file)
        elif city and not self._city:
            raise GeoIP2Exception('Invalid GeoIP city data file: %s' % self._city_file)

        # Return the query string back to the caller. GeoIP2 only takes IP addresses.
<<<<<<< HEAD
        if not (ipv4_re.match(query) or is_valid_ipv6_address(query)):
=======
        try:
            validate_ipv46_address(query)
        except ValidationError:
>>>>>>> 37c99181c9a6b95433d60f8c8ef9af5731096435
            query = socket.gethostbyname(query)

        return query

    def city(self, query):
        """
        Return a dictionary of city information for the given IP address or
        Fully Qualified Domain Name (FQDN). Some information in the dictionary
        may be undefined (None).
        """
Beispiel #12
0
import os
Beispiel #13
0
    def clean(self):
        "validate the form"
        cleaned_data = self.cleaned_data
        submited_field = cleaned_data.get('filtered_field')
        submited_by = int(cleaned_data.get('filtered_by'))
        submited_value = cleaned_data.get('filtered_value')
        if submited_by != 0:
            sbi = (submited_by - 1)
        else:
            sbi = submited_by

        if submited_field in BOOL_FIELDS:
            if not submited_by in BOOL_FILTER:
                filter_items = dict(FILTER_ITEMS)
                error_msg = _("%(field)s does not support the %(filt)s filter") % {
                'field': filter_items[submited_field], 'filt': FILTER_BY[sbi][1]}
                raise forms.ValidationError(error_msg)
        if submited_field in NUM_FIELDS:
            if not submited_by in NUM_FILTER:
                filter_items = dict(FILTER_ITEMS)
                error_msg = _("%(field)s does not support the %(filt)s filter") % {
                'field': filter_items[submited_field], 'filt': FILTER_BY[sbi][1]}
                raise forms.ValidationError(error_msg)
            if submited_value in EMPTY_VALUES:
                raise forms.ValidationError(_("Please supply a value to query"))
            if not isnumeric(submited_value):
                raise forms.ValidationError(_("The value has to be numeric"))
        if submited_field in TEXT_FIELDS:
            if not submited_by in TEXT_FILTER:
                filter_items = dict(FILTER_ITEMS)
                error_msg = _("%(field)s does not support the %(filt)s filter") % {
                'field': filter_items[submited_field], 'filt': FILTER_BY[sbi][1]}
                raise forms.ValidationError(error_msg)
            if submited_value in EMPTY_VALUES and submited_by not in [9, 10]:
                raise forms.ValidationError(_("Please supply a value to query"))
            if ((submited_field == 'from_address' or
                submited_field == 'to_address') and
                submited_by in [1, 2]):
                if not email_re.match(submited_value.strip()):
                    raise forms.ValidationError(
                        _('%(email)s is not a valid e-mail address.')
                        % {'email': force_escape(submited_value)})
            else:
                if submited_by in [7, 8]:
                    try:
                        re.compile(submited_value)
                    except re.error:
                        raise forms.ValidationError(
                            _("Please provide a valid regex")
                        )
            if ((submited_field == 'from_domain' or
                submited_field == 'to_domain') and
                submited_by in [1, 2]):
                if not DOM_RE.match(submited_value.strip()):
                    raise forms.ValidationError(
                        _('Please provide a valid domain name'))
            else:
                if submited_by in [7, 8]:
                    try:
                        re.compile(submited_value)
                    except re.error:
                        raise forms.ValidationError(
                            _("Please provide a valid regex")
                        )
            if submited_field == 'clientip':
                if not ipv4_re.match(submited_value.strip()):
                    raise forms.ValidationError(
                        _('Please provide a valid ipv4 address'))
            if submited_field == 'hostname':
                if ((not ipv4_re.match(submited_value.strip())) and
                (not DOM_RE.match(submited_value.strip()))):
                    raise forms.ValidationError(
                    _("Please provide a valid hostname or ipv4 address")
                    )
        if submited_field in TIME_FIELDS:
            if not submited_by in TIME_FILTER:
                filter_items = dict(FILTER_ITEMS)
                error_msg = _("%(field)s does not support the %(filt)s filter") % {
                'field': filter_items[submited_field], 'filt': FILTER_BY[sbi][1]}
                raise forms.ValidationError(error_msg)
            if submited_value in EMPTY_VALUES:
                raise forms.ValidationError(_("Please supply a value to query"))
            if submited_field == 'date':
                try:
                    datetime.date(
                        *time.strptime(submited_value, '%Y-%m-%d')[:3])
                except ValueError:
                    raise forms.ValidationError(
                        _('Please provide a valid date in YYYY-MM-DD format'))
            if submited_field == 'time':
                try:
                    datetime.time(*time.strptime(submited_value, '%H:%M')[3:6])
                except ValueError:
                    raise forms.ValidationError(
                        _('Please provide valid time in HH:MM format'))

        return cleaned_data
Beispiel #14
0
import os