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 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 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 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 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 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 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 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 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 get_isp(ip): gi = GeoIP(GEO_ISP_FILE ,pygeoip.STANDARD ) isp = gi.org_by_addr(ip) if isp is None: isp = "Unknown" return isp
def get_isp(ip): gi = GeoIP(GEO_ISP_FILE, pygeoip.STANDARD) isp = gi.org_by_addr(ip) if isp is None: isp = "Unknown" return isp