… | |
… | |
4 | use Coro::Handle; |
4 | use Coro::Handle; |
5 | use Coro::Socket; |
5 | use Coro::Socket; |
6 | use Event; |
6 | use Event; |
7 | use Time::HiRes 'time'; |
7 | use Time::HiRes 'time'; |
8 | |
8 | |
|
|
9 | use Text::Abbrev; |
|
|
10 | |
9 | my $last_ts = time; |
11 | my $last_ts = time; |
|
|
12 | |
|
|
13 | my %complete; |
|
|
14 | my @commands = qw(quit squit refresh country restart block info); |
|
|
15 | |
|
|
16 | abbrev \%complete, @commands; |
10 | |
17 | |
11 | sub shell { |
18 | sub shell { |
12 | my $fh = shift; |
19 | my $fh = shift; |
13 | |
20 | |
14 | while (defined (print $fh "cmd> "), $_ = <$fh>) { |
21 | while (defined (print $fh "cmd> "), $_ = <$fh>) { |
15 | s/\015?\012$//; |
22 | s/\015?\012$//; |
16 | chomp; |
23 | if (s/^(\S+)\s*// && (my $cmd = $complete{$1})) { |
17 | if (/^q/) { |
24 | if ($cmd eq "quit") { |
|
|
25 | print "bye bye.\n";#d# |
|
|
26 | last; |
|
|
27 | } elsif ($cmd eq "squit") { |
18 | Event::unloop; |
28 | Event::unloop; |
19 | } elsif (/^i/) { |
29 | } elsif ($cmd eq "block") { |
|
|
30 | print "blocked '$_'\n";#d# |
|
|
31 | $conn::blocked{$_} = time + $::BLOCKTIME; |
|
|
32 | } elsif ($cmd eq "info") { |
20 | $::NOW = time+1e-6; |
33 | $::NOW = time+1e-6; |
21 | my @data; |
34 | my @data; |
22 | for (values %conn::conn) { |
35 | for (values %conn::conn) { |
23 | for (values %$_) { |
36 | for (values %$_) { |
24 | next unless $_; |
37 | next unless $_; |
25 | my $rate = sprintf "%.1f", $_->{written} / ($::NOW - $_->{time}); |
38 | my $rate = sprintf "%.1f", $_->{written} / ($::NOW - $_->{time}); |
26 | push @data, "$_->{country}/$_->{remote_addr} $_->{written} $rate $_->{method} $_->{uri}\n"; |
39 | push @data, "$_->{country}/$_->{remote_addr} $_->{written} $rate $_->{method} $_->{uri}\n"; |
|
|
40 | } |
27 | } |
41 | } |
|
|
42 | print $fh sort @data; |
|
|
43 | print $fh scalar@data, " ($::conns) connections\n";#d# |
|
|
44 | print $fh "$::written bytes written in the last ",$::NOW - $last_ts, " seconds\n"; |
|
|
45 | printf $fh "(%.1f bytes/s)\n", $::written / ($::NOW - $last_ts); |
|
|
46 | ($last_ts, $::written) = ($::NOW, 0); |
|
|
47 | } elsif ($cmd eq "refresh") { |
|
|
48 | read_blocklist; |
|
|
49 | } elsif ($cmd eq "restart") { |
|
|
50 | $::RESTART = 1; |
|
|
51 | unloop; |
|
|
52 | print $fh "restarting, cu!\n"; |
|
|
53 | last; |
|
|
54 | } elsif ($cmd eq "country") { |
|
|
55 | print $fh ip_request($_), "\n"; |
28 | } |
56 | } |
29 | print $fh sort @data; |
|
|
30 | print $fh scalar@data, " ($::conns) 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 { |
57 | } else { |
44 | print $fh "try quit, info, restart, refresh\n"; |
58 | print $fh "try one of @commands\n"; |
45 | } |
59 | } |
46 | } |
60 | } |
47 | } |
61 | } |
48 | |
62 | |
49 | # bind to tcp port |
63 | # bind to tcp port |