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 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 run(self): log.debug("Updating mirror database") geoip = GeoIP(Config.GEOIP_PATH_V4) for status in mirror_statuses(unofficial_mirrors=Config.UNOFFICIAL_MIRRORS): name = status['mirror'] if name == "a.pypi.python.org": # don't include 'a' in the list of mirrors - it's no mirror after all continue time_diff = status['time_diff'] if not isinstance(time_diff, timedelta): continue log.debug(" Processing mirror '%s'", name) record = geoip.record_by_name(name) lat = record['latitude'] lon = record['longitude'] log.debug(" Age: %d, Lat: %0.5f, Lon: %0.5f", time_diff.total_seconds(), lat, lon) try: mirror = Mirror.objects.get(name=name) except ObjectNotFound: mirror = Mirror(name=name) mirror.age = time_diff.total_seconds() mirror.lat = lat mirror.lon = lon mirror.save() self.redis.set(Config.KEY_LAST_UPDATE, time.time()) log.debug("Finished updating mirror database")
def IPToLocation(ipaddr): from ooni.settings import config 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'} if not asn_file or not country_file: log.err("Could not find GeoIP data file in data directories." "Try running ooniresources or" " edit your ooniprobe.conf") return location country_dat = GeoIP(country_file) asn_dat = GeoIP(asn_file) country_code = country_dat.country_code_by_addr(ipaddr) if country_code is not None: location['countrycode'] = country_code asn = asn_dat.org_by_addr(ipaddr) if asn is not None: location['asn'] = asn.split(' ')[0] return location
def __init__(self, packets, layer=3, geo_ip=os.path.expanduser('~/GeoIP.dat')): self.graph = DiGraph() self.layer = layer self.geo_ip = None self.data = {} try: self.geo_ip = GeoIP(geo_ip) except: logging.warning("could not load GeoIP data") if self.layer == 2: edges = map(self._layer_2_edge, packets) elif self.layer == 3: edges = map(self._layer_3_edge, packets) elif self.layer == 4: edges = map(self._layer_4_edge, packets) else: raise ValueError( "Other layers than 2,3 and 4 are not supported yet!") for src, dst, packet in filter(lambda x: not (x is None), edges): if src in self.graph and dst in self.graph[src]: self.graph[src][dst]['packets'].append(packet) else: self.graph.add_edge(src, dst, {'packets': [packet]}) for node in self.graph.nodes(): self._retrieve_node_info(node) for src, dst in self.graph.edges(): self._retrieve_edge_info(src, dst)
def __init__(self): """ Constructor - It starts with some basic initialisations and spawns a coordinator thread which creates more threads to perform the master server query and also the status updates for the servers. """ self.serverqueue = Queue() self.messageque = Queue() self.pulsemessageque = Queue() self.threadcount = 0 self.servercount = 0 self.processedserver = 0 self.filterdcount = 0 self.gui_lock = None self.geo_lock = None coord = Thread(target=self.coordinator) coord.daemon = True coord.start() dbname = Globals.geoip_dir+ '/GeoIP.dat' self.pygeoip = GeoIP(dbname, pygeoip.const.MMAP_CACHE) self.abort = False
def getAutonomous_system_number(hostName): try: geo_ip = GeoIP('GeoIPASNum.dat') autonomous_number = int(geo_ip.org_by_name(hostName).split()[0][2:]) return autonomous_number except: return notFound
def ip2country(request): ip = str(request.REQUEST.get('ip')) geoip = GeoIP(settings.GEOIP, MEMORY_CACHE) c = geoip.country_name_by_addr(ip) c+="; " whois = os.popen("whois %s 2>&1" % ip) file.close for ln in whois: ''' inetnum: 134.36.0.0 - 134.36.255.255 descr: University of Dundee descr: Dundee DD1 4HN descr: Scotland netname: DUNDEE-UNIV descr: University of Dundee country: GB ''' if ln.startswith("inetnum") or ln.startswith("netname") or ln.startswith("descr"): c+=ln.split(":")[1].strip()+"; " if ln.startswith("country"): c+=ln.split(":")[1].strip()+"." break if len(c) > 400: break return HttpResponse(c)
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.'
def run(self): log.debug("Updating mirror database") geoip = GeoIP(Config.GEOIP_PATH_V4) for status in mirror_statuses( unofficial_mirrors=Config.UNOFFICIAL_MIRRORS): name = status['mirror'] if name == "a.pypi.python.org": # don't include 'a' in the list of mirrors - it's no mirror after all continue time_diff = status['time_diff'] if not isinstance(time_diff, timedelta): continue log.debug(" Processing mirror '%s'", name) record = geoip.record_by_name(name) lat = record['latitude'] lon = record['longitude'] log.debug(" Age: %d, Lat: %0.5f, Lon: %0.5f", time_diff.total_seconds(), lat, lon) try: mirror = Mirror.objects.get(name=name) except ObjectNotFound: mirror = Mirror(name=name) mirror.age = time_diff.total_seconds() mirror.lat = lat mirror.lon = lon mirror.save() self.redis.set(Config.KEY_LAST_UPDATE, time.time()) log.debug("Finished updating mirror database")
def IPToLocation(ipaddr): from ooni.settings import config 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 data directories." "Try running ooniresources or" " edit your ooniprobe.conf") 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: asn_dat = GeoIP(asn_file) location['asn'] = asn_dat.org_by_addr(ipaddr).split(' ')[0] except: error() return location
def __init__(self): # Load the GeoIP databases into class attributes since they each need 20+ MB in memory if not self.__class__._geoip4: self.__class__._geoip4 = GeoIP(Config.GEOIP_PATH_V4, MEMORY_CACHE) if not self.__class__._geoip6: self.__class__._geoip6 = GeoIP(Config.GEOIP_PATH_V6, MEMORY_CACHE) self.redis = StrictRedis(Config.REDIS['HOST'], Config.REDIS['PORT'], Config.REDIS['DB'])
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 __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)
def __init__(self, *args, **kwargs): """ Initialiser le manager """ super(IPManager, self).__init__(*args, **kwargs) # Initialiser l'outil GeoIP dans le manager if not hasattr(self, 'geoip'): try: self.geoip = GeoIP(settings.GEOIP_PATH) self.geoisp = GeoIP(settings.GEOISP_PATH) except AttributeError: pass
def update_data(self, request, commit=True): self.user_agent = request.META.get('HTTP_USER_AGENT', None) geo = GeoIP(settings.GEOIP_DATABASE) self.country_code = geo.country_code_by_addr( request.META.get('REMOTE_ADDR', None)) self.visitor_ip = request.META.get('REMOTE_ADDR', None) if hasattr(request, 'user') and request.user.is_authenticated(): self.visitor = request.user if commit: self.save()
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 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 update_data(self, request, commit=True): self.user_agent = request.META.get('HTTP_USER_AGENT', None) geo = GeoIP(settings.GEOIP_DATABASE) self.country_code = geo.country_code_by_addr( request.META.get('REMOTE_ADDR', None) ) self.visitor_ip = request.META.get('REMOTE_ADDR', None) if hasattr(request, 'user') and request.user.is_authenticated(): self.visitor = request.user if commit: self.save()
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
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 calcDistance(self, ipAddr1, ipAddr2): gi = GeoIP("c:\\temp\\GeoLiteCity.dat") gir1 = gi.record_by_name(ipAddr1) gir2 = gi.record_by_name(ipAddr2) #Calculate distance print "Calculating Distance between IP ", ipAddr1, " and IP ", ipAddr2 pt1 = geopy.Point(gir1['latitude'], gir1['longitude']) pt2 = geopy.Point(gir2['latitude'], gir2['longitude']) print "Distance:" dist1 = geopy.distance.distance(pt1, pt2).km print dist1 return dist1
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 process_request(self, request): if 'django_timezone' in request.session: tzname = request.session['django_timezone'] timezone.activate(pytz.timezone(tzname)) else: ip = get_real_ip(request) if ip is not None: gi = GeoIP(settings.GEOIP_DATABASE, MEMORY_CACHE) tzname = gi.time_zone_by_addr(ip) if tzname is not None: request.session['django_timezone'] = tzname timezone.activate(pytz.timezone(tzname)) else: timezone.deactivate()
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 ip_to_country(ip_address): db = GeoIP( path.abspath( path.join( path.dirname(__file__), 'fixtures', 'geoip.dat' ) ), MEMORY_CACHE ) return Country.objects.get( code = db.country_code_by_addr(ip_address) )
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 lookup_country_code(ip_address): country_code = None # Download geoip data file in background (non blocking, not wait for result) gif = Globals.get_geoip_file(wait=False) # If downloaded, use geoip API to get the country if gif: try: country_code = GeoIP(gif).country_code_by_addr(ip_address) except GeoIPError as e: logging.error(e) if not country_code: # If geoip file not present (not yet downloaded) or it did not find the IP, # use web API to get the country code if inetcache.isonline: try: with closing( urllib2.urlopen( "http://getcitydetails.geobytes.com/GetCityDetails?fqcn=%s" % ip_address, None, 5)) as resp: meta = resp.read() r = re.search(r"\"geobytesinternet\"\s*:\s*\"(.*?)\"", meta) if r: country_code = r.group(1) except Exception, e: logging.error(e)
def __init__(self, packets, layer=3, geo_ip=os.path.expanduser('~/GeoIP.dat')): self.graph = DiGraph() self.layer = layer self.geo_ip = None self.data = {} try: self.geo_ip = GeoIP(geo_ip) except: logging.warning("could not load GeoIP data") if self.layer == 2: edges = map(self._layer_2_edge, packets) elif self.layer == 3: edges = map(self._layer_3_edge, packets) elif self.layer == 4: edges = map(self._layer_4_edge, packets) else: raise ValueError("Other layers than 2,3 and 4 are not supported yet!") for src, dst, packet in filter(lambda x: not (x is None), edges): if src in self.graph and dst in self.graph[src]: self.graph[src][dst]['packets'].append(packet) else: self.graph.add_edge(src, dst, {'packets': [packet]}) for node in self.graph.nodes(): self._retrieve_node_info(node) for src, dst in self.graph.edges(): self._retrieve_edge_info(src, dst)
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')
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
class AccountController(BaseController): geoip = GeoIP(os.path.join(config['pylons.paths']['data'], 'geoip.dat')) openid_store = FileOpenIDStore('/var/tmp') @require('guest') def login(self): login = render('account/login.tpl', slacks=True) if request.environ['REQUEST_METHOD'] != 'POST': return login try: form = LoginForm().to_python(request.POST) except validators.Invalid, e: return h.htmlfill(e, form=login) try: cons = Consumer(session=session, store=self.openid_store) auth_request = cons.begin(form['openid_identifier']) auth_request.addExtension( SRegRequest(optional=['nickname', 'email'])) except DiscoveryFailure: h.flash(_('The specified URL is not a valid OpenID end-point.'), 'error') redirect(url(controller='account', action='login')) host = request.headers['host'] realm = '%s://%s' % (request_config().protocol, host) return_url = url(host=host, controller='account', action='login_complete') new_url = auth_request.redirectURL(return_to=return_url, realm=realm) redirect(new_url)
def InitializeConnections(ConfigFile='IpLocator.ini'): # Initialization file (default=IpLocator.ini) with two sections: # [CONNECTION} section with connection string parameters and # [GEODATABASE} section with the pathname of the GeoIP database file # Returns: connection to MSSQL and gi handler to GeoIP database ConnectionString = '' config = ConfigParser() try: with open(ConfigFile) as f: config.read_file(f) Section = 'CONNECTION' if config.has_section(Section): #Driver = config[Section]['Driver'] # only used for odbc connections, not in pymssql or _mssql Server = config[Section]['Server'] # server\instance_name Database = config[Section]['Database'] Uid = config[Section]['Uid'] Pwd = config[Section]['Pwd'] m_writeBlocks = int(config[Section]['WriteBlocks']) else: print('Section: {} does not exist in config file.'.format(Section)) return None, None connectionString = {} connectionString['Server'] = Server connectionString['User'] = Uid connectionString['Password'] = Pwd connectionString['Database'] = Database connection = connect(connectionString['Server'], connectionString['User'], connectionString['Password'], connectionString['Database'], autocommit=False) Section = 'GEODATABASE' if config.has_section(Section): GeoIPFile = config[Section]['GeoIPFile'] gi = GeoIP(GeoIPFile, flags=MEMORY_CACHE) #MEMORY_CACHE else: print('Section: {} does not exist in config file.'.format(Section)) return None, None except IOError as e: print('{}'.format(e)) return None, None except GeoIPError as e: print('{}'.format(e)) except KeyError as e: print('Item {} does not exist in configuration file {}.'.format( e, ConfigFile)) return None, None except InterfaceError as e: print('InterfaceError {}'.format(e)) return None, None except DatabaseError as e: print('DatabaseError {}'.format(e)) return None, None except: print('{}\n{}'.format(connectionString, exc_info()[0])) return None, None return connection, gi, m_writeBlocks
def get_time_zone_by_ip(self, request): ''' http://packages.python.org/pygeoip/ ''' gi = GeoIP(settings.GEOIP_PATH + settings.GEOIP_CITY, pygeoip.STANDARD) client_ip = request.get_host().split(':')[0] try: time_zone = gi.time_zone_by_addr(client_ip) except: return None else: if not time_zone: client_ip = request.META.get('REMOTE_ADDR') time_zone = gi.time_zone_by_addr(client_ip) return time_zone
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 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'} 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 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): 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 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 ip_to_location(ipaddr): from ooni.settings import config country_file = config.get_data_file_path( 'resources/maxmind-geoip/GeoIP.dat') asn_file = config.get_data_file_path( 'resources/maxmind-geoip/GeoIPASNum.dat') location = {'city': None, 'countrycode': 'ZZ', 'asn': 'AS0'} if not asn_file or not country_file: log.err("Could not find GeoIP data file in data directories." "Try running ooniresources or" " edit your ooniprobe.conf") return location country_dat = GeoIP(country_file) asn_dat = GeoIP(asn_file) country_code = country_dat.country_code_by_addr(ipaddr) if country_code is not None: location['countrycode'] = country_code asn = asn_dat.org_by_addr(ipaddr) if asn is not None: location['asn'] = asn.split(' ')[0] 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 application(environ, start_response): from pygeoip import GeoIP from os import path from urlparse import parse_qs import json status = '200 OK' ip = environ.get('HTTP_X_REAL_IP', environ.get('HTTP_X_FORWARDED_FOR', environ.get('REMOTE_ADDR') ) ) qs = parse_qs(environ.get('QUERY_STRING', '')) callback = qs.get('callback', '') if any(callback): callback = callback[0] else: callback = None if ip: filename = path.join(path.dirname(__file__), 'data', 'GeoLiteCity.dat') geo = GeoIP(filename) record = geo.record_by_name(ip) if record: response_headers = [('Content-type', 'application/json')] start_response(status, response_headers) if callback: yield '%s(' % callback yield json.dumps(record) if callback: yield ')' yield
def __call__(self, environ, start_response): """Invoke the Controller""" # WSGIController.__call__ dispatches to the Controller method # the request is routed to. This routing information is # available in environ['pylons.routes_dict'] from pylons.i18n.translation import set_lang set_lang(environ['pylons.routes_dict']['_lang']) #figure out which map to display based on geoloc from pygeoip import GeoIP gi = GeoIP('/usr/share/GeoIP/GeoIP.dat') #GeoIP.GEOIP_MEMORY_CACHE) country_code = gi.country_code_by_addr(str(environ['REMOTE_ADDR'])) #raise Exception('%s from %s'%(country_code,environ['REMOTE_ADDR'])) if not country_code or country_code.lower() in ['a2']: country_code = config['global_conf']['default_country'] country_code=country_code.lower() c.use_google_maps,c.freemap_url = freemap_url_from_country(country_code) try: return WSGIController.__call__(self, environ, start_response) finally: meta.Session.remove()
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
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 get_gic(): global gic if gic == None: if os.path.exists('/usr/share/GeoIP/GeoIP.dat'): default = "/usr/share/GeoIP/GeoIP.dat" elif os.path.exists("/usr/local/share/GeoIP/GeoLiteCity.dat"): default = "/usr/local/share/GeoIP/GeoLiteCity.dat" elif os.path.exists("/usr/local/var/lib/GeoLiteCity.dat"): default = "/usr/local/var/lib/GeoLiteCity.dat" else: default = None filename = BabeBase.get_config_with_env('geoip', 'GEOIP_FILE', {}, default) from pygeoip import GeoIP gic = GeoIP(filename) return gic
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
class RawIPConsumer(Consumer): """ Consumes dummy objects for testing like: { 'ip': 'some_ip', 'tag': 'some_tag', } """ topic = 'narcissus.hits' jsonify = True geoip_url = '/'.join(__file__.split('/')[:-1] + ["public/data/GeoLiteCity.dat"]) gi = GeoIP(geoip_url, GEOIP_MEMORY_CACHE) def consume(self, message): if not message: #self.log.warn("%r got empty message." % self) return #self.log.info("%r got message '%r'" % (self, message)) message = simplejson.loads(message['body']) # Get IP 2 LatLon info rec = self.gi.record_by_addr(message['ip']) if not(rec and rec['latitude'] and rec['longitude']): self.log.warn("Failed to geo-encode %r" % message) return updates = { 'lat' : rec['latitude'], 'lon' : rec['longitude'], 'country' : rec.get('country_name', 'undefined'), } message.update(updates) self.send_message('http_latlon', message)
] __metaclass__ = PoolMeta geoip = None try: from pygeoip import GeoIP except ImportError: logging.error("pygeoip is not installed") else: # Usual location in Ubuntu path1 = '/usr/share/GeoIP/GeoIP.dat' # this is where brew installs it path2 = '/usr/local/Cellar/geoip/1.4.8/share/GeoIP/GeoIP.dat' if os.path.isfile(path1): geoip = GeoIP(path1) elif os.path.isfile(path2): geoip = GeoIP(path2) class NereidUser: """ Add employee """ __name__ = "nereid.user" #: Allow the nereid user to be connected to an internal employee. This #: indicates that the user is an employee and not a regular participant employee = fields.Many2One( 'company.employee', 'Employee',
class IPManager(GeoManager): """ Manager des IP """ # Setter def new(self, ip_string): """ Créer une IP """ try: new_ip = IP() new_ip.set_ip_address(ip_string, save=True) return new_ip except IntegrityError: return None # Getter def get_by_natural_key(self, ip): """ Renvoyer une IP par clé naturelle """ return self.get(ip=ip) def get_by_ip(self, ip_string): """ Renvoyer l'objet IP depuis une chaîne """ try: return self.get(ip=IP.get_ip_value(ip_string)) except IP.DoesNotExist: return self.new(ip_string) def get_localhost(self): """ Renvoyer l'objet IP pour localhost """ return self.get_by_ip('127.0.0.1') def get_by_request(self, request): """ Renvoyer l'objet IP pour un objet Request """ if request is not None: return self.get_by_ip(request.get_ip()) else: return None def for_user(self, user): """ Renvoyer les IPs d'un utilisateur """ return self.filter(userip__user=user) def get_for_user_count(self, user): """ Renvoyer le nombre d'IP utilisées par un utilisateur """ return self.for_user(user).count() def get_country_codes(self): """ Renvoyer la liste de codes pays des """ return self.exclude(country="").values('country').annotate( count=Count('country')).distinct().order_by('-count') # Overrides def __init__(self, *args, **kwargs): """ Initialiser le manager """ super(IPManager, self).__init__(*args, **kwargs) # Initialiser l'outil GeoIP dans le manager if not hasattr(self, 'geoip'): try: self.geoip = GeoIP(settings.GEOIP_PATH) self.geoisp = GeoIP(settings.GEOISP_PATH) except AttributeError: pass def get_isp_by_ip(self, ip): """ Renvoyer le FAI d'une IP """ try: return self.geoisp.org_by_addr(ip) or "" except ValueError: return ""
def geo_connection(): geo_conn = GeoIP("%s/%s" % (settings.GEOIP_DB_LOCATION, settings.GEOIP_FILENAME)) return geo_conn
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import logging from django.utils import timezone from pygeoip import GeoIP, MEMORY_CACHE from django_visitor_information import constants from django_visitor_information import settings __all__ = ['TimezoneMiddleware', 'VisitorInformationMiddleware'] logger = logging.getLogger('django_visitor_information.middleware') gi4 = GeoIP(settings.VISITOR_INFO_GEOIP_DATABASE_PATH, MEMORY_CACHE) class TimezoneMiddleware(object): """ This middleware activates a timezone for an authenticated user. This middleware assumes that a User model references a UserProfile model which has a "timezone" field. """ def process_request(self, request): if request.user.is_authenticated(): profile = request.user.get_profile() user_timezone = \ getattr(profile, settings.VISITOR_INFO_PROFILE_TIMEZONE_FIELD,
from models import * from django.contrib.sessions.models import Session as django_session if not settings.configured: settings.configure() geoip_city_dat = settings.GEOLITECITY_ABSOLUTE_PATH geoip_org_dat = settings.GEOORGANIZATION_ABSOLUTE_PATH # default amount of time which must pass before the same event from the # same user is logged again default_sleep_minutes = 30 gic = None try: gic = GeoIP(geoip_city_dat) except IOError, err: sys.stderr.write(""" ERROR: Could not find GeoIP database. Tried looking in "%s". If this is not where you have your GeoIP .dat file stored, edit GEOLITECITY_ABSOLUTE_PATH in live/local_settings.py\nIf you don't have the GeoIP City database, you can get it from "http://dev.maxmind.com/geoip/geolite". """ % (geoip_city_dat)) sys.exit(1) gio = None try: gio = GeoIP(geoip_org_dat) except IOError: # we don't want to spam the log with warning messages, so don't do anything # here. it's desgined to work without the GeoIP Organization database
class GraphManager(object): """ Generates and processes the graph based on packets """ def __init__(self, packets, layer=3, geo_ip=os.path.expanduser('~/GeoIP.dat')): self.graph = DiGraph() self.layer = layer self.geo_ip = None self.data = {} try: self.geo_ip = GeoIP(geo_ip) except: logging.warning("could not load GeoIP data") if self.layer == 2: edges = map(self._layer_2_edge, packets) elif self.layer == 3: edges = map(self._layer_3_edge, packets) elif self.layer == 4: edges = map(self._layer_4_edge, packets) else: raise ValueError( "Other layers than 2,3 and 4 are not supported yet!") for src, dst, packet in filter(lambda x: not (x is None), edges): if src in self.graph and dst in self.graph[src]: self.graph[src][dst]['packets'].append(packet) else: self.graph.add_edge(src, dst) self.graph[src][dst]['packets'] = [packet] for node in self.graph.nodes(): self._retrieve_node_info(node) for src, dst in self.graph.edges(): self._retrieve_edge_info(src, dst) def get_in_degree(self, print_stdout=True): unsorted_degrees = self.graph.in_degree() return self._sorted_results(unsorted_degrees, print_stdout) def get_out_degree(self, print_stdout=True): unsorted_degrees = self.graph.out_degree() return self._sorted_results(unsorted_degrees, print_stdout) @staticmethod def _sorted_results(unsorted_degrees, print_stdout): sorted_degrees = OrderedDict( sorted(list(unsorted_degrees), key=lambda t: t[1], reverse=True)) for i in sorted_degrees: if print_stdout: print(sorted_degrees[i], i) return sorted_degrees def _retrieve_node_info(self, node): self.data[node] = {} if self.layer >= 3 and self.geo_ip: if self.layer == 3: self.data[node]['ip'] = node elif self.layer == 4: self.data[node]['ip'] = node.split(':')[0] node_ip = self.data[node]['ip'] try: country = self.geo_ip.country_name_by_addr(node_ip) self.data[node]['country'] = country if country else 'private' except: # it seems like we are not dealing with valid IPs... # best effort approach: skip del self.data[node] #TODO layer 2 info? def _retrieve_edge_info(self, src, dst): edge = self.graph[src][dst] if edge: packets = edge['packets'] edge['layers'] = set( list( itertools.chain( *[set(GraphManager.get_layers(p)) for p in packets]))) edge['transmitted'] = sum(len(p) for p in packets) edge['connections'] = len(packets) @staticmethod def get_layers(packet): return list(GraphManager.expand(packet)) @staticmethod def expand(x): yield x.name while x.payload: x = x.payload yield x.name @staticmethod def _layer_2_edge(packet): return packet[0].src, packet[0].dst, packet @staticmethod def _layer_3_edge(packet): if packet.haslayer(IP): return packet[1].src, packet[1].dst, packet @staticmethod def _layer_4_edge(packet): if any(map(lambda p: packet.haslayer(p), [TCP, UDP])): src = packet[1].src dst = packet[1].dst _ = packet[2] return "%s:%i" % (src, _.sport), "%s:%i" % (dst, _.dport), packet def draw(self, filename=None): graph = self.get_graphviz_format() for node in graph.nodes(): if node not in self.data: # node might be deleted, because it's not legit etc. continue node.attr['shape'] = 'circle' node.attr['fontsize'] = '10' node.attr['width'] = '0.5' if 'country' in self.data[str(node)]: country_label = self.data[str(node)]['country'] if country_label == 'private': node.attr['label'] = str(node) else: node.attr['label'] = "%s (%s)" % (str(node), country_label) if not (country_label == 'private'): node.attr['color'] = 'blue' node.attr['style'] = 'filled' #TODO add color based on country or scan? for edge in graph.edges(): connection = self.graph[edge[0]][edge[1]] edge.attr['label'] = 'transmitted: %i bytes\n%s ' % ( connection['transmitted'], ' | '.join(connection['layers'])) edge.attr['fontsize'] = '8' edge.attr['minlen'] = '2' edge.attr['penwidth'] = min( connection['connections'] * 1.0 / len(self.graph.nodes()), 2.0) graph.layout(prog='dot') graph.draw(filename) def get_graphviz_format(self, filename=None): agraph = networkx.drawing.nx_agraph.to_agraph(self.graph) # remove packet information (blows up file size) for edge in agraph.edges(): del edge.attr['packets'] if filename: agraph.write(filename) return agraph
def __init__(self, filename=None): if not filename: filename = config.getValue("geoip", "database_path") GeoIP.__init__(self, filename)
class Proxy(models.Model): """A proxy server""" _geoip = GeoIP(defaults.PROXY_LIST_GEOIP_PATH) anonymity_level_choices = ( # Anonymity can't be determined (None, 'Unknown'), # No anonymity; remote host knows your IP and knows you are using # proxy. (ANONYMITY_NONE, 'None'), # Low anonymity; proxy sent our IP to remote host, but it was sent in # non standard way (unknown header). (ANONYMITY_LOW, 'Low'), # Medium anonymity; remote host knows you are using proxy, but it does # not know your IP (ANONYMITY_MEDIUM, 'Medium'), # High anonymity; remote host does not know your IP and has no direct # proof of proxy usage (proxy-connection family header strings). (ANONYMITY_HIGH, 'High'), ) hostname = models.CharField(max_length=75, unique=True) port = models.PositiveIntegerField() user = models.CharField(blank=True, null=True, max_length=50) password = models.CharField(blank=True, null=True, max_length=50) country = CountryField(blank=True, editable=False) proxy_type = models.CharField(default='http', max_length=10, choices=PROXY_TYPE_CHOICES) anonymity_level = models.PositiveIntegerField( null=True, default=ANONYMITY_NONE, choices=anonymity_level_choices, editable=False) last_check = models.DateTimeField(null=True, blank=True, editable=False) next_check = models.DateTimeField(null=True, blank=True) created = models.DateTimeField(auto_now=False, auto_now_add=True, db_index=True, editable=False) errors = models.PositiveIntegerField(default=0, editable=False) elapsed_time = models.FloatField(blank=True, null=True, editable=False) def _update_next_check(self): """ Calculate and set next check time """ delay = randint(defaults.PROXY_LIST_MIN_CHECK_INTERVAL, defaults.PROXY_LIST_MAX_CHECK_INTERVAL) delay += defaults.PROXY_LIST_ERROR_DELAY * self.errors if self.last_check: self.next_check = self.last_check + timedelta(seconds=delay) else: self.next_check = now() + timedelta(seconds=delay) def update_from_check(self, check, elapsed_time): """ Update data from a ProxyCheckResult """ if check.check_start: self.last_check = check.check_start else: self.last_check = now() self.errors = 0 self.anonymity_level = check.anonymity() self._update_next_check() self.elapsed_time = elapsed_time self.save() def update_from_error(self): """ Last check was an error """ self.last_check = now() self.errors += 1 self._update_next_check() self.save() def save(self, *args, **kwargs): if not self.country: if self.hostname.count('.') == 3: self.country = self._geoip.country_code_by_addr( str(self.hostname)) else: self.country = self._geoip.country_code_by_name( str(self.hostname)) if not self.next_check: self.next_check = (now() - timedelta(seconds=max_check)) super(Proxy, self).save(*args, **kwargs) class Meta: verbose_name = 'Proxy' verbose_name_plural = 'Proxies' ordering = ('-last_check', ) def __unicode__(self): return "%s://%s:%s" % (self.proxy_type, self.hostname, self.port)
from trytond.modules.nereid_checkout.i18n import _ __metaclass__ = PoolMeta __all__ = ['Address'] geoip = None try: from pygeoip import GeoIP except ImportError: logging.error("pygeoip is not installed") else: path = os.environ.get('GEOIP_DATA_PATH', config.get('nereid_webshop', 'geoip_data_path')) if path: geoip = GeoIP(path) class WebshopAddressForm(AddressForm): """Custom address form for webshop """ phone = TextField(_('Phone'), [ validators.DataRequired(), ]) def get_default_country(self): """Get the default country based on geoip data. """ if not geoip or not request.remote_addr: return None