#!/usr/bin/perl

sub dokill {exit;return 1;}
sub parseURL {local $newsite, $newprotocol, $newhost, $newport, $newrest, $newlocation, $newurl, $newrelative;
	$newurl=$_[0];$newurl=~s#"##g;
	($newprotocol, $newrest)=split('://',$newurl,2);
	if ($newrest eq "")
		{$newrest=$newprotocol;$newprotocol="http://";$newport=80;$newrelative=1;}
	else	{$newprotocol="$newprotocol://"; $newrelative=0;
		($newsite, $newlocation)=split('/',$newrest,2);}
	($newhost,$newport)=split(":",$newsite,2);
	($newport,$newcrap)=split(":",$newport,2);
	return ($newrelative, $newprotocol, $newhost, $newport, $newlocation);
}

sub tcpget {
$them=$_[0]; $port=$_[1]; $MSG=$_[2]; $AF_INET = 2; $SOCK_STREAM = 1; $SIG{'INT'} = 'dokill';
$sockaddr='S n a4 x8';                          # how we unpack packet headers for now
chop($hostname=`hostname`);
($name, $aliases, $proto) = getprotobyname('tcp');      # TCP protocol  
($name, $aliases, $port) = getservbyname($port, 'tcp') unless $port =~ /^\d+$/;;        # TCP protocol
($name, $aliases, $type, $len, $thisaddr) = gethostbyname($hostname);
($name, $aliases, $type, $len, $thataddr) = gethostbyname($them);
$this=pack($sockaddr, $AF_INET, 0, $thisaddr);  # setup for bind
$that=pack($sockaddr, $AF_INET, $port, $thataddr);      # setup for bind
socket(S, $AF_INET, $SOCK_STREAM, $proto) || die "socket $!";
bind(S,$this) || die "bind on $port $port: $!";         # bind to the socket or die trying
connect(S, $that) || die "Connect: $!\n";
select(S); $|=1; select(stdout);                        # every byte as it comes in
local $r="";
if ($child = fork) {while (<S>) {$r=$r.$_;} return $r;}
	print S "$MSG"; sleep 2; dokill($child); return 1;
}       

$url=@ARGV[0];
($relative, $protocol, $host, $port, $location)=parseURL($url);
if ($relative == 1) {print "<h2>$url</h2>\n<h1>We need to start with an absolute URL!</h1>\n";}
else	{if ($port eq "") {$port=80;}}
#	print "<h1>Fetching URL $url ($protocol$host:$port:/$location) </h1>\n";
$result=tcpget($host, $port, "GET /$location HTTP/1.0\nHost:$host\nReferer: http://all.net/index.html\nUser-Agent: \"The Security Experts at All.Net\"\n\n");
print "$result";
