Skip to content

Commit 015a49f

Browse files
committed
- read .json data files (to bulk save/load configuration)
- support __END__ to stop reading a configuration file - max_hosts configuration (should probably be renamed?) - Fix bug that disallowed setting default serial/ttl/primary_ns variables git-svn-id: https://svn.develooper.com/projects/pgeodns/trunk@531 e01dd7d5-6be1-0310-aa60-dbbdb055a346
1 parent d9965b4 commit 015a49f

File tree

5 files changed

+49
-25
lines changed

5 files changed

+49
-25
lines changed

CREDITS

+3
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,8 @@ skeleton server. :-)
44
Mark Andrews <[email protected]> pointed out several bugs and
55
mistakes in some of the replies the server sent.
66

7+
Guillaume Filion fixed bugs and experimented with using BGP data
8+
intead of Geo::IP.
9+
710
MaxMind (http://www.maxmind.com/) provides us with excellent geo data
811
and open source libraries to access it.

Changes

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11

2-
See the SVN history for now :-)
32

4-
We'll track "user visible changes" here when we start making proper releases some day.
3+
- read .json data files (to bulk save/load configuration)
4+
- support __END__ to stop reading a configuration file
5+
- max_hosts configuration (should probably be renamed?)
6+
- Fix bug that disallowed setting default serial/ttl/primary_ns variables
7+
8+
r347 -
9+
- see SVN history for history prior to this. :-)

config/base.sample

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ ftphost 192.0.2.5 ftp.dk ftp.europe
2222
#ns ns1.example.org
2323
#ns ns2.example.org
2424
#ttl 300
25-
#serial 2000
25+
#serial 2000
26+
#max_hosts 3
2627
#include config/example.net
2728

2829

lib/GeoDNS.pm

+34-21
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ use Countries qw(continent);
55
use Geo::IP;
66
use List::Util qw/max/;
77
use Carp;
8+
use JSON qw();
89

