/[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 490 - (show annotations)
Sat Jan 23 21:43:00 2010 UTC (14 years, 3 months ago) by dpavlin
File size: 14189 byte(s)
expand dump on mouse hover

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
22 our $title;
23
24 sub html_start {
25 qq{
26 <html>
27 <head>
28 <title>$title</title>
29 </head>
30 <body>
31 }}
32
33 sub html_end {
34 qq{
35 </body>
36 </html>
37 }}
38
39 our $port = 7777;
40
41 use server;
42 our $debug = server::debug;
43 our $url = "http://$server::ip:$port";
44
45 use html;
46 our $static_pids;
47 use progress_bar;
48 use config;
49 use client;
50 use log;
51 use x11;
52 use amt;
53 use daemons;
54
55 use kvm;
56 use browser;
57 use network;
58 use ip;
59 use wireshark;
60 use syslogd;
61 use nmap;
62 use ping;
63 use wol;
64
65 use store;
66
67
68 sub menu {
69 my $store_url = $url;
70 $store_url =~ s{:\d+.+}{:28017};
71 qq{
72 <div style="font-size: 80%; color: #888">
73 <a target=pids href=/ >home</a>
74 |
75 <a target=server href=/server >server</a>
76 <a target=server href=/brctl >brctl</a>
77 <a target=server href=/ip >ip</a>
78 |
79 <a target=store href=$store_url >store</a>
80 <a target=store href=/store/query >query</a>
81 |
82 <a target=client href=/nmap >nmap</a>
83 <a target=client href=/client >client</a>
84 </div>
85
86 }}
87
88
89 sub static {
90 my ($client,$path) = @_;
91
92 my $full = "$server::base_dir/tftp/$path";
93
94 return if ! -f $full;
95
96 return if $full =~ m{\.ico$};
97
98 if ( my $pid = fork ) {
99 # parent
100 close($client);
101 $static_pids->{$pid} = $path;
102 return 1;
103 }
104
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};
109
110 my $size = -s $full || return;
111
112 print $client "HTTP/1.0 200 OK\r\nContent-Type: $type\r\nContent-Length: $size\r\nConnection: close\r\n\r\n";
113
114 open(my $fh, $full);
115
116 my $block = 1400; # try not to fragment packages (pxelinux seems to have problems with it)
117 my $buff;
118 my $pos = 0;
119
120 store::audit( 'static', { pid => $$, path => $path, type => $type, size => $size, block => $block, peerhost => $client->peerhost });
121
122 progress_bar::start;
123
124 while( my $len = read $fh, $buff, $block ) {
125 print $client $buff;
126 $client->flush;
127 $pos += $len;
128 progress_bar::tick( $path, $pos, $size );
129 }
130 close($fh);
131 close($client);
132
133 print STDERR "\n";
134
135 exit(0);
136 }
137
138 sub ok {
139 qq|HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nConnection: close\r\n\r\n| . html_start() . menu()
140 }
141
142 sub redirect {
143 my $to = shift;
144 $to ||= $url;
145 qq|HTTP/1.1 302 Found\r\nContent-type: text/html\r\nLocation: $to\r\n\r\n|
146 }
147
148 sub toggle {
149 my $v = shift;
150 return $v ? 0 : 1;
151 }
152
153 sub get_request {
154 my ( $client, $path, $param ) = @_;
155
156 server->refresh;
157
158 store::audit( 'request', { path => $path, param => $param, peerhost => $client->peerhost } );
159
160 $title = $path;
161
162 if ( my $found = static( $client,$path ) ) {
163 warn "static $found" if $debug;
164 } elsif ( $path eq '/' ) {
165
166 my @rows;
167
168 my $debug_proc = '';
169
170 warn "XXX pids = ", dump( $daemons::pids );
171
172 foreach my $name ( sort keys %$daemons::pids ) {
173 my $pid = $daemons::pids->{$name}; # || next;
174
175 my $html;
176
177 my $proc = "/proc/$pid/status";
178
179 if ( -e $proc ) {
180 $html .= qq|<a href=/start_stop/$name>$pid</a>|;
181 if ( $debug ) {
182 $html .= qq| <a name=$pid href=#proc-$pid>?</a>| if $name->can('start');
183
184 $debug_proc
185 .= qq|<a name=proc-$pid href=#$pid>$proc</a><pre style="font-size: 10%">|
186 . read_file($proc)
187 . qq|</pre>|
188 ;
189 }
190
191 my $class = $name;
192 $class =~ s{\.\d+$}{};
193
194 if ( $class->can('fork_if_active') ) {
195 $html .= qq| <a href=/start_stop/$name/$_>$_</a>| foreach $class->fork_if_active;
196 }
197
198 if ( $class->can('actions') ) {
199 $html .= qq| <a href=/action/$name/$_>$_</a>| foreach $class->actions;
200 }
201 } else {
202 if ( $pid =~ m{^\d+$} ) {
203 $html .= qq|$pid exited |
204 } else {
205 $html .= qq|$pid |;
206 }
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;
210 }
211 }
212
213 die "no html generated" unless $html;
214
215 push @rows, ( $name => $html );
216 }
217
218 my $below_table = '';
219
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}
227 } else {
228 push @rows, ( $path => "$pid competed" );
229 $below_table = qq|<a href="/?clean_completed_downloads=1">clean completed downloads</a>|;
230 }
231 }
232
233 my $kvm = kvm::next_nr;
234 $kvm = qq|<div><a href=/start_stop/kvm?nr=$kvm>create new kvm $kvm</a></div>|;
235
236 print $client ok
237 , html::table( 2, @rows )
238 , $below_table
239 , $kvm
240 , html::tabs( log::mac_changes )
241 , $debug_proc
242 ;
243
244 } elsif ( $path =~ m{^/server} ) {
245 foreach my $name ( keys %$param ) {
246 eval '$server::' . $name . '= $param->{$name}';
247 }
248 my @table = (
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">|
251 );
252
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">| );
256 }
257
258 foreach my $readonly ( 'base_dir', 'conf' ) {
259 my $v = eval '$server::' . $readonly;
260 push @table, ( $readonly, html::tt $v );
261 }
262
263 print $client ok
264 , qq|<form method=get>|
265 , html::table( 2, @table )
266 , qq|
267 <input type=submit name=action value=change>
268 </form>
269 |
270 ;
271
272 } elsif ( $path =~ m{^/store/query} ) {
273 print $client ok
274 , qq|
275 <style type=text/css>
276 .z {
277 background: #eee;
278 }
279 td > pre {
280 margin: 0;
281 max-height: 3em;
282 overflow: hidden;
283 }
284 td:hover > pre {
285 max-height: 100%;
286 overflow: show;
287 }
288 </style>
289 |
290 , qq|<table>|
291 ;
292 my ( $s1,$s2 ) = ( ' class=z', '' );
293 store::query( sub {
294 my $o = shift;
295 my $p = delete( $o->{package} );
296 delete( $o->{_id} );
297
298 # XXX sigh, dump dies if we don't do this
299 delete $o->{$_} foreach ( grep { ! defined $o->{$_} } keys %$o );
300
301 print $client qq|<tr$s1><td>|
302 , join(qq|</td><td>|, map { $p->{$_} } keys %$p )
303 , qq|</td><td><pre>|
304 , dump( $o )
305 , qq|</pre></td></tr>\n|
306 ;
307 ( $s1, $s2 ) = ( $s2, $s1 );
308 });
309 print $client qq|</table>|;
310
311 } elsif ( $path =~ m!^/client(?:/$RE{net}{IPv4}{-keep})?! ) {
312 my $ip = $1;
313 $title = $ip if $ip;
314
315 if ( $param->{action} eq 'remove' ) {
316 client::remove( $param->{change_ip} );
317 print $client redirect("$url/client");
318 return;
319 } elsif ( $param->{action} eq 'change' ) {
320 if ( my $new_ip = client::change_ip( $ip, $param->{change_ip} ) ) {
321 print $client redirect("$url/client#$new_ip");
322 return;
323 }
324 }
325
326 if ( ! $ip ) {
327 my $peer_ip = $client->peerhost;
328
329 my $netmask = ip::to_int $server::netmask;
330 my $network = ip::to_int($server::ip) & $netmask;
331 my $from_int = $network | $server::ip_from;
332 my $to_int = $network | $server::ip_to;
333 my $ip_int = ip::to_int $peer_ip;
334
335 # show edit for clients in our dhcp range
336 if ( $ip_int >= $from_int && $ip_int <= $to_int ) {
337 $ip = $peer_ip;
338 }
339 }
340
341 if ( $ip && $ip ne $server::ip ) {
342
343 my @editable = ( qw/hostname config homepage/ );
344
345 client::conf( $ip, $_ => $param->{$_} ) foreach @editable;
346
347 my $conf = client::all_conf( $ip );
348 my $config = delete $conf->{config};
349
350 my $nmap = qq|<a href=/nmap?scan=$ip>nmap</a>|;
351 my @table = (
352 'ping' => ping::host($ip)
353 ? qq|<span style="color:green">up</span> $nmap|
354 : qq|<span style="color: red">down</span> <a href=/wol/$ip>wol</a> $nmap|
355 ,
356 '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>|,
357 'mac' => format::mac( delete $conf->{mac}, 'html' ),
358 'hostname' => qq|<input type=text name=hostname value="| . delete($conf->{hostname}) . qq|">|,
359 'config' => html::select( 'config', $config, config::available ),
360 html::conf( $ip, $conf, 'edit', @editable )
361 );
362
363 print $client ok
364 , qq|<form method=get>|
365 , html::table( 2, @table ),
366 , qq|
367 <input type=submit name=action value=change>
368 <input type=submit name=action value=remove style="color: red">
369 </form>|
370 ;
371
372 if ( $config ) {
373 if ( my $for_ip = config::for_ip( $ip ) ) {
374 print $client qq|<h2>config::for_ip</h2>| . html::pre( $for_ip );
375 }
376 }
377
378 if ( $conf->{amt} ) {
379 print $client qq|<h2>amt network</h2>|, html::pre_dump( amt::network( $ip ) );
380 print $client qq|<h2>amt log</h2>|, html::pre_dump( amt::log( $ip ) );
381 }
382
383 } else {
384
385 print $client ok qq|<h2>Clients on $server::ip</h2>|;
386
387 my @ping;
388 if ( my $host = $param->{ping_target} ) {
389 @ping = ( $host );
390 } elsif ( $param->{ping} ) {
391 @ping = client::all_ips;
392 }
393
394 my $ping = ping::fping( @ping ) if @ping;
395 my $arp = client::arp_mac_dev;
396
397 my @clients;
398
399 foreach my $ip ( client::all_ips ) {
400
401 my $conf = client::all_conf( $ip );
402 my $mac = delete $conf->{mac} || '';
403 my $dev = $arp->{$mac};
404
405 next unless $dev || $param->{all};
406
407 my $style
408 = 'style="color:'
409 . ( $ping->{$ip} ? 'green' : 'red' )
410 . '"'
411 if $ping;
412
413 $style ||= '';
414 my $ip_text = qq|<tt>$ip</tt>|;
415 $ip_text = qq|<tt><b>$ip</b></tt>| if ip::in_dhcp_range($ip);
416
417 $dev = qq|<tt>$dev</tt>| if $dev;
418
419 push @clients
420 , qq|<a $style name=$ip target=client href=/client/$ip>$ip_text</a>|
421 , format::mac( $mac => 'html' )
422 , $dev
423 , delete $conf->{hostname}
424 , html::conf( $ip, $conf, 'inline' )
425 ;
426 }
427
428 my $all = $param->{all} ? 0 : 1;
429
430 print $client html::table( -5, 'ip', 'mac', qq|<a href="?all=$all">dev</a>|, 'hostname', 'conf', @clients );
431 print $client qq|
432 <form method=get>
433 <input type=text name=ping_target size=15>
434 <input type=submit name=ping value=ping>
435 </form>
436 |;
437 }
438
439
440 } elsif ( $path =~ m{^/brctl} ) {
441
442 system 'brctl addif virtual ' . $param->{addif} if $param->{addif};
443 system 'brctl delif virtual ' . $param->{delif} if $param->{delif};
444
445 my $in_virtual;
446
447 my @table =
448 map {
449 my @c = split(/\t+/,$_,4);
450 if ( $#c == 1 ) {
451 $in_virtual->{ $c[1] }++;
452 @c = ( '', '', '', $c[1] );
453 } else {
454 $in_virtual->{ $c[3] }++;
455 }
456 if ( $c[3] =~ m{\d$} ) {
457 $c[3] = qq|<input type=submit name=delif value=$c[3] style="color:red" title="remove $c[3] from bridge">|;
458 }
459 @c
460 } split(/\n/, `brctl show`)
461 ;
462
463 my @add_ifs = grep { ! $in_virtual->{$_} && $_ ne 'virtual' } ip::devices_up;
464
465 push @table, ( '', '', '', html::select( 'addif', @add_ifs ) . qq|<input type=submit value=add></form>| );
466
467 print $client ok
468 , qq|<form>|
469 , html::table( -4, @table )
470 , qq|</form>|
471 ;
472
473
474 } elsif ( $path =~ m{^/ip/?(\w+)?} ) {
475 print $client ok
476 , join("\n", map { qq|<a href=/ip/$_>$_</a>| } ( qw/link addr route neigh ntable tunnel maddr mroute xfrm/ ))
477 , ip::html( $1 )
478 ;
479 } elsif ( $path =~ m{^/nmap} ) {
480 if ( my $scan = $param->{scan} ) {
481 nmap::scan( $scan );
482 print $client redirect("$url/client#$scan");
483 } else {
484 print $client ok, qq|
485 <form method=get>
486 <input type=text name=scan>
487 <input type=submit value=scan>
488 </form>
489 |;
490 }
491 } elsif ( $path =~ m{^/wol/(\S+)} ) {
492 print $client redirect( "$url/client/$1" ), wol::power_on($1);
493 } elsif ( $path =~ m!^/amt/(\w+)/$RE{net}{IPv4}{-keep}! ) {
494 my ( $run, $ip ) = ( $1, $2 );
495 print $client redirect( "$url/client/$ip" ), amt::RemoteControl( $ip, $run );
496 } elsif ( $path =~ m{^/our/(\w+)/(\S+)} ) {
497 eval 'our $' . $1 . ' = ' . $2;
498 warn $@ if $@;
499 print $client redirect($url), qq|<big>$1 = $2</big><br>Location: <a href="$url">$url</a>|;
500 server::debug( $debug ) if $1 eq 'debug';
501 } elsif ( $path =~ m{^/start_stop/(\S+)} ) {
502 print $client redirect, daemons::start_stop($1,$param);
503 } elsif ( $path =~ m{^/action/([^/]+)/(.+)} ) {
504 my ( $package, $method ) = ( $1, $2 );
505 $ENV{nr} = $1 if $package =~ s{\.(\d+)$}{};
506 $package->$method();
507 print $client redirect;
508 } elsif ( $path =~ m{^/kill/static/(\d+)} ) {
509 print $client redirect;
510 kill 1, $1 || kill 9, $2 && warn "killed $1";
511 } else {
512 print $client "HTTP/1.0 404 $path\r\nConnection: close\r\nContent-type: text/html\r\n\r\n<big>404 $path</big>";
513 warn "404 $path";
514 }
515
516 }
517
518 sub start {
519
520 warn 'network ', network::setup();
521
522 daemons::start_stop 'browser', { url => $url };
523 daemons::start_stop $_ foreach ( qw/dhcpd tftpd dnsd syslogd/ );
524 # daemons::start_stop 'kvm' unless $ENV{DEV}; # skip kvm statup when running on real device
525
526 my $server = IO::Socket::INET->new(
527 Proto => 'tcp',
528 # LocalAddr => $server::ip,
529 LocalPort => $httpd::port,
530 Listen => SOMAXCONN,
531 Reuse => 1
532 ) || die "can't start server on $url: $!";
533
534 print "url $url\n";
535
536 syslogd::install_local;
537 client::rebuild_mac_links;
538
539 while (1) {
540 my $client = $server->accept() || next; # ALARM trickle us
541 my $request = <$client>;
542
543 my $headers;
544
545 while ( my $header = <$client> ) {
546 chomp $header;
547 last if $header =~ m{^\s*$};
548 my ( $n, $v ) = split(/:\s*/, $header);
549 $headers->{ lc $n } = $v;
550 }
551
552 if ( my $host = $headers->{host} ) {
553 $url = 'http://' . $host;
554 $url .= ":$port" unless $url =~ m{:\d+$};
555 }
556
557 warn "## $url ## $request", dump( $headers ) if $debug;
558
559 if ($request =~ m{^GET (/.*) HTTP/1.[01]}) {
560 my $path = $1;
561 $path =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/ge;
562 my $param;
563 if ( $path =~ s{\?(.+)}{} ) {
564 foreach my $p ( split(/[&;]/, $1) ) {
565 my ($n,$v) = split(/=/, $p, 2);
566 $param->{$n} = $v;
567 }
568 warn "param: ",dump( $param ) if $debug;
569 }
570 get_request $client, $path, $param;
571 } else {
572 print $client "HTTP/1.0 500 No method\r\nConnection: close\r\nContent-type: text/plain\r\n\r\n500 $request";
573 warn "500 $request";
574 }
575
576 print $client menu() . html_end() if $client->connected;
577
578 }
579
580 die "server died";
581 }
582
583 warn "loaded";
584
585 1;

  ViewVC Help
Powered by ViewVC 1.1.26