1 |
# a server command shell |
2 |
|
3 |
use Coro; |
4 |
use Coro::Handle; |
5 |
use Coro::Socket; |
6 |
use Event; |
7 |
|
8 |
sub shell { |
9 |
my $fh = shift; |
10 |
|
11 |
while (defined (print $fh "cmd> "), $_ = <$fh>) { |
12 |
s/\015?\012$//; |
13 |
chomp; |
14 |
if (/^q/) { |
15 |
Event::unloop; |
16 |
} elsif (/^i/) { |
17 |
my @data; |
18 |
for (values %conn::conn) { |
19 |
for (values %$_) { |
20 |
next unless $_; |
21 |
push @data, "$_: $_->{remote_addr} $_->{uri}\n"; |
22 |
} |
23 |
} |
24 |
print $fh sort @data; |
25 |
print $fh scalar@data, " connections\n";#d# |
26 |
} elsif (/^r/) { |
27 |
$::RESTART = 1; |
28 |
unloop; |
29 |
print $fh "bye bye.\n"; |
30 |
last; |
31 |
} else { |
32 |
print $fh "try quit, info, restart\n"; |
33 |
} |
34 |
} |
35 |
} |
36 |
|
37 |
# bind to tcp port |
38 |
if ($CMDSHELL_PORT) { |
39 |
my $port = new Coro::Socket |
40 |
LocalAddr => "127.0.0.1", |
41 |
LocalPort => $CMDSHELL_PORT, |
42 |
ReuseAddr => 1, |
43 |
Listen => 1, |
44 |
or die "unable to bind cmdshell port: $!"; |
45 |
|
46 |
push @listen_sockets, $port; |
47 |
|
48 |
async { |
49 |
async \&shell, $port->accept |
50 |
while 1; |
51 |
}; |
52 |
} |
53 |
|
54 |
# bind to stdin (debug) |
55 |
if (1) { |
56 |
my $tty; |
57 |
open $tty, "+</dev/tty" |
58 |
and async \&shell, unblock $tty; |
59 |
} |
60 |
|