class ip2city(object): stats = { 'requests': 0, 'successes': 0, 'errors': 0, } geoip_v4 = None geoip_v6 = None def __init__(self, database, database_v6): self.url_map = Map([ Rule('/', endpoint='resolve'), Rule('/favicon.ico', endpoint='favicon'), Rule('/status', endpoint='status'), ]) self.geoip_v4 = GeoIP(database, MEMORY_CACHE) if database_v6: self.geoip_v6 = GeoIP(database_v6, MEMORY_CACHE) # Serve empty favicon.ico def on_favicon(self, request): return Response() def on_status(self, request): response = self.stats response['status'] = 'Working for you.' return Response(json.dumps(response)) def on_resolve(self, request): ip = request.args.get('ip') self.stats['requests'] += 1 record = {} try: if ':' in ip: if self.geoip_v6: record = self.geoip_v6.record_by_addr(ip) self.stats['successes'] += 1 else: self.stats['errors'] += 1 else: record = self.geoip_v4.record_by_addr(ip) self.stats['successes'] += 1 except GeoIPError, e: print e self.stats['errors'] += 1 return Response(json.dumps(record))
def IPToLocation(ipaddr): from ooni.settings import config city_file = config.get_data_file_path('GeoIP/GeoLiteCity.dat') country_file = config.get_data_file_path('GeoIP/GeoIP.dat') asn_file = config.get_data_file_path('GeoIP/GeoIPASNum.dat') location = {'city': None, 'countrycode': 'ZZ', 'asn': 'AS0'} def error(): log.err("Could not find GeoIP data file in %s." "Try running ooniresources --update-geoip or" " edit your ooniprobe.conf" % config.advanced.geoip_data_dir) try: country_dat = GeoIP(country_file) location['countrycode'] = country_dat.country_code_by_addr(ipaddr) if not location['countrycode']: location['countrycode'] = 'ZZ' except IOError: error() try: city_dat = GeoIP(city_file) location['city'] = city_dat.record_by_addr(ipaddr)['city'] except: error() try: asn_dat = GeoIP(asn_file) location['asn'] = asn_dat.org_by_addr(ipaddr).split(' ')[0] except: error() return location
def get_weather(request): """Return weather of location from IP address. https://pypi.python.org/pypi/django-ipware https://pypi.python.org/pypi/pygeoip https://pypi.python.org/pypi/python-forecastio """ ip = get_real_ip(request) if ip is not None: gi = GeoIP(settings.GEOIP_DATABASE, MEMORY_CACHE) record = gi.record_by_addr(ip) if record is not None: try: latitude = record['latitude'] longitude = record['longitude'] except KeyError: return None try: forecast = forecastio.load_forecast(settings.DARKSKY_API_KEY, latitude, longitude) currently = forecast.currently() except Exception: return None return { 'icon': weather_icons.get(currently.icon, None), 'summary': currently.summary.lower(), 'temperature': int(currently.temperature), 'city': record.get('city', None), 'country': record.get('country_name', None), } else: return None else: return None
def IPToLocation(ipaddr): city_file = os.path.join(config.advanced.geoip_data_dir, 'GeoLiteCity.dat') country_file = os.path.join(config.advanced.geoip_data_dir, 'GeoIP.dat') asn_file = os.path.join(config.advanced.geoip_data_dir, 'GeoIPASNum.dat') location = {'city': None, 'countrycode': None, 'asn': None} try: city_dat = GeoIP(city_file) try: location['city'] = city_dat.record_by_addr(ipaddr)['city'] except TypeError: location['city'] = None country_dat = GeoIP(country_file) location['countrycode'] = country_dat.country_code_by_addr(ipaddr) if not location['countrycode']: location['countrycode'] = 'ZZ' asn_dat = GeoIP(asn_file) try: location['asn'] = asn_dat.org_by_addr(ipaddr).split(' ')[0] except AttributeError: location['asn'] = 'AS0' except IOError: log.err("Could not find GeoIP data files. Go into %s " "and run make geoip or change the geoip_data_dir " "in the config file" % config.advanced.geoip_data_dir) raise GeoIPDataFilesNotFound return location
def scanit(self): #funtion to scan on record results """ scans a target, the target is taken from the TK box target :return: nothing """ gic = GeoIP(GEOIP_DATABASE) #change this to the location of GeoLiteCity.dat target = self.textbox.get() if target != '' and target != 'IP or Hostname': #error checking (not empty string) try: target_ip = socket.gethostbyname(target) #attempts to get an ip from hostnam/ip passed through gic = GeoIP(GEOIP_DATABASE) #load geoIP database addr = gic.record_by_addr(target_ip) #if this works (getting ip) find address of ip lat = addr['latitude'] lng = addr['longitude'] htmlPath = TARGET_HTML + target_ip + '.html' mymap = maps(lat,lng,16) #create google map file mymap.addradpoint(lat, lng, 100, "#0000FF") mymap.draw(htmlPath) # TODO: maybe add this back later... # if lng != 0 and lat != 0: # webbrowser.open(htmlPath) self.nmap_scan(target_ip, [lat,lng]) except socket.gaierror: #if finding IP fails print 'ERROR: Counld not get ip from hostname' except TypeError: #if ip has no address (like 127.0.0.1) set lat and lng to 0,0 # TODO: make more graceful print 'Could not get coordinates from GeoIP Database.'
class GeoIPParser(ExtractFieldParser): ''' Get geo info from IP address. ''' def __init__(self, field='clientip', out_field='geoip', geoip_dat='', use_hash=True, *args, **kwargs): super(GeoIPParser, self).__init__(field, *args, **kwargs) self.out_field = out_field self.get_loc = _loc_geohash if HAS_GEOHASH and use_hash else _loc_point try: self.geoip = GeoIP(geoip_dat) except (IOError, GeoIPError) as exc: self.logger.error('Invalid GeoIP Database file: %s', geoip_dat) raise exc def parse(self, event): ip_addr = self.data try: geo_info = self.geoip.record_by_addr(ip_addr) if 'latitude' in geo_info and 'longitude' in geo_info: geo_info['location'] = self.get_loc(geo_info) event[self.out_field] = geo_info except (IndexError, TypeError): self.logger.warn('Failed to get Geo info from ip: %s', ip_addr) return event
def IPToLocation(ipaddr): from ooni.settings import config city_file = os.path.join(config.advanced.geoip_data_dir, 'GeoLiteCity.dat') country_file = os.path.join(config.advanced.geoip_data_dir, 'GeoIP.dat') asn_file = os.path.join(config.advanced.geoip_data_dir, 'GeoIPASNum.dat') location = {'city': None, 'countrycode': 'ZZ', 'asn': 'AS0'} try: country_dat = GeoIP(country_file) location['countrycode'] = country_dat.country_code_by_addr(ipaddr) if not location['countrycode']: location['countrycode'] = 'ZZ' except IOError: log.err("Could not find GeoIP data file. Go into %s " "and make sure GeoIP.dat is present or change the location " "in the config file" % config.advanced.geoip_data_dir) try: city_dat = GeoIP(city_file) location['city'] = city_dat.record_by_addr(ipaddr)['city'] except: log.err("Could not find the city your IP is from. " "Download the GeoLiteCity.dat file into the geoip_data_dir" " or install geoip-database-contrib.") try: asn_dat = GeoIP(asn_file) location['asn'] = asn_dat.org_by_addr(ipaddr).split(' ')[0] except: log.err("Could not find the ASN for your IP. " "Download the GeoIPASNum.dat file into the geoip_data_dir" " or install geoip-database-contrib.") return location
def perform(self): ''' Main method of the class that perform all actions. ''' # Define list of coordinates coords = [] # Open file for reading with open(self.dict_file, "r") as lines: try: for line in lines.readlines(): fulldomain = line.rstrip() + "." + self.domain try: # Get the A target and preference of a name answers = dns.resolver.query(fulldomain, 'A') if type(answers) == dns.resolver.Answer: # Iterate through answers and getting data. for rdata in answers: ip = rdata.address # Create instance of GeoIP class gi = GeoIP(self.dat_file) # Call method record_by_addr go = gi.record_by_addr(ip) # Get latitude and longitude from DNS answer coord = (go['latitude'], go['longitude']) coords.append([fulldomain, coord]) except: pass # The query name does not exist. except (dns.exception.DNSException): pass # Call method for generate KML file self.to_kml(coords)
def IP_by_DataBase(ip): """ Retorna as iformações de geo posicionamento atraver da base de dados local Disponivel no site http://appliedsec.github.com/pygeoip/ Data Base http://dev.maxmind.com/geoip/geolite """ gi = GeoIP(PATH_GEOIP_CITY) return gi.record_by_addr(ip) or {}
def add_geo(nodes): from pygeoip import GeoIP gi = GeoIP(GEODB) for k, v in nodes.items(): try: nodes[k].update(gi.record_by_addr(v["external-ip"])) except Exception as e: sys.stderr.write(str(e)) sys.stderr.write("Cannot determine GeoData for %s\n" % k) return nodes
def add_geo(nodes): from pygeoip import GeoIP gi = GeoIP(GEODB) for k,v in nodes.items(): try: nodes[k].update(gi.record_by_addr(v["external-ip"])) except Exception as e: sys.stderr.write(str(e)) sys.stderr.write("Cannot determine GeoData for %s\n"%k) return nodes
def add_coords_to_edges(nodes): from pygeoip import GeoIP gi = GeoIP(GEODB) for k,v in nodes.items(): for i,j in enumerate(v.get("to",[])): data=gi.record_by_addr(j["addr"]) try: j["latitude"]=data["latitude"] j["longitude"]=data["longitude"] except Exception as e: pass return nodes
def add_coords_to_edges(nodes): from pygeoip import GeoIP gi = GeoIP(GEODB) for k, v in nodes.items(): for i, j in enumerate(v.get("to", [])): data = gi.record_by_addr(j["addr"]) try: j["latitude"] = data["latitude"] j["longitude"] = data["longitude"] except Exception as e: pass return nodes
def location(): callback = request.args.get('callback', False); ip = request.args.get('ip', client_ip()); gi = GeoIP('data/GeoLiteCity.dat') geodata = gi.record_by_addr(ip); geodata['ip_address'] = ip; geodata = json.dumps(geodata) if callback: content = str(callback) + '(' + str(geodata) + ')' else: content = geodata return current_app.response_class(content, mimetype='application/json')
def get(self, request, *args, **kwargs): ip = request.META.get('X-FORWARDED-FOR') if not ip: ip = request.META['REMOTE_ADDR'] if ip[:3] == "127" or ip[:8] == "168.192." or ip[:10] == "192.168.0.": ip = "201.76.161.146" geo = GeoIP("geoip/GeoLiteCity.dat") region = geo.record_by_addr(ip) return http.HttpResponse(dumps(region, cls=DjangoJSONEncoder), content_type='application/json')
def __init__(self, ip): class CityNotDetected(Exception): pass try: # если не удалось определить через ipgeo # то определяем через pygeoip geo = GeoIP(settings.GEO_CITY_DAT_FILE) location = geo.record_by_addr(ip) if not location: raise CityNotDetected( 'не удалось определить через pygeoip' ) city_name = location['city'] longitude = location['longitude'] latitude = location['latitude'] if location['country_name'] == 'Russian Federation': try: # определяем положение через ipgeo geo = Range.objects.find(ip) if not geo.location: raise CityNotDetected( 'Не удалось определить через ipgeo' ) city_name = geo.location.name longitude = geo.location.lon latitude = geo.location.lat except (CityNotDetected, Range.DoesNotExist): pass self.set_location( city=city_name, longitude=longitude, latitude=latitude, timezone=location['time_zone'] ) except CityNotDetected: # если ничего не определили - считаем что позиция мск self.set_location( city='Москва', longitude=settings.MSK_LONGITUDE, latitude=settings.MSK_LATITUDE, timezone='Europe/Moscow' )
def events(request): lat = request.GET.get("lat") lon = request.GET.get("lon") if not lat or not lon: raise Http404 if request.GET.get("provider"): providers = [request.GET.get("provider")] else: providers = PROVIDERS cur_time = int(time.time()) gi = GeoIP(settings.GEOCITYFILE,pygeoip.STANDARD) if request.META['REMOTE_ADDR']=='127.0.0.1': ip = '64.134.231.43' else: ip = request.META['REMOTE_ADDR'] timezone = pytz.timezone(gi.record_by_addr(ip)['time_zone']) local_time = timezone.localize(datetime.datetime.now()) num_results = int(request.GET.get("num_results")) if request.GET.get("num_results") else 10 offset = int(request.GET.get("offset")) if request.GET.get("offset") else 0 cache_key = "%s%s" % (int(float(lat)*100)/100.00,int(float(lon)*100)/100.00) cached_value = cache.get(cache_key) if cached_value: return HttpResponse(json.dumps({'results':cached_value[offset*num_results:num_results*(offset+1)]})) threads = [Greenlet.spawn(provider_request_map[provider],lat,lon,cur_time,local_time,timezone) for provider in providers] results = [] map(results.extend,[g.get() for g in threads]) results.sort(key = lambda d: d['start_time']) cache.set(cache_key,results,60*10) return HttpResponse(json.dumps({'results':results[offset*num_results:num_results*(offset+1)]}))
def IPToLocation(ipaddr): city_file = os.path.join(config.advanced.geoip_data_dir, 'GeoLiteCity.dat') country_file = os.path.join(config.advanced.geoip_data_dir, 'GeoIP.dat') asn_file = os.path.join(config.advanced.geoip_data_dir, 'GeoIPASNum.dat') location = {'city': None, 'countrycode': None, 'asn': None} try: city_dat = GeoIP(city_file) location['city'] = city_dat.record_by_addr(ipaddr)['city'] country_dat = GeoIP(country_file) location['countrycode'] = country_dat.country_code_by_addr(ipaddr) asn_dat = GeoIP(asn_file) location['asn'] = asn_dat.org_by_addr(ipaddr).split(' ')[0] except IOError: log.err("Could not find GeoIP data files. Go into data/ " "and run make geoip") raise GeoIPDataFilesNotFound return location
def get_geolocation(self, user): if not user.ip: self.parent.librewired.getUserInfo(user.id) if not user.ip: return 0 from os.path import exists dbpath = "GeoLiteCity.dat" if not exists(dbpath): return 0 location = 0 gloc = GeoIP(dbpath) data = gloc.record_by_addr(user.ip) try: geodata = { 'city': data['city'].encode("utf-8"), 'country_code': data['country_code'].encode("utf-8"), 'country_name': data['country_name'].encode("utf-8"), 'time_zone': data['time_zone'].encode("utf-8"), } except: return 0 return geodata
def traceIP(target): try: base = GeoIP('GeoLiteCity.dat') data = base.record_by_addr(target) dnsName = socket.gethostbyaddr(target)[0] formatedData = '''IP: {} City: {} State/Province: {} Country: {} Continent: {} Zip/Postal code: {} Timezone: {} Latitude: {} Longitude: {} DNS name: {}'''.format(target, data['city'], data['region_code'], data['country_name'], data['continent'], data['postal_code'], data['time_zone'], str(data['latitude']), str(data['longitude']), dnsName) print formatedData # compares target to database and print results to console askSave = raw_input('Save data? Y/n: ').lower() if askSave == 'y': ipFileName = raw_input('Filename: ') with open(ipFileName, 'w') as fileName: fileName.write(formatedData) print 'Output saved as {}'.format(ipFileName) else: pass # asks user if they want to save the output pause() main() except socket.herror: pass
class RootWidget(BoxLayout): # these link back to the kv file user_input = ObjectProperty(None) target_str = ObjectProperty(None) out_key = ObjectProperty(None) out_val = ObjectProperty(None) # regex's match ip address or domain name respectively ip = compile(r'(?:\d+\.?){4}') addr = compile(r'([a-z]+://)?(www\.)?[^ ]+\.[a-z.]*') def __init__(self, **kwargs): ''' Initialises the GeoLiteCity database ''' super(RootWidget, self).__init__(**kwargs) self.gip = GeoIP('GeoLiteCity.dat') def get_info(self): ''' main program logic ''' # get user input address = self.user_input.text.lower().strip() host_name = '' # test for matches is_ip = search(self.ip, address) is_addr = search(self.addr, address) # deal with what we matched if is_ip: try: target = is_ip.string ip_info = self.gip.record_by_addr(target) # if is_ip, try to resolve host name try: host_name = gethostbyaddr(target)[0] print host_name except: pass except Exception: self.target_str.text = "[color=#FF6600][b]Can't Connect..[/b][/color]" self.clear_labels() return elif is_addr: try: target = is_addr.string ip_info = self.gip.record_by_name(target) # if is_addr, try to resolve IP try: host_name = gethostbyname(target) except: pass except Exception, e: self.target_str.text = "[color=#FF6600][b]Can't Connect..[/b][/color]" self.clear_labels() return else:
gip = GeoIP(config.CONFIG['IP_DB'], MMAP_CACHE) IPV4RE = re.compile(r"(\d{1,3}(\.|$)){4}") for visit in Visit.all.execute(): lat = None lng = None country_code = None ip = visit['ip'] ip = ip.replace('::ffff:', '') if IPV4RE.match(ip): if (ip == '127.0.0.1' or ip.startswith('192.168.') or ip.startswith('10.')): city = 'Local' country = 'Local' else: location = gip.record_by_addr(ip) city = (location.get('city', 'Unknown').decode('iso-8859-1') if location else 'Unknown') country = (location.get('country_name', 'Unknown').decode('iso-8859-1') if location else 'Unknown') country_code = (location.get('country_code', 'Unknown').decode('iso-8859-1') if location else 'Unknown') lat = location.get('latitude', None) lng = location.get('longitude', None) else: country = 'ipv6' city = 'ipv6' visit['country'] = country visit['country_code'] = country_code
def get_geo_record(ip): gi = GeoIP(GEO_LITE_CITY_FILE, pygeoip.STANDARD) x = gi.record_by_addr(ip) return x
def hit(request): stable_omero_downloads = 'http://downloads.openmicroscopy.org/latest-stable/omero' agent = None try: agt = request.META.get('HTTP_USER_AGENT', '') if agt is not None and agt.startswith("OMERO."): try: agent = Agent.objects.get(agent_name=agt) except Agent.DoesNotExist: return HttpResponseRedirect(UPGRADE_CHECK_URL) except: logger.error(traceback.format_exc()) return HttpResponseRedirect(UPGRADE_CHECK_URL) else: return HttpResponseRedirect(UPGRADE_CHECK_URL) except: logger.error(traceback.format_exc()) return HttpResponseRedirect(UPGRADE_CHECK_URL) logger.debug("Agent %s" % agent) agent_version = '' update = None try: agent_version = request.REQUEST.get('version') ver = Version.objects.get(pk=1) if agent_version is not None: try: regex = re.compile("^.*?[-]?(\\d+[.]\\d+([.]\\d+)?)[-]?.*?$") agent_cleaned = regex.match(agent_version).group(1) agent_split = agent_cleaned.split(".") local_cleaned = regex.match(ver.version).group(1) local_split = local_cleaned.split(".") rv = (agent_split < local_split) except: rv = True if rv: update = 'Please upgrade to %s. See %s for the latest version.' % (ver, stable_omero_downloads) else: update = 'Please upgrade to %s. See %s for the latest version.' % (ver, stable_omero_downloads) except: logger.debug(traceback.format_exc()) logger.debug("Agent version %s" % agent_version) ip = None try: real_ip = None try: # HTTP_X_FORWARDED_FOR can be a comma-separated list of IPs. The # client's IP will be the first one. # http://code.djangoproject.com/ticket/3872 real_ip = request.META['HTTP_X_FORWARDED_FOR'] logger.debug("HTTP_X_FORWARDED_FOR: %s" % real_ip) real_ip = real_ip.split(",")[-1].strip() except KeyError: real_ip = request.META.get('REMOTE_ADDR') if real_ip is not None: try: ip = IP.objects.get(ip=real_ip) except IP.DoesNotExist: latitude = None longitude = None country = None geoip = GeoIP(settings.GEODAT, STANDARD) gir = geoip.record_by_addr(real_ip) if gir is not None: latitude = gir["latitude"] longitude = gir["longitude"] geoip = GeoIP(settings.GEOIP, MEMORY_CACHE) country = geoip.country_name_by_addr(real_ip) logger.debug("IP: %s, latitude: '%s', longitude: '%s'" % (real_ip, latitude, longitude)) ip = IP(ip=real_ip, latitude=latitude, longitude=longitude, country=country) ip.save() except Exception, x: logger.debug(traceback.format_exc()) raise x
def index(): res_data = {} verbose = False prettyprint = False try: client_ip = request.remote_route[0] except: client_ip = request.environ['REMOTE_ADDR'] pass args = request.query_string.split('&') if 'v' in args: verbose = True if 'pp' in args: prettyprint = True res_data['ip'] = client_ip try: ua = request.environ['HTTP_USER_AGENT'] user_agent = user_agents.parse(ua) res_data['user_agent'] = str(user_agent) except Exception as e: user_agent = False pass if user_agent and not verbose: if user_agent.browser.family == 'Other': response.set_header('Content-type', 'text/plain') return res_data['ip'] + '\r\n' try: ip = GeoIP(config.get('app', 'geoip_database')) res_data['country'] = ip.country_name_by_addr(client_ip) res_data['city'] = ip.record_by_addr(client_ip) res_data['asn'] = ip.asn_by_addr(client_ip) except Exception as e: pass try: name = reversename.from_address(client_ip) answers = resolver.query(name, 'PTR') res_data['hostname'] = [] for answer in answers: res_data['hostname'].append(str(answer).rstrip('.')) except Exception as e: pass if user_agent and verbose: if user_agent.browser.family == 'Other': response.set_header('Content-type', 'application/json') if prettyprint: return json.dumps( res_data, sort_keys=True, indent=4, separators=(',', ': ') ) + '\r\n' return json.dumps(res_data) return template( 'index', ua_info=res_data, page_title='Your IP-address is: ' + res_data['ip'], verbose=verbose )
for visit in Visit.all.execute(): lat = None lng = None country_code = None ip = visit['ip'] ip = ip.replace('::ffff:', '') if IPV4RE.match(ip): if (ip == '127.0.0.1' or ip.startswith('192.168.') or ip.startswith('10.')): city = 'Local' country = 'Local' else: location = gip.record_by_addr(ip) city = (location.get('city', 'Unknown') .decode('iso-8859-1') if location else 'Unknown') country = (location.get('country_name', 'Unknown') .decode('iso-8859-1') if location else 'Unknown') country_code = (location.get('country_code', 'Unknown') .decode('iso-8859-1') if location else 'Unknown') lat = location.get('latitude', None) lng = location.get('longitude', None) else: country = 'ipv6' city = 'ipv6' visit['country'] = country
def get_geo_record(ip): gi = GeoIP(GEO_LITE_CITY_FILE, pygeoip.STANDARD ) x = gi.record_by_addr(ip) return x