Commit ca3743d7 by William Tisäter

Catch all db parsing errors and complain

parent 448d8cc8
...@@ -199,38 +199,41 @@ class GeoIP(GeoIPBase): ...@@ -199,38 +199,41 @@ class GeoIP(GeoIPBase):
@return: offset of start of record @return: offset of start of record
@rtype: int @rtype: int
""" """
offset = 0 try:
seek_depth = 127 if len(str(ipnum)) > 10 else 31 offset = 0
seek_depth = 127 if len(str(ipnum)) > 10 else 31
for depth in range(seek_depth, -1, -1):
if self._flags & const.MEMORY_CACHE: for depth in range(seek_depth, -1, -1):
startIndex = 2 * self._recordLength * offset if self._flags & const.MEMORY_CACHE:
endIndex = startIndex + (2 * self._recordLength) startIndex = 2 * self._recordLength * offset
buf = self._memoryBuffer[startIndex:endIndex] endIndex = startIndex + (2 * self._recordLength)
else: buf = self._memoryBuffer[startIndex:endIndex]
startIndex = 2 * self._recordLength * offset else:
readLength = 2 * self._recordLength startIndex = 2 * self._recordLength * offset
self._lock.acquire() readLength = 2 * self._recordLength
self._filehandle.seek(startIndex, os.SEEK_SET) self._lock.acquire()
buf = self._filehandle.read(readLength) self._filehandle.seek(startIndex, os.SEEK_SET)
self._lock.release() buf = self._filehandle.read(readLength)
self._lock.release()
if PY3 and type(buf) is bytes:
buf = buf.decode(ENCODING) if PY3 and type(buf) is bytes:
buf = buf.decode(ENCODING)
x = [0, 0]
for i in range(2): x = [0, 0]
for j in range(self._recordLength): for i in range(2):
byte = buf[self._recordLength * i + j] for j in range(self._recordLength):
x[i] += ord(byte) << (j * 8) byte = buf[self._recordLength * i + j]
if ipnum & (1 << depth): x[i] += ord(byte) << (j * 8)
if x[1] >= self._databaseSegments: if ipnum & (1 << depth):
return x[1] if x[1] >= self._databaseSegments:
offset = x[1] return x[1]
else: offset = x[1]
if x[0] >= self._databaseSegments: else:
return x[0] if x[0] >= self._databaseSegments:
offset = x[0] return x[0]
offset = x[0]
except:
pass
raise GeoIPError('Corrupt database') raise GeoIPError('Corrupt database')
...@@ -449,7 +452,6 @@ class GeoIP(GeoIPBase): ...@@ -449,7 +452,6 @@ class GeoIP(GeoIPBase):
raise ValueError(message) raise ValueError(message)
country_id = self.id_by_addr(addr) country_id = self.id_by_addr(addr)
return const.COUNTRY_CODES[country_id] return const.COUNTRY_CODES[country_id]
elif self._databaseType in const.REGION_CITY_EDITIONS: elif self._databaseType in const.REGION_CITY_EDITIONS:
return self.region_by_addr(addr).get('country_code') return self.region_by_addr(addr).get('country_code')
...@@ -485,7 +487,8 @@ class GeoIP(GeoIPBase): ...@@ -485,7 +487,8 @@ class GeoIP(GeoIPBase):
try: try:
VALID_EDITIONS = (const.COUNTRY_EDITION, const.COUNTRY_EDITION_V6) VALID_EDITIONS = (const.COUNTRY_EDITION, const.COUNTRY_EDITION_V6)
if self._databaseType in VALID_EDITIONS: if self._databaseType in VALID_EDITIONS:
return const.COUNTRY_NAMES[self.id_by_addr(addr)] country_id = self.id_by_addr(addr)
return const.COUNTRY_NAMES[country_id]
elif self._databaseType in const.CITY_EDITIONS: elif self._databaseType in const.CITY_EDITIONS:
return self.record_by_addr(addr).get('country_name') return self.record_by_addr(addr).get('country_name')
else: else:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment