11 perl -Ilib/PXElator -Mhttpd -e httpd::start
15 use Data::Dump qw/dump/;
20 use Regexp::Common qw/net/;
24 <div style="font-size: 80%; color: #888">
26 <a href=/server>server</a>
28 <a href=/client>client</a>
36 our $debug = server::debug;
37 our $url = "http://$server::ip:$port";
59 my ($client,$path) = @_;
61 my $full = "$server::base_dir/tftp/$path";
65 if ( my $pid = fork ) {
68 $static_pids->{$pid} = $path;
72 my $type = 'application/octet-stream';
73 $type = 'text/html' if $path =~ m{\.htm};
74 $type = 'application/javascript' if $path =~ m{\.js};
75 $type = 'text/plain' if $path =~ m{\.txt};
77 my $size = -s $full || return;
79 print $client "HTTP/1.0 200 OK\r\nContent-Type: $type\r\nContent-Length: $size\r\nConnection: close\r\n\r\n";
83 my $block = 1400; # try not to fragment packages (pxelinux seems to have problems with it)
87 CouchDB::audit( 'static', { pid => $$, path => $path, type => $type, size => $size, block => $block, peerhost => $client->peerhost });
91 while( my $len = read $fh, $buff, $block ) {
95 progress_bar::tick( $path, $pos, $size );
106 qq|HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nConnection: close\r\n\r\n| . menu()
112 qq|HTTP/1.1 302 Found\r\nContent-type: text/html\r\nLocation: $to\r\n\r\n|
116 my ( $client, $path, $param ) = @_;
120 CouchDB::audit( 'request', { path => $path, param => $param, peerhost => $client->peerhost } );
122 if ( my $found = static( $client,$path ) ) {
123 warn "static $found" if $debug;
124 } elsif ( $path eq '/' ) {
127 'debug', qq|<a href=/our/debug/| . boolean::toggle($debug) . qq|>$debug</a>|,
132 foreach my $name ( sort keys %$daemons::pids ) {
133 my $pid = $daemons::pids->{$name} || next;
137 my $proc = "/proc/$pid/status";
140 $html .= qq|<a href=/start_stop/$name>$pid</a>|;
142 $html .= qq| <a name=$pid href=#proc-$pid>?</a>| if $name->can('start');
145 .= qq|<a name=proc-$pid href=#$pid>$proc</a><pre style="font-size: 10%">|
151 if ( $name->can('fork_if_active') ) {
152 $html .= qq| <a href=/start_stop/$name/$_>$_</a>| foreach $name->fork_if_active;
155 if ( $name->can('actions') ) {
156 $html .= qq| <a href=/action/$name/$_>$_</a>| foreach $name->actions;
159 $html .= qq|<a href=/start_stop/$name>restart</a> $pid exited| if $name->can('start');
160 if ( $name->can('fork_actions') ) {
161 $html .= qq| <a href=/start_stop/$name/$_>$_</a>| foreach $name->fork_actions;
165 push @rows, ( $name => $html );
168 my $below_table = '';
170 warn 'static_pids: ', dump( $static_pids ) if $debug;
171 foreach my $pid ( keys %$static_pids ) {
172 my $path = $static_pids->{$pid};
173 if ( -d "/proc/$pid" ) {
174 push @rows, ( $path => qq|<a href=/kill/static/$pid>$pid</a>| );
175 } elsif ( $param->{clean_completed_downloads} ) {
176 delete $static_pids->{$pid}
178 push @rows, ( $path => "$pid competed" );
179 $below_table = qq|<a href="/?clean_completed_downloads=1">clean completed downloads</a>|;
184 , html::table( 2, @rows )
186 , html::tabs( log::mac_changes )
190 } elsif ( $path =~ m{^/server} ) {
192 , html::table( 2, map { ( $_, html::tt eval '$server::'.$_ ) } ( 'ip', 'netmask', 'ip_from', 'ip_to', 'domain_name', 'base_dir', 'conf' ) )
194 } elsif ( $path =~ m!^/client(?:/$RE{net}{IPv4}{-keep})?! ) {
195 my $ip = $1 || $client->peerhost;
197 if ( my $new_ip = $param->{change_ip} ) {
198 client::change_ip( $ip, $new_ip );
202 if ( $ip ne $server::ip ) {
203 my $hostname = client::conf( $ip, 'hostname' => $param->{hostname} );
206 '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>|,
207 'hostname' => qq|<input type=text name=hostname value="$hostname">|,
212 if ( my $mac = client::mac_from_ip( $ip ) ) {
213 $deploy = client::conf( $ip, 'deploy' => $param->{deploy} );
215 'mac' => format::mac( $mac => 'html' ),
216 'deploy' => html::select( 'deploy', $deploy, config::available )
218 $deploy = qq|<h2>PXElinux</h2>| . html::pre( config::for_ip( $ip ) );
222 , qq|<form method=get>|
223 , html::table( 2, @table ),
224 , qq|<input type=submit value=change></form>|
228 if ( my $amt = client::conf( $ip, 'amt' ) ) {
229 print $client qq|<h2>AMT</h2>|, amt::info( $amt );
235 my @c = split(/\s+/,$_);
237 ( uc $c[3] => [ $c[0] , $c[5] ] )
240 } read_file('/proc/net/arp')
243 warn "# arp ",dump( $arp );
246 , qq|<h2>Clients on $server::ip</h2>|
248 'ip', 'hostname', 'mac', 'deploy', 'arp',
252 my $mac = client::mac_from_ip $ip;
253 my $arp = $arp->{ $mac };
254 $arp = $arp ? $arp->[1] : '';
257 qq|<a href=/client/$ip>$ip</a>|
258 , client::conf( $ip, 'hostname' )
259 , format::mac( $mac => 'html' )
260 , html::tt( client::conf( $ip, 'deploy' ) )
264 glob("$server::conf/ip/*")
267 , html::table( -3, 'mac', 'dev', 'ip',
270 ( format::mac( $_ => 'html' ), $c->[1], $c->[0] )
275 } elsif ( $path =~ m{^/ip/?(\w+)?} ) {
277 , join("\n", map { qq|<a href=/ip/$_>$_</a>| } ( qw/link addr route neigh ntable tunnel maddr mroute xfrm/ ))
280 } elsif ( $path =~ m{^/our/(\w+)/(\S+)} ) {
281 eval 'our $' . $1 . ' = ' . $2;
283 print $client redirect($url), qq|<big>$1 = $2</big><br>Location: <a href="$url">$url</a>|;
284 server::debug( $debug ) if $1 eq 'debug';
285 } elsif ( $path =~ m{^/start_stop/(\S+)} ) {
286 print $client redirect, daemons::start_stop($1);
287 } elsif ( $path =~ m{^/action/([^/]+)/(.+)} ) {
289 print $client redirect;
290 } elsif ( $path =~ m{^/kill/static/(\d+)} ) {
291 print $client redirect;
292 kill 1, $1 || kill 9, $2 && warn "killed $1";
294 print $client "HTTP/1.0 404 $path\r\nConnection: close\r\nContent-type: text/html\r\n\r\n<big>404 $path</big>";
302 warn 'tap ', network::tap();
304 daemons::start_stop 'browser', $url;
305 daemons::start_stop $_ foreach ( qw/dhcpd tftpd dnsd/ );
306 daemons::start_stop 'kvm' unless $ENV{DEV}; # skip kvm statup when running on real device
308 my $server = IO::Socket::INET->new(
310 LocalAddr => $server::ip,
311 LocalPort => $httpd::port,
314 ) || die "can't start server on $url: $!";
319 my $client = $server->accept() || next; # ALARM trickle us
320 my $request = <$client>;
322 warn "request $request\n" if $debug;
324 if ($request =~ m{^GET (/.*) HTTP/1.[01]}) {
327 if ( $path =~ s{\?(.+)}{} ) {
328 foreach my $p ( split(/[&;]/, $1) ) {
329 my ($n,$v) = split(/=/, $p, 2);
332 warn "param: ",dump( $param ) if $debug;
334 get_request $client, $path, $param;
336 print $client "HTTP/1.0 500 No method\r\nConnection: close\r\nContent-type: text/plain\r\n\r\n500 $request";
340 print $client menu() if $client->connected;