#!/usr/bin/perl -w

use strict;

use Net::SOCKS;

sub usage {
    print "Usage:\n";
    print "\texample <socks_addr> <socks_port> <target_addr> <target_port>\n";
    print "to connect to <target_addr>:<target_port> through SOCKS5 at <socks_addr>:<socks_port>\n";
}

if ($#ARGV != 3) {
    &usage();
    exit(1);
}

my ($socks_addr, $socks_port, $target_addr, $target_port) = @ARGV;

print "Attempting to connect to $target_addr at port $target_port using the socks\n";
print "server at $socks_addr port $socks_port\n";

my $sock = new Net::SOCKS(socks_addr => $socks_addr,
		socks_port => $socks_port,
		#user_id => 'the_user',
		#user_password => 'the_password',
		#force_nonanonymous => 1, 
		protocol_version => 5);
my $f= $sock->connect(peer_addr => $target_addr, peer_port => $target_port);
print "connect status: ",
	Net::SOCKS::status_message($sock->param('status_num')), "\n";

if ($sock->param('status_num') == SOCKS_OKAY) {
  print $f "clintdw\n";
  while (<$f>) { print }
  $sock->close();
}

print "Attempting to listen() using the server at $socks_addr port $socks_port\n";

$sock = new Net::SOCKS(socks_addr => $socks_addr,
		socks_port => $socks_port,
		#user_id => 'the_user',
		#user_password => 'the_password',
		#force_nonanonymous => 1, 
		protocol_version => 5);

# We expect a connection from $target_addr:9999
# This is just for the sake of testing that the SOCKS proxy lets us
# listen: we of course won't initiate a connection from 
# $target_addr:9999 (and in the majoirty of cases, we simply *can not*,
# since $target_addr is likely to be remote)
# --Seb
my ($ip, $ip_dot_dec, $port) = $sock->bind(peer_addr => $target_addr,
					   peer_port => 9999);
print "bind status: ",
	Net::SOCKS::status_message($sock->param('status_num')), "\n";

if ($sock->param('status_num') == SOCKS_OKAY) {
  print "Listening at the IP of ", $ip_dot_dec, " at port ", $port, "\n";

# Following is commented out, according to comment above.
#  $f= $sock->accept();
#}
#print "accept status: ", 
#	Net::SOCKS::status_message($sock->param('status_num')), "\n";
#
#if ($sock->param('status_num') == SOCKS_OKAY) {
#  while (<$f>) { print }
#}
}
$sock->close();

