@@ -5,8 +5,9 @@ use Countries qw(continent);
5
5
use Geo::IP;
6
6
use List::Util qw/ max/ ;
7
7
use Carp;
8
+ use JSON qw( ) ;
8
9
9
- my $VERSION = (' $Rev: 347 $' =~ m / (\d +)/ )[0];
10
+ our $VERSION = (' $Rev: 347 $' =~ m / (\d +)/ )[0];
10
11
my $HeadURL = (' $HeadURL: http://svn.develooper.com/repos/pgeodns/trunk/pgeodns.pl $' =~ m ! http:(//[^/]+.*)/pgeodns.pl! )[0];
11
12
12
13
my $config ;
@@ -20,10 +21,6 @@ sub new {
20
21
bless \%args , $class ;
21
22
}
22
23
23
- sub log {
24
- carp @_ ;
25
- }
26
-
27
24
sub config {
28
25
my ($self , $base ) = @_ ;
29
26
return $config unless $base ;
@@ -114,7 +111,7 @@ sub reply_handler {
114
111
push @ans , grep { $_ -> address eq $config_base -> {ns }-> {$qname } } @{ ($self -> get_ns_records($config_base ))[1] };
115
112
@add = grep { $_ -> address ne $config_base -> {ns }-> {$qname } } @add ;
116
113
return (' NOERROR' , \@ans , \@auth , \@add , { aa => 1 });
117
- }
114
+ }
118
115
119
116
elsif ($qname =~ m / ^status\.\Q $base \E $ / ) {
120
117
my $uptime = time - $stats -> {started } || 1;
@@ -155,7 +152,7 @@ sub get_soa_record {
155
152
my ($self , $config_base ) = @_ ;
156
153
Net::DNS::RR-> new
157
154
(" $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}" );
159
156
}
160
157
161
158
sub pick_groups {
@@ -185,19 +182,22 @@ sub pick_groups {
185
182
sub pick_hosts {
186
183
my ($self , $config_base , $group ) = @_ ;
187
184
188
- return unless $config_base -> {groups }-> {$group };
185
+ return unless $config_base -> {groups }-> {$group } and $config_base -> { groups } -> { $group } -> { servers } ;
189
186
190
187
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 } };
193
190
194
191
my $loop = 0;
195
192
196
193
while (@answer < $max ) {
197
194
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 ;
199
198
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 });
201
201
}
202
202
203
203
@answer ;
@@ -221,6 +221,8 @@ sub load_config {
221
221
222
222
read_config( shift || ' pgeodns.conf' );
223
223
224
+ delete $config -> {base };
225
+
224
226
# warn Data::Dumper->Dump([\$config], [qw(config)]);
225
227
226
228
# the default serial is timestamp of the newest config file.
@@ -254,10 +256,10 @@ sub read_config {
254
256
die " Oops, recursive inclusion of $file - parent(s): " , join " , " , @config_file_stack ;
255
257
}
256
258
257
- push @config_file_stack , $file ;
258
-
259
259
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 ;
261
263
262
264
push @{ $config -> {files } }, [$file , (stat ($file ))[9]];
263
265
@@ -266,11 +268,20 @@ sub read_config {
266
268
s / ^\s +// ;
267
269
s /\s +$// ;
268
270
next if / ^\# / or /^$/ ;
271
+ last if / ^__END__$ / ;
269
272
270
273
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 ;
274
285
next ;
275
286
}
276
287
elsif (s / ^include\s +// ) {
@@ -289,6 +300,7 @@ sub read_config {
289
300
}
290
301
elsif (s / ^(serial|ttl|primary_ns)\s +// ) {
291
302
$config -> {$1 } = $_ ;
303
+ next ;
292
304
}
293
305
}
294
306
@@ -306,18 +318,19 @@ sub read_config {
306
318
$config_base -> {primary_ns } = $name
307
319
unless $config_base -> {primary_ns };
308
320
}
309
- elsif (s / ^(serial|ttl|primary_ns)\s +// ) {
321
+ elsif (s / ^(serial|ttl|primary_ns|max_hosts )\s +// ) {
310
322
$config_base -> {$1 } = $_ ;
311
323
}
312
324
else {
313
325
s / ^\s *10+\s +// ;
314
326
my ($host , $ip , $groups ) = split (/ \s +/ ,$_ ,3);
327
+ die " Bad configuration line: [$_ ]\n " unless $groups ;
315
328
$host = " $host ." unless $host =~ m /\. $ / ;
316
329
$config_base -> {hosts }-> {$host } = { ip => $ip };
317
330
for my $group_name (split /\s +/, $groups ) {
318
331
$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 ] ;
321
334
}
322
335
}
323
336
}
0 commit comments