mirror of
https://github.com/adulau/DomainClassifier.git
synced 2024-11-23 02:17:07 +00:00
Better error catching
- Timeout of DNS reduce to 1 sec - Socket exception catched - Ranking default value is now 1.0
This commit is contained in:
parent
738060ab57
commit
0be08a31c6
1 changed files with 26 additions and 13 deletions
|
@ -27,6 +27,7 @@ class Extract:
|
||||||
self.rawtext = rawtext
|
self.rawtext = rawtext
|
||||||
self.presolver = dns.resolver.Resolver()
|
self.presolver = dns.resolver.Resolver()
|
||||||
self.presolver.nameservers = ['149.13.33.69']
|
self.presolver.nameservers = ['149.13.33.69']
|
||||||
|
self.presolver.lifetime = 1.0
|
||||||
self.bgprankingserver = 'pdns.circl.lu'
|
self.bgprankingserver = 'pdns.circl.lu'
|
||||||
self.vdomain = []
|
self.vdomain = []
|
||||||
|
|
||||||
|
@ -38,14 +39,16 @@ class Extract:
|
||||||
|
|
||||||
if ipaddr:
|
if ipaddr:
|
||||||
clook = IPy.IP(str(ipaddr)).reverseName().replace('.in-addr.arpa.','.origin.asn.cymru.com')
|
clook = IPy.IP(str(ipaddr)).reverseName().replace('.in-addr.arpa.','.origin.asn.cymru.com')
|
||||||
a = self.presolver.query(clook, 'TXT')
|
try: a = self.presolver.query(clook, 'TXT')
|
||||||
if a:
|
except dns.resolver.NXDOMAIN: return None
|
||||||
x = str(a[0]).split("|")
|
except dns.exception.Timeout: return None
|
||||||
# why so many spaces?
|
if a:
|
||||||
x = map (lambda t: t.replace("\"","").strip(), x)
|
x = str(a[0]).split("|")
|
||||||
return (x[0],x[2])
|
# why so many spaces?
|
||||||
else:
|
x = map (lambda t: t.replace("\"","").strip(), x)
|
||||||
return None
|
return (x[0],x[2])
|
||||||
|
else:
|
||||||
|
return None
|
||||||
"""__bgpanking return the ranking the float value of an ASN.
|
"""__bgpanking return the ranking the float value of an ASN.
|
||||||
"""
|
"""
|
||||||
def __bgpranking(self, asn=None):
|
def __bgpranking(self, asn=None):
|
||||||
|
@ -61,8 +64,13 @@ class Extract:
|
||||||
break
|
break
|
||||||
s.close()
|
s.close()
|
||||||
if len(r) > 0:
|
if len(r) > 0:
|
||||||
rank = r.split("\n")[1].split(",")[1]
|
try: rr = r.split("\n")[1].split(",")
|
||||||
return float(rank)
|
except IndexError: return None
|
||||||
|
if len(rr) > 1:
|
||||||
|
rank = rr[1]
|
||||||
|
return float(rank)
|
||||||
|
else:
|
||||||
|
return None
|
||||||
else:
|
else:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
@ -136,16 +144,21 @@ class Extract:
|
||||||
if self.validdomain:
|
if self.validdomain:
|
||||||
for dom in self.validdomain:
|
for dom in self.validdomain:
|
||||||
rank = None
|
rank = None
|
||||||
|
asn = None
|
||||||
if dom[1] == 'A':
|
if dom[1] == 'A':
|
||||||
ip = dom[2]
|
ip = dom[2]
|
||||||
asn = self.__origin(ipaddr=dom[2])[0]
|
o = self.__origin(ipaddr=dom[2])
|
||||||
|
if o:
|
||||||
|
asn = o[0]
|
||||||
rank = self.__bgpranking(asn)
|
rank = self.__bgpranking(asn)
|
||||||
t = (rank, dom[0])
|
t = (rank, dom[0])
|
||||||
self.rankdom.append(t)
|
self.rankdom.append(t)
|
||||||
elif dom[1] == 'CNAME':
|
elif dom[1] == 'CNAME':
|
||||||
cname = str(dom[2])
|
cname = str(dom[2])
|
||||||
ip = socket.gethostbyname(cname)
|
try: ip = socket.gethostbyname(cname)
|
||||||
asn = self.__origin(ipaddr=ip)[0]
|
except: continue
|
||||||
|
try: asn = self.__origin(ipaddr=ip)[0]
|
||||||
|
except TypeError: continue
|
||||||
rank = self.__bgpranking(asn)
|
rank = self.__bgpranking(asn)
|
||||||
t = (rank, dom[0])
|
t = (rank, dom[0])
|
||||||
self.rankdom.append(t)
|
self.rankdom.append(t)
|
||||||
|
|
Loading…
Reference in a new issue