diff --git a/.gitmodules b/.gitmodules index 1ac9b49..e69de29 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +0,0 @@ -[submodule "cymruwhois"] - path = cymruwhois - url = https://github.com/JustinAzoff/python-cymruwhois.git diff --git a/MANIFEST.in b/MANIFEST.in index b6c0f3f..427cfae 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -1,2 +1 @@ include LICENSE README.md TODO.md public-servers.txt -include cymruwhois/*.py diff --git a/cymruwhois b/cymruwhois deleted file mode 160000 index a345433..0000000 --- a/cymruwhois +++ /dev/null @@ -1 +0,0 @@ -Subproject commit a34543335cbef02b1b615e774ce5b6187afb0cc2 diff --git a/dnseval.py b/dnseval.py index 892a036..d282e3b 100755 --- a/dnseval.py +++ b/dnseval.py @@ -40,7 +40,7 @@ import dns.resolver __author__ = 'Babak Farrokhi (babak@farrokhi.net)' __license__ = 'BSD' -__version__ = "1.6.2" +__version__ = "1.6.3" __progname__ = os.path.basename(sys.argv[0]) shutdown = False diff --git a/dnsping.py b/dnsping.py index 6d075a7..eab8e75 100755 --- a/dnsping.py +++ b/dnsping.py @@ -40,7 +40,7 @@ import dns.resolver __author__ = 'Babak Farrokhi (babak@farrokhi.net)' __license__ = 'BSD' -__version__ = "1.6.2" +__version__ = "1.6.3" __progname__ = os.path.basename(sys.argv[0]) shutdown = False diff --git a/dnstraceroute.py b/dnstraceroute.py index 4c66301..705585f 100755 --- a/dnstraceroute.py +++ b/dnstraceroute.py @@ -39,13 +39,20 @@ import dns.query import dns.rdatatype import dns.resolver -from cymruwhois import cymruwhois +import cymruwhois +# Global Variables __author__ = 'Babak Farrokhi (babak@farrokhi.net)' __license__ = 'BSD' -__version__ = "1.6.2" +__version__ = "1.6.3" _ttl = None quiet = False +whois_cache = {} +shutdown = False + +# Constants +__progname__ = os.path.basename(sys.argv[0]) +WHOIS_CACHE = 'whois.cache' class CustomSocket(socket.socket): @@ -64,11 +71,6 @@ def test_import(): pass -# Constants -__progname__ = os.path.basename(sys.argv[0]) -WHOIS_CACHE = 'whois.cache' - - class Colors(object): N = '\033[m' # native R = '\033[31m' # red @@ -85,35 +87,41 @@ class Colors(object): self.B = '' -# Globarl Variables -shutdown = False - - -def whoisrecord(ip): +def whois_lookup(ip): try: - currenttime = time.perf_counter() + global whois_cache + currenttime = time.time() ts = currenttime - if ip in whois: - asn, ts = whois[ip] + if ip in whois_cache: + asn, ts = whois_cache[ip] else: ts = 0 if (currenttime - ts) > 36000: c = cymruwhois.Client() asn = c.lookup(ip) - whois[ip] = (asn, currenttime) + whois_cache[ip] = (asn, currenttime) return asn except Exception as e: return e -try: - pkl_file = open(WHOIS_CACHE, 'rb') +def load_whois_cache(cachefile): try: - whois = pickle.load(pkl_file) - except EOFError: + pkl_file = open(cachefile, 'rb') + try: + whois = pickle.load(pkl_file) + pkl_file.close() + except Exception: + whois = {} + except IOError: whois = {} -except IOError: - whois = {} + return whois + + +def save_whois_cache(cachefile, whois_data): + pkl_file = open(cachefile, 'wb') + pickle.dump(whois_data, pkl_file) + pkl_file.close() def usage(): @@ -367,11 +375,11 @@ def main(): if curr_addr: as_name = "" if as_lookup: - asn = whoisrecord(curr_addr) + asn = whois_lookup(curr_addr) as_name = '' try: if asn and asn.asn != "NA": - as_name = "[%s %s] " % (asn.asn, asn.owner) + as_name = "[AS%s %s] " % (asn.asn, asn.owner) except AttributeError: if shutdown: sys.exit(0) @@ -404,7 +412,7 @@ def main(): if __name__ == '__main__': try: + whois_cache = load_whois_cache(WHOIS_CACHE) main() finally: - pkl_file = open(WHOIS_CACHE, 'wb') - pickle.dump(whois, pkl_file) + save_whois_cache(WHOIS_CACHE, whois_cache) diff --git a/requirements.txt b/requirements.txt index fae5618..43b4b3f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1,2 @@ dnspython>=1.15.0 +cymruwhois>=1.6 diff --git a/setup.py b/setup.py index 9eb423a..3d5edab 100644 --- a/setup.py +++ b/setup.py @@ -2,10 +2,10 @@ from setuptools import setup, find_packages setup( name="dnsdiag", - version="1.6.2", + version="1.6.3", packages=find_packages(), scripts=["dnseval.py", "dnsping.py", "dnstraceroute.py"], - install_requires=['dnspython>=1.15.0'], + install_requires=['dnspython>=1.15.0', 'cymruwhois>=1.6'], classifiers=[ "Topic :: System :: Networking",