def query_freegeoip(target): Logger.log_more_verbose("Querying freegeoip.net for: " + target) try: resp = requests.get("http://freegeoip.net/json/" + target) if resp.status_code == 200: return json_decode(resp.content) Logger.log_more_verbose( "Response from freegeoip.net for %s: %s" % (target, resp.content)) except Exception: raise RuntimeError( "Freegeoip.net webservice is not available," " possible network error?" )
def recv_info(self, info): # This is where we'll collect the data we'll return. results = [] # Get the IP address or domain name. # Skip unsupported targets. if info.data_subtype == IP.data_subtype: if info.version != 4: return target = info.address parsed = netaddr.IPAddress(target) if parsed.is_loopback() or \ parsed.is_private() or \ parsed.is_link_local(): return elif info.data_subtype == Domain.data_subtype: target = info.hostname if "." not in target: return else: assert False, type(info) # Query the freegeoip.net service. # FIXME: the service supports SSL, but we need up to date certificates. Logger.log_more_verbose("Querying freegeoip.net for: " + target) resp = requests.get("http://freegeoip.net/json/" + target) if not resp.content: raise RuntimeError( "Freegeoip.net webservice is not available," " possible network error?" ) kwargs = json_decode(resp.content) # Remove the IP address from the response. address = kwargs.pop("ip") # Create a Geolocation object. geoip = Geolocation(**kwargs) geoip.add_resource(info) results.append(geoip) # Log the location. try: Logger.log_verbose("%s is in %s" % (target, geoip)) except Exception, e: fmt = traceback.format_exc() Logger.log_error("Error: %s" % str(e)) Logger.log_error_more_verbose(fmt)
def query_freegeoip(ip): Logger.log_more_verbose("Querying freegeoip.net for: " + ip) try: resp = requests.get("http://freegeoip.net/json/" + ip) if resp.status_code == 200: return json_decode(resp.content) if resp.status_code == 404: Logger.log_more_verbose( "No results from freegeoip.net for IP: " + ip) else: Logger.log_more_verbose( "Response from freegeoip.net for %s: %s" % (ip, resp.content)) except Exception: raise RuntimeError("Freegeoip.net webservice is not available," " possible network error?")