def test_geo_getCountryCode_ipv6_class(self): """Should return the CC since the IP is an ``ipaddr.IPAddress``.""" cc = geo.getCountryCode(self.ipv6) self.assertIsNotNone(cc) self.assertIsInstance(cc, basestring) self.assertEqual(len(cc), 2) self.assertEqual(cc, self.expectedCC)
def withoutBlockInCountry(self, data): """Determine which countries the bridges for this **request** should not be blocked in. If :data:`addClientCountryCode` is ``True``, the the client's own geolocated country code will be added to the to the :data:`notBlockedIn` list. :param dict data: The decoded data from the JSON API request. """ countryCodes = data.get("unblocked", list()) for countryCode in countryCodes: try: country = UNBLOCKED_PATTERN.match(countryCode).group() except (TypeError, AttributeError): pass else: if country: self.notBlockedIn.append(country.lower()) logging.info("Moat request for bridges not blocked in: %r" % country) if self.addClientCountryCode: # Look up the country code of the input IP, and request bridges # not blocked in that country. if addr.isIPAddress(self.client): country = geo.getCountryCode(ipaddr.IPAddress(self.client)) if country: self.notBlockedIn.append(country.lower()) logging.info( ("Moat client's bridges also shouldn't be blocked " "in their GeoIP country code: %s") % country)
def withoutBlockInCountry(self, data): """Determine which countries the bridges for this **request** should not be blocked in. If :data:`addClientCountryCode` is ``True``, the the client's own geolocated country code will be added to the to the :data:`notBlockedIn` list. :param dict data: The decoded data from the JSON API request. """ countryCodes = data.get("unblocked", list()) for countryCode in countryCodes: try: country = UNBLOCKED_PATTERN.match(countryCode).group() except (TypeError, AttributeError): pass else: if country: self.notBlockedIn.append(country.lower()) logging.info( "Moat request for bridges not blocked in: %r" % country) if self.addClientCountryCode: # Look up the country code of the input IP, and request bridges # not blocked in that country. if addr.isIPAddress(self.client): country = geo.getCountryCode(ipaddr.IPAddress(self.client)) if country: self.notBlockedIn.append(country.lower()) logging.info( ("Moat client's bridges also shouldn't be blocked " "in their GeoIP country code: %s") % country)
def withoutBlockInCountry(self, request): """Determine which countries the bridges for this **request** should not be blocked in. .. note:: Currently, a **request** for unblocked bridges is recognized if it contains an HTTP GET parameter ``unblocked=`` whose value is a comma-separater list of two-letter country codes. Any two-letter country code found in the :api:`request <twisted.web.http.Request>` ``unblocked=`` HTTP GET parameter will be added to the :data:`notBlockedIn` list. If :data:`addClientCountryCode` is ``True``, the the client's own geolocated country code will be added to the to the :data`notBlockedIn` list. :type request: :api:`twisted.web.http.Request` :param request: A ``Request`` object containing the HTTP method, full URI, and any URL/POST arguments and headers present. """ countryCodes = request.args.get("unblocked", list()) for countryCode in countryCodes: try: country = UNBLOCKED_PATTERN.match(countryCode).group() except (TypeError, AttributeError): pass else: if country: self.notBlockedIn.append(country.lower()) logging.info( "HTTPS request for bridges not blocked in: %r" % country) if self.addClientCountryCode: # Look up the country code of the input IP, and request bridges # not blocked in that country. if addr.isIPAddress(self.client): country = geo.getCountryCode(ipaddr.IPAddress(self.client)) if country: self.notBlockedIn.append(country.lower()) logging.info( ("HTTPS client's bridges also shouldn't be blocked " "in their GeoIP country code: %s") % country)
def withoutBlockInCountry(self, request): """Determine which countries the bridges for this **request** should not be blocked in. .. note:: Currently, a **request** for unblocked bridges is recognized if it contains an HTTP GET parameter ``unblocked=`` whose value is a comma-separater list of two-letter country codes. Any two-letter country code found in the :api:`request <twisted.web.http.Request>` ``unblocked=`` HTTP GET parameter will be added to the :data:`notBlockedIn` list. If :data:`addClientCountryCode` is ``True``, the the client's own geolocated country code will be added to the to the :data`notBlockedIn` list. :type request: :api:`twisted.web.http.Request` :param request: A ``Request`` object containing the HTTP method, full URI, and any URL/POST arguments and headers present. """ countryCodes = request.args.get("unblocked", list()) for countryCode in countryCodes: try: country = UNBLOCKED_PATTERN.match(countryCode).group() except (TypeError, AttributeError): pass else: if country: self.notBlockedIn.append(country.lower()) logging.info("HTTPS request for bridges not blocked in: %r" % country) if self.addClientCountryCode: # Look up the country code of the input IP, and request bridges # not blocked in that country. if addr.isIPAddress(self.client): country = geo.getCountryCode(ipaddr.IPAddress(self.client)) if country: self.notBlockedIn.append(country.lower()) logging.info( ("HTTPS client's bridges also shouldn't be blocked " "in their GeoIP country code: %s") % country)
def resolveCountryCode(ipAddr): """Return the country code of the given IP address. :param str ipAddr: The IP address to resolve. :rtype: str :returns: A two-letter country code. """ if ipAddr is None: logging.warning("Given IP address was None. Using %s as country " "code." % UNKNOWN_CC) return UNKNOWN_CC if PROXIES is None: logging.warning("Proxies are not yet set.") elif ipAddr in PROXIES: return PROXY_CC countryCode = geo.getCountryCode(ipaddr.IPAddress(ipAddr)) # countryCode may be None if GeoIP is unable to map an IP address to a # country. return UNKNOWN_CC if countryCode is None else countryCode
def test_geo_getCountryCode_no_geoipv6(self): """When missing the geo.geoipv6 database, getCountryCode() should return None. """ geo.geoipv6 = None self.assertIsNone(geo.getCountryCode(self.ipv4))
def test_geo_getCountryCode_ipv6_no_geoip_loopback(self): """Should return None since this IP isn't geolocatable (hopefully ever).""" ipv6 = ipaddr.IPAddress('::1') self.assertIsNone(geo.getCountryCode(ipv6))
def test_geo_getCountryCode_ipv6_no_geoip_record(self): """Should return None since this IP isn't geolocatable (yet).""" ipv6 = ipaddr.IPAddress('20::72a:e224:44d8:a606:4115') self.assertIsNone(geo.getCountryCode(ipv6))
def test_geo_getCountryCode_ipv6_str(self): """Should return None since the IP isn't an ``ipaddr.IPAddress``.""" self.assertIsNone(geo.getCountryCode(str(self.ipv6)))