ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Coro/myhttpd/shell.pl
Revision: 1.10
Committed: Tue Aug 14 02:28:51 2001 UTC (22 years, 11 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.9: +40 -26 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.1 # a server command shell
2    
3     use Coro;
4     use Coro::Handle;
5     use Coro::Socket;
6     use Event;
7 root 1.8 use Time::HiRes 'time';
8    
9 root 1.10 use Text::Abbrev;
10    
11 root 1.8 my $last_ts = time;
12 root 1.1
13 root 1.10 my %complete;
14     my @commands = qw(quit squit refresh country restart block info);
15    
16     abbrev \%complete, @commands;
17    
18 root 1.1 sub shell {
19     my $fh = shift;
20    
21     while (defined (print $fh "cmd> "), $_ = <$fh>) {
22 root 1.2 s/\015?\012$//;
23 root 1.10 if (s/^(\S+)\s*// && (my $cmd = $complete{$1})) {
24     if ($cmd eq "quit") {
25     print "bye bye.\n";#d#
26     last;
27     } elsif ($cmd eq "squit") {
28     Event::unloop;
29     } elsif ($cmd eq "block") {
30     print "blocked '$_'\n";#d#
31     $conn::blocked{$_} = time + $::BLOCKTIME;
32     } elsif ($cmd eq "info") {
33     $::NOW = time+1e-6;
34     my @data;
35     for (values %conn::conn) {
36     for (values %$_) {
37     next unless $_;
38     my $rate = sprintf "%.1f", $_->{written} / ($::NOW - $_->{time});
39     push @data, "$_->{country}/$_->{remote_addr} $_->{written} $rate $_->{method} $_->{uri}\n";
40     }
41 root 1.1 }
42 root 1.10 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";
56 root 1.1 }
57     } else {
58 root 1.10 print $fh "try one of @commands\n";
59 root 1.1 }
60     }
61     }
62    
63     # bind to tcp port
64     if ($CMDSHELL_PORT) {
65     my $port = new Coro::Socket
66     LocalAddr => "127.0.0.1",
67     LocalPort => $CMDSHELL_PORT,
68     ReuseAddr => 1,
69     Listen => 1,
70     or die "unable to bind cmdshell port: $!";
71 root 1.2
72     push @listen_sockets, $port;
73 root 1.1
74     async {
75 root 1.7 while () {
76     async \&shell, scalar $port->accept;
77     }
78 root 1.1 };
79     }
80    
81     # bind to stdin (debug)
82     if (1) {
83     my $tty;
84     open $tty, "+</dev/tty"
85     and async \&shell, unblock $tty;
86     }
87