/[pxelator]/lib/PXElator/httpd.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /lib/PXElator/httpd.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 391 by dpavlin, Mon Sep 7 21:53:45 2009 UTC revision 487 by dpavlin, Sat Jan 23 20:47:17 2010 UTC
# Line 18  use File::Slurp; Line 18  use File::Slurp;
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 YAML;
22    
23  our $title;  our $title;
24    
# Line 36  qq{ Line 37  qq{
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;
# Line 63  use client; Line 51  use client;
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;
# Line 76  use nmap; Line 63  use nmap;
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 >store</a>
81    <a target=store href=/store/query >query</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) = @_;
# Line 85  sub static { Line 94  sub static {
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);
# Line 107  sub static { Line 118  sub static {
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    
# Line 135  sub redirect { Line 146  sub redirect {
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    
# Line 215  warn "XXX pids = ", dump( $daemons::pids Line 231  warn "XXX pids = ", dump( $daemons::pids
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_name', 'base_dir', 'conf' )  
                         )  
                         , qq|</form>|  
271                          ;                          ;
272    
273            } elsif ( $path =~ m{^/store/query} ) {
274                    print $client ok
275                            , qq|<table>|
276                    ;
277                    store::query( sub {
278                            my $o = shift;
279                            my $p = delete( $o->{package} );
280                            delete( $o->{_id} );
281                            # XXX sigh, dump dies if we don't do this
282                            my $tmp = delete( $o->{param} );
283                            $o->{param} = $tmp if defined $tmp;
284                            print $client qq|<tr><td>|, join(qq|</td><td>|, map { $p->{$_} } keys %$p ), qq|</td><td><pre>|, dump( $o ), qq|</pre></td></tr>\n|;
285                    });
286                    print $client qq|</table>|;
287    
288          } elsif ( $path =~ m!^/client(?:/$RE{net}{IPv4}{-keep})?! ) {          } elsif ( $path =~ m!^/client(?:/$RE{net}{IPv4}{-keep})?! ) {
289                  my $ip = $1;                  my $ip = $1;
290                  $title = $ip;                  $title = $ip if $ip;
291    
292                  if ( $param->{action} eq 'remove' ) {                  if ( $param->{action} eq 'remove' ) {
293                          client::remove( $param->{change_ip} );                          client::remove( $param->{change_ip} );
# Line 311  warn "XXX pids = ", dump( $daemons::pids Line 359  warn "XXX pids = ", dump( $daemons::pids
359    
360                  } else {                  } else {
361    
362                            print $client ok qq|<h2>Clients on $server::ip</h2>|;
363    
364                          my @ping;                          my @ping;
365                          if ( my $host = $param->{ping_target} ) {                          if ( my $host = $param->{ping_target} ) {
366                                  @ping = ( $host );                                  @ping = ( $host );
# Line 321  warn "XXX pids = ", dump( $daemons::pids Line 371  warn "XXX pids = ", dump( $daemons::pids
371                          my $ping = ping::fping( @ping ) if @ping;                          my $ping = ping::fping( @ping ) if @ping;
372                          my $arp = client::arp_mac_dev;                          my $arp = client::arp_mac_dev;
373    
374                          print $client ok                          my @clients;
375                                  , qq|<h2>Clients on $server::ip</h2>|  
376                                  , html::table( -5,                          foreach my $ip ( client::all_ips ) {
377                                          'ip', 'mac', 'dev', 'hostname', 'conf',                                  
378                                          map {                                  my $conf = client::all_conf( $ip );
379                                                  my $ip = $_;                                  my $mac = delete $conf->{mac} || '';
380                                                  my $conf = client::all_conf( $ip );                                  my $dev = $arp->{$mac};
381                                                  my $mac = delete $conf->{mac} || '';  
382                                                  my $style;                                  next unless $dev || $param->{all};
383                                                  $style  
384                                                          = 'style="color:'                                  my $style
385                                                          . ( $ping->{$ip} ? 'green' : 'red' )                                          = 'style="color:'
386                                                          . '"'                                          . ( $ping->{$ip} ? 'green' : 'red' )
387                                                          if $ping;                                          . '"'
388                                                  $style ||= '';                                          if $ping;
389                                                  (  
390                                                          qq|<a $style name=$ip target=$ip href=/client/$ip>$ip</a>|                                  $style ||= '';
391                                                          , format::mac( $mac => 'html' )                                  my $ip_text = qq|<tt>$ip</tt>|;
392                                                          , $arp->{$mac}                                  $ip_text = qq|<tt><b>$ip</b></tt>| if ip::in_dhcp_range($ip);
393                                                          , delete $conf->{hostname}  
394                                                          , html::conf( $ip, $conf, 'inline' )                                  $dev = qq|<tt>$dev</tt>| if $dev;
395                                                  )  
396                                          } client::all_ips                                  push @clients
397                                  )                                          , qq|<a $style name=$ip target=client href=/client/$ip>$ip_text</a>|
398                                            , format::mac( $mac => 'html' )
399                                            , $dev
400                                            , delete $conf->{hostname}
401                                            , html::conf( $ip, $conf, 'inline' )
402                                  ;                                  ;
403                            }
404    
405                            my $all = $param->{all} ? 0 : 1;
406    
407                            print $client html::table( -5, 'ip', 'mac', qq|<a href="?all=$all">dev</a>|, 'hostname', 'conf', @clients );
408                          print $client qq|                          print $client qq|
409                                  <form method=get>                                  <form method=get>
410                                  <input type=text   name=ping_target   size=15>                                  <input type=text   name=ping_target   size=15>
# Line 353  warn "XXX pids = ", dump( $daemons::pids Line 412  warn "XXX pids = ", dump( $daemons::pids
412                                  </form>                                  </form>
413                          |;                          |;
414                  }                  }
415    
416    
417          } elsif ( $path =~ m{^/brctl} ) {          } elsif ( $path =~ m{^/brctl} ) {
418                  print $client ok, html::table( -4,  
419                    system 'brctl addif virtual ' . $param->{addif} if $param->{addif};
420                    system 'brctl delif virtual ' . $param->{delif} if $param->{delif};
421    
422                    my $in_virtual;
423    
424                    my @table =
425                          map {                          map {
426                                  my @c = split(/\t+/,$_,4);                                  my @c = split(/\t+/,$_,4);
427                                  if ( $#c == 1 ) {                                  if ( $#c == 1 ) {
428                                          ( '', '', '', $c[1] )                                          $in_virtual->{ $c[1] }++;
429                                            @c = ( '', '', '', $c[1] );
430                                  } else {                                  } else {
431                                          @c                                          $in_virtual->{ $c[3] }++;
432                                    }
433                                    if ( $c[3] =~ m{\d$} ) {
434                                            $c[3] = qq|<input type=submit name=delif value=$c[3] style="color:red" title="remove $c[3] from bridge">|;
435                                  }                                  }
436                                    @c
437                          } split(/\n/, `brctl show`)                          } split(/\n/, `brctl show`)
438                  );                  ;
439    
440                    my @add_ifs = grep { ! $in_virtual->{$_} && $_ ne 'virtual' } ip::devices_up;
441    
442                    push @table, ( '', '', '', html::select( 'addif', @add_ifs ) . qq|<input type=submit value=add></form>| );
443    
444                    print $client ok
445                            , qq|<form>|
446                            , html::table( -4, @table )
447                            , qq|</form>|
448                            ;
449    
450    
451          } elsif ( $path =~ m{^/ip/?(\w+)?} ) {          } elsif ( $path =~ m{^/ip/?(\w+)?} ) {
452                  print $client ok                  print $client ok
453                          , 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/ ))
# Line 394  warn "XXX pids = ", dump( $daemons::pids Line 478  warn "XXX pids = ", dump( $daemons::pids
478          } elsif ( $path =~ m{^/start_stop/(\S+)} ) {          } elsif ( $path =~ m{^/start_stop/(\S+)} ) {
479                  print $client redirect, daemons::start_stop($1,$param);                  print $client redirect, daemons::start_stop($1,$param);
480          } elsif ( $path =~ m{^/action/([^/]+)/(.+)} ) {          } elsif ( $path =~ m{^/action/([^/]+)/(.+)} ) {
481                  $1->$2();                  my ( $package, $method ) = ( $1, $2 );
482                    $ENV{nr} = $1 if $package =~ s{\.(\d+)$}{};
483                    $package->$method();
484                  print $client redirect;                  print $client redirect;
485          } elsif ( $path =~ m{^/kill/static/(\d+)} ) {          } elsif ( $path =~ m{^/kill/static/(\d+)} ) {
486                  print $client redirect;                  print $client redirect;
# Line 416  sub start { Line 502  sub start {
502    
503          my $server = IO::Socket::INET->new(          my $server = IO::Socket::INET->new(
504                          Proto     => 'tcp',                          Proto     => 'tcp',
505                          LocalAddr => $server::ip,  #                       LocalAddr => $server::ip,
506                          LocalPort => $httpd::port,                          LocalPort => $httpd::port,
507                          Listen    => SOMAXCONN,                          Listen    => SOMAXCONN,
508                          Reuse     => 1                          Reuse     => 1
# Line 425  sub start { Line 511  sub start {
511          print "url $url\n";          print "url $url\n";
512    
513          syslogd::install_local;          syslogd::install_local;
514            client::rebuild_mac_links;
515    
516          while (1) {          while (1) {
517                  my $client = $server->accept() || next; # ALARM trickle us                  my $client = $server->accept() || next; # ALARM trickle us
518                  my $request = <$client>;                  my $request = <$client>;
519    
520                  warn "request $request\n" if $debug;                  my $headers;
521    
522                    while ( my $header = <$client> ) {
523                            chomp $header;
524                            last if $header =~ m{^\s*$};
525                            my ( $n, $v ) = split(/:\s*/, $header);
526                            $headers->{ lc $n } = $v;
527                    }
528    
529                    if ( my $host = $headers->{host} ) {
530                            $url = 'http://' . $host;
531                            $url .= ":$port" unless $url =~ m{:\d+$};
532                    }
533    
534                    warn "## $url ## $request", dump( $headers ) if $debug;
535    
536                  if ($request =~ m{^GET (/.*) HTTP/1.[01]}) {                  if ($request =~ m{^GET (/.*) HTTP/1.[01]}) {
537                          my $path = $1;                          my $path = $1;

Legend:
Removed from v.391  
changed lines
  Added in v.487

  ViewVC Help
Powered by ViewVC 1.1.26