9-
my $VERSION = ('$Rev: 347 $' =~ m/(\d+)/)[0];
10+
our $VERSION = ('$Rev: 347 $' =~ m/(\d+)/)[0];
1011
my $HeadURL = ('$HeadURL: http://svn.develooper.com/repos/pgeodns/trunk/pgeodns.pl $' =~ m!http:(//[^/]+.*)/pgeodns.pl!)[0];
1112

1213
my $config;
@@ -20,10 +21,6 @@ sub new {
2021
bless \%args, $class;
2122
}
2223

23-
sub log {
24-
carp @_;
25-
}
26-
2724
sub config {
2825
my ($self, $base) = @_;
2926
return $config unless $base;
@@ -114,7 +111,7 @@ sub reply_handler {
114111
push @ans, grep { $_->address eq $config_base->{ns}->{$qname} } @{ ($self->get_ns_records($config_base))[1] };
115112
@add = grep { $_->address ne $config_base->{ns}->{$qname} } @add;
116113
return ('NOERROR', \@ans, \@auth, \@add, { aa => 1 });
117-
}
114+
}
118115

119116
elsif ($qname =~ m/^status\.\Q$base\E$/) {
120117
my $uptime = time - $stats->{started} || 1;
@@ -155,7 +152,7 @@ sub get_soa_record {
155152
my ($self, $config_base) = @_;
156153
Net::DNS::RR->new
157154
("$config_base->{base}. 3600 IN SOA $config_base->{primary_ns};
158-
dns.perl.org. $config_base->{serial} 5400 5400 2419200 $config_base->{ttl}");
155+
support.bitnames.com. $config_base->{serial} 5400 5400 2419200 $config_base->{ttl}");
159156
}
160157

161158
sub pick_groups {
@@ -185,19 +182,22 @@ sub pick_groups {
185182
sub pick_hosts {
186183
my ($self, $config_base, $group) = @_;
187184

188-
return unless $config_base->{groups}->{$group};
185+
return unless $config_base->{groups}->{$group} and $config_base->{groups}->{$group}->{servers};
189186

190187
my @answer;
191-
my $max = 2;
192-
$max = 1 unless scalar @{ $config_base->{groups}->{$group} };
188+
my $max = $config_base->{max_hosts} || 2;
189+
$max = 1 unless scalar @{ $config_base->{groups}->{$group}->{servers} };
193190

194191
my $loop = 0;
195192

196193
while (@answer < $max) {
197194
last if ++$loop > 10; # bad configuration could make us loop ...
198-
my ($host) = ( @{ $config_base->{groups}->{$group} } )[rand scalar @{ $config_base->{groups}->{$group} }];
195+
my ($host) = ( @{ $config_base->{groups}->{$group}->{servers} }
196+
)[rand scalar @{ $config_base->{groups}->{$group}->{servers} }];
197+
($host, my $priority) = @$host;
199198
next if grep { $host eq $_->{name} } @answer;
200-
push @answer, ({ name => $host, ip => $config_base->{hosts}->{$host}->{ip} });
199+
my $ip = $host =~ m/^\d{1,3}(.\d{1,3}){3}$/ ? $host : $config_base->{hosts}->{$host}->{ip};
200+
push @answer, ({ name => $host, ip => $ip });
201201
}
202202

203203
@answer;
@@ -221,6 +221,8 @@ sub load_config {
221221

222222
read_config( shift || 'pgeodns.conf' );
223223

224+
delete $config->{base};
225+
224226
# warn Data::Dumper->Dump([\$config], [qw(config)]);
225227

226228
# the default serial is timestamp of the newest config file.
@@ -254,10 +256,10 @@ sub read_config {
254256
die "Oops, recursive inclusion of $file - parent(s): ", join ", ", @config_file_stack;
255257
}
256258

257-
push @config_file_stack, $file;
258-
259259
open my $fh, $file
260-
or &log("Can't open config file: $file: $!");
260+
or warn "Can't open config file: $file: $!\n" and return;
261+
262+
push @config_file_stack, $file;
261263

262264
push @{ $config->{files} }, [$file, (stat($file))[9]];
263265

@@ -266,11 +268,20 @@ sub read_config {
266268
s/^\s+//;
267269
s/\s+$//;
268270
next if /^\#/ or /^$/;
271+
last if /^__END__$/;
269272

270273
if (s/^base\s+//) {
271-
$_ .= '.' unless m/\.$/;
272-
$config->{base} = $_;
273-
$config->{bases}->{$_} ||= { base => $_ };
274+
my ($base_name, $json_file) = split /\s+/, $_;
275+
$base_name .= '.' unless $base_name =~ m/\.$/;
276+
$config->{base} = $base_name;
277+
if ($json_file) {
278+
open my $json_fh, $json_file or warn "Could not open $json_file: $!\n" and next;
279+
push @{ $config->{files} }, [$json_file, (stat($json_file))[9]];
280+
my $json = eval { local $/ = undef; <$json_fh> };
281+
close $json_fh;
282+
$config->{bases}->{$base_name} = JSON::jsonToObj($json);
283+
}
284+
$config->{bases}->{$base_name}->{base} ||= $base_name;
274285
next;
275286
}
276287
elsif (s/^include\s+//) {
@@ -289,6 +300,7 @@ sub read_config {
289300
}
290301
elsif (s/^(serial|ttl|primary_ns)\s+//) {
291302
$config->{$1} = $_;
303+
next;
292304
}
293305
}
294306

@@ -306,18 +318,19 @@ sub read_config {
306318
$config_base->{primary_ns} = $name
307319
unless $config_base->{primary_ns};
308320
}
309-
elsif (s/^(serial|ttl|primary_ns)\s+//) {
321+
elsif (s/^(serial|ttl|primary_ns|max_hosts)\s+//) {
310322
$config_base->{$1} = $_;
311323
}
312324
else {
313325
s/^\s*10+\s+//;
314326
my ($host, $ip, $groups) = split(/\s+/,$_,3);
327+
die "Bad configuration line: [$_]\n" unless $groups;
315328
$host = "$host." unless $host =~ m/\.$/;
316329
$config_base->{hosts}->{$host} = { ip => $ip };
317330
for my $group_name (split /\s+/, $groups) {
318331
$group_name = '' if $group_name eq '@';
319-
$config_base->{groups}->{$group_name} ||= [];
320-
push @{$config_base->{groups}->{$group_name}}, $host;
332+
$config_base->{groups}->{$group_name}->{servers} ||= [];
333+
push @{$config_base->{groups}->{$group_name}->{servers}}, [ $host, 1 ];
321334
}
322335
}
323336
}

pgeodns.conf

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11

22
include config/dist/base
33

4-
#include config/base
4+
# include config/jsontest
5+
6+
# include config/base
57
# include config/cpan
68

79

0 commit comments

Comments
 (0)