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