Dear All,

  I want to write a bounce server that sits on a machine, and when a client
connects to it, the server connects to a remote server and acts as a
gateway between the two computers.  Everything the remote server says gets
told to the client connected to the bounce server, and everything the
client says gets told to the remote server.
  I wrote a real quickie thing, and was wondering if this would work, or if
this is how you'd do it.  I am good at doing batch stuff back and forth
from server to client, but to make it closely interact, getting variable
amount of stuff back and forth, I'm kinda sketchy on.
   Here is what I have.  Any help would be greatly appreciated: (sorry
about the poor syntax, it is was a huge rush-job.)

- Adam Stern

#!/opt/local/GNU/bin/perl

use strict;
BEGIN { $ENV{PATH} = '/usr/ucb:/bin' }
use Socket;
use Carp;

sub server {
  my $port = 80;
  my $proto = getprotobyname('tcp');
  $port = $1 if $port =~ /(\d+)/; # untaint port number

  socket(Server, PF_INET, SOCK_STREAM, $proto)        || die "socket: $!";
  bind(Server, sockaddr_in($port, INADDR_ANY))        || die "bind: $!";
  listen(Server,SOMAXCONN)                            || die "listen: $!";

  logmsg "server started on port $port";
  my $paddr;

  $SIG{CHLD} = \&REAPER;

  for ( ; $paddr = accept(Client,Server); close Client) {
    my($port,$iaddr) = sockaddr_in($paddr);
    my $name = gethostbyaddr($iaddr,AF_INET);

    $host = "www.somewebserver.com";
    $remote = IO::Socket::INET->new( Proto     => "tcp",
                                   PeerAddr  => $host,
                                   PeerPort  => "http(80)",
                                 );
    unless ($remote) { die "cannot connect to http daemon on $host" }
    $remote->autoflush(1);

      while (<Client>) {
        push(@cli_in,$_);
      }
      while(<$remote>) {
        push(@serv_in,$_);
      }
      print $remote @cli_in;
      print Client @serv_in;
      @cli_in = ("");
      @serv_in = ("");
    }
    close $remote;
  }
}

server();


Reply via email to