]> git.refcnt.org Git - distdns.git/blob - client.pl
Prefix error message with program name
[distdns.git] / client.pl
1 #!/usr/bin/perl
2 #
3 # Copyright (c) 2013 Michel Ketterle, Steven Schubiger
4 #
5 # This file is part of distdns.
6 #
7 # distdns is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # distdns is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with distdns. If not, see <http://www.gnu.org/licenses/>.
19
20 use strict;
21 use warnings;
22 use constant false => 0;
23
24 use Config::Tiny;
25 use Digest::MD5 qw(md5_hex);
26 use Fcntl ':flock';
27 use File::Spec::Functions qw(catfile rel2abs);
28 use FindBin qw($Bin);
29 use Getopt::Long qw(:config no_auto_abbrev no_ignore_case);
30 use JSON qw(decode_json);
31 use LWP::UserAgent;
32 use Sys::Hostname qw(hostname);
33 use Tie::File;
34
35 my $VERSION = '0.05';
36
37 my $conf_file = catfile($Bin, 'client.conf');
38
39 sub usage
40 {
41 print <<"USAGE";
42 Usage: $0
43 -d, --debug server debugging
44 -h, --help this help screen
45 -i, --init initialize session data
46 USAGE
47 exit;
48 }
49
50 my %opts;
51 GetOptions(\%opts, qw(d|debug h|help i|init)) or usage();
52 usage() if $opts{h};
53
54 my $config = Config::Tiny->new;
55 $config = Config::Tiny->read($conf_file);
56
57 my $get_config_opts = sub
58 {
59 my ($section, $options) = @_;
60
61 die "$0: Section $section missing in $conf_file\n" unless exists $config->{$section};
62
63 my %options;
64 @options{@$options} = @{$config->{$section}}{@$options};
65
66 foreach my $option (@$options) {
67 die "$0: Option $option not set in $conf_file\n" unless defined $options{$option} && length $options{$option};
68 }
69
70 return @options{@$options};
71 };
72
73 my ($hosts_file, $session_file) = map rel2abs($_, $Bin), $get_config_opts->('path', [ qw(hosts_file session_file) ]);
74
75 my ($server_url) = $get_config_opts->('url', [ qw(server_url) ]);
76 my ($netz, $name) = $get_config_opts->('data', [ qw(netz name) ]);
77
78 my $save_session = sub
79 {
80 my ($session) = @_;
81
82 open(my $fh, '>', $session_file) or die "$0: Cannot open client-side $session_file for writing: $!\n";
83 print {$fh} "$session\n";
84 close($fh);
85 };
86
87 my $get_session = sub
88 {
89 open(my $fh, '<', $session_file) or die "$0: Cannot open client-side $session_file for reading: $!\nPerhaps try running --init\n";
90 my $session = do { local $/; <$fh> };
91 chomp $session;
92 close($fh);
93
94 return $session;
95 };
96
97 my $session = $opts{i} ? substr(md5_hex(md5_hex(time() . {} . rand() . $$)), 0, 32) : $get_session->();
98
99 my %params = (
100 netz => $netz,
101 pc => hostname(),
102 name => $name,
103 debug => $opts{d} || false,
104 init => $opts{i} || false,
105 session => $session,
106 );
107
108 my $ua = LWP::UserAgent->new;
109
110 my $response = $ua->post($server_url, \%params);
111
112 if ($response->is_success) {
113 my $data;
114
115 eval {
116 $data = decode_json($response->decoded_content);
117 } or exit;
118
119 die "$0: $data->{error}" if defined $data->{error};
120
121 $save_session->($session) if $opts{i};
122
123 my %list;
124 foreach my $entry (@{$data->{entries}}) {
125 my $host = "$entry->{ip}\t" . join '.', @$entry{qw(name pc netz)};
126 push @{$list{$entry->{netz}}}, $host;
127 }
128
129 my $o = tie my @hosts, 'Tie::File', $hosts_file or die "$0: Cannot tie $hosts_file: $!\n";
130 $o->flock(LOCK_EX);
131
132 foreach my $network (keys %list) {
133 my %indexes;
134 for (my $i = 0; $i < @hosts; $i++) {
135 if ($hosts[$i] =~ /^\#$network\#$/i) {
136 $indexes{start} = $i;
137 }
138 elsif (exists $indexes{start} && $hosts[$i] =~ /^\#\/$network\#$/i) {
139 $indexes{end} = $i;
140 my $count = ($indexes{end} - $indexes{start} > 1)
141 ? $indexes{end} - $indexes{start} - 1
142 : 0;
143 splice @hosts, $indexes{start} + 1, $count, @{$list{$network}};
144 last;
145 }
146 }
147 }
148
149 undef $o;
150 untie @hosts;
151 }
152 else {
153 warn $response->status_line, "\n";
154 }