<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-

# filename: openresolver.py
#
# author : j0lly [at] anche.no
# Open DNS Resolver crwaler
# For fun and profit
#
# Lcense BSD

from Queue import Queue
import dns.resolver
import threading
import iptools
import sys
import argparse

__version__ = '0.5'
__author__ = 'J0lly'
__date__ = '2014-11-23'
__email__ = '<EMAIL>'

class thread_resolve(threading.Thread):

    def __init__(self, nameserver, q, hostname, dns_type):
        threading.Thread.__init__(self)
        self.nameserver = nameserver
	self.q = q
	self.hostname = hostname
	self.dns_type = dns_type
    def run(self):
	resolve(self.nameserver, self.q, self.hostname, self.dns_type)

def resolve(nameserver, q, hostname, dns_record) :
	'''just resolve a well known dns query in order to check whetever the ip host is an open resolver'''

        # Set the DNS Server
	resolver = dns.resolver.Resolver()
	resolver.nameservers = [nameserver]
	resolver.timeout = 5
	resolver.lifetime = 5

	try:
	        print "resolving with %s"%(nameserver)
		for rdata in resolver.query(hostname, dns_record) :
                        print 'we can resolve %s to %s'%(hostname, rdata)
    			print 'adding %s to the list'%(nameserver)
                        q.put(nameserver)
	except:
		print 'no resolver for %s'%(nameserver)
		sys.exit()
	
def chunker(iterable, chunksize):
        return map(None,*[iter(iterable)]*chunksize)

def main():

        bad_networks = [ iptools.ipv4.PRIVATE_NETWORK_10, iptools.ipv4.LOOPBACK,
                        iptools.ipv4.LINK_LOCAL, iptools.ipv4.IPV6_TO_IPV4_RELAY,
                        iptools.ipv4.CURRENT_NETWORK, iptools.ipv4.DUAL_STACK_LITE,
                        iptools.ipv4.MULTICAST, iptools.ipv4.MULTICAST_INTERNETWORK,
                        iptools.ipv4.MULTICAST_LOCAL, iptools.ipv4.PRIVATE_NETWORK_172_16,
                        iptools.ipv4.PRIVATE_NETWORK_192_168, iptools.ipv4.RESERVED
                        ]
<commit_msg>some refactoring + exceptions handling<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

# filename: openresolver.py
#
# author : j0lly [at] anche.no
# Open DNS Resolver crwaler
# For fun and profit
#
# Lcense BSD

from Queue import Queue
import dns.resolver
import threading
import iptools
import sys
import argparse

__version__ = '0.7'
__author__ = 'J0lly'
__date__ = '2014-11-23'
__email__ = '<EMAIL>'

class thread_resolve(threading.Thread):

    def __init__(self, nameserver, q, hostname, dns_type):
        threading.Thread.__init__(self)
        self.nameserver = nameserver
	self.q = q
	self.hostname = hostname
	self.dns_type = dns_type
    def run(self):
	resolve(self.nameserver, self.q, self.hostname, self.dns_type)

def resolve(nameserver, q, hostname, dns_record) :
	'''just resolve a well known dns query in order to check whetever the ip host is an open resolver'''

        # Set the DNS Server
	resolver = dns.resolver.Resolver()
	resolver.nameservers = [nameserver]
	resolver.timeout = 5
	resolver.lifetime = 5
	print "resolving with %s"%(nameserver)

	try:
		rdata = resolver.query(hostname, dns_record)
	
	except dns.resolver.NoAnswer :
		print 'no resolver for %s'%(nameserver)
		sys.exit()
	except dns.rdatatype.UnknownRdatatype :
		print 'bad Record Type %s'%(dns_record)
		sys.exit()
	except dns.resolver.NXDOMAIN :
		print '%s is not existent'%(hostname)
		sys.exit()
	except dns.exception.Timeout :
		print 'timeout while performing query on %s'%(nameserver)
		sys.exit()
	except :
		print 'Unknown error'
		sys.exit()
	
	#rdata.response for complete response body
	print 'we can resolve %s'%(hostname)
    	print 'adding %s to the list'%(nameserver)
	q.put(nameserver)	

def chunker(iterable, chunksize):
        return map(None,*[iter(iterable)]*chunksize)

def main():

        bad_networks = [ iptools.ipv4.PRIVATE_NETWORK_10, iptools.ipv4.LOOPBACK,
                        iptools.ipv4.LINK_LOCAL, iptools.ipv4.IPV6_TO_IPV4_RELAY,
                        iptools.ipv4.CURRENT_NETWORK, iptools.ipv4.DUAL_STACK_LITE,
                        iptools.ipv4.MULTICAST, iptools.ipv4.MULTICAST_INTERNETWORK,
                        iptools.ipv4.MULTICAST_LOCAL, iptools.ipv4.PRIVATE_NETWORK_172_16,
                        iptools.ipv4.PRIVATE_NETWORK_192_168, iptools.ipv4.RESERVED
                        ]

{'fraction_non_alphanumeric': 0.10348006453099792, 'fraction_numerical': 0.019359299377736807, 'mean_word_length': 3.048507462686567, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4774960', 'n_tokens_mistral': 1629, 'n_tokens_neox': 1531, 'n_words': 341}