-
Notifications
You must be signed in to change notification settings - Fork 11
/
geodns-config
executable file
·62 lines (54 loc) · 1.83 KB
/
geodns-config
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
#!/usr/bin/env perl
use strict;
use warnings;
use lib 'lib';
use GeoDNS;
use JSON qw(encode_json);
use Getopt::Long qw(GetOptions);
GetOptions(
'verbose!' => \(my $opt_verbose = 0),
'config=s' => \(my $opt_config = 'pgeodns.conf'),
'out=s' => \(my $opt_out),
) or die "invalid options";
my $g = GeoDNS->new(server_id => 'config',);
$g->load_config($opt_config);
my $c = $g->{config};
while (my ($domain, $config) = each %{$g->{config}->{bases}}) {
next if $config->{internal};
delete $config->{base};
delete $config->{data}->{""}->{soa};
if (defined $config->{max_hosts}) {
$config->{max_hosts} += 0;
}
for my $zone (keys %{$config->{data}}) {
my $data = $config->{data}->{$zone};
if (defined $data->{max_hosts}) {
$data->{max_hosts} += 0;
}
for my $t (qw(a aaaa)) {
next unless $data->{$t};
for my $r (@{$data->{$t}}) {
my $ip;
my $hostname = $r->[0];
if ($hostname =~ m/^\d{1,3}(.\d{1,3}){3}$/x) {
$ip = $hostname;
} elsif ($hostname =~ m/:/x) {
$ip = $hostname;
} else {
$ip = $config->{hosts}->{$hostname}->{ip};
}
$r->[0] = $ip;
}
}
}
delete @{$config}{qw(hosts json_config primary_ns files)};
$domain =~ s/\.$//;
my $file = $opt_out . "/$domain.json";
my $old = ((stat($file))[9] || 0);
next unless $config->{mtime} > $old;
my $file_tmp = $file . ".$$.tmp";
open my $fh, ">", $file_tmp or die "Could not open $file_tmp: $!";
print $fh encode_json($config) or die "Could not print to $file_tmp: $!";
close $fh or die "Could not close $file_tmp: $!";
rename $file_tmp, $file or die "Could not rename $file_tmp: $!";
}