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 |
|
|
22 |
our $port = 7777; |
our $port = 7777; |
23 |
our $debug = 1; |
our $debug = 1; |
25 |
use server; |
use server; |
26 |
our $url = "http://$server::ip:$port/"; |
our $url = "http://$server::ip:$port/"; |
27 |
|
|
28 |
|
use html; |
29 |
|
|
30 |
sub static { |
sub static { |
31 |
my ($client,$path) = @_; |
my ($client,$path) = @_; |
32 |
|
|
53 |
|
|
54 |
my $ok = "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nConnection: close\r\n\r\n"; |
my $ok = "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nConnection: close\r\n\r\n"; |
55 |
|
|
56 |
|
use boolean; |
57 |
|
|
58 |
|
use screen; |
59 |
|
use kvm; |
60 |
|
our $pids; |
61 |
|
|
62 |
|
$SIG{CHLD} = 'IGNORE'; |
63 |
|
|
64 |
|
sub start_stop { |
65 |
|
my $daemon = shift; |
66 |
|
my $pid = $pids->{$daemon}; |
67 |
|
|
68 |
|
warn "start_stop $daemon pids: ",dump( $pids ); |
69 |
|
|
70 |
|
if ( $pid ) { |
71 |
|
warn "kill 9 $pid"; |
72 |
|
kill 9, $pid; |
73 |
|
delete $pids->{$daemon}; |
74 |
|
return qq|$daemon pid $pid stopped|; |
75 |
|
} else { |
76 |
|
if ( $pid = fork ) { |
77 |
|
# parent |
78 |
|
$pids->{$daemon} = $pid; |
79 |
|
warn "forked $daemon $pid"; |
80 |
|
return qq|$daemon pid $pid started|; |
81 |
|
} elsif ( defined $pid ) { |
82 |
|
# child |
83 |
|
my $eval = $daemon . '::start'; |
84 |
|
warn "eval $eval"; |
85 |
|
eval $eval; |
86 |
|
warn "can't start $daemon: $@" if $@; |
87 |
|
exit; |
88 |
|
} else { |
89 |
|
die "fork error $!"; |
90 |
|
} |
91 |
|
} |
92 |
|
} |
93 |
|
|
94 |
|
sub get_request { |
95 |
|
my ( $client, $path, $param ) = @_; |
96 |
|
|
97 |
|
warn "get_request $client $path ",dump( $param ); |
98 |
|
|
99 |
|
if ( my $found = static( $client,$path ) ) { |
100 |
|
warn "static $found" if $debug; |
101 |
|
} elsif ( $path eq '/' ) { |
102 |
|
|
103 |
|
my $screen = $pids->{screen} ? qq|stop <tt>$pids->{screen}</tt>| : 'start'; |
104 |
|
my $kvm = $pids->{kvm} ? qq|stop <tt>$pids->{kvm}</tt>| : |
105 |
|
$pids->{screen} ? qq|start| : qq|start screen first|; |
106 |
|
|
107 |
|
print $client $ok, |
108 |
|
html::table( 2, |
109 |
|
'pid', $$, |
110 |
|
'debug', qq|<a href=/our/debug/| . boolean::toggle($debug) . qq|>$debug</a>|, |
111 |
|
'screen', qq|<a href=/screen>$screen</a>|, |
112 |
|
'kvm', qq|<a href=/kvm>$kvm</a>|, |
113 |
|
); |
114 |
|
|
115 |
|
} elsif ( $path =~ m{^/our/(\w+)/(\S+)} ) { |
116 |
|
eval 'our $' . $1 . ' = ' . $2; |
117 |
|
warn $@ if $@; |
118 |
|
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>|; |
119 |
|
} elsif ( $path =~ m{^/(screen|kvm)} ) { |
120 |
|
print $client $ok, start_stop($1); |
121 |
|
} elsif ( $path =~ m{/boot} ) { |
122 |
|
print $client qq{$ok |
123 |
|
#!gpxe |
124 |
|
imgfree |
125 |
|
login |
126 |
|
chain http://$server::ip:$httpd::port/ |
127 |
|
|
128 |
|
}; |
129 |
|
} else { |
130 |
|
print $client "HTTP/1.0 404 $path\r\nConnection: close\r\nContent-type: text/html\r\n\r\n<big>404 $path</big>"; |
131 |
|
warn "404 $path"; |
132 |
|
} |
133 |
|
|
134 |
|
} |
135 |
|
|
136 |
sub start { |
sub start { |
137 |
|
|
140 |
LocalPort => $httpd::port, |
LocalPort => $httpd::port, |
141 |
Listen => SOMAXCONN, |
Listen => SOMAXCONN, |
142 |
Reuse => 1 |
Reuse => 1 |
143 |
); |
) || die "can't start server on $url: $!"; |
|
|
|
|
die "can't setup server" unless $server; |
|
144 |
|
|
145 |
print "url $url\n"; |
print "url $url\n"; |
146 |
|
|
152 |
|
|
153 |
warn "request $request\n" if $debug; |
warn "request $request\n" if $debug; |
154 |
|
|
155 |
|
Module::Refresh->refresh; |
156 |
|
|
157 |
if ($request =~ m{^GET (/.*) HTTP/1.[01]}) { |
if ($request =~ m{^GET (/.*) HTTP/1.[01]}) { |
158 |
my $method = $1; |
my $path = $1; |
159 |
my $param; |
my $param; |
160 |
if ( $method =~ s{\?(.+)}{} ) { |
if ( $path =~ s{\?(.+)}{} ) { |
161 |
foreach my $p ( split(/[&;]/, $1) ) { |
foreach my $p ( split(/[&;]/, $1) ) { |
162 |
my ($n,$v) = split(/=/, $p, 2); |
my ($n,$v) = split(/=/, $p, 2); |
163 |
$param->{$n} = $v; |
$param->{$n} = $v; |
164 |
} |
} |
165 |
warn "param: ",dump( $param ) if $debug; |
warn "param: ",dump( $param ) if $debug; |
166 |
} |
} |
167 |
warn "method $method"; |
warn "path $path param: ",dump( $param ); |
168 |
|
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"; |
|
|
} |
|
169 |
} else { |
} else { |
170 |
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"; |
171 |
warn "500 $request"; |
warn "500 $request"; |
172 |
} |
} |
173 |
|
|
174 |
print $client qq{ |
print $client qq{ |
175 |
<a href="">reload</a> |
<div style="font-size: 80%; color: #888"> |
176 |
}; |
<a href="">reload</a> |
177 |
|
<a href="/">index</a> |
178 |
|
</div> |
179 |
|
} if $client->connected; |
180 |
|
|
|
close($client); |
|
181 |
} |
} |
182 |
|
|
183 |
die "server died"; |
die "server died"; |
184 |
} |
} |
185 |
|
|
186 |
|
warn "loaded"; |
187 |
|
|
188 |
1; |
1; |