2018-10-07 10:11:59 +00:00
|
|
|
"""
|
|
|
|
All location related functions and converters.
|
2018-10-07 10:33:43 +00:00
|
|
|
|
|
|
|
The main entry point is `location_processing`
|
|
|
|
which gets `location` and `source_ip_address`
|
|
|
|
and basing on this information generates
|
|
|
|
precise location description.
|
|
|
|
|
2018-10-07 10:11:59 +00:00
|
|
|
"""
|
2018-12-02 08:41:29 +00:00
|
|
|
from __future__ import print_function
|
2018-10-07 10:11:59 +00:00
|
|
|
|
2018-10-07 10:33:43 +00:00
|
|
|
import os
|
2018-10-07 10:11:59 +00:00
|
|
|
import json
|
2018-10-07 10:33:43 +00:00
|
|
|
import socket
|
2018-10-07 10:11:59 +00:00
|
|
|
import requests
|
|
|
|
import geoip2.database
|
|
|
|
|
2018-10-07 10:33:43 +00:00
|
|
|
from globals import GEOLITE, GEOLOCATOR_SERVICE, IP2LCACHE, IP2LOCATION_KEY, NOT_FOUND_LOCATION, \
|
|
|
|
ALIASES, BLACKLIST, IATA_CODES_FILE
|
|
|
|
|
2018-10-07 10:11:59 +00:00
|
|
|
GEOIP_READER = geoip2.database.Reader(GEOLITE)
|
|
|
|
|
|
|
|
def ascii_only(string):
|
|
|
|
"Check if `string` contains only ASCII symbols"
|
|
|
|
|
|
|
|
try:
|
|
|
|
for _ in range(5):
|
|
|
|
string = string.encode('utf-8')
|
|
|
|
return True
|
|
|
|
except UnicodeDecodeError:
|
|
|
|
return False
|
|
|
|
|
2018-10-07 10:33:43 +00:00
|
|
|
def is_ip(ip_addr):
|
|
|
|
"""
|
|
|
|
Check if `ip_addr` looks like an IP Address
|
|
|
|
"""
|
|
|
|
|
|
|
|
try:
|
2019-07-07 17:10:05 +00:00
|
|
|
socket.inet_pton(socket.AF_INET, ip_addr.encode("utf-8"))
|
2018-10-07 10:33:43 +00:00
|
|
|
return True
|
|
|
|
except socket.error:
|
2019-07-07 17:10:05 +00:00
|
|
|
try:
|
|
|
|
socket.inet_pton(socket.AF_INET6, ip_addr.encode("utf-8"))
|
|
|
|
return True
|
|
|
|
except socket.error:
|
|
|
|
return False
|
2018-10-07 10:33:43 +00:00
|
|
|
|
|
|
|
def location_normalize(location):
|
|
|
|
"""
|
|
|
|
Normalize location name `location`
|
|
|
|
"""
|
|
|
|
#translation_table = dict.fromkeys(map(ord, '!@#$*;'), None)
|
|
|
|
def _remove_chars(chars, string):
|
|
|
|
return ''.join(x for x in string if x not in chars)
|
|
|
|
|
|
|
|
location = location.lower().replace('_', ' ').replace('+', ' ').strip()
|
|
|
|
if not location.startswith('moon@'):
|
|
|
|
location = _remove_chars(r'!@#$*;:\\', location)
|
|
|
|
return location
|
|
|
|
|
|
|
|
|
2018-10-07 10:11:59 +00:00
|
|
|
|
|
|
|
def geolocator(location):
|
|
|
|
"""
|
|
|
|
Return a GPS pair for specified `location` or None
|
|
|
|
if nothing can't be found
|
|
|
|
"""
|
|
|
|
|
|
|
|
try:
|
|
|
|
geo = requests.get('%s/%s' % (GEOLOCATOR_SERVICE, location)).text
|
|
|
|
except requests.exceptions.ConnectionError as exception:
|
2018-12-02 08:41:29 +00:00
|
|
|
print("ERROR: %s" % exception)
|
2018-10-07 10:11:59 +00:00
|
|
|
return None
|
|
|
|
|
|
|
|
if geo == "":
|
|
|
|
return None
|
|
|
|
|
|
|
|
try:
|
|
|
|
answer = json.loads(geo.encode('utf-8'))
|
|
|
|
return answer
|
|
|
|
except ValueError as exception:
|
2018-12-02 08:41:29 +00:00
|
|
|
print("ERROR: %s" % exception)
|
2018-10-07 10:11:59 +00:00
|
|
|
return None
|
|
|
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
def ip2location(ip_addr):
|
|
|
|
"Convert IP address `ip_addr` to a location name"
|
|
|
|
|
|
|
|
cached = os.path.join(IP2LCACHE, ip_addr)
|
|
|
|
if not os.path.exists(IP2LCACHE):
|
|
|
|
os.makedirs(IP2LCACHE)
|
|
|
|
|
2018-10-26 17:35:13 +00:00
|
|
|
location = None
|
|
|
|
|
2018-10-07 10:11:59 +00:00
|
|
|
if os.path.exists(cached):
|
|
|
|
location = open(cached, 'r').read()
|
2018-10-26 17:35:13 +00:00
|
|
|
else:
|
2019-05-09 15:40:22 +00:00
|
|
|
# if IP2LOCATION_KEY is not set, do not the query,
|
|
|
|
# because the query wont be processed anyway
|
|
|
|
if IP2LOCATION_KEY:
|
|
|
|
try:
|
|
|
|
ip2location_response = requests\
|
|
|
|
.get('http://api.ip2location.com/?ip=%s&key=%s&package=WS3' \
|
|
|
|
% (ip_addr, IP2LOCATION_KEY)).text
|
|
|
|
if ';' in ip2location_response:
|
|
|
|
open(cached, 'w').write(ip2location_response)
|
|
|
|
location = ip2location_response
|
|
|
|
except requests.exceptions.ConnectionError:
|
|
|
|
pass
|
2018-10-26 17:35:13 +00:00
|
|
|
|
2019-05-15 15:54:39 +00:00
|
|
|
if location and ';' in location:
|
2018-10-26 22:19:22 +00:00
|
|
|
location = location.split(';')[3], location.split(';')[1]
|
|
|
|
else:
|
|
|
|
location = location, None
|
2018-10-07 10:11:59 +00:00
|
|
|
|
2018-10-26 17:35:13 +00:00
|
|
|
return location
|
2018-10-07 10:11:59 +00:00
|
|
|
|
|
|
|
def get_location(ip_addr):
|
|
|
|
"""
|
|
|
|
Return location pair (CITY, COUNTRY) for `ip_addr`
|
|
|
|
"""
|
|
|
|
|
2018-11-02 17:12:53 +00:00
|
|
|
try:
|
|
|
|
response = GEOIP_READER.city(ip_addr)
|
|
|
|
country = response.country.name
|
|
|
|
city = response.city.name
|
|
|
|
except geoip2.errors.AddressNotFoundError:
|
|
|
|
country = None
|
|
|
|
city = None
|
2018-10-07 10:11:59 +00:00
|
|
|
|
|
|
|
#
|
|
|
|
# temporary disabled it because of geoip services capcacity
|
|
|
|
#
|
|
|
|
#if city is None and response.location:
|
|
|
|
# coord = "%s, %s" % (response.location.latitude, response.location.longitude)
|
|
|
|
# try:
|
|
|
|
# location = geolocator.reverse(coord, language='en')
|
|
|
|
# city = location.raw.get('address', {}).get('city')
|
|
|
|
# except:
|
|
|
|
# pass
|
|
|
|
if city is None:
|
2018-10-26 22:19:22 +00:00
|
|
|
city, country = ip2location(ip_addr)
|
|
|
|
|
2018-10-26 22:58:42 +00:00
|
|
|
# workaround for the strange bug with the country name
|
|
|
|
# maybe some other countries has this problem too
|
|
|
|
if country == 'Russian Federation':
|
|
|
|
country = 'Russia'
|
|
|
|
|
2018-10-26 22:19:22 +00:00
|
|
|
if city:
|
|
|
|
return city, country
|
|
|
|
else:
|
|
|
|
return NOT_FOUND_LOCATION, None
|
2018-10-07 10:11:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
def location_canonical_name(location):
|
|
|
|
"Find canonical name for `location`"
|
|
|
|
|
|
|
|
location = location_normalize(location)
|
2019-02-04 11:12:02 +00:00
|
|
|
if location.lower() in LOCATION_ALIAS:
|
2018-10-07 10:11:59 +00:00
|
|
|
return LOCATION_ALIAS[location.lower()]
|
|
|
|
return location
|
|
|
|
|
|
|
|
def load_aliases(aliases_filename):
|
|
|
|
"""
|
|
|
|
Load aliases from the aliases file
|
|
|
|
"""
|
|
|
|
aliases_db = {}
|
|
|
|
with open(aliases_filename, 'r') as f_aliases:
|
|
|
|
for line in f_aliases.readlines():
|
|
|
|
from_, to_ = line.decode('utf-8').split(':', 1)
|
|
|
|
aliases_db[location_normalize(from_)] = location_normalize(to_)
|
|
|
|
return aliases_db
|
|
|
|
|
|
|
|
def load_iata_codes(iata_codes_filename):
|
|
|
|
"""
|
|
|
|
Load IATA codes from the IATA codes file
|
|
|
|
"""
|
|
|
|
with open(iata_codes_filename, 'r') as f_iata_codes:
|
|
|
|
result = []
|
|
|
|
for line in f_iata_codes.readlines():
|
|
|
|
result.append(line.strip())
|
|
|
|
return set(result)
|
|
|
|
|
|
|
|
LOCATION_ALIAS = load_aliases(ALIASES)
|
|
|
|
LOCATION_BLACK_LIST = [x.strip() for x in open(BLACKLIST, 'r').readlines()]
|
|
|
|
IATA_CODES = load_iata_codes(IATA_CODES_FILE)
|
|
|
|
|
|
|
|
def is_location_blocked(location):
|
2018-10-07 10:34:36 +00:00
|
|
|
"""
|
|
|
|
Return True if this location is blocked
|
|
|
|
or False if it is allowed
|
|
|
|
"""
|
2018-10-07 10:11:59 +00:00
|
|
|
return location is not None and location.lower() in LOCATION_BLACK_LIST
|
|
|
|
|
|
|
|
|
2018-10-07 10:34:36 +00:00
|
|
|
def location_processing(location, ip_addr):
|
|
|
|
"""
|
|
|
|
"""
|
2018-10-07 10:11:59 +00:00
|
|
|
|
2018-10-07 10:34:36 +00:00
|
|
|
# if location is starting with ~
|
|
|
|
# or has non ascii symbols
|
|
|
|
# it should be handled like a search term (for geolocator)
|
|
|
|
override_location_name = None
|
|
|
|
full_address = None
|
2019-01-11 09:58:08 +00:00
|
|
|
hide_full_address = False
|
|
|
|
force_show_full_address = location is not None and location.startswith('~')
|
2018-10-07 10:34:36 +00:00
|
|
|
|
2019-01-11 09:58:08 +00:00
|
|
|
# location ~ means that it should be detected automatically,
|
|
|
|
# and shown in the location line below the report
|
|
|
|
if location == '~':
|
|
|
|
location = None
|
|
|
|
|
2019-02-01 22:32:17 +00:00
|
|
|
if location and location.lstrip('~ ').startswith('@'):
|
2019-01-11 09:58:08 +00:00
|
|
|
try:
|
|
|
|
location, country = get_location(
|
|
|
|
socket.gethostbyname(
|
2019-02-01 22:32:17 +00:00
|
|
|
location.lstrip('~ ')[1:]))
|
2019-01-11 09:58:08 +00:00
|
|
|
location = '~' + location
|
2019-02-01 11:04:26 +00:00
|
|
|
if country:
|
|
|
|
location += ", %s" % country
|
2019-01-11 09:58:08 +00:00
|
|
|
hide_full_address = not force_show_full_address
|
|
|
|
except:
|
|
|
|
location, country = NOT_FOUND_LOCATION, None
|
|
|
|
|
|
|
|
query_source_location = get_location(ip_addr)
|
|
|
|
|
|
|
|
country = None
|
2019-07-07 16:39:53 +00:00
|
|
|
if not location or location == 'MyLocation':
|
|
|
|
location = ip_addr
|
2019-01-11 09:58:08 +00:00
|
|
|
|
2019-07-07 16:39:53 +00:00
|
|
|
if is_ip(location):
|
2019-01-11 09:58:08 +00:00
|
|
|
location, country = get_location(location)
|
|
|
|
|
|
|
|
# here too
|
|
|
|
if location:
|
|
|
|
location = '~' + location
|
2019-02-01 11:04:26 +00:00
|
|
|
if country:
|
|
|
|
location += ", %s" % country
|
2019-01-11 09:58:08 +00:00
|
|
|
hide_full_address = not force_show_full_address
|
|
|
|
|
|
|
|
if location and not location.startswith('~'):
|
2019-02-01 22:32:17 +00:00
|
|
|
tmp_location = location_canonical_name(location)
|
|
|
|
if tmp_location != location:
|
|
|
|
override_location_name = location
|
2019-02-04 11:12:02 +00:00
|
|
|
location = tmp_location
|
2019-01-11 09:58:08 +00:00
|
|
|
|
|
|
|
# up to this point it is possible that the name
|
|
|
|
# contains some unicode symbols
|
|
|
|
# here we resolve them
|
2019-08-25 18:37:54 +00:00
|
|
|
if location is not None: # and not ascii_only(location):
|
2019-02-01 22:32:17 +00:00
|
|
|
location = "~" + location.lstrip('~ ')
|
2018-10-07 10:34:36 +00:00
|
|
|
|
2019-08-25 18:37:54 +00:00
|
|
|
# if location is not None and location.upper() in IATA_CODES:
|
|
|
|
# location = '~%s' % location
|
2018-10-07 10:34:36 +00:00
|
|
|
|
|
|
|
if location is not None and location.startswith('~'):
|
|
|
|
geolocation = geolocator(location_canonical_name(location[1:]))
|
|
|
|
if geolocation is not None:
|
|
|
|
override_location_name = location[1:].replace('+', ' ')
|
|
|
|
location = "%s,%s" % (geolocation['latitude'], geolocation['longitude'])
|
2019-01-11 16:27:14 +00:00
|
|
|
country = None
|
2019-01-11 09:58:08 +00:00
|
|
|
if not hide_full_address:
|
|
|
|
full_address = geolocation['address']
|
|
|
|
else:
|
|
|
|
full_address = None
|
2018-10-07 10:34:36 +00:00
|
|
|
else:
|
|
|
|
location = NOT_FOUND_LOCATION #location[1:]
|
2018-10-26 22:19:22 +00:00
|
|
|
|
2018-10-07 10:34:36 +00:00
|
|
|
return location, \
|
|
|
|
override_location_name, \
|
|
|
|
full_address, \
|
|
|
|
country, \
|
|
|
|
query_source_location
|