Log ip resolution queries

pull/574/head
Igor Chubin 4 years ago
parent a6d0adbbf7
commit fe39b77813

@ -32,8 +32,10 @@ Exports:
is_location_blocked is_location_blocked
""" """
from __future__ import print_function from __future__ import print_function
import datetime
import json import json
import os import os
import socket import socket
@ -46,12 +48,15 @@ import requests
from globals import GEOLITE, GEOLOCATOR_SERVICE, IP2LCACHE, IP2LOCATION_KEY, NOT_FOUND_LOCATION, \ from globals import GEOLITE, GEOLOCATOR_SERVICE, IP2LCACHE, IP2LOCATION_KEY, NOT_FOUND_LOCATION, \
ALIASES, BLACKLIST, IATA_CODES_FILE, IPLOCATION_ORDER, IPINFO_TOKEN ALIASES, BLACKLIST, IATA_CODES_FILE, IPLOCATION_ORDER, IPINFO_TOKEN
GEOIP_READER = geoip2.database.Reader(GEOLITE) GEOIP_READER = geoip2.database.Reader(GEOLITE)
COUNTRY_MAP = {"Russian Federation": "Russia"} COUNTRY_MAP = {"Russian Federation": "Russia"}
def _debug_log(s): def _debug_log(s):
with open("/tmp/debug.log", "a") as f: if os.environ.get("WTTR_DEBUG_LOCATION"):
f.write(s+"\n") with open("/tmp/location-debug.log", "a") as f:
f.write("%s %s\n" % (datetime.datetime.now(),s))
def _is_ip(ip_addr): def _is_ip(ip_addr):
@ -140,8 +145,6 @@ def _ipcache(ip_addr):
TODO: When cache becomes more robust, transition to using latlong TODO: When cache becomes more robust, transition to using latlong
""" """
cachefile = os.path.join(IP2LCACHE, ip_addr) cachefile = os.path.join(IP2LCACHE, ip_addr)
if not os.path.exists(IP2LCACHE):
os.makedirs(IP2LCACHE)
if os.path.exists(cachefile): if os.path.exists(cachefile):
try: try:
@ -151,6 +154,8 @@ def _ipcache(ip_addr):
# cache entry is malformed: should be # cache entry is malformed: should be
# [ccode];country;region;city;[lat];[long];... # [ccode];country;region;city;[lat];[long];...
return None return None
else:
_debug_log("[_ipcache] %s not found" % ip_addr)
return None return None
@ -168,6 +173,7 @@ def _ip2location(ip_addr):
if not IP2LOCATION_KEY: if not IP2LOCATION_KEY:
return None return None
try: try:
_debug_log("[_ip2location] %s search" % ip_addr)
r = requests.get( r = requests.get(
'http://api.ip2location.com/?ip=%s&key=%s&package=WS3' # WS5 provides latlong 'http://api.ip2location.com/?ip=%s&key=%s&package=WS3' # WS5 provides latlong
% (ip_addr, IP2LOCATION_KEY)) % (ip_addr, IP2LOCATION_KEY))
@ -177,11 +183,13 @@ def _ip2location(ip_addr):
parts = location.split(';') parts = location.split(';')
if len(parts) >= 4: if len(parts) >= 4:
# ccode, country, region, city, (rest) # ccode, country, region, city, (rest)
_debug_log("[_ip2location] %s found" % ip_addr)
return [parts[3], parts[2], parts[1], parts[0]] + parts[4:] return [parts[3], parts[2], parts[1], parts[0]] + parts[4:]
return None return None
except requests.exceptions.RequestException: except requests.exceptions.RequestException:
return None return None
def _ipinfo(ip_addr): def _ipinfo(ip_addr):
if not IPINFO_TOKEN: if not IPINFO_TOKEN:
return None return None
@ -272,6 +280,7 @@ def _location_canonical_name(location):
return LOCATION_ALIAS[location.lower()] return LOCATION_ALIAS[location.lower()]
return location return location
def _load_aliases(aliases_filename): def _load_aliases(aliases_filename):
""" """
Load aliases from the aliases file Load aliases from the aliases file
@ -287,6 +296,7 @@ def _load_aliases(aliases_filename):
aliases_db[_location_normalize(from_)] = _location_normalize(to_) aliases_db[_location_normalize(from_)] = _location_normalize(to_)
return aliases_db return aliases_db
def _load_iata_codes(iata_codes_filename): def _load_iata_codes(iata_codes_filename):
""" """
Load IATA codes from the IATA codes file Load IATA codes from the IATA codes file
@ -297,10 +307,12 @@ def _load_iata_codes(iata_codes_filename):
result.append(line.strip()) result.append(line.strip())
return set(result) return set(result)
LOCATION_ALIAS = _load_aliases(ALIASES) LOCATION_ALIAS = _load_aliases(ALIASES)
LOCATION_BLACK_LIST = [x.strip() for x in open(BLACKLIST, 'r').readlines()] LOCATION_BLACK_LIST = [x.strip() for x in open(BLACKLIST, 'r').readlines()]
IATA_CODES = _load_iata_codes(IATA_CODES_FILE) IATA_CODES = _load_iata_codes(IATA_CODES_FILE)
def is_location_blocked(location): def is_location_blocked(location):
""" """
Return True if this location is blocked Return True if this location is blocked
@ -351,6 +363,7 @@ def _fully_qualified_location(location, region, country):
location += ", %s" % country location += ", %s" % country
return location return location
def location_processing(location, ip_addr): def location_processing(location, ip_addr):
""" """
""" """
@ -450,10 +463,8 @@ def _main_():
for filename in glob.glob(os.path.join(IP2LCACHE, "*")): for filename in glob.glob(os.path.join(IP2LCACHE, "*")):
ip_address = os.path.basename(filename) ip_address = os.path.basename(filename)
if not _ipcache(ip_address): if not _ipcache(ip_address):
print(ip_address)
shutil.move(filename, os.path.join("/wttr.in/cache/ip2l-broken", ip_address)) shutil.move(filename, os.path.join("/wttr.in/cache/ip2l-broken", ip_address))
if __name__ == "__main__": if __name__ == "__main__":
#_main_() print(_geoip("173.216.90.56"))
print(_get_location("104.26.4.59"))

Loading…
Cancel
Save