From 44aa3d19d4e291ddb209ffb5d6dc15c03f3387ae Mon Sep 17 00:00:00 2001 From: Baptiste Jonglez Date: Mon, 27 Jan 2014 22:13:23 +0100 Subject: [PATCH 1/5] Add support for a configurable whois server --- lg.cfg | 2 ++ lg.py | 5 ++++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/lg.cfg b/lg.cfg index 29678b4..9211d03 100644 --- a/lg.cfg +++ b/lg.cfg @@ -21,4 +21,6 @@ AS_NUMBER = { "h3" : "197422" } +#WHOIS_SERVER = "whois.foo.bar" + SESSION_KEY = '\xd77\xf9\xfa\xc2\xb5\xcd\x85)`+H\x9d\xeeW\\%\xbe/\xbaT\x89\xe8\xa7' diff --git a/lg.py b/lg.py index a0fcc35..7d409d7 100644 --- a/lg.py +++ b/lg.py @@ -96,7 +96,10 @@ def set_session(request_type, hosts, proto, request_args): def whois_command(query): - return subprocess.Popen(['whois', query], stdout=subprocess.PIPE).communicate()[0].decode('utf-8', 'ignore') + server = [] + if app.config.get("WHOIS_SERVER", ""): + server = [ "-h", app.config.get("WHOIS_SERVER") ] + return subprocess.Popen(['whois'] + server + [query], stdout=subprocess.PIPE).communicate()[0].decode('utf-8', 'ignore') def bird_command(host, proto, query): From ada668055de9b771707afce124dab3bce66d9a3e Mon Sep 17 00:00:00 2001 From: Baptiste Jonglez Date: Mon, 27 Jan 2014 22:33:37 +0100 Subject: [PATCH 2/5] =?UTF-8?q?Add=20support=20for=20configuring=20the=20D?= =?UTF-8?q?NS-based=20ASN=20=E2=86=92=20name=20mapping=20service.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit It's a bit hackish to make it configurable. --- toolbox.py | 4 +++- toolboxconfig.py | 2 ++ 2 files changed, 5 insertions(+), 1 deletion(-) create mode 100644 toolboxconfig.py diff --git a/toolbox.py b/toolbox.py index 1543b85..b7a6d78 100644 --- a/toolbox.py +++ b/toolbox.py @@ -19,6 +19,8 @@ # ### +from toolboxconfig import ASN_ZONE + from dns import resolver import socket import pickle @@ -28,7 +30,7 @@ def resolve(n, q): return str(resolver.query(n,q)[0]) def get_asn_from_as(n): - data = resolve("AS%s.asn.cymru.com" % n ,"TXT").replace("'","").replace('"','') + data = resolve("AS%s.%s" % (n, ASN_ZONE) ,"TXT").replace("'","").replace('"','') return [ field.strip() for field in data.split("|") ] def mask_is_valid(n): diff --git a/toolboxconfig.py b/toolboxconfig.py new file mode 100644 index 0000000..b26d5da --- /dev/null +++ b/toolboxconfig.py @@ -0,0 +1,2 @@ +# DNS zone to query for ASN -> name mapping +ASN_ZONE = "asn.cymru.com" From fc6472071b6e265cc2ced5b9d3aff428e4d39131 Mon Sep 17 00:00:00 2001 From: Baptiste Jonglez Date: Tue, 28 Jan 2014 15:39:13 +0100 Subject: [PATCH 3/5] Cleaner solution for making the ASN service configurable --- lg.cfg | 3 +++ lg.py | 8 +++++++- toolbox.py | 4 ---- toolboxconfig.py | 2 -- 4 files changed, 10 insertions(+), 7 deletions(-) delete mode 100644 toolboxconfig.py diff --git a/lg.cfg b/lg.cfg index 9211d03..55db922 100644 --- a/lg.cfg +++ b/lg.cfg @@ -23,4 +23,7 @@ AS_NUMBER = { #WHOIS_SERVER = "whois.foo.bar" +# DNS zone to query for ASN -> name mapping +ASN_ZONE = "asn.cymru.com" + SESSION_KEY = '\xd77\xf9\xfa\xc2\xb5\xcd\x85)`+H\x9d\xeeW\\%\xbe/\xbaT\x89\xe8\xa7' diff --git a/lg.py b/lg.py index 7d409d7..c39aa22 100644 --- a/lg.py +++ b/lg.py @@ -29,7 +29,7 @@ from urllib import quote, unquote import json import random -from toolbox import mask_is_valid, ipv6_is_valid, ipv4_is_valid, resolve, save_cache_pickle, load_cache_pickle, get_asn_from_as, unescape +from toolbox import mask_is_valid, ipv6_is_valid, ipv4_is_valid, resolve, save_cache_pickle, load_cache_pickle, unescape #from xml.sax.saxutils import escape @@ -46,6 +46,12 @@ file_handler.setLevel(getattr(logging, app.config["LOG_LEVEL"].upper())) app.logger.addHandler(file_handler) +def get_asn_from_as(n): + asn_zone = app.config.get("ASN_ZONE", "asn.cymru.com") + data = resolve("AS%s.%s" % (n, asn_zone) ,"TXT").replace("'","").replace('"','') + return [ field.strip() for field in data.split("|") ] + + def add_links(text): """Browser a string and replace ipv4, ipv6, as number, with a whois link """ diff --git a/toolbox.py b/toolbox.py index b7a6d78..8e4cd75 100644 --- a/toolbox.py +++ b/toolbox.py @@ -29,10 +29,6 @@ import xml.parsers.expat def resolve(n, q): return str(resolver.query(n,q)[0]) -def get_asn_from_as(n): - data = resolve("AS%s.%s" % (n, ASN_ZONE) ,"TXT").replace("'","").replace('"','') - return [ field.strip() for field in data.split("|") ] - def mask_is_valid(n): if not n: return True diff --git a/toolboxconfig.py b/toolboxconfig.py deleted file mode 100644 index b26d5da..0000000 --- a/toolboxconfig.py +++ /dev/null @@ -1,2 +0,0 @@ -# DNS zone to query for ASN -> name mapping -ASN_ZONE = "asn.cymru.com" From 3fae79ed543a40aca397844e20743fda06b3e9b2 Mon Sep 17 00:00:00 2001 From: Baptiste Jonglez Date: Tue, 28 Jan 2014 15:32:27 +0100 Subject: [PATCH 4/5] Fix bgpmap (Graphviz does not seem to like empty labels) --- lg.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lg.py b/lg.py index c39aa22..74a49ba 100644 --- a/lg.py +++ b/lg.py @@ -467,7 +467,10 @@ def show_bgpmap(): add_node(_as, fillcolor=(first and "#F5A9A9" or "white")) - edge = add_edge(nodes[previous_as], nodes[_as] , label=hop_label, fontsize="7") + if hop_label: + edge = add_edge(nodes[previous_as], nodes[_as], label=hop_label, fontsize="7") + else: + edge = add_edge(nodes[previous_as], nodes[_as], fontsize="7") hop_label = "" From 48dc1046edc2a9c4a6208d338e30f67760088f9b Mon Sep 17 00:00:00 2001 From: Baptiste Jonglez Date: Tue, 28 Jan 2014 15:45:03 +0100 Subject: [PATCH 5/5] Catch possible exceptions thrown by the ASN DNS resolver --- lg.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lg.py b/lg.py index 74a49ba..949ea84 100644 --- a/lg.py +++ b/lg.py @@ -48,7 +48,10 @@ app.logger.addHandler(file_handler) def get_asn_from_as(n): asn_zone = app.config.get("ASN_ZONE", "asn.cymru.com") - data = resolve("AS%s.%s" % (n, asn_zone) ,"TXT").replace("'","").replace('"','') + try: + data = resolve("AS%s.%s" % (n, asn_zone) ,"TXT").replace("'","").replace('"','') + except: + return " "*5 return [ field.strip() for field in data.split("|") ]