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 @conn; |
18 |
push @conn, values %$_ for values %conn::conn; |
19 |
for (values %conn::conn) { |
20 |
for (values %$_) { |
21 |
next unless $_; |
22 |
print $fh "$_: $_->{remote_addr} $_->{uri}\n"; |
23 |
} |
24 |
} |
25 |
} elsif (/^r/) { |
26 |
$::RESTART = 1; |
27 |
unloop; |
28 |
print $fh "bye bye.\n"; |
29 |
last; |
30 |
} else { |
31 |
print $fh "try quit, info, restart\n"; |
32 |
} |
33 |
} |
34 |
} |
35 |
|
36 |
# bind to tcp port |
37 |
if ($CMDSHELL_PORT) { |
38 |
my $port = new Coro::Socket |
39 |
LocalAddr => "127.0.0.1", |
40 |
LocalPort => $CMDSHELL_PORT, |
41 |
ReuseAddr => 1, |
42 |
Listen => 1, |
43 |
or die "unable to bind cmdshell port: $!"; |
44 |
|
45 |
push @listen_sockets, $port; |
46 |
|
47 |
async { |
48 |
async \&shell, $port->accept; |
49 |
}; |
50 |
} |
51 |
|
52 |
# bind to stdin (debug) |
53 |
if (1) { |
54 |
my $tty; |
55 |
open $tty, "+</dev/tty" |
56 |
and async \&shell, unblock $tty; |
57 |
} |
58 |
|