/[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

Contents of /lib/PXElator/httpd.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 487 - (show annotations)
Sat Jan 23 20:47:17 2010 UTC (14 years, 3 months ago) by dpavlin
File size: 13905 byte(s)
simple and ugly viewer for store data

1 package httpd;
2
3 use warnings;
4 use strict;
5 use autodie;
6
7 =head1 httpd
8
9 Start with:
10
11 perl -Ilib/PXElator -Mhttpd -e httpd::start
12
13 =cut
14
15 use Data::Dump qw/dump/;
16 use Carp qw/confess/;
17 use File::Slurp;
18 #use JSON;
19 use IO::Socket::INET;
20 use Regexp::Common qw/net/;
21 use YAML;
22
23 our $title;
24
25 sub html_start {
26 qq{
27 <html>
28 <head>
29 <title>$title</title>
30 </head>
31 <body>
32 }}
33
34 sub html_end {
35 qq{
36 </body>
37 </html>
38 }}
39
40 our $port = 7777;
41
42 use server;
43 our $debug = server::debug;
44 our $url = "http://$server::ip:$port";
45
46 use html;
47 our $static_pids;
48 use progress_bar;
49 use config;
50 use client;
51 use log;
52 use x11;
53 use amt;
54 use daemons;
55
56 use kvm;
57 use browser;
58 use network;
59 use ip;
60 use wireshark;
61 use syslogd;
62 use nmap;
63 use ping;
64 use wol;
65
66 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 {
91 my ($client,$path) = @_;
92
93 my $full = "$server::base_dir/tftp/$path";
94
95 return if ! -f $full;
96
97 return if $full =~ m{\.ico$};
98
99 if ( my $pid = fork ) {
100 # parent
101 close($client);
102 $static_pids->{$pid} = $path;
103 return 1;
104 }
105
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};
110
111 my $size = -s $full || return;
112
113 print $client "HTTP/1.0 200 OK\r\nContent-Type: $type\r\nContent-Length: $size\r\nConnection: close\r\n\r\n";
114
115 open(my $fh, $full);
116
117 my $block = 1400; # try not to fragment packages (pxelinux seems to have problems with it)
118 my $buff;
119 my $pos = 0;
120
121 store::audit( 'static', { pid => $$, path => $path, type => $type, size => $size, block => $block, peerhost => $client->peerhost });
122
123 progress_bar::start;
124
125 while( my $len = read $fh, $buff, $block ) {
126 print $client $buff;
127 $client->flush;
128 $pos += $len;
129 progress_bar::tick( $path, $pos, $size );
130 }
131 close($fh);
132 close($client);
133
134 print STDERR "\n";
135
136 exit(0);
137 }
138
139 sub ok {
140 qq|HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nConnection: close\r\n\r\n| . html_start() . menu()
141 }
142
143 sub redirect {
144 my $to = shift;
145 $to ||= $url;
146 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 {
155 my ( $client, $path, $param ) = @_;
156
157 server->refresh;
158
159 store::audit( 'request', { path => $path, param => $param, peerhost => $client->peerhost } );
160
161 $title = $path;
162
163 if ( my $found = static( $client,$path ) ) {
164 warn "static $found" if $debug;
165 } elsif ( $path eq '/' ) {
166
167 my @rows;
168
169 my $debug_proc = '';
170
171 warn "XXX pids = ", dump( $daemons::pids );
172
173 foreach my $name ( sort keys %$daemons::pids ) {
174 my $pid = $daemons::pids->{$name}; # || next;
175
176 my $html;
177
178 my $proc = "/proc/$pid/status";
179
180 if ( -e $proc ) {
181 $html .= qq|<a href=/start_stop/$name>$pid</a>|;
182 if ( $debug ) {
183 $html .= qq| <a name=$pid href=#proc-$pid>?</a>| if $name->can('start');
184
185 $debug_proc
186 .= qq|<a name=proc-$pid href=#$pid>$proc</a><pre style="font-size: 10%">|
187 . read_file($proc)
188 . qq|</pre>|
189 ;
190 }
191
192 my $class = $name;
193 $class =~ s{\.\d+$}{};
194
195 if ( $class->can('fork_if_active') ) {
196 $html .= qq| <a href=/start_stop/$name/$_>$_</a>| foreach $class->fork_if_active;
197 }
198
199 if ( $class->can('actions') ) {
200 $html .= qq| <a href=/action/$name/$_>$_</a>| foreach $class->actions;
201 }
202 } else {
203 if ( $pid =~ m{^\d+$} ) {
204 $html .= qq|$pid exited |
205 } else {
206 $html .= qq|$pid |;
207 }
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;
211 }
212 }
213
214 die "no html generated" unless $html;
215
216 push @rows, ( $name => $html );
217 }
218
219 my $below_table = '';
220
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}
228 } else {
229 push @rows, ( $path => "$pid competed" );
230 $below_table = qq|<a href="/?clean_completed_downloads=1">clean completed downloads</a>|;
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
238 , html::table( 2, @rows )
239 , $below_table
240 , $kvm
241 , html::tabs( log::mac_changes )
242 , $debug_proc
243 ;
244
245 } elsif ( $path =~ m{^/server} ) {
246 foreach my $name ( keys %$param ) {
247 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
265 , qq|<form method=get>|
266 , html::table( 2, @table )
267 , qq|
268 <input type=submit name=action value=change>
269 </form>
270 |
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})?! ) {
289 my $ip = $1;
290 $title = $ip if $ip;
291
292 if ( $param->{action} eq 'remove' ) {
293 client::remove( $param->{change_ip} );
294 print $client redirect("$url/client");
295 return;
296 } elsif ( $param->{action} eq 'change' ) {
297 if ( my $new_ip = client::change_ip( $ip, $param->{change_ip} ) ) {
298 print $client redirect("$url/client#$new_ip");
299 return;
300 }
301 }
302
303 if ( ! $ip ) {
304 my $peer_ip = $client->peerhost;
305
306 my $netmask = ip::to_int $server::netmask;
307 my $network = ip::to_int($server::ip) & $netmask;
308 my $from_int = $network | $server::ip_from;
309 my $to_int = $network | $server::ip_to;
310 my $ip_int = ip::to_int $peer_ip;
311
312 # show edit for clients in our dhcp range
313 if ( $ip_int >= $from_int && $ip_int <= $to_int ) {
314 $ip = $peer_ip;
315 }
316 }
317
318 if ( $ip && $ip ne $server::ip ) {
319
320 my @editable = ( qw/hostname config homepage/ );
321
322 client::conf( $ip, $_ => $param->{$_} ) foreach @editable;
323
324 my $conf = client::all_conf( $ip );
325 my $config = delete $conf->{config};
326
327 my $nmap = qq|<a href=/nmap?scan=$ip>nmap</a>|;
328 my @table = (
329 'ping' => ping::host($ip)
330 ? qq|<span style="color:green">up</span> $nmap|
331 : qq|<span style="color: red">down</span> <a href=/wol/$ip>wol</a> $nmap|
332 ,
333 '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>|,
334 'mac' => format::mac( delete $conf->{mac}, 'html' ),
335 'hostname' => qq|<input type=text name=hostname value="| . delete($conf->{hostname}) . qq|">|,
336 'config' => html::select( 'config', $config, config::available ),
337 html::conf( $ip, $conf, 'edit', @editable )
338 );
339
340 print $client ok
341 , qq|<form method=get>|
342 , html::table( 2, @table ),
343 , qq|
344 <input type=submit name=action value=change>
345 <input type=submit name=action value=remove style="color: red">
346 </form>|
347 ;
348
349 if ( $config ) {
350 if ( my $for_ip = config::for_ip( $ip ) ) {
351 print $client qq|<h2>config::for_ip</h2>| . html::pre( $for_ip );
352 }
353 }
354
355 if ( $conf->{amt} ) {
356 print $client qq|<h2>amt network</h2>|, html::pre_dump( amt::network( $ip ) );
357 print $client qq|<h2>amt log</h2>|, html::pre_dump( amt::log( $ip ) );
358 }
359
360 } else {
361
362 print $client ok qq|<h2>Clients on $server::ip</h2>|;
363
364 my @ping;
365 if ( my $host = $param->{ping_target} ) {
366 @ping = ( $host );
367 } elsif ( $param->{ping} ) {
368 @ping = client::all_ips;
369 }
370
371 my $ping = ping::fping( @ping ) if @ping;
372 my $arp = client::arp_mac_dev;
373
374 my @clients;
375
376 foreach my $ip ( client::all_ips ) {
377
378 my $conf = client::all_conf( $ip );
379 my $mac = delete $conf->{mac} || '';
380 my $dev = $arp->{$mac};
381
382 next unless $dev || $param->{all};
383
384 my $style
385 = 'style="color:'
386 . ( $ping->{$ip} ? 'green' : 'red' )
387 . '"'
388 if $ping;
389
390 $style ||= '';
391 my $ip_text = qq|<tt>$ip</tt>|;
392 $ip_text = qq|<tt><b>$ip</b></tt>| if ip::in_dhcp_range($ip);
393
394 $dev = qq|<tt>$dev</tt>| if $dev;
395
396 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|
409 <form method=get>
410 <input type=text name=ping_target size=15>
411 <input type=submit name=ping value=ping>
412 </form>
413 |;
414 }
415
416
417 } elsif ( $path =~ m{^/brctl} ) {
418
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 {
426 my @c = split(/\t+/,$_,4);
427 if ( $#c == 1 ) {
428 $in_virtual->{ $c[1] }++;
429 @c = ( '', '', '', $c[1] );
430 } else {
431 $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`)
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+)?} ) {
452 print $client ok
453 , join("\n", map { qq|<a href=/ip/$_>$_</a>| } ( qw/link addr route neigh ntable tunnel maddr mroute xfrm/ ))
454 , ip::html( $1 )
455 ;
456 } elsif ( $path =~ m{^/nmap} ) {
457 if ( my $scan = $param->{scan} ) {
458 nmap::scan( $scan );
459 print $client redirect("$url/client#$scan");
460 } else {
461 print $client ok, qq|
462 <form method=get>
463 <input type=text name=scan>
464 <input type=submit value=scan>
465 </form>
466 |;
467 }
468 } elsif ( $path =~ m{^/wol/(\S+)} ) {
469 print $client redirect( "$url/client/$1" ), wol::power_on($1);
470 } elsif ( $path =~ m!^/amt/(\w+)/$RE{net}{IPv4}{-keep}! ) {
471 my ( $run, $ip ) = ( $1, $2 );
472 print $client redirect( "$url/client/$ip" ), amt::RemoteControl( $ip, $run );
473 } elsif ( $path =~ m{^/our/(\w+)/(\S+)} ) {
474 eval 'our $' . $1 . ' = ' . $2;
475 warn $@ if $@;
476 print $client redirect($url), qq|<big>$1 = $2</big><br>Location: <a href="$url">$url</a>|;
477 server::debug( $debug ) if $1 eq 'debug';
478 } elsif ( $path =~ m{^/start_stop/(\S+)} ) {
479 print $client redirect, daemons::start_stop($1,$param);
480 } elsif ( $path =~ m{^/action/([^/]+)/(.+)} ) {
481 my ( $package, $method ) = ( $1, $2 );
482 $ENV{nr} = $1 if $package =~ s{\.(\d+)$}{};
483 $package->$method();
484 print $client redirect;
485 } elsif ( $path =~ m{^/kill/static/(\d+)} ) {
486 print $client redirect;
487 kill 1, $1 || kill 9, $2 && warn "killed $1";
488 } else {
489 print $client "HTTP/1.0 404 $path\r\nConnection: close\r\nContent-type: text/html\r\n\r\n<big>404 $path</big>";
490 warn "404 $path";
491 }
492
493 }
494
495 sub start {
496
497 warn 'network ', network::setup();
498
499 daemons::start_stop 'browser', { url => $url };
500 daemons::start_stop $_ foreach ( qw/dhcpd tftpd dnsd syslogd/ );
501 # daemons::start_stop 'kvm' unless $ENV{DEV}; # skip kvm statup when running on real device
502
503 my $server = IO::Socket::INET->new(
504 Proto => 'tcp',
505 # LocalAddr => $server::ip,
506 LocalPort => $httpd::port,
507 Listen => SOMAXCONN,
508 Reuse => 1
509 ) || die "can't start server on $url: $!";
510
511 print "url $url\n";
512
513 syslogd::install_local;
514 client::rebuild_mac_links;
515
516 while (1) {
517 my $client = $server->accept() || next; # ALARM trickle us
518 my $request = <$client>;
519
520 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]}) {
537 my $path = $1;
538 $path =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/ge;
539 my $param;
540 if ( $path =~ s{\?(.+)}{} ) {
541 foreach my $p ( split(/[&;]/, $1) ) {
542 my ($n,$v) = split(/=/, $p, 2);
543 $param->{$n} = $v;
544 }
545 warn "param: ",dump( $param ) if $debug;
546 }
547 get_request $client, $path, $param;
548 } else {
549 print $client "HTTP/1.0 500 No method\r\nConnection: close\r\nContent-type: text/plain\r\n\r\n500 $request";
550 warn "500 $request";
551 }
552
553 print $client menu() . html_end() if $client->connected;
554
555 }
556
557 die "server died";
558 }
559
560 warn "loaded";
561
562 1;

  ViewVC Help
Powered by ViewVC 1.1.26