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