18 |
#use JSON; |
#use JSON; |
19 |
use IO::Socket::INET; |
use IO::Socket::INET; |
20 |
use Regexp::Common qw/net/; |
use Regexp::Common qw/net/; |
21 |
|
use POSIX qw(strftime); |
22 |
|
|
23 |
our $title; |
our $title; |
24 |
|
|
37 |
</html> |
</html> |
38 |
}} |
}} |
39 |
|
|
|
sub menu { |
|
|
qq{ |
|
|
<div style="font-size: 80%; color: #888"> |
|
|
<a href=/>home</a> |
|
|
<a href=/server>server</a> |
|
|
<a href=/brctl>brctl</a> |
|
|
<a href=/ip>ip</a> |
|
|
<a href=/nmap>nmap</a> |
|
|
<a href=/client>client</a> |
|
|
</div> |
|
|
|
|
|
}} |
|
|
|
|
40 |
our $port = 7777; |
our $port = 7777; |
41 |
|
|
42 |
use server; |
use server; |
51 |
use log; |
use log; |
52 |
use x11; |
use x11; |
53 |
use amt; |
use amt; |
|
use boolean; |
|
54 |
use daemons; |
use daemons; |
55 |
|
|
56 |
use kvm; |
use kvm; |
63 |
use ping; |
use ping; |
64 |
use wol; |
use wol; |
65 |
|
|
66 |
use CouchDB; |
use store; |
67 |
|
|
68 |
|
|
69 |
|
sub menu { |
70 |
|
my $store_url = $url; |
71 |
|
$store_url =~ s{:\d+.+}{:28017}; |
72 |
|
qq{ |
73 |
|
<div style="font-size: 80%; color: #888"> |
74 |
|
<a target=pids href=/ >home</a> |
75 |
|
| |
76 |
|
<a target=server href=/server >server</a> |
77 |
|
<a target=server href=/brctl >brctl</a> |
78 |
|
<a target=server href=/ip >ip</a> |
79 |
|
| |
80 |
|
<a target=store href=$store_url >MongoDB</a> |
81 |
|
<a target=store href=/store/latest >latest</a> |
82 |
|
| |
83 |
|
<a target=client href=/nmap >nmap</a> |
84 |
|
<a target=client href=/client >client</a> |
85 |
|
</div> |
86 |
|
|
87 |
|
}} |
88 |
|
|
89 |
|
|
90 |
sub static { |
sub static { |
91 |
my ($client,$path) = @_; |
my ($client,$path) = @_; |
94 |
|
|
95 |
return if ! -f $full; |
return if ! -f $full; |
96 |
|
|
97 |
|
return if $full =~ m{\.ico$}; |
98 |
|
|
99 |
if ( my $pid = fork ) { |
if ( my $pid = fork ) { |
100 |
# parent |
# parent |
101 |
close($client); |
close($client); |
118 |
my $buff; |
my $buff; |
119 |
my $pos = 0; |
my $pos = 0; |
120 |
|
|
121 |
CouchDB::audit( 'static', { pid => $$, path => $path, type => $type, size => $size, block => $block, peerhost => $client->peerhost }); |
store::audit( 'static', { pid => $$, path => $path, type => $type, size => $size, block => $block, peerhost => $client->peerhost }); |
122 |
|
|
123 |
progress_bar::start; |
progress_bar::start; |
124 |
|
|
146 |
qq|HTTP/1.1 302 Found\r\nContent-type: text/html\r\nLocation: $to\r\n\r\n| |
qq|HTTP/1.1 302 Found\r\nContent-type: text/html\r\nLocation: $to\r\n\r\n| |
147 |
} |
} |
148 |
|
|
149 |
|
sub toggle { |
150 |
|
my $v = shift; |
151 |
|
return $v ? 0 : 1; |
152 |
|
} |
153 |
|
|
154 |
sub get_request { |
sub get_request { |
155 |
my ( $client, $path, $param ) = @_; |
my ( $client, $path, $param ) = @_; |
156 |
|
|
157 |
server->refresh; |
server->refresh; |
158 |
|
|
159 |
CouchDB::audit( 'request', { path => $path, param => $param, peerhost => $client->peerhost } ); |
store::audit( 'request', { path => $path, param => $param, peerhost => $client->peerhost } ); |
160 |
|
|
161 |
$title = $path; |
$title = $path; |
162 |
|
|
231 |
} |
} |
232 |
} |
} |
233 |
|
|
234 |
|
my $kvm = kvm::next_nr; |
235 |
|
$kvm = qq|<div><a href=/start_stop/kvm?nr=$kvm>create new kvm $kvm</a></div>|; |
236 |
|
|
237 |
print $client ok |
print $client ok |
238 |
, html::table( 2, @rows ) |
, html::table( 2, @rows ) |
239 |
, $below_table |
, $below_table |
240 |
|
, $kvm |
241 |
, html::tabs( log::mac_changes ) |
, html::tabs( log::mac_changes ) |
242 |
, $debug_proc |
, $debug_proc |
243 |
; |
; |
244 |
|
|
245 |
} elsif ( $path =~ m{^/server} ) { |
} elsif ( $path =~ m{^/server} ) { |
246 |
if ( my $c = $param->{new_clients} ) { |
foreach my $name ( keys %$param ) { |
247 |
server::shared( 'new_clients', $c ); |
eval '$server::' . $name . '= $param->{$name}'; |
248 |
|
} |
249 |
|
my @table = ( |
250 |
|
'debug' => qq|<a href=/our/debug/| . toggle($debug) . qq|>$debug</a>|, |
251 |
|
, 'new_clients' => qq|<input type=text name=new_clients size=3 value="$server::new_clients">| |
252 |
|
); |
253 |
|
|
254 |
|
foreach my $editable ( 'ip', 'bcast', 'netmask', 'ip_from', 'ip_to', 'domain' ) { |
255 |
|
my $v = eval '$server::' . $editable; |
256 |
|
push @table, ( $editable, qq|<input type=text name=$editable value="$v">| ); |
257 |
} |
} |
258 |
|
|
259 |
|
foreach my $readonly ( 'base_dir', 'conf' ) { |
260 |
|
my $v = eval '$server::' . $readonly; |
261 |
|
push @table, ( $readonly, html::tt $v ); |
262 |
|
} |
263 |
|
|
264 |
print $client ok |
print $client ok |
265 |
, qq|<form method=get>| |
, qq|<form method=get>| |
266 |
, html::table( 2, |
, html::table( 2, @table ) |
267 |
'debug' => qq|<a href=/our/debug/| . boolean::toggle($debug) . qq|>$debug</a>|, |
, qq| |
268 |
'new_clients' => qq|<input type=text name=new_clients size=3 value="| . server::shared( 'new_clients' ) . qq|">|, |
<input type=submit name=action value=change> |
269 |
map { |
</form> |
270 |
( $_, html::tt eval '$server::'.$_ ) |
| |
|
} ( 'ip', 'netmask', 'ip_from', 'ip_to', 'domain', 'base_dir', 'conf' ) |
|
|
) |
|
|
, qq|</form>| |
|
271 |
; |
; |
272 |
|
|
273 |
|
} elsif ( $path =~ m{^/store/latest} ) { |
274 |
|
print $client ok |
275 |
|
, qq| |
276 |
|
<style type=text/css> |
277 |
|
.z { |
278 |
|
background: #eee; |
279 |
|
} |
280 |
|
td > pre { |
281 |
|
margin: 0; |
282 |
|
max-height: 3em; |
283 |
|
overflow: hidden; |
284 |
|
} |
285 |
|
td:hover > pre { |
286 |
|
max-height: 100%; |
287 |
|
overflow: show; |
288 |
|
} |
289 |
|
</style> |
290 |
|
| |
291 |
|
, qq|<table>| |
292 |
|
; |
293 |
|
my ( $s1,$s2 ) = ( ' class=z', '' ); |
294 |
|
my @cols; |
295 |
|
|
296 |
|
my $from_t = $param->{from_t}; |
297 |
|
|
298 |
|
my $q; |
299 |
|
$q->{'package.time'} = { '$lt' => $from_t * 1 } if $from_t; |
300 |
|
# $q->{'package.name'} = { '$ne' => 'syslogd' }; |
301 |
|
$q->{'package.name'} = { '$ne' => 'dhcpd' }; |
302 |
|
# $q->{'package.name'} = { '$ne' => [ 'dhcpd', 'dnsd' ] }; |
303 |
|
$q->{'tag'} = { '$ne' => 'CRON' }; |
304 |
|
store::query( $q, sub { |
305 |
|
my $o = shift; |
306 |
|
my $p = delete( $o->{package} ); |
307 |
|
delete( $o->{_id} ); |
308 |
|
|
309 |
|
if ( ! @cols ) { |
310 |
|
#@cols = keys %$p; |
311 |
|
@cols = qw( time name ); |
312 |
|
print $client qq|<tr><th>| |
313 |
|
, join(qq|</th><th>|, @cols) |
314 |
|
, qq|</th><th></th></tr>| |
315 |
|
; |
316 |
|
} |
317 |
|
|
318 |
|
# XXX sigh, dump dies if we don't do this |
319 |
|
# delete $o->{$_} foreach ( grep { ! defined $o->{$_} } keys %$o ); |
320 |
|
|
321 |
|
print $client qq|<tr$s1>| |
322 |
|
, strftime( qq|<td title="%Y-%m-%d">%H:%M:%S</td>|, localtime($p->{time}) ) |
323 |
|
, map { qq|<td>$_<td>| } ( $p->{name} , html::pre_dump($o) ) |
324 |
|
, qq|</tr>\n| |
325 |
|
; |
326 |
|
( $s1, $s2 ) = ( $s2, $s1 ); |
327 |
|
$from_t = $p->{time}; |
328 |
|
}); |
329 |
|
print $client qq|</table>|, |
330 |
|
qq|<a href="/store/latest?from_t=$from_t">more</a>| |
331 |
|
; |
332 |
|
|
333 |
} elsif ( $path =~ m!^/client(?:/$RE{net}{IPv4}{-keep})?! ) { |
} elsif ( $path =~ m!^/client(?:/$RE{net}{IPv4}{-keep})?! ) { |
334 |
my $ip = $1; |
my $ip = $1; |
335 |
$title = $ip; |
$title = $ip if $ip; |
336 |
|
|
337 |
if ( $param->{action} eq 'remove' ) { |
if ( $param->{action} eq 'remove' ) { |
338 |
client::remove( $param->{change_ip} ); |
client::remove( $param->{change_ip} ); |
362 |
|
|
363 |
if ( $ip && $ip ne $server::ip ) { |
if ( $ip && $ip ne $server::ip ) { |
364 |
|
|
|
my @editable = ( qw/hostname config homepage/ ); |
|
|
|
|
|
client::conf( $ip, $_ => $param->{$_} ) foreach @editable; |
|
|
|
|
365 |
my $conf = client::all_conf( $ip ); |
my $conf = client::all_conf( $ip ); |
366 |
my $config = delete $conf->{config}; |
my $config = delete $conf->{config}; |
367 |
|
|
368 |
|
my @editable = ( qw/hostname config homepage/ ); |
369 |
|
# add params with config prefix if config exists |
370 |
|
push @editable, grep { m{^\Q$config\E} } keys %$conf if $config; |
371 |
|
my @update = grep { defined $param->{$_} } @editable; |
372 |
|
if ( @update ) { |
373 |
|
client::conf( $ip, $_ => $param->{$_} ) foreach @update; |
374 |
|
print $client redirect("$url/client/$ip"); |
375 |
|
return; |
376 |
|
} |
377 |
|
|
378 |
my $nmap = qq|<a href=/nmap?scan=$ip>nmap</a>|; |
my $nmap = qq|<a href=/nmap?scan=$ip>nmap</a>|; |
379 |
my @table = ( |
my @table = ( |
380 |
'ping' => ping::host($ip) |
'ping' => ping::host($ip) |
410 |
|
|
411 |
} else { |
} else { |
412 |
|
|
413 |
|
print $client ok qq|<h2>Clients on $server::ip</h2>|; |
414 |
|
|
415 |
my @ping; |
my @ping; |
416 |
if ( my $host = $param->{ping_target} ) { |
if ( my $host = $param->{ping_target} ) { |
417 |
@ping = ( $host ); |
@ping = ( $host ); |
422 |
my $ping = ping::fping( @ping ) if @ping; |
my $ping = ping::fping( @ping ) if @ping; |
423 |
my $arp = client::arp_mac_dev; |
my $arp = client::arp_mac_dev; |
424 |
|
|
425 |
print $client ok |
my @clients; |
426 |
, qq|<h2>Clients on $server::ip</h2>| |
|
427 |
, html::table( -5, |
foreach my $ip ( client::all_ips ) { |
428 |
'ip', 'mac', 'dev', 'hostname', 'conf', |
|
429 |
map { |
my $conf = client::all_conf( $ip ); |
430 |
my $ip = $_; |
my $mac = delete $conf->{mac} || ''; |
431 |
my $conf = client::all_conf( $ip ); |
my $dev = $arp->{$mac}; |
432 |
my $mac = delete $conf->{mac} || ''; |
|
433 |
my $style; |
my $in_dhcp_range = ip::in_dhcp_range($ip); |
434 |
$style |
|
435 |
= 'style="color:' |
next unless $dev || $param->{all} || $in_dhcp_range; |
436 |
. ( $ping->{$ip} ? 'green' : 'red' ) |
|
437 |
. '"' |
my $style |
438 |
if $ping; |
= 'style="color:' |
439 |
$style ||= ''; |
. ( $ping->{$ip} ? 'green' : 'red' ) |
440 |
( |
. '"' |
441 |
qq|<a $style name=$ip target=$ip href=/client/$ip>$ip</a>| |
if $ping; |
442 |
, format::mac( $mac => 'html' ) |
|
443 |
, $arp->{$mac} |
$style ||= ''; |
444 |
, delete $conf->{hostname} |
my $ip_text = qq|<tt>$ip</tt>|; |
445 |
, html::conf( $ip, $conf, 'inline' ) |
$ip_text = qq|<tt><b>$ip</b></tt>| if $in_dhcp_range; |
446 |
) |
|
447 |
} client::all_ips |
$dev = qq|<tt>$dev</tt>| if $dev; |
448 |
) |
|
449 |
|
push @clients |
450 |
|
, qq|<a $style name=$ip target=client href=/client/$ip>$ip_text</a>| |
451 |
|
, format::mac( $mac => 'html' ) |
452 |
|
, $dev |
453 |
|
, delete $conf->{hostname} |
454 |
|
, html::conf( $ip, $conf, 'inline' ) |
455 |
; |
; |
456 |
|
} |
457 |
|
|
458 |
|
my $all = $param->{all} ? 0 : 1; |
459 |
|
|
460 |
|
print $client html::table( -5, 'ip', 'mac', qq|<a href="?all=$all">dev</a>|, 'hostname', 'conf', @clients ); |
461 |
print $client qq| |
print $client qq| |
462 |
<form method=get> |
<form method=get> |
463 |
<input type=text name=ping_target size=15> |
<input type=text name=ping_target size=15> |
465 |
</form> |
</form> |
466 |
|; |
|; |
467 |
} |
} |
468 |
|
|
469 |
|
|
470 |
} elsif ( $path =~ m{^/brctl} ) { |
} elsif ( $path =~ m{^/brctl} ) { |
471 |
print $client ok, html::table( -4, |
|
472 |
|
system 'brctl addif virtual ' . $param->{addif} if $param->{addif}; |
473 |
|
system 'brctl delif virtual ' . $param->{delif} if $param->{delif}; |
474 |
|
|
475 |
|
my $in_virtual; |
476 |
|
|
477 |
|
my @table = |
478 |
map { |
map { |
479 |
my @c = split(/\t+/,$_,4); |
my @c = split(/\t+/,$_,4); |
480 |
if ( $#c == 1 ) { |
if ( $#c == 1 ) { |
481 |
( '', '', '', $c[1] ) |
$in_virtual->{ $c[1] }++; |
482 |
|
@c = ( '', '', '', $c[1] ); |
483 |
} else { |
} else { |
484 |
@c |
$in_virtual->{ $c[3] }++; |
485 |
|
} |
486 |
|
if ( $c[3] =~ m{\d$} ) { |
487 |
|
$c[3] = qq|<input type=submit name=delif value=$c[3] style="color:red" title="remove $c[3] from bridge">|; |
488 |
} |
} |
489 |
|
@c |
490 |
} split(/\n/, `brctl show`) |
} split(/\n/, `brctl show`) |
491 |
); |
; |
492 |
|
|
493 |
|
my @add_ifs = grep { ! $in_virtual->{$_} && $_ ne 'virtual' } ip::devices_up; |
494 |
|
|
495 |
|
push @table, ( '', '', '', html::select( 'addif', @add_ifs ) . qq|<input type=submit value=add></form>| ); |
496 |
|
|
497 |
|
print $client ok |
498 |
|
, qq|<form>| |
499 |
|
, html::table( -4, @table ) |
500 |
|
, qq|</form>| |
501 |
|
; |
502 |
|
|
503 |
|
|
504 |
} elsif ( $path =~ m{^/ip/?(\w+)?} ) { |
} elsif ( $path =~ m{^/ip/?(\w+)?} ) { |
505 |
print $client ok |
print $client ok |
506 |
, join("\n", map { qq|<a href=/ip/$_>$_</a>| } ( qw/link addr route neigh ntable tunnel maddr mroute xfrm/ )) |
, join("\n", map { qq|<a href=/ip/$_>$_</a>| } ( qw/link addr route neigh ntable tunnel maddr mroute xfrm/ )) |
555 |
|
|
556 |
my $server = IO::Socket::INET->new( |
my $server = IO::Socket::INET->new( |
557 |
Proto => 'tcp', |
Proto => 'tcp', |
558 |
LocalAddr => $server::ip, |
# LocalAddr => $server::ip, |
559 |
LocalPort => $httpd::port, |
LocalPort => $httpd::port, |
560 |
Listen => SOMAXCONN, |
Listen => SOMAXCONN, |
561 |
Reuse => 1 |
Reuse => 1 |
564 |
print "url $url\n"; |
print "url $url\n"; |
565 |
|
|
566 |
syslogd::install_local; |
syslogd::install_local; |
567 |
|
client::rebuild_mac_links; |
568 |
|
|
569 |
while (1) { |
while (1) { |
570 |
my $client = $server->accept() || next; # ALARM trickle us |
my $client = $server->accept() || next; # ALARM trickle us |
571 |
my $request = <$client>; |
my $request = <$client>; |
572 |
|
|
573 |
warn "request $request\n" if $debug; |
my $headers; |
574 |
|
|
575 |
|
while ( my $header = <$client> ) { |
576 |
|
chomp $header; |
577 |
|
last if $header =~ m{^\s*$}; |
578 |
|
my ( $n, $v ) = split(/:\s*/, $header); |
579 |
|
$headers->{ lc $n } = $v; |
580 |
|
} |
581 |
|
|
582 |
|
if ( my $host = $headers->{host} ) { |
583 |
|
$url = 'http://' . $host; |
584 |
|
$url .= ":$port" unless $url =~ m{:\d+$}; |
585 |
|
} |
586 |
|
|
587 |
|
warn "## $url ## $request", dump( $headers ) if $debug; |
588 |
|
|
589 |
if ($request =~ m{^GET (/.*) HTTP/1.[01]}) { |
if ($request =~ m{^GET (/.*) HTTP/1.[01]}) { |
590 |
my $path = $1; |
my $path = $1; |