1 |
# a server command shell |
2 |
|
3 |
use Coro; |
4 |
use Coro::Handle; |
5 |
use Coro::Socket; |
6 |
use Event; |
7 |
use Time::HiRes 'time'; |
8 |
|
9 |
use Text::Abbrev; |
10 |
|
11 |
my $last_ts = time; |
12 |
|
13 |
my %complete; |
14 |
my @commands = qw(quit squit refresh country restart block info print); |
15 |
|
16 |
abbrev \%complete, @commands; |
17 |
|
18 |
sub shell { |
19 |
my $fh = shift; |
20 |
|
21 |
while (defined (print $fh "cmd> "), $_ = <$fh>) { |
22 |
s/\015?\012$//; |
23 |
if (s/^(\S+)\s*// && (my $cmd = $complete{$1})) { |
24 |
if ($cmd eq "quit") { |
25 |
print $fh "bye bye.\n";#d# |
26 |
last; |
27 |
} elsif ($cmd eq "squit") { |
28 |
print $fh "server quit.\n";#d# |
29 |
Event::unloop; |
30 |
last; |
31 |
} elsif ($cmd eq "print") { |
32 |
my @res = eval $_; |
33 |
print $fh "eval: $@\n" if $@; |
34 |
print $fh "RES = ", (join " : ", @res), "\n"; |
35 |
} elsif ($cmd eq "block") { |
36 |
print "blocked '$_'\n";#d# |
37 |
$conn::blocked{$_} = time + $::BLOCKTIME; |
38 |
} elsif ($cmd eq "info") { |
39 |
$::NOW = time+1e-6; |
40 |
my @data; |
41 |
for (values %conn::conn) { |
42 |
for (values %$_) { |
43 |
next unless $_; |
44 |
my $rate = sprintf "%.1f", $_->{written} / ($::NOW - $_->{time}); |
45 |
push @data, "$_->{country}/$_->{remote_addr} $_->{written} $rate $_->{method} $_->{uri}\n"; |
46 |
} |
47 |
} |
48 |
print $fh sort @data; |
49 |
print $fh scalar@data, " ($::conns) connections\n";#d# |
50 |
print $fh "$::written bytes written in the last ",$::NOW - $last_ts, " seconds\n"; |
51 |
printf $fh "(%.1f bytes/s)\n", $::written / ($::NOW - $last_ts); |
52 |
($last_ts, $::written) = ($::NOW, 0); |
53 |
} elsif ($cmd eq "refresh") { |
54 |
do "config.pl"; |
55 |
print $fh "config.pl: $@\n" if $@; |
56 |
read_blocklist; |
57 |
} elsif ($cmd eq "restart") { |
58 |
$::RESTART = 1; |
59 |
unloop; |
60 |
print $fh "restarting, cu!\n"; |
61 |
last; |
62 |
} elsif ($cmd eq "country") { |
63 |
print $fh ip_request($_), "\n"; |
64 |
} |
65 |
} else { |
66 |
print $fh "try one of @commands\n"; |
67 |
} |
68 |
} |
69 |
} |
70 |
|
71 |
# bind to tcp port |
72 |
if ($CMDSHELL_PORT) { |
73 |
my $port = new Coro::Socket |
74 |
LocalAddr => "127.0.0.1", |
75 |
LocalPort => $CMDSHELL_PORT, |
76 |
ReuseAddr => 1, |
77 |
Listen => 1, |
78 |
or die "unable to bind cmdshell port: $!"; |
79 |
|
80 |
push @listen_sockets, $port; |
81 |
|
82 |
async { |
83 |
while () { |
84 |
async \&shell, scalar $port->accept; |
85 |
} |
86 |
}; |
87 |
} |
88 |
|
89 |
# bind to stdin (debug) |
90 |
if (1) { |
91 |
my $tty; |
92 |
open $tty, "+</dev/tty" |
93 |
and async \&shell, unblock $tty; |
94 |
} |
95 |
|