]> git.refcnt.org Git - distdns.git/blob - client.pl
Make switches mutually exclusive
[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.06';
36
37 my $conf_file = catfile($Bin, 'client.conf');
38
39 sub _die { die "$0: [client] $_[0]" }
40
41 sub usage
42 {
43 warn "$_[0]\n" if defined $_[0];
44 print <<"USAGE";
45 Usage: $0 [options]
46 -d, --debug server debugging
47 -h, --help this help screen
48 -i, --init initialize session data
49 -l, --list list remote entries
50 USAGE
51 exit;
52 }
53
54 my %opts;
55 GetOptions(\%opts, qw(d|debug h|help i|init l|list)) or usage();
56 usage() if $opts{h};
57
58 usage('Cannot combine --init and --list') if $opts{i} && $opts{l};
59
60 my $config = Config::Tiny->new;
61 $config = Config::Tiny->read($conf_file);
62
63 my $get_config_opts = sub
64 {
65 my ($section, $options) = @_;
66
67 _die "Section '$section' missing in $conf_file\n" unless exists $config->{$section};
68
69 my %options;
70 @options{@$options} = @{$config->{$section}}{@$options};
71
72 foreach my $option (@$options) {
73 _die "Option '$option' not set in $conf_file\n" unless defined $options{$option} && length $options{$option};
74 }
75
76 return @options{@$options};
77 };
78
79 my ($hosts_file, $session_file) = map rel2abs($_, $Bin), $get_config_opts->('path', [ qw(hosts_file session_file) ]);
80
81 my ($server_url) = $get_config_opts->('url', [ qw(server_url) ]);
82 my ($netz, $name) = $get_config_opts->('data', [ qw(netz name) ]);
83
84 my $save_session = sub
85 {
86 my ($session) = @_;
87
88 open(my $fh, '>', $session_file) or _die "Cannot open $session_file for writing: $!\n";
89 print {$fh} "$session\n";
90 close($fh);
91 };
92
93 my $get_session = sub
94 {
95 open(my $fh, '<', $session_file) or _die "Cannot open $session_file for reading: $!\nPerhaps try running --init\n";
96 my $session = do { local $/; <$fh> };
97 chomp $session;
98 close($fh);
99
100 return $session;
101 };
102
103 my $session = $opts{i} ? substr(md5_hex(md5_hex(time() . {} . rand() . $$)), 0, 32) : $get_session->();
104
105 my %params = (
106 netz => $netz,
107 pc => hostname(),
108 name => $name,
109 debug => $opts{d} || false,
110 init => $opts{i} || false,
111 list => $opts{l} || false,
112 session => $session,
113 );
114
115 my $ua = LWP::UserAgent->new;
116
117 my $response = $ua->post($server_url, \%params);
118
119 if ($response->is_success) {
120 my $data;
121
122 eval {
123 $data = decode_json($response->decoded_content);
124 } or exit;
125
126 die "$0: [server] $data->{error}" if defined $data->{error};
127
128 if ($opts{i}) {
129 $save_session->($session);
130 }
131 elsif ($opts{l}) {
132 format STDOUT_TOP =
133 IP Name PC Netz
134 =============================================================================
135 .
136 foreach my $entry (sort { $a->{netz} cmp $b->{netz} } @{$data->{entries}}) {
137 format STDOUT =
138 @<<<<<<<<<<<<<< @<<<<<<<<<<<<<< @<<<<<<<<<<<<<<<<<<< @<<<<<<<<<<<<<<
139 @$entry{qw(ip name pc netz)}
140 .
141 write;
142 }
143 }
144 else {
145 my %list;
146 foreach my $entry (@{$data->{entries}}) {
147 my $host = "$entry->{ip}\t" . join '.', @$entry{qw(name pc netz)};
148 push @{$list{$entry->{netz}}}, $host;
149 }
150
151 my $o = tie my @hosts, 'Tie::File', $hosts_file or _die "Cannot tie $hosts_file: $!\n";
152 $o->flock(LOCK_EX);
153
154 foreach my $network (keys %list) {
155 my %indexes;
156 for (my $i = 0; $i < @hosts; $i++) {
157 if ($hosts[$i] =~ /^\#$network\#$/i) {
158 $indexes{start} = $i;
159 }
160 elsif (exists $indexes{start} && $hosts[$i] =~ /^\#\/$network\#$/i) {
161 $indexes{end} = $i;
162 my $count = ($indexes{end} - $indexes{start} > 1)
163 ? $indexes{end} - $indexes{start} - 1
164 : 0;
165 splice @hosts, $indexes{start} + 1, $count, @{$list{$network}};
166 last;
167 }
168 }
169 }
170
171 undef $o;
172 untie @hosts;
173 }
174 }
175 else {
176 warn $response->status_line, "\n";
177 }