You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

base.py 8.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. import socket
  2. from pathlib import Path
  3. import geoip2.database
  4. from django.conf import settings
  5. from django.core.exceptions import ValidationError
  6. from django.core.validators import validate_ipv46_address
  7. from .resources import City, Country
  8. # Creating the settings dictionary with any settings, if needed.
  9. GEOIP_SETTINGS = {
  10. 'GEOIP_PATH': getattr(settings, 'GEOIP_PATH', None),
  11. 'GEOIP_CITY': getattr(settings, 'GEOIP_CITY', 'GeoLite2-City.mmdb'),
  12. 'GEOIP_COUNTRY': getattr(settings, 'GEOIP_COUNTRY', 'GeoLite2-Country.mmdb'),
  13. }
  14. class GeoIP2Exception(Exception):
  15. pass
  16. class GeoIP2:
  17. # The flags for GeoIP memory caching.
  18. # Try MODE_MMAP_EXT, MODE_MMAP, MODE_FILE in that order.
  19. MODE_AUTO = 0
  20. # Use the C extension with memory map.
  21. MODE_MMAP_EXT = 1
  22. # Read from memory map. Pure Python.
  23. MODE_MMAP = 2
  24. # Read database as standard file. Pure Python.
  25. MODE_FILE = 4
  26. # Load database into memory. Pure Python.
  27. MODE_MEMORY = 8
  28. cache_options = frozenset((MODE_AUTO, MODE_MMAP_EXT, MODE_MMAP, MODE_FILE, MODE_MEMORY))
  29. # Paths to the city & country binary databases.
  30. _city_file = ''
  31. _country_file = ''
  32. # Initially, pointers to GeoIP file references are NULL.
  33. _city = None
  34. _country = None
  35. def __init__(self, path=None, cache=0, country=None, city=None):
  36. """
  37. Initialize the GeoIP object. No parameters are required to use default
  38. settings. Keyword arguments may be passed in to customize the locations
  39. of the GeoIP datasets.
  40. * path: Base directory to where GeoIP data is located or the full path
  41. to where the city or country data files (*.mmdb) are located.
  42. Assumes that both the city and country data sets are located in
  43. this directory; overrides the GEOIP_PATH setting.
  44. * cache: The cache settings when opening up the GeoIP datasets. May be
  45. an integer in (0, 1, 2, 4, 8) corresponding to the MODE_AUTO,
  46. MODE_MMAP_EXT, MODE_MMAP, MODE_FILE, and MODE_MEMORY,
  47. `GeoIPOptions` C API settings, respectively. Defaults to 0,
  48. meaning MODE_AUTO.
  49. * country: The name of the GeoIP country data file. Defaults to
  50. 'GeoLite2-Country.mmdb'; overrides the GEOIP_COUNTRY setting.
  51. * city: The name of the GeoIP city data file. Defaults to
  52. 'GeoLite2-City.mmdb'; overrides the GEOIP_CITY setting.
  53. """
  54. # Checking the given cache option.
  55. if cache in self.cache_options:
  56. self._cache = cache
  57. else:
  58. raise GeoIP2Exception('Invalid GeoIP caching option: %s' % cache)
  59. # Getting the GeoIP data path.
  60. path = path or GEOIP_SETTINGS['GEOIP_PATH']
  61. if not path:
  62. raise GeoIP2Exception('GeoIP path must be provided via parameter or the GEOIP_PATH setting.')
  63. if not isinstance(path, str):
  64. raise TypeError('Invalid path type: %s' % type(path).__name__)
  65. path = Path(path)
  66. if path.is_dir():
  67. # Constructing the GeoIP database filenames using the settings
  68. # dictionary. If the database files for the GeoLite country
  69. # and/or city datasets exist, then try to open them.
  70. country_db = path / (country or GEOIP_SETTINGS['GEOIP_COUNTRY'])
  71. if country_db.is_file():
  72. self._country = geoip2.database.Reader(str(country_db), mode=cache)
  73. self._country_file = country_db
  74. city_db = path / (city or GEOIP_SETTINGS['GEOIP_CITY'])
  75. if city_db.is_file():
  76. self._city = geoip2.database.Reader(str(city_db), mode=cache)
  77. self._city_file = city_db
  78. if not self._reader:
  79. raise GeoIP2Exception('Could not load a database from %s.' % path)
  80. elif path.is_file():
  81. # Otherwise, some detective work will be needed to figure out
  82. # whether the given database path is for the GeoIP country or city
  83. # databases.
  84. reader = geoip2.database.Reader(str(path), mode=cache)
  85. db_type = reader.metadata().database_type
  86. if db_type.endswith('City'):
  87. # GeoLite City database detected.
  88. self._city = reader
  89. self._city_file = path
  90. elif db_type.endswith('Country'):
  91. # GeoIP Country database detected.
  92. self._country = reader
  93. self._country_file = path
  94. else:
  95. raise GeoIP2Exception('Unable to recognize database edition: %s' % db_type)
  96. else:
  97. raise GeoIP2Exception('GeoIP path must be a valid file or directory.')
  98. @property
  99. def _reader(self):
  100. return self._country or self._city
  101. @property
  102. def _country_or_city(self):
  103. if self._country:
  104. return self._country.country
  105. else:
  106. return self._city.city
  107. def __del__(self):
  108. # Cleanup any GeoIP file handles lying around.
  109. if self._reader:
  110. self._reader.close()
  111. def __repr__(self):
  112. meta = self._reader.metadata()
  113. version = '[v%s.%s]' % (meta.binary_format_major_version, meta.binary_format_minor_version)
  114. return '<%(cls)s %(version)s _country_file="%(country)s", _city_file="%(city)s">' % {
  115. 'cls': self.__class__.__name__,
  116. 'version': version,
  117. 'country': self._country_file,
  118. 'city': self._city_file,
  119. }
  120. def _check_query(self, query, country=False, city=False, city_or_country=False):
  121. "Check the query and database availability."
  122. # Making sure a string was passed in for the query.
  123. if not isinstance(query, str):
  124. raise TypeError('GeoIP query must be a string, not type %s' % type(query).__name__)
  125. # Extra checks for the existence of country and city databases.
  126. if city_or_country and not (self._country or self._city):
  127. raise GeoIP2Exception('Invalid GeoIP country and city data files.')
  128. elif country and not self._country:
  129. raise GeoIP2Exception('Invalid GeoIP country data file: %s' % self._country_file)
  130. elif city and not self._city:
  131. raise GeoIP2Exception('Invalid GeoIP city data file: %s' % self._city_file)
  132. # Return the query string back to the caller. GeoIP2 only takes IP addresses.
  133. try:
  134. validate_ipv46_address(query)
  135. except ValidationError:
  136. query = socket.gethostbyname(query)
  137. return query
  138. def city(self, query):
  139. """
  140. Return a dictionary of city information for the given IP address or
  141. Fully Qualified Domain Name (FQDN). Some information in the dictionary
  142. may be undefined (None).
  143. """
  144. enc_query = self._check_query(query, city=True)
  145. return City(self._city.city(enc_query))
  146. def country_code(self, query):
  147. "Return the country code for the given IP Address or FQDN."
  148. enc_query = self._check_query(query, city_or_country=True)
  149. return self.country(enc_query)['country_code']
  150. def country_name(self, query):
  151. "Return the country name for the given IP Address or FQDN."
  152. enc_query = self._check_query(query, city_or_country=True)
  153. return self.country(enc_query)['country_name']
  154. def country(self, query):
  155. """
  156. Return a dictionary with the country code and name when given an
  157. IP address or a Fully Qualified Domain Name (FQDN). For example, both
  158. '24.124.1.80' and 'djangoproject.com' are valid parameters.
  159. """
  160. # Returning the country code and name
  161. enc_query = self._check_query(query, city_or_country=True)
  162. return Country(self._country_or_city(enc_query))
  163. # #### Coordinate retrieval routines ####
  164. def coords(self, query, ordering=('longitude', 'latitude')):
  165. cdict = self.city(query)
  166. if cdict is None:
  167. return None
  168. else:
  169. return tuple(cdict[o] for o in ordering)
  170. def lon_lat(self, query):
  171. "Return a tuple of the (longitude, latitude) for the given query."
  172. return self.coords(query)
  173. def lat_lon(self, query):
  174. "Return a tuple of the (latitude, longitude) for the given query."
  175. return self.coords(query, ('latitude', 'longitude'))
  176. def geos(self, query):
  177. "Return a GEOS Point object for the given query."
  178. ll = self.lon_lat(query)
  179. if ll:
  180. from django.contrib.gis.geos import Point
  181. return Point(ll, srid=4326)
  182. else:
  183. return None
  184. # #### GeoIP Database Information Routines ####
  185. @property
  186. def info(self):
  187. "Return information about the GeoIP library and databases in use."
  188. meta = self._reader.metadata()
  189. return 'GeoIP Library:\n\t%s.%s\n' % (meta.binary_format_major_version, meta.binary_format_minor_version)
  190. @classmethod
  191. def open(cls, full_path, cache):
  192. return GeoIP2(full_path, cache)