Permalink
Fetching contributors…
Cannot retrieve contributors at this time
executable file 154 lines (106 sloc) 3.66 KB
#!/usr/bin/env perl
use lib 'lib';
use GeoDNS;
use Net::DNS;
use Net::DNS::Nameserver;
use strict;
use warnings;
use POSIX qw(setuid);
use Getopt::Long;
use Socket;
GetOptions('interface=s@' => \(my $opt_interface),
'user=s' => \(my $opt_user),
'verbose!' => \(my $opt_verbose = 0),
'config=s' => \(my $opt_config = 'pgeodns.conf'),
'configtest!' => \(my $opt_configtest = 0),
'development!' => \(my $opt_development = 0),
'port=i' => \(my $opt_port = 53)
)
or die "invalid options";
exit !GeoDNS::load_config({}, $opt_config)
if $opt_configtest;
die "--interface [ip|hostname] required\n" unless $opt_interface;
die "--user [user|uid] required\n" if $> == 0 and !$opt_user;
$opt_interface = [$opt_interface] unless ref $opt_interface;
$opt_interface = [map { split /\s*,\s*/ } @{$opt_interface}];
for my $i (0 .. scalar @{$opt_interface} - 1) {
my $localaddr = $opt_interface->[$i];
# TODO: Do a proper IP address check
if ($localaddr =~ /[^\d\.:a-f]/) {
my $addr = inet_ntoa((gethostbyname($localaddr))[4]);
die "could not lookup $localaddr\n" unless $addr;
$opt_interface->[$i] = $addr;
}
}
my $g = GeoDNS->new(
server_id => $opt_interface->[0],
development => ($opt_development ? 1 : 0),
log => 1,
);
printf "\nStarting GeoDNS %s\n", $g->version_full;
my $ns = Net::DNS::Nameserver->new(
LocalPort => $opt_port,
LocalAddr => $opt_interface,
ReplyHandler => sub {
my @reply = $g->reply_handler(@_);
#warn Data::Dumper->Dump([\@reply], [qw(reply)]);
@reply;
},
Verbose => $opt_verbose,
);
# print error?
die "couldn't create nameserver object\n" unless $ns;
if (my $uid = $opt_user) {
$uid = getpwnam($uid) or die "could not lookup uid"
if $uid =~ m/\D/;
setuid($uid) or die "could not setuid: $!";
}
$g->load_config($opt_config);
if ($ns) {
$ns->main_loop;
}
else {
die "couldn't create nameserver object\n";
}
__END__
=pod
=head1 NAME
pgeodns - Perl Geographic DNS Server
=head1 OVERVIEW
A small perl dns server for distributing different replies based on
the source location of the request. It uses Geo::IP to make the
determination.
=head1 OPTIONS
=over 4
=item --interface [ip]
The interface to bind to. Can be specified multiple times and contain
comma separated IPs for specifying multiple interfaces. The first
interface will be used as the "server id" in diagnostic outputs.
If a hostname is used instead of an IP, a DNS lookup will be done on
startup to find the IP address.
=item --user [user / uid]
The username or uid to run as after binding to port 53.
=item --config [config file]
Base configuration file; defaults to ./pgeodns.conf
=item --verbose
Print even more status output.
=back
=head1 CONFIGURATION
pgeodns.conf in the current directory. Review it and the included
samples in config/* until it gets documented. :-)
=head1 REFERENCES
RFC2308 http://www.faqs.org/rfcs/rfc2308.html
=head1 BUGS? COMMENTS?
Send them to ask@develooper.com.
=head1 COPYRIGHT
Copyright 2004-2010 Ask Bjoern Hansen, Develooper LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
=cut