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