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