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