Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Perl -> Net::XMPP::Client connecting via proxy

Options
  • 08-05-2008 5:07pm
    #1
    Registered Users Posts: 545 ✭✭✭


    Heyho,

    I've got a small problem that I can't seem to find a solution for. I want to connect to google talk using a small XMPP-based perl script, but i want to do so via a proxy. According to the docs, it can be done, but not an example to be seen anywhere...

    Heres the relevant snippet:
    use Net::XMPP;
    
    my $hostname = 'talk.google.com';
    my $port = 5222;
    my $componentname = 'gmail.com';
    my $connectiontype = 'tcpip';
    my $tls = 1;
    
    my $Connection = new Net::XMPP::Client();
    
    # Connect to talk.google.com
    my $status = $Connection->Connect(hostname => $hostname, port => $port,
                                      componentname => $componentname,
                                      connectiontype => $connectiontype, tls => $tls); 
    
    if (! defined($status)){
        die "Can't connect to gmail - $! ...\n";
    }
    

    long shot: anyone had to do this, or want to make a stab at how to make this code use a proxy? The docs only say that setting $connectiontype to 'http' enables the headers needed to use a proxy...

    Link to docs:
    http://search.cpan.org/~hacker/Net-XMPP-1.02/lib/Net/XMPP/Client.pm


Comments

  • Registered Users Posts: 2,931 ✭✭✭Ginger


    What proxy are you going through .. and does it require authentication?


  • Registered Users Posts: 545 ✭✭✭ravydavygravy


    Ginger wrote: »
    What proxy are you going through .. and does it require authentication?

    I need the solution to to proxy-independant, but in my case it's squid. No proxy auth required.


  • Registered Users Posts: 2,931 ✭✭✭Ginger


    Have you allowed the port access out so that it can work.. as in removed the proxy from the equation and checked to see if the code works.

    What errors are you getting


  • Registered Users Posts: 545 ✭✭✭ravydavygravy


    The code works fine - thats not the problem - i need/want to change it to work through a proxy.


  • Registered Users Posts: 2,931 ✭✭✭Ginger


    Ah right .. jeez is that all

    Change my $connectiontype = 'tcpip'; to my $connectiontype = 'http';

    As per the docs


  • Advertisement
  • Registered Users Posts: 545 ✭✭✭ravydavygravy


    I've tried that, but that obviously can't be the solution - there needs to be code to send the connection via the proxy. At the moment, the code has no referance to the proxy at all.


  • Registered Users Posts: 2,931 ✭✭✭Ginger


    Have you tried the address of your web proxy with the port number such as http://proxy:8080


  • Registered Users Posts: 545 ✭✭✭ravydavygravy


    yes, but thats no good either - how does the proxy know where to send your connection request?

    The initial point is: There is some extra code required to send the XMPP traffic through a proxy, but I can't for the life of me figure out what. The XMPP docs don't elaborate on whats required of the developer, and any example I can find avoids the problem by not using a proxy.

    So, my question is: does anyone have any idea how to tell the Connect() attempt to go through a proxy, as opposed to trying a direct attempt. I've read the Net::XMPP::Connection module code, to see if it was an undocumented option, but no joy...

    Setting the http_proxy enviroment variable doesn't work either btw, but even if it did, that would be a crappy solution.


  • Registered Users Posts: 2,931 ✭✭✭Ginger


    Can you show me you connection code, as in the code snippet that shows the connection details and your connection attempt


  • Registered Users Posts: 545 ✭✭✭ravydavygravy


    Ginger wrote: »
    Can you show me you connection code, as in the code snippet that shows the connection details and your connection attempt

    its in the initial post?


  • Advertisement
  • Registered Users Posts: 6,509 ✭✭✭daymobrew


    Setting the http_proxy enviroment variable doesn't work either btw, but even if it did, that would be a crappy solution.
    Why?

    You could try emailing the author.


  • Registered Users Posts: 545 ✭✭✭ravydavygravy


    I'd rather have the solution all in the code, rather than having to rely on an external factor like an enviroment variable - for example, if the script got run as a different user, it could then fall over if they didn't have the variable set in their shell.

    I think i'll have to email the guy. It seems to be a pretty obscure problem, couldn't find any help on specialist perl-related forums either.

    If i find a solution, I'll post it here to wrap up the thread.


  • Registered Users Posts: 2,931 ✭✭✭Ginger


    Your initial code is for tcpip.. can i see the one with the proxy


  • Registered Users Posts: 545 ✭✭✭ravydavygravy


    Ginger wrote: »
    Your initial code is for tcpip.. can i see the one with the proxy

    This is the problem - I don't know what code to wrap around this to make the connect() call use a proxy.

    I've emailed the guy who wrote the module, maybe he can elaborate on what one is supposed to do.


  • Registered Users Posts: 2,931 ✭✭✭Ginger


    Ok.. see what I was saying was taht the variable connection type determines if its using a proxy or not, so setting it to the value of your http proxy should do the trick as its abstracted by the code..

    So .. did you change the connectiontype to "http://server:port" or just put in the IP?


  • Registered Users Posts: 545 ✭✭✭ravydavygravy


    Ok, i finally figured it out.

    heres the code:
    use strict;
    use Net::XMPP;
    
    ### Set enviroment variable in the script so user doesn't care about it...
    $ENV{'https_proxy'} = 'http://10.100.22.69:8080';
    
    ## Google Talk & Jabber parameters :
    my $hostname = 'talk.google.com';
    my $port = 443;
    my $componentname = 'gmail.com';
    my $connectiontype = 'http';
    my $tls = 0;
    my $ssl = 1;
    
    my $Connection = new Net::XMPP::Client();
    
    print "Attempting to connect\n";
    
    # Connect to talk.google.com
    my $status = $Connection->Connect(hostname => $hostname, 
                                      port => $port,
                                      componentname => $componentname,
                                      connectiontype => $connectiontype, 
                                      tls => $tls,
                                      ssl => $ssl); 
    
    if (! defined($status))
    {
        die "Can't connect to gmail - $! ...\n";
    }
    
    print "Connected!";
    

    So to make it work i had to do the following:

    1) set the https_proxy variable, not http_proxy
    2) use the https port (443) instead of 5222
    3) enable SSL connection using undocumented ssl arg to connect()

    The port switch was necessary because most proxies are configured to not allow http traffic on ports other than 80. I guess this is why it didn't work when i was just using http_proxy

    To figure it out, it was very helpful to edit the Net/XMPP/Connection.pm module file and up the debug level from '-1' to '2' - it spews out all the dirty details then....


  • Registered Users Posts: 6,509 ✭✭✭daymobrew


    I'd rather have the solution all in the code, rather than having to rely on an external factor like an enviroment variable - for example, if the script got run as a different user, it could then fall over if they didn't have the variable set in their shell.
    You could always have a default in the code.


  • Registered Users Posts: 545 ✭✭✭ravydavygravy


    yeah, thats kinda what I did in the end. I set the https_proxy enviroment variable in the code, to avoid the need for the user to bother about it. A small change whoul let the users own setting override the one in the code, something like:
    $ENV{'https_proxy'} = 'http://10.100.22.69:8080' if (!defined($ENV{'https_proxy'}));
    


Advertisement