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