19 |
use IO::Socket::INET; |
use IO::Socket::INET; |
20 |
use Module::Refresh; |
use Module::Refresh; |
21 |
|
|
22 |
|
our $pids = { httpd => $$ }; |
23 |
|
|
24 |
|
sub DESTROY { |
25 |
|
warn "pids ",dump( $pids ); |
26 |
|
foreach ( values %$pids ) { |
27 |
|
warn "kill $_"; |
28 |
|
kill 1,$_ || kill 9, $_; |
29 |
|
} |
30 |
|
} |
31 |
|
|
32 |
our $port = 7777; |
our $port = 7777; |
33 |
our $debug = 1; |
our $debug = 0; |
34 |
|
|
35 |
use server; |
use server; |
36 |
our $url = "http://$server::ip:$port/"; |
our $url = "http://$server::ip:$port"; |
37 |
|
|
38 |
use html; |
use html; |
39 |
|
|
40 |
sub static { |
sub static { |
41 |
my ($client,$path) = @_; |
my ($client,$path) = @_; |
42 |
|
|
43 |
$path = "tftp/$path"; |
my $full = "$server::base_dir/tftp/$path"; |
44 |
|
|
45 |
if ( ! -e $path || -d $path ) { |
return if ! -f $full; |
|
print $client "HTTP/1.0 404 $path not found\r\n"; |
|
|
return; |
|
|
} |
|
46 |
|
|
47 |
my $type = 'text/plain'; |
my $type = 'application/octet-stream'; |
48 |
$type = 'text/html' if $path =~ m{\.htm}; |
$type = 'text/html' if $path =~ m{\.htm}; |
49 |
$type = 'application/javascript' if $path =~ m{\.js}; |
$type = 'application/javascript' if $path =~ m{\.js}; |
50 |
|
$type = 'text/plain' if $path =~ m{\.txt}; |
51 |
|
|
52 |
|
my $size = -s $full || return; |
53 |
|
|
54 |
|
print $client "HTTP/1.0 200 OK\r\nContent-Type: $type\r\nContent-Length: $size\r\nConnection: close\r\n\r\n"; |
55 |
|
|
56 |
print $client "HTTP/1.0 200 OK\r\nContent-Type: $type\r\nContent-Length: ", -s $path,"\r\n\r\n"; |
open(my $fh, $full); |
57 |
open(my $html, $path); |
print "static $path $type $size\n"; |
58 |
while(<$html>) { |
|
59 |
print $client $_; |
my $block = 8192; |
60 |
|
my $buff; |
61 |
|
my $pos = 0; |
62 |
|
|
63 |
|
while( my $len = read $fh, $buff, $block ) { |
64 |
|
print $client $buff; |
65 |
|
$pos += $len; |
66 |
|
printf "%s %d/%d %.2f%%\r", $path, $pos, $size, $pos * 100 / $size; |
67 |
} |
} |
68 |
close($html); |
close($fh); |
69 |
|
close($client); |
70 |
|
|
71 |
|
print "$path $pos == $size OK\n"; |
72 |
|
|
73 |
return $path; |
return $path; |
74 |
} |
} |
77 |
|
|
78 |
use boolean; |
use boolean; |
79 |
|
|
80 |
|
use screen; |
81 |
|
use kvm; |
82 |
|
|
83 |
|
$SIG{CHLD} = 'IGNORE'; |
84 |
|
|
85 |
|
sub start_stop { |
86 |
|
my $daemon = shift; |
87 |
|
my $pid = $pids->{$daemon}; |
88 |
|
|
89 |
|
warn "start_stop $daemon pids: ",dump( $pids ); |
90 |
|
|
91 |
|
if ( $pid ) { |
92 |
|
warn "kill 9 $pid"; |
93 |
|
kill 9, $pid; |
94 |
|
$pids->{$daemon} = 'stopped'; |
95 |
|
return qq|$daemon pid $pid stopped|; |
96 |
|
} else { |
97 |
|
if ( $pid = fork ) { |
98 |
|
# parent |
99 |
|
$pids->{$daemon} = $pid; |
100 |
|
warn "forked $daemon $pid"; |
101 |
|
return qq|$daemon pid $pid started|; |
102 |
|
} elsif ( defined $pid ) { |
103 |
|
# child |
104 |
|
my $eval = $daemon . '::start(' . dump(@_) . ')'; |
105 |
|
warn "eval $eval"; |
106 |
|
eval $eval; |
107 |
|
warn "can't start $daemon: $@" if $@; |
108 |
|
exit; |
109 |
|
} else { |
110 |
|
die "fork error $!"; |
111 |
|
} |
112 |
|
} |
113 |
|
} |
114 |
|
|
115 |
sub get_request { |
sub get_request { |
116 |
my ( $client, $path, $param ) = @_; |
my ( $client, $path, $param ) = @_; |
117 |
|
|
118 |
warn "get_request $client $path $param"; |
warn "get_request $client $path ",dump( $param ); |
119 |
|
|
120 |
if ( my $found = static( $client,$path ) ) { |
if ( my $found = static( $client,$path ) ) { |
121 |
warn "static $found" if $debug; |
warn "static $found" if $debug; |
122 |
} elsif ( $path eq '/' ) { |
} elsif ( $path eq '/' ) { |
123 |
print $client $ok, |
|
124 |
html::table( 2, |
my $screen = $pids->{screen} ? qq|stop <tt>$pids->{screen}</tt>| : 'start'; |
125 |
'pid', $$, |
my $kvm = $pids->{kvm} ? qq|stop <tt>$pids->{kvm}</tt>| : |
126 |
'debug', qq|<a href=/our/debug/| . boolean::toggle($debug) . qq|>$debug</a>|, |
$pids->{screen} ? qq|start| : qq|start screen first|; |
127 |
|
|
128 |
|
my @rows = ( |
129 |
|
'ip', html::tt( $server::ip ), |
130 |
|
'netmask', html::tt( $server::netmask ), |
131 |
|
|
132 |
|
'debug', qq|<a href=/our/debug/| . boolean::toggle($debug) . qq|>$debug</a>|, |
133 |
); |
); |
134 |
|
foreach my $name ( %$pids ) { |
135 |
|
my $pid = $pids->{$name} || next; |
136 |
|
|
137 |
|
my $html = qq|<a href=/$name>$pid</a>|; |
138 |
|
$html .= qq|<pre style="font-size: 10%">| |
139 |
|
. ( $debug ? read_file("/proc/$pid/status") : '' ) |
140 |
|
. qq|</pre>| if $debug; |
141 |
|
|
142 |
|
push @rows, ( $name => $html ); |
143 |
|
} |
144 |
|
|
145 |
|
print $client $ok, html::table( 2, @rows ); |
146 |
|
|
147 |
} elsif ( $path =~ m{^/our/(\w+)/(\S+)} ) { |
} elsif ( $path =~ m{^/our/(\w+)/(\S+)} ) { |
148 |
eval 'our $' . $1 . ' = ' . $2; |
eval 'our $' . $1 . ' = ' . $2; |
149 |
warn $@ if $@; |
warn $@ if $@; |
150 |
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>|; |
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>|; |
151 |
|
} elsif ( $path =~ m{^/(screen|kvm)} ) { |
152 |
|
print $client $ok, start_stop($1); |
153 |
|
} elsif ( $path eq '/exit' ) { |
154 |
|
# DESTROY; |
155 |
|
exit 0; |
156 |
} elsif ( $path =~ m{/boot} ) { |
} elsif ( $path =~ m{/boot} ) { |
157 |
print $client qq{$ok |
print $client qq{$ok |
158 |
#!gpxe |
#!gpxe |
167 |
} |
} |
168 |
|
|
169 |
} |
} |
170 |
|
|
171 |
|
use browser; |
172 |
|
|
173 |
sub start { |
sub start { |
174 |
|
|
175 |
my $server = IO::Socket::INET->new( |
my $server = IO::Socket::INET->new( |
181 |
|
|
182 |
print "url $url\n"; |
print "url $url\n"; |
183 |
|
|
184 |
system "/mnt/llin/rest/cvs/uzbl/uzbl -u $url &"; |
start_stop 'browser', $url; |
185 |
|
start_stop 'screen'; |
186 |
|
start_stop 'kvm'; |
187 |
|
|
188 |
while (my $client = $server->accept()) { |
while (my $client = $server->accept()) { |
189 |
$client->autoflush(1); |
$client->autoflush(1); |
213 |
print $client qq{ |
print $client qq{ |
214 |
<div style="font-size: 80%; color: #888"> |
<div style="font-size: 80%; color: #888"> |
215 |
<a href="">reload</a> |
<a href="">reload</a> |
216 |
<a href="/">index</a> |
<a href=/>index</a> |
217 |
|
<a href=/exit>exit</a> |
218 |
</div> |
</div> |
219 |
} if $client->connected; |
} if $client->connected; |
220 |
|
|
223 |
die "server died"; |
die "server died"; |
224 |
} |
} |
225 |
|
|
226 |
|
warn "loaded"; |
227 |
|
|
228 |
1; |
1; |