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