--- lib/PXElator/httpd.pm 2009/08/27 12:42:53 303 +++ lib/PXElator/httpd.pm 2010/01/25 18:30:47 493 @@ -18,17 +18,23 @@ #use JSON; use IO::Socket::INET; use Regexp::Common qw/net/; +use POSIX qw(strftime); -sub menu {qq{ +our $title; -
-home -server -brctl -ip -client -
+sub html_start { +qq{ + + +$title + + +}} +sub html_end { +qq{ + + }} our $port = 7777; @@ -45,7 +51,6 @@ use log; use x11; use amt; -use boolean; use daemons; use kvm; @@ -54,8 +59,33 @@ use ip; use wireshark; use syslogd; +use nmap; +use ping; +use wol; + +use store; + + +sub menu { + my $store_url = $url; + $store_url =~ s{:\d+.+}{:28017}; +qq{ +
+home +| +server +brctl +ip +| +MongoDB +latest +| +nmap +client +
+ +}} -use CouchDB; sub static { my ($client,$path) = @_; @@ -64,6 +94,8 @@ return if ! -f $full; + return if $full =~ m{\.ico$}; + if ( my $pid = fork ) { # parent close($client); @@ -86,7 +118,7 @@ my $buff; my $pos = 0; - 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 }); progress_bar::start; @@ -105,7 +137,7 @@ } sub ok { - qq|HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nConnection: close\r\n\r\n| . menu() + qq|HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nConnection: close\r\n\r\n| . html_start() . menu() } sub redirect { @@ -114,12 +146,19 @@ qq|HTTP/1.1 302 Found\r\nContent-type: text/html\r\nLocation: $to\r\n\r\n| } +sub toggle { + my $v = shift; + return $v ? 0 : 1; +} + sub get_request { my ( $client, $path, $param ) = @_; server->refresh; - CouchDB::audit( 'request', { path => $path, param => $param, peerhost => $client->peerhost } ); + store::audit( 'request', { path => $path, param => $param, peerhost => $client->peerhost } ); + + $title = $path; if ( my $found = static( $client,$path ) ) { warn "static $found" if $debug; @@ -150,12 +189,15 @@ ; } - if ( $name->can('fork_if_active') ) { - $html .= qq| $_| foreach $name->fork_if_active; + my $class = $name; + $class =~ s{\.\d+$}{}; + + if ( $class->can('fork_if_active') ) { + $html .= qq| $_| foreach $class->fork_if_active; } - if ( $name->can('actions') ) { - $html .= qq| $_| foreach $name->actions; + if ( $class->can('actions') ) { + $html .= qq| $_| foreach $class->actions; } } else { if ( $pid =~ m{^\d+$} ) { @@ -189,124 +231,290 @@ } } + my $kvm = kvm::next_nr; + $kvm = qq|
create new kvm $kvm
|; + print $client ok , html::table( 2, @rows ) , $below_table + , $kvm , html::tabs( log::mac_changes ) , $debug_proc ; } elsif ( $path =~ m{^/server} ) { + foreach my $name ( keys %$param ) { + eval '$server::' . $name . '= $param->{$name}'; + } + my @table = ( + 'debug' => qq|$debug|, + , 'new_clients' => qq|| + ); + + foreach my $editable ( 'ip', 'bcast', 'netmask', 'ip_from', 'ip_to', 'domain' ) { + my $v = eval '$server::' . $editable; + push @table, ( $editable, qq|| ); + } + + foreach my $readonly ( 'base_dir', 'conf' ) { + my $v = eval '$server::' . $readonly; + push @table, ( $readonly, html::tt $v ); + } + print $client ok - , html::table( 2, - 'debug' => qq|$debug|, - map { - ( $_, html::tt eval '$server::'.$_ ) - } ( 'ip', 'netmask', 'ip_from', 'ip_to', 'domain_name', 'base_dir', 'conf' ) - ) + , qq|
| + , html::table( 2, @table ) + , qq| + +
+ | + ; + + } elsif ( $path =~ m{^/store/latest} ) { + print $client ok + , qq| + + | + , qq|| + ; + my ( $s1,$s2 ) = ( ' class=z', '' ); + my @cols; + + store::query( sub { + my $o = shift; + my $p = delete( $o->{package} ); + delete( $o->{_id} ); + + if ( ! @cols ) { + #@cols = keys %$p; + @cols = qw( time name ); + print $client qq|| + ; + } + + # XXX sigh, dump dies if we don't do this +# delete $o->{$_} foreach ( grep { ! defined $o->{$_} } keys %$o ); + + print $client qq|| + , strftime( qq||, localtime($p->{time}) ) + , map { qq|\n| ; + ( $s1, $s2 ) = ( $s2, $s1 ); + }); + print $client qq|
| + , join(qq||, @cols) + , qq|
%H:%M:%S$_| } ( $p->{name} , html::pre_dump($o) ) + , qq|
|; + } elsif ( $path =~ m!^/client(?:/$RE{net}{IPv4}{-keep})?! ) { - my $ip = $1 || $client->peerhost; + my $ip = $1; + $title = $ip if $ip; - if ( my $new_ip = $param->{change_ip} ) { - client::change_ip( $ip, $new_ip ); - $ip = $new_ip; + if ( $param->{action} eq 'remove' ) { + client::remove( $param->{change_ip} ); + print $client redirect("$url/client"); + return; + } elsif ( $param->{action} eq 'change' ) { + if ( my $new_ip = client::change_ip( $ip, $param->{change_ip} ) ) { + print $client redirect("$url/client#$new_ip"); + return; + } } - my $ip_short = (split(/\./, $ip, 4))[3]; + if ( ! $ip ) { + my $peer_ip = $client->peerhost; + + my $netmask = ip::to_int $server::netmask; + my $network = ip::to_int($server::ip) & $netmask; + my $from_int = $network | $server::ip_from; + my $to_int = $network | $server::ip_to; + my $ip_int = ip::to_int $peer_ip; + + # show edit for clients in our dhcp range + if ( $ip_int >= $from_int && $ip_int <= $to_int ) { + $ip = $peer_ip; + } + } + + if ( $ip && $ip ne $server::ip ) { + + my @editable = ( qw/hostname config homepage/ ); - # if ( $ip ne $server::ip ) -- not flexible enough for tunnel endpoints - if ( $ip_short >= $server::ip_from && $ip_short <= $server::ip_to ) { - my $hostname = client::conf( $ip, 'hostname' => $param->{hostname} ); + client::conf( $ip, $_ => $param->{$_} ) foreach @editable; + my $conf = client::all_conf( $ip ); + my $config = delete $conf->{config}; + + my $nmap = qq|nmap|; my @table = ( + 'ping' => ping::host($ip) + ? qq|up $nmap| + : qq|down wol $nmap| + , 'ip' => qq|