#!/usr/bin/perl -w # tunnel.pl # # Usage: tunnel.pl telnet-proxy telnet-proxy-port destination-host destination-port # # Ssh-tunnel over a telnet proxy. # # Written for HTTP CONNECT by Urban Kaveus , # modified by Simon Josefsson June 2000. if ( $#ARGV != 3 ) { print STDERR "Usage: $0 ssl-proxy port destination port\n"; exit(1); } $AF_INET = 2; if ($ENV{"OSTYPE"} eq "SunOS5") { $SOCK_STREAM = 2; } else { $SOCK_STREAM = 1; } $sslproxy = shift; $proxyport = shift; $destination = shift; $destport = shift; # Set up network communication ($protocol) = (getprotobyname("tcp"))[2]; ($proxyip) = (gethostbyname($sslproxy))[4]; $localaddr = pack('S n a4 x8', $AF_INET, 0, "\0\0\0\0"); $proxyaddr = pack('S n a4 x8', $AF_INET, $proxyport, $proxyip); socket (PROXY, $AF_INET, $SOCK_STREAM, $protocol) or die("Failed to create socket: $!\n"); bind (PROXY, $localaddr) or die("Failed to bind socket"); connect (PROXY, $proxyaddr) or die("Failed to connect to $sslproxy port $proxyport"); # Force flushing of socket buffers select (PROXY); $| = 1; select (STDOUT); $| = 1; print PROXY "CONNECT $destination $destport\r\n"; while() { chomp; # Strip last if /^Connected/; } # Start copying packets in both directions. if($child = fork) { # Parent process while (sysread(STDIN,$_,4096)) { print PROXY; } sleep 2; kill(15,$child) if $child; } else { # Child process while (sysread(PROXY,$_,4096)) { print STDOUT; } }