1 |
root |
1.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 |
root |
1.2 |
s/\015?\012$//; |
13 |
root |
1.1 |
chomp; |
14 |
root |
1.2 |
if (/^q/) { |
15 |
root |
1.1 |
Event::unloop; |
16 |
root |
1.2 |
} elsif (/^i/) { |
17 |
root |
1.3 |
my @data; |
18 |
root |
1.1 |
for (values %conn::conn) { |
19 |
|
|
for (values %$_) { |
20 |
|
|
next unless $_; |
21 |
root |
1.3 |
push @data, "$_: $_->{remote_addr} $_->{uri}\n"; |
22 |
root |
1.1 |
} |
23 |
|
|
} |
24 |
root |
1.3 |
print $fh sort @data; |
25 |
|
|
print $fh scalar@data, " connections\n";#d# |
26 |
root |
1.2 |
} elsif (/^r/) { |
27 |
|
|
$::RESTART = 1; |
28 |
|
|
unloop; |
29 |
|
|
print $fh "bye bye.\n"; |
30 |
|
|
last; |
31 |
root |
1.1 |
} else { |
32 |
root |
1.2 |
print $fh "try quit, info, restart\n"; |
33 |
root |
1.1 |
} |
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 |
root |
1.2 |
|
46 |
|
|
push @listen_sockets, $port; |
47 |
root |
1.1 |
|
48 |
|
|
async { |
49 |
root |
1.4 |
async \&shell, $port->accept |
50 |
|
|
while 1; |
51 |
root |
1.1 |
}; |
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 |
|
|
|