11 perl -Ilib/PXElator -Mhttpd -e httpd::start
15 use Data::Dump qw/dump/;
20 use Regexp::Common qw/net/;
43 our $debug = server::debug;
44 our $url = "http://$server::ip:$port";
71 $store_url =~ s{:\d+.+}{:28017};
73 <div style="font-size: 80%; color: #888">
74 <a target=pids href=/ >home</a>
76 <a target=server href=/server >server</a>
77 <a target=server href=/brctl >brctl</a>
78 <a target=server href=/ip >ip</a>
80 <a target=store href=$store_url >store</a>
81 <a target=store href=/store/query >query</a>
83 <a target=client href=/nmap >nmap</a>
84 <a target=client href=/client >client</a>
91 my ($client,$path) = @_;
93 my $full = "$server::base_dir/tftp/$path";
97 return if $full =~ m{\.ico$};
99 if ( my $pid = fork ) {
102 $static_pids->{$pid} = $path;
106 my $type = 'application/octet-stream';
107 $type = 'text/html' if $path =~ m{\.htm};
108 $type = 'application/javascript' if $path =~ m{\.js};
109 $type = 'text/plain' if $path =~ m{\.txt};
111 my $size = -s $full || return;
113 print $client "HTTP/1.0 200 OK\r\nContent-Type: $type\r\nContent-Length: $size\r\nConnection: close\r\n\r\n";
117 my $block = 1400; # try not to fragment packages (pxelinux seems to have problems with it)
121 store::audit( 'static', { pid => $$, path => $path, type => $type, size => $size, block => $block, peerhost => $client->peerhost });
125 while( my $len = read $fh, $buff, $block ) {
129 progress_bar::tick( $path, $pos, $size );
140 qq|HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nConnection: close\r\n\r\n| . html_start() . menu()
146 qq|HTTP/1.1 302 Found\r\nContent-type: text/html\r\nLocation: $to\r\n\r\n|
155 my ( $client, $path, $param ) = @_;
159 store::audit( 'request', { path => $path, param => $param, peerhost => $client->peerhost } );
163 if ( my $found = static( $client,$path ) ) {
164 warn "static $found" if $debug;
165 } elsif ( $path eq '/' ) {
171 warn "XXX pids = ", dump( $daemons::pids );
173 foreach my $name ( sort keys %$daemons::pids ) {
174 my $pid = $daemons::pids->{$name}; # || next;
178 my $proc = "/proc/$pid/status";
181 $html .= qq|<a href=/start_stop/$name>$pid</a>|;
183 $html .= qq| <a name=$pid href=#proc-$pid>?</a>| if $name->can('start');
186 .= qq|<a name=proc-$pid href=#$pid>$proc</a><pre style="font-size: 10%">|
193 $class =~ s{\.\d+$}{};
195 if ( $class->can('fork_if_active') ) {
196 $html .= qq| <a href=/start_stop/$name/$_>$_</a>| foreach $class->fork_if_active;
199 if ( $class->can('actions') ) {
200 $html .= qq| <a href=/action/$name/$_>$_</a>| foreach $class->actions;
203 if ( $pid =~ m{^\d+$} ) {
204 $html .= qq|$pid exited |
208 $html .= qq|<a href=/start_stop/$name>restart</a>| if $pid || $name->can('start');
209 if ( $name->can('fork_actions') ) {
210 $html .= qq| <a href=/start_stop/$name/$_>$_</a>| foreach $name->fork_actions;
214 die "no html generated" unless $html;
216 push @rows, ( $name => $html );
219 my $below_table = '';
221 warn 'static_pids: ', dump( $static_pids ) if $debug;
222 foreach my $pid ( keys %$static_pids ) {
223 my $path = $static_pids->{$pid};
224 if ( -d "/proc/$pid" ) {
225 push @rows, ( $path => qq|<a href=/kill/static/$pid>$pid</a>| );
226 } elsif ( $param->{clean_completed_downloads} ) {
227 delete $static_pids->{$pid}
229 push @rows, ( $path => "$pid competed" );
230 $below_table = qq|<a href="/?clean_completed_downloads=1">clean completed downloads</a>|;
234 my $kvm = kvm::next_nr;
235 $kvm = qq|<div><a href=/start_stop/kvm?nr=$kvm>create new kvm $kvm</a></div>|;
238 , html::table( 2, @rows )
241 , html::tabs( log::mac_changes )
245 } elsif ( $path =~ m{^/server} ) {
246 foreach my $name ( keys %$param ) {
247 eval '$server::' . $name . '= $param->{$name}';
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">|
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">| );
259 foreach my $readonly ( 'base_dir', 'conf' ) {
260 my $v = eval '$server::' . $readonly;
261 push @table, ( $readonly, html::tt $v );
265 , qq|<form method=get>|
266 , html::table( 2, @table )
268 <input type=submit name=action value=change>
273 } elsif ( $path =~ m{^/store/query} ) {
277 my ( $s1,$s2 ) = ( ' style="background: #eee"', '' );
280 my $p = delete( $o->{package} );
283 # XXX sigh, dump dies if we don't do this
284 delete $o->{$_} foreach ( grep { ! defined $o->{$_} } keys %$o );
286 print $client qq|<tr$s1><td>|, join(qq|</td><td>|, map { $p->{$_} } keys %$p ), qq|</td><td><pre>|, dump( $o ), qq|</pre></td></tr>\n|;
287 ( $s1, $s2 ) = ( $s2, $s1 );
289 print $client qq|</table>|;
291 } elsif ( $path =~ m!^/client(?:/$RE{net}{IPv4}{-keep})?! ) {
295 if ( $param->{action} eq 'remove' ) {
296 client::remove( $param->{change_ip} );
297 print $client redirect("$url/client");
299 } elsif ( $param->{action} eq 'change' ) {
300 if ( my $new_ip = client::change_ip( $ip, $param->{change_ip} ) ) {
301 print $client redirect("$url/client#$new_ip");
307 my $peer_ip = $client->peerhost;
309 my $netmask = ip::to_int $server::netmask;
310 my $network = ip::to_int($server::ip) & $netmask;
311 my $from_int = $network | $server::ip_from;
312 my $to_int = $network | $server::ip_to;
313 my $ip_int = ip::to_int $peer_ip;
315 # show edit for clients in our dhcp range
316 if ( $ip_int >= $from_int && $ip_int <= $to_int ) {
321 if ( $ip && $ip ne $server::ip ) {
323 my @editable = ( qw/hostname config homepage/ );
325 client::conf( $ip, $_ => $param->{$_} ) foreach @editable;
327 my $conf = client::all_conf( $ip );
328 my $config = delete $conf->{config};
330 my $nmap = qq|<a href=/nmap?scan=$ip>nmap</a>|;
332 'ping' => ping::host($ip)
333 ? qq|<span style="color:green">up</span> $nmap|
334 : qq|<span style="color: red">down</span> <a href=/wol/$ip>wol</a> $nmap|
336 '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>|,
337 'mac' => format::mac( delete $conf->{mac}, 'html' ),
338 'hostname' => qq|<input type=text name=hostname value="| . delete($conf->{hostname}) . qq|">|,
339 'config' => html::select( 'config', $config, config::available ),
340 html::conf( $ip, $conf, 'edit', @editable )
344 , qq|<form method=get>|
345 , html::table( 2, @table ),
347 <input type=submit name=action value=change>
348 <input type=submit name=action value=remove style="color: red">
353 if ( my $for_ip = config::for_ip( $ip ) ) {
354 print $client qq|<h2>config::for_ip</h2>| . html::pre( $for_ip );
358 if ( $conf->{amt} ) {
359 print $client qq|<h2>amt network</h2>|, html::pre_dump( amt::network( $ip ) );
360 print $client qq|<h2>amt log</h2>|, html::pre_dump( amt::log( $ip ) );
365 print $client ok qq|<h2>Clients on $server::ip</h2>|;
368 if ( my $host = $param->{ping_target} ) {
370 } elsif ( $param->{ping} ) {
371 @ping = client::all_ips;
374 my $ping = ping::fping( @ping ) if @ping;
375 my $arp = client::arp_mac_dev;
379 foreach my $ip ( client::all_ips ) {
381 my $conf = client::all_conf( $ip );
382 my $mac = delete $conf->{mac} || '';
383 my $dev = $arp->{$mac};
385 next unless $dev || $param->{all};
389 . ( $ping->{$ip} ? 'green' : 'red' )
394 my $ip_text = qq|<tt>$ip</tt>|;
395 $ip_text = qq|<tt><b>$ip</b></tt>| if ip::in_dhcp_range($ip);
397 $dev = qq|<tt>$dev</tt>| if $dev;
400 , qq|<a $style name=$ip target=client href=/client/$ip>$ip_text</a>|
401 , format::mac( $mac => 'html' )
403 , delete $conf->{hostname}
404 , html::conf( $ip, $conf, 'inline' )
408 my $all = $param->{all} ? 0 : 1;
410 print $client html::table( -5, 'ip', 'mac', qq|<a href="?all=$all">dev</a>|, 'hostname', 'conf', @clients );
413 <input type=text name=ping_target size=15>
414 <input type=submit name=ping value=ping>
420 } elsif ( $path =~ m{^/brctl} ) {
422 system 'brctl addif virtual ' . $param->{addif} if $param->{addif};
423 system 'brctl delif virtual ' . $param->{delif} if $param->{delif};
429 my @c = split(/\t+/,$_,4);
431 $in_virtual->{ $c[1] }++;
432 @c = ( '', '', '', $c[1] );
434 $in_virtual->{ $c[3] }++;
436 if ( $c[3] =~ m{\d$} ) {
437 $c[3] = qq|<input type=submit name=delif value=$c[3] style="color:red" title="remove $c[3] from bridge">|;
440 } split(/\n/, `brctl show`)
443 my @add_ifs = grep { ! $in_virtual->{$_} && $_ ne 'virtual' } ip::devices_up;
445 push @table, ( '', '', '', html::select( 'addif', @add_ifs ) . qq|<input type=submit value=add></form>| );
449 , html::table( -4, @table )
454 } elsif ( $path =~ m{^/ip/?(\w+)?} ) {
456 , join("\n", map { qq|<a href=/ip/$_>$_</a>| } ( qw/link addr route neigh ntable tunnel maddr mroute xfrm/ ))
459 } elsif ( $path =~ m{^/nmap} ) {
460 if ( my $scan = $param->{scan} ) {
462 print $client redirect("$url/client#$scan");
464 print $client ok, qq|
466 <input type=text name=scan>
467 <input type=submit value=scan>
471 } elsif ( $path =~ m{^/wol/(\S+)} ) {
472 print $client redirect( "$url/client/$1" ), wol::power_on($1);
473 } elsif ( $path =~ m!^/amt/(\w+)/$RE{net}{IPv4}{-keep}! ) {
474 my ( $run, $ip ) = ( $1, $2 );
475 print $client redirect( "$url/client/$ip" ), amt::RemoteControl( $ip, $run );
476 } elsif ( $path =~ m{^/our/(\w+)/(\S+)} ) {
477 eval 'our $' . $1 . ' = ' . $2;
479 print $client redirect($url), qq|<big>$1 = $2</big><br>Location: <a href="$url">$url</a>|;
480 server::debug( $debug ) if $1 eq 'debug';
481 } elsif ( $path =~ m{^/start_stop/(\S+)} ) {
482 print $client redirect, daemons::start_stop($1,$param);
483 } elsif ( $path =~ m{^/action/([^/]+)/(.+)} ) {
484 my ( $package, $method ) = ( $1, $2 );
485 $ENV{nr} = $1 if $package =~ s{\.(\d+)$}{};
487 print $client redirect;
488 } elsif ( $path =~ m{^/kill/static/(\d+)} ) {
489 print $client redirect;
490 kill 1, $1 || kill 9, $2 && warn "killed $1";
492 print $client "HTTP/1.0 404 $path\r\nConnection: close\r\nContent-type: text/html\r\n\r\n<big>404 $path</big>";
500 warn 'network ', network::setup();
502 daemons::start_stop 'browser', { url => $url };
503 daemons::start_stop $_ foreach ( qw/dhcpd tftpd dnsd syslogd/ );
504 # daemons::start_stop 'kvm' unless $ENV{DEV}; # skip kvm statup when running on real device
506 my $server = IO::Socket::INET->new(
508 # LocalAddr => $server::ip,
509 LocalPort => $httpd::port,
512 ) || die "can't start server on $url: $!";
516 syslogd::install_local;
517 client::rebuild_mac_links;
520 my $client = $server->accept() || next; # ALARM trickle us
521 my $request = <$client>;
525 while ( my $header = <$client> ) {
527 last if $header =~ m{^\s*$};
528 my ( $n, $v ) = split(/:\s*/, $header);
529 $headers->{ lc $n } = $v;
532 if ( my $host = $headers->{host} ) {
533 $url = 'http://' . $host;
534 $url .= ":$port" unless $url =~ m{:\d+$};
537 warn "## $url ## $request", dump( $headers ) if $debug;
539 if ($request =~ m{^GET (/.*) HTTP/1.[01]}) {
541 $path =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/ge;
543 if ( $path =~ s{\?(.+)}{} ) {
544 foreach my $p ( split(/[&;]/, $1) ) {
545 my ($n,$v) = split(/=/, $p, 2);
548 warn "param: ",dump( $param ) if $debug;
550 get_request $client, $path, $param;
552 print $client "HTTP/1.0 500 No method\r\nConnection: close\r\nContent-type: text/plain\r\n\r\n500 $request";
556 print $client menu() . html_end() if $client->connected;