3 # Simple Asterisk Manager Proxy, Version 1.01
5 # Copyright (c) 2004 David C. Troy <dave@popvox.com>
7 # This code is based on Flash Operator Panel 'op_server.pl'
11 # David C. Troy <dave@popvox.com>
12 # Nicolas Gudino <nicolas@house.com.ar>
14 # This program is free software, distributed under the terms of
15 # the GNU General Public License.
17 # Security consideration: This script will open your manager port
18 # for unauthenticated logins. Be careful out there :-)
19 #############################
21 #############################
23 #############################
29 #############################
30 # User Configurable Options
31 #############################
32 # Configuration for logging in to your asterisk server
33 # Check you Asterisk config file "manager.conf" for details
34 my $manager_host = '127.0.0.1';
35 my $manager_port = 5038;
36 my $manager_user = 'your_username';
37 my $manager_secret = 'your_secret';
39 my $listen_port = 1234;
40 my $manager_pid = "/var/run/asterisk_managerproxy.pid";
42 #############################
44 #############################
52 $SIG{PIPE
} = 'IGNORE';
53 $SIG{INT
} = 'close_all';
54 $SIG{USR1
} = 'list_clients';
56 if (defined($ARGV[0]))
60 defined(my $pid = fork) or die "Can't Fork: $!";
62 setsid
or die "Can't start a new session: $!";
63 open MYPIDFILE
, ">$manager_pid";
74 new IO
::Socket
::INET
->new(
75 PeerAddr
=> $manager_host,
76 PeerPort
=> $manager_port,
80 or die "\nCould not connect to Asterisk Manager Port at $manager_host\n";
85 send_command_to_manager
( "Action: Login\r\nUsername: $manager_user\r\nSecret: $manager_secret\r\n\r\n" );
87 # Start up listener for new connections
89 new IO
::Socket
::INET
(Listen
=> 1, LocalPort
=> $listen_port, ReuseAddr
=> 1)
90 or die "\nCan't listen to port $listen_port\n";
91 $O = new IO
::Select
();
96 sub manager_reconnect
()
99 my $total_attempts = 60;
103 log_debug
("** Attempt reconnection to manager port # $attempt", 16);
105 new IO
::Socket
::INET
->new(
106 PeerAddr
=> $manager_host,
107 PeerPort
=> $manager_port,
112 if ($attempt > $total_attempts)
114 die("!! Could not reconnect to Asterisk Manager port");
116 sleep(10); # wait 10 seconds before trying to reconnect
119 send_command_to_manager
(
120 "Action: Login\r\nUsername: $manager_user\r\nSecret: $manager_secret\r\n\r\n"
124 # Loop, continuously processing new connections, input from those connections, and input from Manager conn
127 while (@S = $O->can_read)
133 log_debug
("** New client connection", 16);
135 $proxy_clients{$C} = \
$C;
136 print "New Connection: $C\n" if $debug;
139 # It's not a new client connection
141 my $R = sysread($_, $i, 2); # 2048; interleave every two bytes?
142 if (defined($R) && $R == 0)
144 # Confirm it's really dead by trying to write to it?
145 my $T = syswrite($_, ' ', 2); # 2048
148 # connection went away...
152 # If we lost the socket for the Asterisk Mgr, then reconnect
156 "** Asterisk Manager connection lost!!!!!",
160 # Remove handle from proxy_clients hash
161 print "Closed Connection: $_\n" if $debug;
162 delete $proxy_clients{$_};
166 else # Socket is active and we are ready to read something from it
169 next if ($blocks{$_} !~ /\r\n\r\n$/);
170 # do a 'next' unless we have completed a block; we are not ready to continue
172 # Process the completed block
173 # If block is from asterisk, send to clients
175 # block is from asterisk, send to clients
176 print "asterisk: $_\n$blocks{$_}" if $debug;
178 foreach my $client (values %proxy_clients) {
179 print "writing to $$client...\n" if $debug;
180 syswrite($$client, $blocks{$_});
183 print "sent block to $cnt clients\n" if $debug;
185 # Blocks are from clients, send to asterisk
186 syswrite($p, $blocks{$_});
187 print "client: $_\n$blocks{$_}\n" if $debug;
191 } # end if read succeeded
192 } # end if new client connection
193 } # end foreach @S -> can read
199 log_debug
("Exiting...", 0);
201 foreach my $hd ($O->handles)
210 sub send_command_to_manager
215 my @lineas = split("\r\n", $comando);
216 foreach my $linea (@lineas)
218 syswrite($p, "$linea\r\n");
219 log_debug
("-> $linea", 2);
222 syswrite($p, "\r\n");
230 print "$texto\n" if $debug;
236 foreach my $client (values %proxy_clients) {
237 print "client: $$client\n";
240 print "$cnt clients.\n\n";