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 | |
9 | use Text::Abbrev; |
10 | use Text::Abbrev; |
10 | |
11 | |
11 | my $last_ts = time; |
12 | my $last_ts = time; |
12 | |
13 | |
13 | my %complete; |
14 | my %complete; |
14 | my @commands = qw(quit squit refresh country restart block info print); |
15 | my @commands = qw(quit squit refresh country restart block info print clrdiridx); |
15 | |
16 | |
16 | abbrev \%complete, @commands; |
17 | abbrev \%complete, @commands; |
17 | |
18 | |
18 | sub shell { |
19 | sub shell { |
19 | my $fh = shift; |
20 | my $fh = shift; |
… | |
… | |
24 | if ($cmd eq "quit") { |
25 | if ($cmd eq "quit") { |
25 | print $fh "bye bye.\n";#d# |
26 | print $fh "bye bye.\n";#d# |
26 | last; |
27 | last; |
27 | } elsif ($cmd eq "squit") { |
28 | } elsif ($cmd eq "squit") { |
28 | print $fh "server quit.\n";#d# |
29 | print $fh "server quit.\n";#d# |
29 | Event::unloop; |
30 | EV::unloop; |
30 | last; |
31 | last; |
31 | } elsif ($cmd eq "print") { |
32 | } elsif ($cmd eq "print") { |
32 | my @res = eval $_; |
33 | my @res = eval $_; |
33 | print $fh "eval: $@\n" if $@; |
34 | print $fh "eval: $@\n" if $@; |
34 | print $fh "RES = ", (join " : ", @res), "\n"; |
35 | print $fh "RES = ", (join " : ", @res), "\n"; |
35 | } elsif ($cmd eq "block") { |
36 | } elsif ($cmd eq "block") { |
36 | print "blocked '$_'\n";#d# |
37 | print "blocked '$_'\n";#d# |
37 | $conn::blocked{$_} = time + $::BLOCKTIME; |
38 | $conn::blocked{$_} = [time + $::BLOCKTIME, "blocked by operator"]; |
38 | } elsif ($cmd eq "info") { |
39 | } elsif ($cmd eq "info") { |
39 | $::NOW = time+1e-6; |
40 | $::NOW = time+1e-6; |
40 | my @data; |
41 | my @data; |
41 | for (values %conn::conn) { |
42 | for (values %conn::conn) { |
42 | for (values %$_) { |
43 | for (values %$_) { |
… | |
… | |
45 | push @data, "$_->{country}/$_->{remote_addr} $_->{written} $rate $_->{method} $_->{uri}\n"; |
46 | push @data, "$_->{country}/$_->{remote_addr} $_->{written} $rate $_->{method} $_->{uri}\n"; |
46 | } |
47 | } |
47 | } |
48 | } |
48 | print $fh sort @data; |
49 | print $fh sort @data; |
49 | print $fh scalar@data, " ($::conns) connections\n";#d# |
50 | print $fh scalar@data, " ($::conns) connections\n";#d# |
50 | print $fh "$::written bytes written in the last ",$::NOW - $last_ts, " seconds\n"; |
51 | 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 | printf $fh "(%.1f bytes/s)\n", $::written / ($::NOW - $last_ts); |
52 | ($last_ts, $::written) = ($::NOW, 0); |
53 | ($last_ts, $::written) = ($::NOW, 0); |
53 | } elsif ($cmd eq "refresh") { |
54 | } elsif ($cmd eq "refresh") { |
54 | do "config.pl"; |
55 | do "config.pl"; |
55 | print $fh "config.pl: $@\n" if $@; |
56 | print $fh "config.pl: $@\n" if $@; |
|
|
57 | %statdata_cache = (); |
56 | read_blockuri; |
58 | conn::read_blockuri; |
57 | read_blockref; |
59 | conn::read_blockref; |
|
|
60 | } elsif ($cmd eq "clrdiridx") { |
|
|
61 | %statdata_cache = (); |
|
|
62 | delete $diridx{$_} for keys %diridx; # server error on %diridx = (); |
58 | } elsif ($cmd eq "restart") { |
63 | } elsif ($cmd eq "restart") { |
59 | $::RESTART = 1; |
64 | $::RESTART = 1; |
60 | unloop; |
65 | EV::unloop; |
61 | print $fh "restarting, cu!\n"; |
66 | print $fh "restarting, cu!\n"; |
62 | last; |
67 | last; |
63 | } elsif ($cmd eq "country") { |
68 | } elsif ($cmd eq "country") { |
64 | print $fh ip_request($_), "\n"; |
69 | print $fh netgeo::ip_request($_), "\n"; |
65 | } |
70 | } |
66 | } else { |
71 | } else { |
67 | print $fh "try one of @commands\n"; |
72 | print $fh "try one of @commands\n"; |
68 | } |
73 | } |
69 | } |
74 | } |