def trace(self, host, use_cache=True, dont_cache=False): """ This is the main interface of AsnTracer class to the outside world. Given a host it performs a traceroute to the host and then looksup the asns of each hop in the traceroute and returns the result. If the mapping fails it returns the whois info instead (when possible). Since this is the most time consuming part of the torpaths program, it uses a cache to avoid performing traceroutes, IP to asn mappings and whois lookups multiple times. To always perform a lookup even if the result is in the cache pass use_cache=False as an argument. To prevent a result from being cache pass dont_cache=True as argument. Note that if a host appears multiple times in a traceroute it will only appear once in the results. If it is a private address then it will not be included at all. """ if use_cache and host in self.trace_cache: return self.trace_cache[host] ans, unans = scapy_traceroute(host) traceroute_hops = set(result.src for _, result in ans if not is_addr_private(result.src)) result = self.hosts_to_asns(traceroute_hops) if not dont_cache: self.trace_cache[host] = result return result
def _trace(self, host): """ Traces the Asns to a host and to the nameservers used to find the host. Returns the nameservers queried, and the Asns traversed to each host. """ def asn_tracer_dns_helper(contacted_host): """ For use on DNS servers. """ return { 'host': contacted_host, 'traversed_asns': self.asn_tracer.trace(contacted_host) } dirty_queried_dns_servers = self.dns_tracer.trace(host) queried_dns_servers = [dns_server for dns_server in dirty_queried_dns_servers if dns_server and not is_addr_private(dns_server)] return { 'host': host, 'traversed_asns': self.asn_tracer.trace(host), 'queried_dns_servers': self.pool.map( asn_tracer_dns_helper, queried_dns_servers ) }
def trace(self, host, use_cache=True, dont_cache=False): """ This is the main interface of AsnTracer class to the outside world. Given a host it performs a traceroute to the host and then looksup the asns of each hop in the traceroute and returns the result. If the mapping fails it returns the whois info instead (when possible). Since this is the most time consuming part of the torpaths program, it uses a cache to avoid performing traceroutes, IP to asn mappings and whois lookups multiple times. To always perform a lookup even if the result is in the cache pass use_cache=False as an argument. To prevent a result from being cache pass dont_cache=True as argument. Note that if a host appears multiple times in a traceroute it will only appear once in the results. If it is a private address then it will not be included at all. """ if use_cache and host in self.trace_cache: return self.trace_cache[host] ans, unans = scapy_traceroute(host) traceroute_hops = set( result.src for _, result in ans if not is_addr_private(result.src) ) result = self.hosts_to_asns(traceroute_hops) if not dont_cache: self.trace_cache[host] = result return result
def _trace(self, host): """ Traces the Asns to a host and to the nameservers used to find the host. Returns the nameservers queried, and the Asns traversed to each host. """ def asn_tracer_dns_helper(contacted_host): """ For use on DNS servers. """ return { 'host': contacted_host, 'traversed_asns': self.asn_tracer.trace(contacted_host) } dirty_queried_dns_servers = self.dns_tracer.trace(host) queried_dns_servers = [ dns_server for dns_server in dirty_queried_dns_servers if dns_server and not is_addr_private(dns_server) ] return { 'host': host, 'traversed_asns': self.asn_tracer.trace(host), 'queried_dns_servers': self.pool.map(asn_tracer_dns_helper, queried_dns_servers) }
def trace(self, domain_name): """ The main interface of the DNSTracer class. Takes a domain_name as an argument and returns a list of nameservers that must be queried to receive an answer """ # TODO: use scapy for this instead of dig and envoy proc = envoy.run('dig +trace ' + domain_name) assert proc.status_code == 0 results = self.REGEX.findall(proc.std_out) return [result for result in results if not is_addr_private(result)]
def trace(self, domain_name): """ The main interface of the DNSTracer class. Takes a domain_name as an argument and returns a list of nameservers that must be queried to receive an answer """ # TODO: use scapy for this instead of dig and envoy proc = envoy.run('dig +trace '+ domain_name) assert proc.status_code == 0 results = self.REGEX.findall(proc.std_out) return [result for result in results if not is_addr_private(result)]