#!/usr/bin/perl -w # i (df) wrote this mainly to make sure that the deepdns IPs are configured correctly # but since it also tells you how long it took to receive a response maybe someone # would want to use this to see which DNS server would be fastest for them $|++; use strict; use IO::Socket; # Debian: apt-get install libtime-hires-perl # RHEL: yum install perl-Time-HiRes use Time::HiRes qw( gettimeofday tv_interval); # Debian: apt-get install libnet-dns-perl # RHEL: yum install perl-Net-DNS use Net::DNS; # Should already be included in your Perl installation use Term::ANSIColor qw(:constants); my (@god, @dammit); print "Getting DeepDNS IPs...\n"; my @ddns_ips = gethostbyname("public.deepdns.net") or die "Can't resolve public.deepdns.net: $!\n"; @ddns_ips = map { inet_ntoa($_) } @ddns_ips[4 .. $#ddns_ips]; print "Checking regular DNS resolution...\n"; for (@ddns_ips) { &check($_,"whoami.cryptostorm.is"); } print "Checking .onion resolution...\n"; for (@ddns_ips) { &check($_,"stormwayszuh4juycoy4kwoww5gvcu2c4tdtpkup667pdwe4qenzwayd.onion"); } print "Checking .i2p resolution...\n"; for (@ddns_ips) { &check($_,"stats.i2p"); } sub check { our $ip = $_[0]; our $host = $_[1]; our $message; sub timeout { print "$ip " . BLINK BOLD RED "timed out when resolving", RESET . " $host\n"; close($message); } eval { local $SIG{ALRM} = sub { &timeout; }; alarm 10; my $now = [gettimeofday()]; my ($datagram,$flags); my $data = &lookup("$host",$ip); if (defined($data)) { if ($data =~ /^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/) { my $andagain = tv_interval($now)*1000; my $ms = sprintf("%d", $andagain); # comment out the below line to only show broken things print "$ip " . BOLD GREEN "correctly resolved $host", RESET . " and responded in $ms ms\n"; if ($ms =~ /^[0-9]{3}$/) { $ms =~ s/^/0/; } if ($ms =~ /^[0-9]{2}$/) { $ms =~ s/^/00/; } if ($ms =~ /^[0-9]{1}$/) { $ms =~ s/^/000/; } push(@god,"$ms:$ip"); } } alarm 0; } } sub lookup { my ( $hostname, $server ) = @_; my $res = new Net::DNS::Resolver; $res->nameservers($server); my $packet = $res->query($hostname); if ( !$packet ) { warn "$server responded, " . BLINK BOLD RED "but didn't return any data for $hostname", RESET . "\n"; return; } my (@results); foreach my $rr ( $packet->answer ) { push ( @results, $rr->address ); } return join( ', ', sort @results ); }