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 |
|
sub menu {qq{ |
22 |
|
|
23 |
|
<div style="font-size: 80%; color: #888"> |
24 |
|
<a href=/>home</a> |
25 |
|
<a href=/server>server</a> |
26 |
|
<a href=/client>client</a> |
27 |
|
</div> |
28 |
|
|
29 |
|
}} |
30 |
|
|
31 |
our $pids; |
our $pids; |
32 |
$pids = { httpd => $$ } unless defined $pids; # keep pids on refresh |
$pids = { httpd => $$ } unless defined $pids; # keep pids on refresh |
48 |
use html; |
use html; |
49 |
our $static_pids; |
our $static_pids; |
50 |
use progress_bar; |
use progress_bar; |
51 |
|
use config; |
52 |
|
|
53 |
sub static { |
sub static { |
54 |
my ($client,$path) = @_; |
my ($client,$path) = @_; |
102 |
|
|
103 |
use boolean; |
use boolean; |
104 |
|
|
|
use screen; |
|
105 |
use kvm; |
use kvm; |
106 |
|
|
107 |
$SIG{CHLD} = 'IGNORE'; |
$SIG{CHLD} = 'IGNORE'; |
108 |
|
|
109 |
sub start_stop { |
sub start_stop { |
110 |
my $daemon = shift; |
my $daemon = shift; |
111 |
my $pid = $pids->{$daemon}; |
my $pid = $pids->{$daemon} || 'not started'; |
112 |
|
|
113 |
warn "start_stop $daemon $pid\n"; |
warn "start_stop $daemon $pid\n"; |
114 |
|
|
129 |
# child |
# child |
130 |
my $invoke = 'start'; |
my $invoke = 'start'; |
131 |
$invoke = $1 if $daemon =~ s{/(.+)}{}; |
$invoke = $1 if $daemon =~ s{/(.+)}{}; |
132 |
my $eval = $daemon . '::' . $invoke . '(' . ( @_ ? dump(@_) : '' ) . ')'; |
if ( $daemon =~ m{dhcpd|tftpd|dnsd} ) { |
133 |
warn "eval $eval"; |
my $exec = "perl -I$server::base_dir/lib -I$server::base_dir/lib/PXElator -M$daemon -e ${daemon}::${invoke}"; |
134 |
eval $eval; |
warn "exec $exec"; |
135 |
warn "can't start $daemon: $@" if $@; |
exec "xterm -T $daemon -n $daemon -e $exec"; |
136 |
|
} else { |
137 |
|
my $eval = $daemon . '::' . $invoke . '(' . ( @_ ? dump(@_) : '' ) . ')'; |
138 |
|
warn "eval $eval"; |
139 |
|
eval $eval; |
140 |
|
warn "can't start $daemon: $@" if $@; |
141 |
|
} |
142 |
exit; |
exit; |
143 |
} else { |
} else { |
144 |
die "fork error $!"; |
die "fork error $!"; |
152 |
sub get_request { |
sub get_request { |
153 |
my ( $client, $path, $param ) = @_; |
my ( $client, $path, $param ) = @_; |
154 |
|
|
155 |
|
server->refresh; |
156 |
|
|
157 |
warn "get_request $path ", $param ? dump( $param ) : '', "\n"; |
warn "get_request $path ", $param ? dump( $param ) : '', "\n"; |
158 |
|
|
159 |
if ( my $found = static( $client,$path ) ) { |
if ( my $found = static( $client,$path ) ) { |
160 |
warn "static $found" if $debug; |
warn "static $found" if $debug; |
161 |
} elsif ( $path eq '/' ) { |
} elsif ( $path eq '/' ) { |
162 |
|
|
|
my $screen = $pids->{screen} ? qq|stop <tt>$pids->{screen}</tt>| : 'start'; |
|
|
my $kvm = $pids->{kvm} ? qq|stop <tt>$pids->{kvm}</tt>| : |
|
|
$pids->{screen} ? qq|start| : qq|start screen first|; |
|
|
|
|
163 |
my @rows = ( |
my @rows = ( |
|
'ip', html::tt( $server::ip ), |
|
|
'netmask', html::tt( $server::netmask ), |
|
|
|
|
164 |
'debug', qq|<a href=/our/debug/| . boolean::toggle($debug) . qq|>$debug</a>|, |
'debug', qq|<a href=/our/debug/| . boolean::toggle($debug) . qq|>$debug</a>|, |
165 |
); |
); |
166 |
|
|
213 |
} |
} |
214 |
|
|
215 |
print $client $ok |
print $client $ok |
216 |
|
, menu() |
217 |
, html::table( 2, @rows ) |
, html::table( 2, @rows ) |
218 |
, $below_table |
, $below_table |
219 |
, html::tabs( log::mac_changes ) |
, html::tabs( log::mac_changes ) |
220 |
, $debug_proc |
, $debug_proc |
221 |
; |
; |
222 |
|
|
223 |
|
} elsif ( $path =~ m{^/server} ) { |
224 |
|
print $client $ok |
225 |
|
, menu() |
226 |
|
, html::table( 2, map { ( $_, eval '$server::'.$_ ) } ( 'ip', 'netmask', 'ip_from', 'ip_to', 'domain_name', 'base_dir' ) ) |
227 |
|
; |
228 |
|
} elsif ( $path =~ m{^/client} ) { |
229 |
|
my $ip = $client->peerhost; |
230 |
|
my $hostname = server::shared( "hostname/$ip", $param->{hostname} ); |
231 |
|
my $deploy = server::shared( "deploy/$ip", $param->{deploy} ); |
232 |
|
print $client $ok |
233 |
|
, menu() |
234 |
|
, qq|<form method=get>| |
235 |
|
, html::table( 2, |
236 |
|
'ip' => $ip, |
237 |
|
'hostname' => qq|<input type=text name=hostname value=$hostname>|, |
238 |
|
'deploy' => html::select( 'deploy', $deploy, config::available ), |
239 |
|
) |
240 |
|
, qq|<input type=submit value=change></form><pre>| |
241 |
|
, config::for_ip( $ip ) |
242 |
|
, qq|</pre>| |
243 |
|
; |
244 |
} elsif ( $path =~ m{^/our/(\w+)/(\S+)} ) { |
} elsif ( $path =~ m{^/our/(\w+)/(\S+)} ) { |
245 |
eval 'our $' . $1 . ' = ' . $2; |
eval 'our $' . $1 . ' = ' . $2; |
246 |
warn $@ if $@; |
warn $@ if $@; |
247 |
print $client $redirect, qq|<big>$1 = $2</big><br>Location: <a href="$url">$url</a>|; |
print $client $redirect, qq|<big>$1 = $2</big><br>Location: <a href="$url">$url</a>|; |
248 |
server::debug( $debug ) if $1 eq 'debug'; |
server::debug( $debug ) if $1 eq 'debug'; |
249 |
} elsif ( $path =~ m{^/start_stop/((?:screen|kvm).*)} ) { # XXX we don't want to stop all classes |
} elsif ( $path =~ m{^/start_stop/(\S+)} ) { |
250 |
print $client $redirect, start_stop($1); |
print $client $redirect, start_stop($1); |
251 |
} elsif ( $path =~ m{^/action/([^/]+)/(.+)} ) { |
} elsif ( $path =~ m{^/action/([^/]+)/(.+)} ) { |
252 |
$1->$2(); |
$1->$2(); |
273 |
} |
} |
274 |
|
|
275 |
use browser; |
use browser; |
276 |
|
use network; |
277 |
|
|
278 |
sub start { |
sub start { |
279 |
|
|
280 |
|
warn 'tap ', network::tap(); |
281 |
|
|
282 |
my $server = IO::Socket::INET->new( |
my $server = IO::Socket::INET->new( |
283 |
Proto => 'tcp', |
Proto => 'tcp', |
284 |
LocalPort => $httpd::port, |
LocalPort => $httpd::port, |
289 |
print "url $url\n"; |
print "url $url\n"; |
290 |
|
|
291 |
start_stop 'browser', $url; |
start_stop 'browser', $url; |
292 |
start_stop 'screen'; |
start_stop 'dhcpd'; |
293 |
|
start_stop 'tftpd'; |
294 |
|
start_stop 'dnsd'; |
295 |
start_stop 'kvm'; |
start_stop 'kvm'; |
296 |
|
|
297 |
while (my $client = $server->accept()) { |
while (1) { |
298 |
$client->autoflush(1); |
my $client = $server->accept() || next; # ALARM trickle us |
299 |
my $request = <$client>; |
my $request = <$client>; |
300 |
|
|
301 |
warn "request $request\n" if $debug; |
warn "request $request\n" if $debug; |
302 |
|
|
|
Module::Refresh->refresh; |
|
|
|
|
303 |
if ($request =~ m{^GET (/.*) HTTP/1.[01]}) { |
if ($request =~ m{^GET (/.*) HTTP/1.[01]}) { |
304 |
my $path = $1; |
my $path = $1; |
305 |
my $param; |
my $param; |
316 |
warn "500 $request"; |
warn "500 $request"; |
317 |
} |
} |
318 |
|
|
319 |
print $client qq{ |
print $client menu() if $client->connected; |
|
<div style="font-size: 80%; color: #888"> |
|
|
<a href="">reload</a> |
|
|
<a href=/>index</a> |
|
|
<a href=/exit>exit</a> |
|
|
</div> |
|
|
} if $client->connected; |
|
320 |
|
|
321 |
} |
} |
322 |
|
|