Use /etc/hosts for all forward/reverse DNS

This commit is contained in:
Oliver Gorwits
2014-07-17 13:43:04 +01:00
parent 91c09305e9
commit 2a77e13e10
2 changed files with 25 additions and 11 deletions

View File

@@ -4,6 +4,7 @@
* Custom path handling for DataTables ajax calls * Custom path handling for DataTables ajax calls
* [#116] Log true client IP when running through a proxy * [#116] Log true client IP when running through a proxy
* Use /etc/hosts for all forward/reverse DNS
2.028004 - 2014-07-16 2.028004 - 2014-07-16

View File

@@ -8,6 +8,13 @@ use Net::DNS;
use AnyEvent::DNS; use AnyEvent::DNS;
use NetAddr::IP::Lite ':lower'; use NetAddr::IP::Lite ':lower';
use base 'Exporter';
our @EXPORT = ();
our @EXPORT_OK = qw/
hostname_from_ip hostnames_resolve_async ipv4_from_hostname
/;
our %EXPORT_TAGS = (all => \@EXPORT_OK);
# AE::DNS::EtcHosts only works for A/AAAA/SRV, but we want PTR. # AE::DNS::EtcHosts only works for A/AAAA/SRV, but we want PTR.
# this loads+parses /etc/hosts file using AE. dirty hack. # this loads+parses /etc/hosts file using AE. dirty hack.
use AnyEvent::Socket 'format_address'; use AnyEvent::Socket 'format_address';
@@ -15,12 +22,10 @@ use AnyEvent::DNS::EtcHosts;
AnyEvent::DNS::EtcHosts::_load_hosts_unless(sub{},AE::cv); AnyEvent::DNS::EtcHosts::_load_hosts_unless(sub{},AE::cv);
no AnyEvent::DNS::EtcHosts; # unimport no AnyEvent::DNS::EtcHosts; # unimport
use base 'Exporter'; our %HOSTS = ();
our @EXPORT = (); $HOSTS{$_} = [ map { [ $_ ? (format_address $_->[0]) : '' ] }
our @EXPORT_OK = qw/ @{$AnyEvent::DNS::EtcHosts::HOSTS{$_}} ]
hostname_from_ip hostnames_resolve_async ipv4_from_hostname for keys %AnyEvent::DNS::EtcHosts::HOSTS;
/;
our %EXPORT_TAGS = (all => \@EXPORT_OK);
=head1 NAME =head1 NAME
@@ -47,6 +52,13 @@ sub hostname_from_ip {
my $ip = shift; my $ip = shift;
return unless $ip; return unless $ip;
# check /etc/hosts file and short-circuit if found
foreach my $name (reverse sort keys %HOSTS) {
if ($HOSTS{$name}->[0]->[0] eq $ip) {
return $name;
}
}
my $res = Net::DNS::Resolver->new; my $res = Net::DNS::Resolver->new;
my $query = $res->search($ip); my $query = $res->search($ip);
@@ -72,6 +84,12 @@ sub ipv4_from_hostname {
my $name = shift; my $name = shift;
return unless $name; return unless $name;
# check /etc/hosts file and short-circuit if found
if (exists $HOSTS{$name}) {
my $ip = NetAddr::IP::Lite->new($HOSTS{$name}->[0]->[0]);
return $ip->addr if $ip and $ip->bits == 32;
}
my $res = Net::DNS::Resolver->new; my $res = Net::DNS::Resolver->new;
my $query = $res->search($name); my $query = $res->search($name);
@@ -104,11 +122,6 @@ addresses which resolved.
sub hostnames_resolve_async { sub hostnames_resolve_async {
my $ips = shift; my $ips = shift;
my %HOSTS = ();
$HOSTS{$_} = [ map { [ $_ ? (format_address $_->[0]) : '' ] }
@{$AnyEvent::DNS::EtcHosts::HOSTS{$_}} ]
for keys %AnyEvent::DNS::EtcHosts::HOSTS;
# Set up the condvar # Set up the condvar
my $done = AE::cv; my $done = AE::cv;
$done->begin( sub { shift->send } ); $done->begin( sub { shift->send } );