11 perl -Ilib/PXElator -Mhttpd -e httpd::start
15 use Data::Dump qw/dump/;
20 use Regexp::Common qw/net/;
42 our $debug = server::debug;
43 our $url = "http://$server::ip:$port";
70 $store_url =~ s{:\d+.+}{:28017};
72 <div style="font-size: 80%; color: #888">
73 <a target=pids href=/ >home</a>
75 <a target=server href=/server >server</a>
76 <a target=server href=/brctl >brctl</a>
77 <a target=server href=/ip >ip</a>
79 <a target=store href=$store_url >store</a>
80 <a target=store href=/store/query >query</a>
82 <a target=client href=/nmap >nmap</a>
83 <a target=client href=/client >client</a>
90 my ($client,$path) = @_;
92 my $full = "$server::base_dir/tftp/$path";
96 return if $full =~ m{\.ico$};
98 if ( my $pid = fork ) {
101 $static_pids->{$pid} = $path;
105 my $type = 'application/octet-stream';
106 $type = 'text/html' if $path =~ m{\.htm};
107 $type = 'application/javascript' if $path =~ m{\.js};
108 $type = 'text/plain' if $path =~ m{\.txt};
110 my $size = -s $full || return;
112 print $client "HTTP/1.0 200 OK\r\nContent-Type: $type\r\nContent-Length: $size\r\nConnection: close\r\n\r\n";
116 my $block = 1400; # try not to fragment packages (pxelinux seems to have problems with it)
120 store::audit( 'static', { pid => $$, path => $path, type => $type, size => $size, block => $block, peerhost => $client->peerhost });
124 while( my $len = read $fh, $buff, $block ) {
128 progress_bar::tick( $path, $pos, $size );
139 qq|HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nConnection: close\r\n\r\n| . html_start() . menu()
145 qq|HTTP/1.1 302 Found\r\nContent-type: text/html\r\nLocation: $to\r\n\r\n|
154 my ( $client, $path, $param ) = @_;
158 store::audit( 'request', { path => $path, param => $param, peerhost => $client->peerhost } );
162 if ( my $found = static( $client,$path ) ) {
163 warn "static $found" if $debug;
164 } elsif ( $path eq '/' ) {
170 warn "XXX pids = ", dump( $daemons::pids );
172 foreach my $name ( sort keys %$daemons::pids ) {
173 my $pid = $daemons::pids->{$name}; # || next;
177 my $proc = "/proc/$pid/status";
180 $html .= qq|<a href=/start_stop/$name>$pid</a>|;
182 $html .= qq| <a name=$pid href=#proc-$pid>?</a>| if $name->can('start');
185 .= qq|<a name=proc-$pid href=#$pid>$proc</a><pre style="font-size: 10%">|
192 $class =~ s{\.\d+$}{};
194 if ( $class->can('fork_if_active') ) {
195 $html .= qq| <a href=/start_stop/$name/$_>$_</a>| foreach $class->fork_if_active;
198 if ( $class->can('actions') ) {
199 $html .= qq| <a href=/action/$name/$_>$_</a>| foreach $class->actions;
202 if ( $pid =~ m{^\d+$} ) {
203 $html .= qq|$pid exited |
207 $html .= qq|<a href=/start_stop/$name>restart</a>| if $pid || $name->can('start');
208 if ( $name->can('fork_actions') ) {
209 $html .= qq| <a href=/start_stop/$name/$_>$_</a>| foreach $name->fork_actions;
213 die "no html generated" unless $html;
215 push @rows, ( $name => $html );
218 my $below_table = '';
220 warn 'static_pids: ', dump( $static_pids ) if $debug;
221 foreach my $pid ( keys %$static_pids ) {
222 my $path = $static_pids->{$pid};
223 if ( -d "/proc/$pid" ) {
224 push @rows, ( $path => qq|<a href=/kill/static/$pid>$pid</a>| );
225 } elsif ( $param->{clean_completed_downloads} ) {
226 delete $static_pids->{$pid}
228 push @rows, ( $path => "$pid competed" );
229 $below_table = qq|<a href="/?clean_completed_downloads=1">clean completed downloads</a>|;
233 my $kvm = kvm::next_nr;
234 $kvm = qq|<div><a href=/start_stop/kvm?nr=$kvm>create new kvm $kvm</a></div>|;
237 , html::table( 2, @rows )
240 , html::tabs( log::mac_changes )
244 } elsif ( $path =~ m{^/server} ) {
245 foreach my $name ( keys %$param ) {
246 eval '$server::' . $name . '= $param->{$name}';
249 'debug' => qq|<a href=/our/debug/| . toggle($debug) . qq|>$debug</a>|,
250 , 'new_clients' => qq|<input type=text name=new_clients size=3 value="$server::new_clients">|
253 foreach my $editable ( 'ip', 'bcast', 'netmask', 'ip_from', 'ip_to', 'domain' ) {
254 my $v = eval '$server::' . $editable;
255 push @table, ( $editable, qq|<input type=text name=$editable value="$v">| );
258 foreach my $readonly ( 'base_dir', 'conf' ) {
259 my $v = eval '$server::' . $readonly;
260 push @table, ( $readonly, html::tt $v );
264 , qq|<form method=get>|
265 , html::table( 2, @table )
267 <input type=submit name=action value=change>
272 } elsif ( $path =~ m{^/store/query} ) {
275 <style type=text/css>
292 my ( $s1,$s2 ) = ( ' class=z', '' );
297 my $p = delete( $o->{package} );
302 print $client qq|<tr><th>|
303 , join(qq|</th><th>|, @cols)
304 , qq|</th><th></th></tr>|
308 # XXX sigh, dump dies if we don't do this
309 delete $o->{$_} foreach ( grep { ! defined $o->{$_} } keys %$o );
311 print $client qq|<tr$s1><td>|
312 , join(qq|</td><td>|, map { $p->{$_} } @cols )
315 , qq|</pre></td></tr>\n|
317 ( $s1, $s2 ) = ( $s2, $s1 );
319 print $client qq|</table>|;
321 } elsif ( $path =~ m!^/client(?:/$RE{net}{IPv4}{-keep})?! ) {
325 if ( $param->{action} eq 'remove' ) {
326 client::remove( $param->{change_ip} );
327 print $client redirect("$url/client");
329 } elsif ( $param->{action} eq 'change' ) {
330 if ( my $new_ip = client::change_ip( $ip, $param->{change_ip} ) ) {
331 print $client redirect("$url/client#$new_ip");
337 my $peer_ip = $client->peerhost;
339 my $netmask = ip::to_int $server::netmask;
340 my $network = ip::to_int($server::ip) & $netmask;
341 my $from_int = $network | $server::ip_from;
342 my $to_int = $network | $server::ip_to;
343 my $ip_int = ip::to_int $peer_ip;
345 # show edit for clients in our dhcp range
346 if ( $ip_int >= $from_int && $ip_int <= $to_int ) {
351 if ( $ip && $ip ne $server::ip ) {
353 my @editable = ( qw/hostname config homepage/ );
355 client::conf( $ip, $_ => $param->{$_} ) foreach @editable;
357 my $conf = client::all_conf( $ip );
358 my $config = delete $conf->{config};
360 my $nmap = qq|<a href=/nmap?scan=$ip>nmap</a>|;
362 'ping' => ping::host($ip)
363 ? qq|<span style="color:green">up</span> $nmap|
364 : qq|<span style="color: red">down</span> <a href=/wol/$ip>wol</a> $nmap|
366 'ip' => qq|<input type=text name=change_ip value="$ip" onChange="document.getElementById('old_ip').style.display = '';"><span id=old_ip style="display: none; color: #888;">old: $ip<span>|,
367 'mac' => format::mac( delete $conf->{mac}, 'html' ),
368 'hostname' => qq|<input type=text name=hostname value="| . delete($conf->{hostname}) . qq|">|,
369 'config' => html::select( 'config', $config, config::available ),
370 html::conf( $ip, $conf, 'edit', @editable )
374 , qq|<form method=get>|
375 , html::table( 2, @table ),
377 <input type=submit name=action value=change>
378 <input type=submit name=action value=remove style="color: red">
383 if ( my $for_ip = config::for_ip( $ip ) ) {
384 print $client qq|<h2>config::for_ip</h2>| . html::pre( $for_ip );
388 if ( $conf->{amt} ) {
389 print $client qq|<h2>amt network</h2>|, html::pre_dump( amt::network( $ip ) );
390 print $client qq|<h2>amt log</h2>|, html::pre_dump( amt::log( $ip ) );
395 print $client ok qq|<h2>Clients on $server::ip</h2>|;
398 if ( my $host = $param->{ping_target} ) {
400 } elsif ( $param->{ping} ) {
401 @ping = client::all_ips;
404 my $ping = ping::fping( @ping ) if @ping;
405 my $arp = client::arp_mac_dev;
409 foreach my $ip ( client::all_ips ) {
411 my $conf = client::all_conf( $ip );
412 my $mac = delete $conf->{mac} || '';
413 my $dev = $arp->{$mac};
415 next unless $dev || $param->{all};
419 . ( $ping->{$ip} ? 'green' : 'red' )
424 my $ip_text = qq|<tt>$ip</tt>|;
425 $ip_text = qq|<tt><b>$ip</b></tt>| if ip::in_dhcp_range($ip);
427 $dev = qq|<tt>$dev</tt>| if $dev;
430 , qq|<a $style name=$ip target=client href=/client/$ip>$ip_text</a>|
431 , format::mac( $mac => 'html' )
433 , delete $conf->{hostname}
434 , html::conf( $ip, $conf, 'inline' )
438 my $all = $param->{all} ? 0 : 1;
440 print $client html::table( -5, 'ip', 'mac', qq|<a href="?all=$all">dev</a>|, 'hostname', 'conf', @clients );
443 <input type=text name=ping_target size=15>
444 <input type=submit name=ping value=ping>
450 } elsif ( $path =~ m{^/brctl} ) {
452 system 'brctl addif virtual ' . $param->{addif} if $param->{addif};
453 system 'brctl delif virtual ' . $param->{delif} if $param->{delif};
459 my @c = split(/\t+/,$_,4);
461 $in_virtual->{ $c[1] }++;
462 @c = ( '', '', '', $c[1] );
464 $in_virtual->{ $c[3] }++;
466 if ( $c[3] =~ m{\d$} ) {
467 $c[3] = qq|<input type=submit name=delif value=$c[3] style="color:red" title="remove $c[3] from bridge">|;
470 } split(/\n/, `brctl show`)
473 my @add_ifs = grep { ! $in_virtual->{$_} && $_ ne 'virtual' } ip::devices_up;
475 push @table, ( '', '', '', html::select( 'addif', @add_ifs ) . qq|<input type=submit value=add></form>| );
479 , html::table( -4, @table )
484 } elsif ( $path =~ m{^/ip/?(\w+)?} ) {
486 , join("\n", map { qq|<a href=/ip/$_>$_</a>| } ( qw/link addr route neigh ntable tunnel maddr mroute xfrm/ ))
489 } elsif ( $path =~ m{^/nmap} ) {
490 if ( my $scan = $param->{scan} ) {
492 print $client redirect("$url/client#$scan");
494 print $client ok, qq|
496 <input type=text name=scan>
497 <input type=submit value=scan>
501 } elsif ( $path =~ m{^/wol/(\S+)} ) {
502 print $client redirect( "$url/client/$1" ), wol::power_on($1);
503 } elsif ( $path =~ m!^/amt/(\w+)/$RE{net}{IPv4}{-keep}! ) {
504 my ( $run, $ip ) = ( $1, $2 );
505 print $client redirect( "$url/client/$ip" ), amt::RemoteControl( $ip, $run );
506 } elsif ( $path =~ m{^/our/(\w+)/(\S+)} ) {
507 eval 'our $' . $1 . ' = ' . $2;
509 print $client redirect($url), qq|<big>$1 = $2</big><br>Location: <a href="$url">$url</a>|;
510 server::debug( $debug ) if $1 eq 'debug';
511 } elsif ( $path =~ m{^/start_stop/(\S+)} ) {
512 print $client redirect, daemons::start_stop($1,$param);
513 } elsif ( $path =~ m{^/action/([^/]+)/(.+)} ) {
514 my ( $package, $method ) = ( $1, $2 );
515 $ENV{nr} = $1 if $package =~ s{\.(\d+)$}{};
517 print $client redirect;
518 } elsif ( $path =~ m{^/kill/static/(\d+)} ) {
519 print $client redirect;
520 kill 1, $1 || kill 9, $2 && warn "killed $1";
522 print $client "HTTP/1.0 404 $path\r\nConnection: close\r\nContent-type: text/html\r\n\r\n<big>404 $path</big>";
530 warn 'network ', network::setup();
532 daemons::start_stop 'browser', { url => $url };
533 daemons::start_stop $_ foreach ( qw/dhcpd tftpd dnsd syslogd/ );
534 # daemons::start_stop 'kvm' unless $ENV{DEV}; # skip kvm statup when running on real device
536 my $server = IO::Socket::INET->new(
538 # LocalAddr => $server::ip,
539 LocalPort => $httpd::port,
542 ) || die "can't start server on $url: $!";
546 syslogd::install_local;
547 client::rebuild_mac_links;
550 my $client = $server->accept() || next; # ALARM trickle us
551 my $request = <$client>;
555 while ( my $header = <$client> ) {
557 last if $header =~ m{^\s*$};
558 my ( $n, $v ) = split(/:\s*/, $header);
559 $headers->{ lc $n } = $v;
562 if ( my $host = $headers->{host} ) {
563 $url = 'http://' . $host;
564 $url .= ":$port" unless $url =~ m{:\d+$};
567 warn "## $url ## $request", dump( $headers ) if $debug;
569 if ($request =~ m{^GET (/.*) HTTP/1.[01]}) {
571 $path =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/ge;
573 if ( $path =~ s{\?(.+)}{} ) {
574 foreach my $p ( split(/[&;]/, $1) ) {
575 my ($n,$v) = split(/=/, $p, 2);
578 warn "param: ",dump( $param ) if $debug;
580 get_request $client, $path, $param;
582 print $client "HTTP/1.0 500 No method\r\nConnection: close\r\nContent-type: text/plain\r\n\r\n500 $request";
586 print $client menu() . html_end() if $client->connected;