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