ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Coro/myhttpd/shell.pl
Revision: 1.23
Committed: Sat Dec 8 21:01:17 2007 UTC (16 years, 7 months ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: rel-4_91, rel-4_748, rel-4_8, rel-4_9, rel-4_741, rel-4_743, rel-4_742, rel-4_744, rel-4_747, rel-4_74, rel-4_71, rel-4_72, rel-4_73, rel-4_802, rel-4_803, rel-4_801, rel-4_804, rel-4_479, rel-4_50, rel-4_51, rel-4_4, rel-4_45, rel-4_745, rel-4_901, rel-4_49, rel-4_48, rel-4_746, rel-4_47, rel-4_46, rel-4_7, rel-4_911, rel-4_912, rel-4_32, rel-4_33, rel-4_34, rel-4_35, rel-4_36, rel-4_37
Changes since 1.22: +4 -3 lines
Log Message:
convert from Event to EV

File Contents

# Content
1 # a server command shell
2
3 use EV;
4 use Coro;
5 use Coro::EV;
6 use Coro::Handle;
7 use Coro::Socket;
8 use Time::HiRes 'time';
9
10 use Text::Abbrev;
11
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;
18
19 sub shell {
20 my $fh = shift;
21
22 while (defined (print $fh "cmd> "), $_ = <$fh>) {
23 s/\015?\012$//;
24 if (s/^(\S+)\s*// && (my $cmd = $complete{$1})) {
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#
30 EV::unloop;
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") {
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 }
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";
70 }
71 } else {
72 print $fh "try one of @commands\n";
73 }
74 }
75 }
76
77 # bind to tcp port
78 if ($CMDSHELL_PORT) {
79 my $port = new Coro::Socket
80 #LocalAddr => "127.0.0.1",
81 LocalPort => $CMDSHELL_PORT,
82 ReuseAddr => 1,
83 Listen => 1,
84 or die "unable to bind cmdshell port: $!";
85
86 push @listen_sockets, $port;
87
88 async {
89 while () {
90 async \&shell, scalar $port->accept;
91 }
92 };
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