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";
71 $couch_url =~ s{:\d+.+}{:5984/_utils/};
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=couch href=$couch_url >couchdb</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|
149 my ( $client, $path, $param ) = @_;
153 store::audit( 'request', { path => $path, param => $param, peerhost => $client->peerhost } );
157 if ( my $found = static( $client,$path ) ) {
158 warn "static $found" if $debug;
159 } elsif ( $path eq '/' ) {
165 warn "XXX pids = ", dump( $daemons::pids );
167 foreach my $name ( sort keys %$daemons::pids ) {
168 my $pid = $daemons::pids->{$name}; # || next;
172 my $proc = "/proc/$pid/status";
175 $html .= qq|<a href=/start_stop/$name>$pid</a>|;
177 $html .= qq| <a name=$pid href=#proc-$pid>?</a>| if $name->can('start');
180 .= qq|<a name=proc-$pid href=#$pid>$proc</a><pre style="font-size: 10%">|
187 $class =~ s{\.\d+$}{};
189 if ( $class->can('fork_if_active') ) {
190 $html .= qq| <a href=/start_stop/$name/$_>$_</a>| foreach $class->fork_if_active;
193 if ( $class->can('actions') ) {
194 $html .= qq| <a href=/action/$name/$_>$_</a>| foreach $class->actions;
197 if ( $pid =~ m{^\d+$} ) {
198 $html .= qq|$pid exited |
202 $html .= qq|<a href=/start_stop/$name>restart</a>| if $pid || $name->can('start');
203 if ( $name->can('fork_actions') ) {
204 $html .= qq| <a href=/start_stop/$name/$_>$_</a>| foreach $name->fork_actions;
208 die "no html generated" unless $html;
210 push @rows, ( $name => $html );
213 my $below_table = '';
215 warn 'static_pids: ', dump( $static_pids ) if $debug;
216 foreach my $pid ( keys %$static_pids ) {
217 my $path = $static_pids->{$pid};
218 if ( -d "/proc/$pid" ) {
219 push @rows, ( $path => qq|<a href=/kill/static/$pid>$pid</a>| );
220 } elsif ( $param->{clean_completed_downloads} ) {
221 delete $static_pids->{$pid}
223 push @rows, ( $path => "$pid competed" );
224 $below_table = qq|<a href="/?clean_completed_downloads=1">clean completed downloads</a>|;
228 my $kvm = kvm::next_nr;
229 $kvm = qq|<div><a href=/start_stop/kvm?nr=$kvm>create new kvm $kvm</a></div>|;
232 , html::table( 2, @rows )
235 , html::tabs( log::mac_changes )
239 } elsif ( $path =~ m{^/server} ) {
240 foreach my $name ( keys %$param ) {
241 eval '$server::' . $name . '= $param->{$name}';
244 'debug' => qq|<a href=/our/debug/| . boolean::toggle($debug) . qq|>$debug</a>|,
245 , 'new_clients' => qq|<input type=text name=new_clients size=3 value="$server::new_clients">|
248 foreach my $editable ( 'ip', 'bcast', 'netmask', 'ip_from', 'ip_to', 'domain' ) {
249 my $v = eval '$server::' . $editable;
250 push @table, ( $editable, qq|<input type=text name=$editable value="$v">| );
253 foreach my $readonly ( 'base_dir', 'conf' ) {
254 my $v = eval '$server::' . $readonly;
255 push @table, ( $readonly, html::tt $v );
259 , qq|<form method=get>|
260 , html::table( 2, @table )
262 <input type=submit name=action value=change>
267 } elsif ( $path =~ m!^/client(?:/$RE{net}{IPv4}{-keep})?! ) {
271 if ( $param->{action} eq 'remove' ) {
272 client::remove( $param->{change_ip} );
273 print $client redirect("$url/client");
275 } elsif ( $param->{action} eq 'change' ) {
276 if ( my $new_ip = client::change_ip( $ip, $param->{change_ip} ) ) {
277 print $client redirect("$url/client#$new_ip");
283 my $peer_ip = $client->peerhost;
285 my $netmask = ip::to_int $server::netmask;
286 my $network = ip::to_int($server::ip) & $netmask;
287 my $from_int = $network | $server::ip_from;
288 my $to_int = $network | $server::ip_to;
289 my $ip_int = ip::to_int $peer_ip;
291 # show edit for clients in our dhcp range
292 if ( $ip_int >= $from_int && $ip_int <= $to_int ) {
297 if ( $ip && $ip ne $server::ip ) {
299 my @editable = ( qw/hostname config homepage/ );
301 client::conf( $ip, $_ => $param->{$_} ) foreach @editable;
303 my $conf = client::all_conf( $ip );
304 my $config = delete $conf->{config};
306 my $nmap = qq|<a href=/nmap?scan=$ip>nmap</a>|;
308 'ping' => ping::host($ip)
309 ? qq|<span style="color:green">up</span> $nmap|
310 : qq|<span style="color: red">down</span> <a href=/wol/$ip>wol</a> $nmap|
312 '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>|,
313 'mac' => format::mac( delete $conf->{mac}, 'html' ),
314 'hostname' => qq|<input type=text name=hostname value="| . delete($conf->{hostname}) . qq|">|,
315 'config' => html::select( 'config', $config, config::available ),
316 html::conf( $ip, $conf, 'edit', @editable )
320 , qq|<form method=get>|
321 , html::table( 2, @table ),
323 <input type=submit name=action value=change>
324 <input type=submit name=action value=remove style="color: red">
329 if ( my $for_ip = config::for_ip( $ip ) ) {
330 print $client qq|<h2>config::for_ip</h2>| . html::pre( $for_ip );
334 if ( $conf->{amt} ) {
335 print $client qq|<h2>amt network</h2>|, html::pre_dump( amt::network( $ip ) );
336 print $client qq|<h2>amt log</h2>|, html::pre_dump( amt::log( $ip ) );
341 print $client ok qq|<h2>Clients on $server::ip</h2>|;
344 if ( my $host = $param->{ping_target} ) {
346 } elsif ( $param->{ping} ) {
347 @ping = client::all_ips;
350 my $ping = ping::fping( @ping ) if @ping;
351 my $arp = client::arp_mac_dev;
355 foreach my $ip ( client::all_ips ) {
357 my $conf = client::all_conf( $ip );
358 my $mac = delete $conf->{mac} || '';
359 my $dev = $arp->{$mac};
361 next unless $dev || $param->{all};
365 . ( $ping->{$ip} ? 'green' : 'red' )
370 my $ip_text = qq|<tt>$ip</tt>|;
371 $ip_text = qq|<tt><b>$ip</b></tt>| if ip::in_dhcp_range($ip);
373 $dev = qq|<tt>$dev</tt>| if $dev;
376 , qq|<a $style name=$ip target=client href=/client/$ip>$ip_text</a>|
377 , format::mac( $mac => 'html' )
379 , delete $conf->{hostname}
380 , html::conf( $ip, $conf, 'inline' )
384 my $all = $param->{all} ? 0 : 1;
386 print $client html::table( -5, 'ip', 'mac', qq|<a href="?all=$all">dev</a>|, 'hostname', 'conf', @clients );
389 <input type=text name=ping_target size=15>
390 <input type=submit name=ping value=ping>
396 } elsif ( $path =~ m{^/brctl} ) {
398 system 'brctl addif virtual ' . $param->{addif} if $param->{addif};
399 system 'brctl delif virtual ' . $param->{delif} if $param->{delif};
405 my @c = split(/\t+/,$_,4);
407 $in_virtual->{ $c[1] }++;
408 @c = ( '', '', '', $c[1] );
410 $in_virtual->{ $c[3] }++;
412 if ( $c[3] =~ m{\d$} ) {
413 $c[3] = qq|<input type=submit name=delif value=$c[3] style="color:red" title="remove $c[3] from bridge">|;
416 } split(/\n/, `brctl show`)
419 my @add_ifs = grep { ! $in_virtual->{$_} && $_ ne 'virtual' } ip::devices_up;
421 push @table, ( '', '', '', html::select( 'addif', @add_ifs ) . qq|<input type=submit value=add></form>| );
425 , html::table( -4, @table )
430 } elsif ( $path =~ m{^/ip/?(\w+)?} ) {
432 , join("\n", map { qq|<a href=/ip/$_>$_</a>| } ( qw/link addr route neigh ntable tunnel maddr mroute xfrm/ ))
435 } elsif ( $path =~ m{^/nmap} ) {
436 if ( my $scan = $param->{scan} ) {
438 print $client redirect("$url/client#$scan");
440 print $client ok, qq|
442 <input type=text name=scan>
443 <input type=submit value=scan>
447 } elsif ( $path =~ m{^/wol/(\S+)} ) {
448 print $client redirect( "$url/client/$1" ), wol::power_on($1);
449 } elsif ( $path =~ m!^/amt/(\w+)/$RE{net}{IPv4}{-keep}! ) {
450 my ( $run, $ip ) = ( $1, $2 );
451 print $client redirect( "$url/client/$ip" ), amt::RemoteControl( $ip, $run );
452 } elsif ( $path =~ m{^/our/(\w+)/(\S+)} ) {
453 eval 'our $' . $1 . ' = ' . $2;
455 print $client redirect($url), qq|<big>$1 = $2</big><br>Location: <a href="$url">$url</a>|;
456 server::debug( $debug ) if $1 eq 'debug';
457 } elsif ( $path =~ m{^/start_stop/(\S+)} ) {
458 print $client redirect, daemons::start_stop($1,$param);
459 } elsif ( $path =~ m{^/action/([^/]+)/(.+)} ) {
460 my ( $package, $method ) = ( $1, $2 );
461 $ENV{nr} = $1 if $package =~ s{\.(\d+)$}{};
463 print $client redirect;
464 } elsif ( $path =~ m{^/kill/static/(\d+)} ) {
465 print $client redirect;
466 kill 1, $1 || kill 9, $2 && warn "killed $1";
468 print $client "HTTP/1.0 404 $path\r\nConnection: close\r\nContent-type: text/html\r\n\r\n<big>404 $path</big>";
476 warn 'network ', network::setup();
478 daemons::start_stop 'browser', { url => $url };
479 daemons::start_stop $_ foreach ( qw/dhcpd tftpd dnsd syslogd/ );
480 # daemons::start_stop 'kvm' unless $ENV{DEV}; # skip kvm statup when running on real device
482 my $server = IO::Socket::INET->new(
484 # LocalAddr => $server::ip,
485 LocalPort => $httpd::port,
488 ) || die "can't start server on $url: $!";
492 syslogd::install_local;
493 client::rebuild_mac_links;
496 my $client = $server->accept() || next; # ALARM trickle us
497 my $request = <$client>;
501 while ( my $header = <$client> ) {
503 last if $header =~ m{^\s*$};
504 my ( $n, $v ) = split(/:\s*/, $header);
505 $headers->{ lc $n } = $v;
508 if ( my $host = $headers->{host} ) {
509 $url = 'http://' . $host;
510 $url .= ":$port" unless $url =~ m{:\d+$};
513 warn "## $url ## $request", dump( $headers ) if $debug;
515 if ($request =~ m{^GET (/.*) HTTP/1.[01]}) {
517 $path =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/ge;
519 if ( $path =~ s{\?(.+)}{} ) {
520 foreach my $p ( split(/[&;]/, $1) ) {
521 my ($n,$v) = split(/=/, $p, 2);
524 warn "param: ",dump( $param ) if $debug;
526 get_request $client, $path, $param;
528 print $client "HTTP/1.0 500 No method\r\nConnection: close\r\nContent-type: text/plain\r\n\r\n500 $request";
532 print $client menu() . html_end() if $client->connected;