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