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