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 Module::Refresh; |
21 |
|
22 |
our $pids; |
23 |
$pids = { httpd => $$ } unless defined $pids; # keep pids on refresh |
24 |
|
25 |
sub DESTROY { |
26 |
warn "pids ",dump( $pids ); |
27 |
foreach ( values %$pids ) { |
28 |
warn "kill $_"; |
29 |
kill 1,$_ || kill 9, $_; |
30 |
} |
31 |
} |
32 |
|
33 |
our $port = 7777; |
34 |
|
35 |
use server; |
36 |
our $debug = server::debug; |
37 |
our $url = "http://$server::ip:$port"; |
38 |
|
39 |
use html; |
40 |
our $static_pids; |
41 |
|
42 |
use Time::HiRes qw/time/; |
43 |
|
44 |
sub static { |
45 |
my ($client,$path) = @_; |
46 |
|
47 |
my $full = "$server::base_dir/tftp/$path"; |
48 |
|
49 |
return if ! -f $full; |
50 |
|
51 |
if ( my $pid = fork ) { |
52 |
# parent |
53 |
close($client); |
54 |
print "http static child $pid\n"; |
55 |
$static_pids->{$pid} = $path; |
56 |
return 1; |
57 |
} |
58 |
|
59 |
my $type = 'application/octet-stream'; |
60 |
$type = 'text/html' if $path =~ m{\.htm}; |
61 |
$type = 'application/javascript' if $path =~ m{\.js}; |
62 |
$type = 'text/plain' if $path =~ m{\.txt}; |
63 |
|
64 |
my $size = -s $full || return; |
65 |
|
66 |
print $client "HTTP/1.0 200 OK\r\nContent-Type: $type\r\nContent-Length: $size\r\nConnection: close\r\n\r\n"; |
67 |
|
68 |
open(my $fh, $full); |
69 |
|
70 |
my $block = 1400; # try not to fragment packages (pxelinux seems to have problems with it) |
71 |
my $buff; |
72 |
my $pos = 0; |
73 |
|
74 |
STDERR->autoflush(1); |
75 |
warn "static $path $type $size block: $block\n"; |
76 |
|
77 |
my $start_t = time(); |
78 |
my $last_t = $start_t; |
79 |
|
80 |
while( my $len = read $fh, $buff, $block ) { |
81 |
syswrite $client,$buff; |
82 |
$client->flush; |
83 |
$pos += $len; |
84 |
my $t = time(); |
85 |
next unless $t - $last_t > 0.75; |
86 |
$last_t = $t; |
87 |
my $speed = ( $pos ) / ( $t - $start_t ); |
88 |
printf STDERR "%s %d/%d %.2f%% %.2f K/s ETA %.1fs\r" |
89 |
, $path, $pos |
90 |
, $size, $pos * 100 / $size |
91 |
, $speed / 1024 |
92 |
, ( $size - $pos ) / $speed |
93 |
; |
94 |
} |
95 |
close($fh); |
96 |
close($client); |
97 |
|
98 |
print STDERR "\n"; |
99 |
|
100 |
exit; |
101 |
} |
102 |
|
103 |
use boolean; |
104 |
|
105 |
use screen; |
106 |
use kvm; |
107 |
|
108 |
$SIG{CHLD} = 'IGNORE'; |
109 |
|
110 |
sub start_stop { |
111 |
my $daemon = shift; |
112 |
my $pid = $pids->{$daemon}; |
113 |
|
114 |
warn "start_stop $daemon $pid\n"; |
115 |
|
116 |
if ( $pid =~ m{^\d+$} ) { |
117 |
my $pstree = `pstree -p $pid`; |
118 |
my @pids = $pstree =~ m{\((\d+)\)}g; |
119 |
warn "pstree $pstree pids ",dump( @pids ); |
120 |
kill 1, $_ foreach reverse @pids; |
121 |
$pids->{$daemon} = 'stopped'; |
122 |
return qq|$daemon pid $pid stopped|; |
123 |
} else { |
124 |
if ( $pid = fork ) { |
125 |
# parent |
126 |
$pids->{$daemon} = $pid; |
127 |
warn "forked $daemon $pid\n"; |
128 |
return qq|$daemon pid $pid started|; |
129 |
} elsif ( defined $pid ) { |
130 |
# child |
131 |
my $invoke = 'start'; |
132 |
$invoke = $1 if $daemon =~ s{/(.+)}{}; |
133 |
my $eval = $daemon . '::' . $invoke . '(' . ( @_ ? dump(@_) : '' ) . ')'; |
134 |
warn "eval $eval"; |
135 |
eval $eval; |
136 |
warn "can't start $daemon: $@" if $@; |
137 |
exit; |
138 |
} else { |
139 |
die "fork error $!"; |
140 |
} |
141 |
} |
142 |
} |
143 |
|
144 |
my $ok = qq|HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nConnection: close\r\n\r\n|; |
145 |
my $redirect = qq|HTTP/1.1 302 Found\r\nContent-type: text/html\r\nLocation: $url\r\n\r\n|; |
146 |
|
147 |
sub get_request { |
148 |
my ( $client, $path, $param ) = @_; |
149 |
|
150 |
warn "get_request $path ", $param ? dump( $param ) : '', "\n"; |
151 |
|
152 |
if ( my $found = static( $client,$path ) ) { |
153 |
warn "static $found" if $debug; |
154 |
} elsif ( $path eq '/' ) { |
155 |
|
156 |
my $screen = $pids->{screen} ? qq|stop <tt>$pids->{screen}</tt>| : 'start'; |
157 |
my $kvm = $pids->{kvm} ? qq|stop <tt>$pids->{kvm}</tt>| : |
158 |
$pids->{screen} ? qq|start| : qq|start screen first|; |
159 |
|
160 |
my @rows = ( |
161 |
'ip', html::tt( $server::ip ), |
162 |
'netmask', html::tt( $server::netmask ), |
163 |
|
164 |
'debug', qq|<a href=/our/debug/| . boolean::toggle($debug) . qq|>$debug</a>|, |
165 |
); |
166 |
|
167 |
my $debug_proc = ''; |
168 |
|
169 |
warn 'pids: ', dump( $pids ) if $debug; |
170 |
foreach my $name ( sort keys %$pids ) { |
171 |
my $pid = $pids->{$name} || next; |
172 |
|
173 |
my $html = qq|<a href=/start_stop/$name>$pid</a>|; |
174 |
|
175 |
my $proc = "/proc/$pid/status"; |
176 |
|
177 |
if ( -e $proc ) { |
178 |
if ( $debug ) { |
179 |
$html .= qq| <a name=$pid href=#proc-$pid>?</a>|; |
180 |
|
181 |
$debug_proc |
182 |
.= qq|<a name=proc-$pid href=#$pid>$proc</a><pre style="font-size: 10%">| |
183 |
. read_file($proc) |
184 |
. qq|</pre>| |
185 |
; |
186 |
} |
187 |
|
188 |
if ( $name->can('start_fork') ) { |
189 |
$html .= qq| <a href=/start_stop/kvm/$_>$_</a>| foreach $name->start_fork; |
190 |
} |
191 |
|
192 |
if ( $name->can('actions') ) { |
193 |
$html .= qq| <a href=/action/kvm/$_>$_</a>| foreach $name->actions; |
194 |
} |
195 |
} |
196 |
|
197 |
push @rows, ( $name => $html ); |
198 |
} |
199 |
|
200 |
my $below_table = ''; |
201 |
|
202 |
warn 'static_pids: ', dump( $static_pids ) if $debug; |
203 |
foreach my $pid ( keys %$static_pids ) { |
204 |
my $path = $static_pids->{$pid}; |
205 |
if ( -d "/proc/$pid" ) { |
206 |
push @rows, ( $path => qq|<a href=/kill/static/$pid>$pid</a>| ); |
207 |
} elsif ( $param->{clean_completed_downloads} ) { |
208 |
delete $static_pids->{$pid} |
209 |
} else { |
210 |
push @rows, ( $path => "$pid competed" ); |
211 |
$below_table = qq|<a href="/?clean_completed_downloads=1">clean completed downloads</a>|; |
212 |
} |
213 |
} |
214 |
|
215 |
print $client $ok |
216 |
, html::table( 2, @rows ) |
217 |
, $below_table |
218 |
, html::tabs( log::mac_changes ) |
219 |
, $debug_proc |
220 |
; |
221 |
|
222 |
} elsif ( $path =~ m{^/our/(\w+)/(\S+)} ) { |
223 |
eval 'our $' . $1 . ' = ' . $2; |
224 |
warn $@ if $@; |
225 |
print $client $redirect, qq|<big>$1 = $2</big><br>Location: <a href="$url">$url</a>|; |
226 |
server::debug( $debug ) if $1 eq 'debug'; |
227 |
} elsif ( $path =~ m{^/start_stop/((?:screen|kvm).*)} ) { # XXX we don't want to stop all classes |
228 |
print $client $redirect, start_stop($1); |
229 |
} elsif ( $path =~ m{^/action/([^/]+)/(.+)} ) { |
230 |
$1->$2(); |
231 |
print $client $redirect; |
232 |
} elsif ( $path =~ m{^/kill/static/(\d+)} ) { |
233 |
print $client $redirect; |
234 |
kill 1, $1 || kill 9, $2 && warn "killed $1"; |
235 |
} elsif ( $path eq '/exit' ) { |
236 |
# DESTROY; |
237 |
exit 0; |
238 |
} elsif ( $path =~ m{/boot} ) { |
239 |
print $client qq{$ok |
240 |
#!gpxe |
241 |
imgfree |
242 |
login |
243 |
chain http://$server::ip:$httpd::port/ |
244 |
|
245 |
}; |
246 |
} else { |
247 |
print $client "HTTP/1.0 404 $path\r\nConnection: close\r\nContent-type: text/html\r\n\r\n<big>404 $path</big>"; |
248 |
warn "404 $path"; |
249 |
} |
250 |
|
251 |
} |
252 |
|
253 |
use browser; |
254 |
|
255 |
sub start { |
256 |
|
257 |
my $server = IO::Socket::INET->new( |
258 |
Proto => 'tcp', |
259 |
LocalPort => $httpd::port, |
260 |
Listen => SOMAXCONN, |
261 |
Reuse => 1 |
262 |
) || die "can't start server on $url: $!"; |
263 |
|
264 |
print "url $url\n"; |
265 |
|
266 |
start_stop 'browser', $url; |
267 |
start_stop 'screen'; |
268 |
start_stop 'kvm'; |
269 |
|
270 |
while (my $client = $server->accept()) { |
271 |
$client->autoflush(1); |
272 |
my $request = <$client>; |
273 |
|
274 |
warn "request $request\n" if $debug; |
275 |
|
276 |
Module::Refresh->refresh; |
277 |
|
278 |
if ($request =~ m{^GET (/.*) HTTP/1.[01]}) { |
279 |
my $path = $1; |
280 |
my $param; |
281 |
if ( $path =~ s{\?(.+)}{} ) { |
282 |
foreach my $p ( split(/[&;]/, $1) ) { |
283 |
my ($n,$v) = split(/=/, $p, 2); |
284 |
$param->{$n} = $v; |
285 |
} |
286 |
warn "param: ",dump( $param ) if $debug; |
287 |
} |
288 |
get_request $client, $path, $param; |
289 |
} else { |
290 |
print $client "HTTP/1.0 500 No method\r\nConnection: close\r\nContent-type: text/plain\r\n\r\n500 $request"; |
291 |
warn "500 $request"; |
292 |
} |
293 |
|
294 |
print $client qq{ |
295 |
<div style="font-size: 80%; color: #888"> |
296 |
<a href="">reload</a> |
297 |
<a href=/>index</a> |
298 |
<a href=/exit>exit</a> |
299 |
</div> |
300 |
} if $client->connected; |
301 |
|
302 |
} |
303 |
|
304 |
die "server died"; |
305 |
} |
306 |
|
307 |
warn "loaded"; |
308 |
|
309 |
1; |