davies147 / astmanproxy

Asterisk Manager Proxy
27 stars 10 forks source link

Long AMI Responses Timeout #7

Open Deantwo opened 8 years ago

Deantwo commented 8 years ago

Having some problems with receiving long responses from the AMI when going through the proxy. By "long responses" I mean many lines of text, such as the result of the core show help AMI command which is 257 lines.

I made some tests using the AsterNET library in C#.

using AsterNET.Manager;
using AsterNET.Manager.Action;
using AsterNET.Manager.Response;

public string[] LongResponseTest()
{
    ManagerConnection myManagerConn = new ManagerConnection();
    myManagerConn.Hostname = _hostname;
    myManagerConn.Port = _port;
    myManagerConn.Username = _userName;
    myManagerConn.Password = _userSecret;
    try
    {
        myManagerConn.Login();
        CommandResponse commandActionResponse = (CommandResponse)myManagerConn.SendAction(new CommandAction("core show help"));
        return commandActionResponse.Result.ToArray();
    }
    catch (Exception ex)
    {
        throw;
    }
    finally
    {
        myManagerConn.Logoff();
    }
}

This code works when targeting the AMI directly, but timeout when using the proxy. Even setting SendAction's timeout property to 60000 results in a timeout.

I have found no settings in the proxy's configuration that could be causing this, but I could have missed something. The proxy is running mostly default configuration without SSL.

davies147 commented 8 years ago

Have you tried running astmanproxy in debug mode in the foreground? It should report what it receives from you and how it plans to respond. Which protocol does the C# manager library use? I've only really done any work on the raw AMI protocol, and not the HTTP path.

Deantwo commented 8 years ago

Sorry about that, I mainly program C# so that was just code the I knew best and was working with at the time. It uses the standard AMI protocol, I don't know what programming langue you prefer though.

The code that actually failed when switching to the proxy is a PHP script, I am just not that experienced with PHP though.

<?php
$ast_server_ip = $_SERVER['SERVER_ADDR'];
#$ast_ami_port  = "5038";
$ast_ami_port  = "5039";
$ast_mgr_user  = "admin";
$ast_mgr_pass  = "1234";

$test_result   = '';

# Open socket to asterisk and connect to AMI
$socket = fsockopen($ast_server_ip, $ast_ami_port, $errno, $errstr);

if (!$socket) {
    echo "$errstr ($errno)<br />\n";
    echo "Login fejlede";
}
else {
    # login to AMI
    fputs($socket, "\r\n");
    fputs($socket, "Action: Login\r\n");
    fputs($socket, "UserName: $ast_mgr_user\r\n");
    fputs($socket, "Secret: $ast_mgr_pass\r\n\r\n");

    # Request full info on help text and disconnect from AMI
    fputs($socket, "Action: Command\r\n");
    fputs($socket, "Command: core show help\r\n\r\n");
    fputs($socket, "Action: Logoff\r\n\r\n");

    # Read data and close socket
    while (!feof($socket)) {
        $test_result .= fread($socket, 8192); 
    }

    fclose($socket);
}

# Print the result
echo "Long Response Test result:<br>\n";
echo $test_result;
?>

Doing some tests with this instead seems to work on my test server, but not a production server. It may be Asterisk version related too? Since my C# program fail using the proxy every time on either, but the PHP script only fails on the production Asterisk through the proxy.

Test Asterisk version: 1.8.11 Production Asterisk version: 11.7.0

Summary:

I am gonna attempt some tests running proxy in debug mode now.

Deantwo commented 8 years ago

I am gonna attempt some tests running proxy in debug mode now.

According to the proxy's debug mode, it is indeed receiving the response from the AMI. I am not sure if it is replying though.

This is what I see when debug in enabled: image

davies147 commented 8 years ago

Okay, this may be caused by a nasty limitation of astmanproxy which has always been there - All of the command/response message buffers are statically allocated - This means that each line is fixed length, and that there are a limited number of lines per command/response (255 lines)

Looks like your 'core show command' is exceeding this, and I wonder if it is just not sending it because of that? Can you send a command that has a shorter result to see if it helps?

You could also try to increase the max lines per message, though that is a nasty and evil solution :(

Of course this may not be correct, but all of the responses have a response size of 255, which is a suspicious size...

Steve

davies147 commented 8 years ago

FYI,

define MAX_HEADERS 256

in file: astmanproxy/src/include/astmanproxy.h

Deantwo commented 8 years ago

Looks like your 'core show command' is exceeding this, and I wonder if it is just not sending it because of that? Can you send a command that has a shorter result to see if it helps?

Commands with shorter responses do work.

Okay, this may be caused by a nasty limitation of astmanproxy which has always been there - All of the command/response message buffers are statically allocated - This means that each line is fixed length, and that there are a limited number of lines per command/response (255 lines)

That would indeed explain everything. The difference in the two Asterisk versions (1.8.11 and 11.7.0) 's core show help is 14 new commands, which means 14 extra lines.

It doesn't explain why my C# program is failing, but that could be a problem with the AsterNET library also limiting it somehow, I'll take it up with them if it is. It may just be because it adds a few extra lines for some reason.

You could also try to increase the max lines per message, though that is a nasty and evil solution :(

I'll give it a try. Not sure I will be able to until next week though.

But I am alfarid changing it won't help much, since the real commands that caused this whole mess was core show hints and database show, both of which don't have a defined length. I defiantly have some scripts that needs a rewrite, so maybe it can be fixed by using other commands on my end.