I just got our company's new VPN client installed. Whow, installed and worked, can't believe it... *g*

But, why is every normal internet access soooo slow to start? Gotcha, it's the DNS server. All DNS queries have to been answered by the DNS server behind the VPN connection, because routing is done on IPs only. Ok, let's speed that up.

Here's the result, a simple DNS sever, that forwards all requests to two (or more) real DNS servers at the same time and returns the answer that comes first. Does some caching, too; no cache timeouts so, can always do that with Ctrl-C.

#!/usr/bin/perl use strict; use warnings; use Net::DNS; use Net::DNS::Nameserver; use Time::HiRes qw(sleep time); our @res = (); for my $nameserver ( '1.2.3.4', # enter some real ones here! '999.99.9.0', ) { my $res = Net::DNS::Resolver->new; $res->nameservers($nameserver); $res->retry(1); $res->recurse(1); $res->dnsrch(0); $res->udp_timeout(2); push @res, $res; } our %cache = ( '1.20.168.192.in-addr.arpa IN PTR' => [ # must reverse lookup my +self 'NOERROR', [Net::DNS::RR->new("1.20.168.192.in-addr.arpa. 86400 PTR local +host")], [], # VPN client doesn't allow 127.0.0.1 for DNS server, so [], # use one of my VMWARE interfaces for it {} ] ); sub reply_handler { my ($qname, $qclass, $qtype, $peerhost) = @_; print "\n"; # cache? if (exists $cache{"$qname $qclass $qtype"}) { print " Answering from cache\n"; return @{ $cache{"$qname $qclass $qtype"} }; } # send queries to all servers my @bgres = (); RES0: foreach my $res (@res) { print ' Querying '.$res->{nameservers}[0]."\n"; push @bgres, [ $res, $res->bgsend($qname, $qtype, $qclass) ]; } # ans wait for the answers my $starttime = time(); my $failedcount = 0; RES1: while (grep { defined($_) } @bgres) { # finish if all server +s have answered RES: foreach my $resl (@bgres) { next unless defined $resl; my ($res, $sock) = @$resl; next RES unless $res->bgisready($sock); my $answer = $res->bgread($sock); $resl = undef; # mark this server as 'has answered' next RES unless $answer; if ($answer->{header}->{rcode} eq 'NOERROR') { print " Gotcha from ".$res->{nameservers}[0]."!\n"; $cache{"$qname $qclass $qtype"} = [ $answer->{header}->{rcode}, $answer->{answer}, $answer->{authority}, $answer->{additional}, { aa => $answer->{header}->{aa}, ad => $answer->{header}->{ad}, ra => $answer->{header}->{ra} } ]; return @{ $cache{"$qname $qclass $qtype"} }; } else { print " Failed from ".$res->{nameservers}[0]."!\n"; $failedcount++; } } last RES1 if (time() - $starttime) > 5; # finish after timeout sleep 0.05; # be nice to out CPU } if ($failedcount == @bgres) { # cache negative answers only if there was no timeout, # that is, when all servers reported NXDOMAIN... $cache{"$qname $qclass $qtype"} = [ 'NXDOMAIN', [], [], [], +{} ]; } return ( 'NXDOMAIN', [], [], [], {} ); } # add some interface to it, if you're not firewalled already my $ns = Net::DNS::Nameserver->new( LocalPort => 53, ReplyHandler => \&reply_handler, Verbose => 1, ) || die "couldn't create nameserver object\n"; while (1) { eval { $ns->main_loop; } }
For this to work for you, you must enter some real name server addresses. I'd suggest to use one from behind the VPN tunnel und one from your internet connection.

You also must change the IP address in the precached PTR record to match the IP you'll let this program run on.

Oh, I nearly forgot: FOR PERSONAL USE ONLY. NOT SUITABLE FOR MULTIUSER USAGE. MUST BE PROTECTED FROM UNAUTHORIZED ACCESS---SECURITY RISK! DON'T CONFIGURE TOO MANY DNS SERVERS. DON'T CONFIGURE DNS SERVER YOU'RE NOT AUTHORIZED TO USE.

Feel free to ask questions or make suggestions.


Search, Ask, Know

In reply to Speeding up DNS queries for VPN connections by Beechbone

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.