nat traverse

Discussion in 'Perl Misc' started by Larry, Nov 5, 2007.

  1. Larry

    Larry Guest

    Hi,

    I've found this script script that I've found very usefull
    (http://linide.sourceforge.net/nat-traverse/), yet I'd love to use nat
    traverse to set up a udp tunnel to make a browser on host A to connect
    to apache listening on HOST B. Both HOST A and HOST B are behind NAT.

    ** on host A I have this:

    perl nat-traverse --cmd="nc -vlp 65000" 40000:host B:40001

    Now, "nc" is bound to nat-traverse...

    I use my browser to connect to 127.0.0.1:65000 ("nc" gets tha request
    and sends it to the UDP tunnel...than waits for the response from he
    tunnel)

    ** on HOST B I have the following:

    perl nat-traverse --cmd="perl mediator.pl" 40001:host A:40000

    mediator gets data from the UDP tunnel and make a req to apache
    (listening locally on port 80) than sends the apache response back to
    the tunnel...(whereas NC gets the response and send it to the browser)

    here's mediator.pl code:

    #!/usr/bin/perl

    use strict;
    use warnings;
    use IO::Socket::INET;
    use IO::Handle;
    STDOUT->autoflush();

    my $key;
    my %header;
    my $req;
    my $line;

    while(1)
    {
    chomp($line = <STDIN>);

    while( defined($_ = <STDIN>) )
    {
    s/[\r\n]+$//;
    last unless length $_;
    /^ ([\w\-]+) :[\ \t]+ (.+) $/x;
    $key = uc($1);
    $key =~ tr/-/_/;
    $header{$key} = $2
    }

    $req = "$line\n";

    foreach (sort keys %header)
    {
    $req .= $_ . ':' . " $header{$_}\n";
    }

    $req .= "\n";

    {
    my ($buff, $sock);
    $sock = IO::Socket::INET->new(PeerAddr => '127.0.0.1', PeerPort =>
    '80', Proto => 'tcp') || die "$!";
    $sock->autoflush(1);

    syswrite $sock, $req;

    while ( sysread($sock, $buff, 1024) )
    {
    print STDOUT $buff;
    }

    close($sock);
    }

    }

    __END__;

    tha thing with the whole above scenario is that "nc" exits when the
    browser closes the connection...

    Does anyone how to sort this out??

    How can I bound STDIN e STDOUT to fifo files??

    thanks ever so much
    Larry, Nov 5, 2007
    #1
    1. Advertising

  2. Larry

    Peter Wyzl Guest

    "Larry" <> wrote in message
    news:...
    > Hi,
    >
    > I've found this script script that I've found very usefull
    > (http://linide.sourceforge.net/nat-traverse/), yet I'd love to use nat
    > traverse to set up a udp tunnel to make a browser on host A to connect
    > to apache listening on HOST B. Both HOST A and HOST B are behind NAT.
    >
    > ** on host A I have this:
    >
    > perl nat-traverse --cmd="nc -vlp 65000" 40000:host B:40001
    >
    > Now, "nc" is bound to nat-traverse...
    >
    > I use my browser to connect to 127.0.0.1:65000 ("nc" gets tha request
    > and sends it to the UDP tunnel...than waits for the response from he
    > tunnel)
    >
    > ** on HOST B I have the following:
    >
    > perl nat-traverse --cmd="perl mediator.pl" 40001:host A:40000
    >
    > mediator gets data from the UDP tunnel and make a req to apache
    > (listening locally on port 80) than sends the apache response back to
    > the tunnel...(whereas NC gets the response and send it to the browser)
    >


    <snip>

    > tha thing with the whole above scenario is that "nc" exits when the
    > browser closes the connection...
    >
    > Does anyone how to sort this out??
    >
    > How can I bound STDIN e STDOUT to fifo files??


    Looks like you need to have a way to have the 'server' end fork another
    socket for the next request. Check the technique for a preforking proxy
    written by Randal Schwartz in one of his columns here:

    http://www.stonehenge.com/merlyn/WebTechniques/col34.html

    You should be able to adapt that technique, i.e you don't need the
    compression stuff, and this assumes your OS supports forking.

    --
    P
    Peter Wyzl, Nov 7, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jo Voordeckers
    Replies:
    2
    Views:
    855
  2. Roedy Green

    Understanding NAT, Firewalls, TCP/IP

    Roedy Green, Jan 11, 2006, in forum: Java
    Replies:
    26
    Views:
    948
    Alan Krueger
    Jan 14, 2006
  3. tobias.weih

    RMI app behind NAT firewall

    tobias.weih, Jan 25, 2006, in forum: Java
    Replies:
    1
    Views:
    4,817
    tobias.weih
    Jan 30, 2006
  4. Janto Dreijer

    UDP packets to PC behind NAT

    Janto Dreijer, Sep 15, 2006, in forum: Python
    Replies:
    12
    Views:
    876
    John J. Lee
    Sep 17, 2006
  5. polaris

    rmi application thru NAT

    polaris, Oct 29, 2006, in forum: Java
    Replies:
    1
    Views:
    587
    Tom Forsmo
    Oct 31, 2006
Loading...

Share This Page