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