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