1 |
root |
1.1 |
# a server command shell |
2 |
|
|
|
3 |
|
|
use Coro; |
4 |
|
|
use Coro::Handle; |
5 |
|
|
use Coro::Socket; |
6 |
|
|
use Event; |
7 |
root |
1.8 |
use Time::HiRes 'time'; |
8 |
|
|
|
9 |
|
|
my $last_ts = time; |
10 |
root |
1.1 |
|
11 |
|
|
sub shell { |
12 |
|
|
my $fh = shift; |
13 |
|
|
|
14 |
|
|
while (defined (print $fh "cmd> "), $_ = <$fh>) { |
15 |
root |
1.2 |
s/\015?\012$//; |
16 |
root |
1.1 |
chomp; |
17 |
root |
1.2 |
if (/^q/) { |
18 |
root |
1.1 |
Event::unloop; |
19 |
root |
1.2 |
} elsif (/^i/) { |
20 |
root |
1.8 |
$::NOW = time+1e-6; |
21 |
root |
1.3 |
my @data; |
22 |
root |
1.1 |
for (values %conn::conn) { |
23 |
|
|
for (values %$_) { |
24 |
|
|
next unless $_; |
25 |
root |
1.8 |
my $rate = sprintf "%.1f", $_->{written} / ($::NOW - $_->{time}); |
26 |
|
|
push @data, "$_->{country}/$_->{remote_addr} $_->{written} $rate $_->{method} $_->{uri}\n"; |
27 |
root |
1.1 |
} |
28 |
|
|
} |
29 |
root |
1.3 |
print $fh sort @data; |
30 |
|
|
print $fh scalar@data, " connections\n";#d# |
31 |
root |
1.8 |
print $fh "$::written bytes written in the last ",$::NOW - $last_ts, " seconds\n"; |
32 |
|
|
printf $fh "(%.1f bytes/s)\n", $::written / ($::NOW - $last_ts); |
33 |
|
|
($last_ts, $::written) = ($::NOW, 0); |
34 |
root |
1.5 |
} elsif (/^ref/) { |
35 |
|
|
read_blocklist; |
36 |
root |
1.2 |
} elsif (/^r/) { |
37 |
|
|
$::RESTART = 1; |
38 |
|
|
unloop; |
39 |
|
|
print $fh "bye bye.\n"; |
40 |
|
|
last; |
41 |
root |
1.8 |
} elsif (/^co\S*\s+(\S+)/) { |
42 |
|
|
print $fh ip_request($1), "\n"; |
43 |
root |
1.1 |
} else { |
44 |
root |
1.5 |
print $fh "try quit, info, restart, refresh\n"; |
45 |
root |
1.1 |
} |
46 |
|
|
} |
47 |
|
|
} |
48 |
|
|
|
49 |
|
|
# bind to tcp port |
50 |
|
|
if ($CMDSHELL_PORT) { |
51 |
|
|
my $port = new Coro::Socket |
52 |
|
|
LocalAddr => "127.0.0.1", |
53 |
|
|
LocalPort => $CMDSHELL_PORT, |
54 |
|
|
ReuseAddr => 1, |
55 |
|
|
Listen => 1, |
56 |
|
|
or die "unable to bind cmdshell port: $!"; |
57 |
root |
1.2 |
|
58 |
|
|
push @listen_sockets, $port; |
59 |
root |
1.1 |
|
60 |
|
|
async { |
61 |
root |
1.7 |
while () { |
62 |
|
|
async \&shell, scalar $port->accept; |
63 |
|
|
} |
64 |
root |
1.1 |
}; |
65 |
|
|
} |
66 |
|
|
|
67 |
|
|
# bind to stdin (debug) |
68 |
|
|
if (1) { |
69 |
|
|
my $tty; |
70 |
|
|
open $tty, "+</dev/tty" |
71 |
|
|
and async \&shell, unblock $tty; |
72 |
|
|
} |
73 |
|
|
|