forked from abh/pgeodns
-
Notifications
You must be signed in to change notification settings - Fork 1
/
pgeodns.pl
executable file
·148 lines (101 loc) · 3.16 KB
/
pgeodns.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#!/usr/bin/perl -w
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;
my %opts = (verbose => 0);
GetOptions (\%opts,
'interface=s',
'user=s',
'verbose!',
'config=s',
'configtest!',
'development!',
'port=i',
) or die "invalid options";
my $config_file = $opts{config} || 'pgeodns.conf';
my $port = $opts{port} || 53;
exit !GeoDNS::load_config({},$config_file)
if $opts{configtest};
die "--interface [ip|hostname] required\n" unless $opts{interface};
die "--user [user|uid] required\n" if $> ==0 and !$opts{user};
my $g = GeoDNS->new(interface => $opts{interface},
debug => 1,
config_file => $opts{config},
development => ($opts{development} ? 1 : 0),
);
my $localaddr = $opts{interface};
if ($localaddr =~ /[^\d\.]/) {
my $addr = inet_ntoa((gethostbyname($localaddr))[4]);
die "could not lookup $localaddr\n" unless $addr;
$localaddr = $addr;
}
printf "\nStarting GeoDNS %s\n", $g->version_full;
my $ns = Net::DNS::Nameserver->new
(
LocalPort => $port,
LocalAddr => $localaddr,
ReplyHandler => sub {
my @reply = $g->reply_handler(@_);
#warn Data::Dumper->Dump([\@reply], [qw(reply)]);
@reply
},
Verbose => $opts{verbose},
);
# print error?
die "couldn't create nameserver object\n" unless $ns;
if (my $uid = $opts{user}) {
$uid = getpwnam($uid) or die "could not lookup uid"
if $uid =~ m/\D/;
setuid($uid) or die "could not setuid: $!";
}
$g->load_config($config_file);
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.
=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-2007 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