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 |
|
</div> |
27 |
|
|
28 |
|
}} |
29 |
|
|
30 |
our $pids; |
our $pids; |
31 |
$pids = { httpd => $$ } unless defined $pids; # keep pids on refresh |
$pids = { httpd => $$ } unless defined $pids; # keep pids on refresh |
46 |
|
|
47 |
use html; |
use html; |
48 |
our $static_pids; |
our $static_pids; |
49 |
|
use progress_bar; |
|
use Time::HiRes qw/time/; |
|
50 |
|
|
51 |
sub static { |
sub static { |
52 |
my ($client,$path) = @_; |
my ($client,$path) = @_; |
55 |
|
|
56 |
return if ! -f $full; |
return if ! -f $full; |
57 |
|
|
|
my $start_t = time(); |
|
|
|
|
58 |
if ( my $pid = fork ) { |
if ( my $pid = fork ) { |
59 |
# parent |
# parent |
60 |
close($client); |
close($client); |
70 |
|
|
71 |
my $size = -s $full || return; |
my $size = -s $full || return; |
72 |
|
|
|
$client->autoflush(1); |
|
|
|
|
73 |
print $client "HTTP/1.0 200 OK\r\nContent-Type: $type\r\nContent-Length: $size\r\nConnection: close\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 |
|
|
75 |
open(my $fh, $full); |
open(my $fh, $full); |
78 |
my $buff; |
my $buff; |
79 |
my $pos = 0; |
my $pos = 0; |
80 |
|
|
81 |
print "static $path $type $size block: $block\n"; |
warn "static $path $type $size block: $block\n"; |
82 |
|
|
83 |
|
progress_bar::start; |
84 |
|
|
85 |
while( my $len = read $fh, $buff, $block ) { |
while( my $len = read $fh, $buff, $block ) { |
86 |
print $client $buff; |
print $client $buff; |
87 |
|
$client->flush; |
88 |
$pos += $len; |
$pos += $len; |
89 |
printf "%s %d/%d %.2f%% %.2f K/s\r" |
progress_bar::tick( $path, $pos, $size ); |
|
, $path, $pos |
|
|
, $size, $pos * 100 / $size |
|
|
, ( $pos / 1024 ) / ( time() - $start_t ) |
|
|
; |
|
90 |
} |
} |
91 |
close($fh); |
close($fh); |
92 |
close($client); |
close($client); |
93 |
|
|
94 |
print "\n"; |
print STDERR "\n"; |
95 |
|
|
96 |
exit; |
warn "exit static child"; |
97 |
|
|
98 |
|
exit(0); |
99 |
} |
} |
100 |
|
|
101 |
use boolean; |
use boolean; |
107 |
|
|
108 |
sub start_stop { |
sub start_stop { |
109 |
my $daemon = shift; |
my $daemon = shift; |
110 |
my $pid = $pids->{$daemon}; |
my $pid = $pids->{$daemon} || 'not started'; |
111 |
|
|
112 |
warn "start_stop $daemon $pid\n"; |
warn "start_stop $daemon $pid\n"; |
113 |
|
|
145 |
sub get_request { |
sub get_request { |
146 |
my ( $client, $path, $param ) = @_; |
my ( $client, $path, $param ) = @_; |
147 |
|
|
148 |
|
server->refresh; |
149 |
|
|
150 |
warn "get_request $path ", $param ? dump( $param ) : '', "\n"; |
warn "get_request $path ", $param ? dump( $param ) : '', "\n"; |
151 |
|
|
152 |
if ( my $found = static( $client,$path ) ) { |
if ( my $found = static( $client,$path ) ) { |
158 |
$pids->{screen} ? qq|start| : qq|start screen first|; |
$pids->{screen} ? qq|start| : qq|start screen first|; |
159 |
|
|
160 |
my @rows = ( |
my @rows = ( |
|
'ip', html::tt( $server::ip ), |
|
|
'netmask', html::tt( $server::netmask ), |
|
|
|
|
161 |
'debug', qq|<a href=/our/debug/| . boolean::toggle($debug) . qq|>$debug</a>|, |
'debug', qq|<a href=/our/debug/| . boolean::toggle($debug) . qq|>$debug</a>|, |
162 |
); |
); |
163 |
|
|
210 |
} |
} |
211 |
|
|
212 |
print $client $ok |
print $client $ok |
213 |
|
, menu() |
214 |
, html::table( 2, @rows ) |
, html::table( 2, @rows ) |
215 |
, $below_table |
, $below_table |
216 |
, html::tabs( log::mac_changes ) |
, html::tabs( log::mac_changes ) |
217 |
, $debug_proc |
, $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+)} ) { |
} elsif ( $path =~ m{^/our/(\w+)/(\S+)} ) { |
226 |
eval 'our $' . $1 . ' = ' . $2; |
eval 'our $' . $1 . ' = ' . $2; |
227 |
warn $@ if $@; |
warn $@ if $@; |
254 |
} |
} |
255 |
|
|
256 |
use browser; |
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, |
273 |
start_stop 'screen'; |
start_stop 'screen'; |
274 |
start_stop 'kvm'; |
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; |
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> |
|
|
<a href=/exit>exit</a> |
|
|
</div> |
|
|
} if $client->connected; |
|
299 |
|
|
300 |
} |
} |
301 |
|
|