#!/usr/bin/perl -w
#
# looking for:
# Proto Recv-Q Send-Q Local Address    Foreign Address       State       PID/Program name
# tcp        0   9877 a.b.c.d:4662     w.x.y.z:1250          ESTABLISHED 1234/amule 
#
# 20060222 (c) Ewald...
#
# This program will kill incomming connections on port 4662
# You have to keep the firewall open for this port otherwise you
# get LowID and are effectively not able to download stuff yourself
# However since uploading is illegal under Dutch legislation we
# terminate all upload attemps.

use Socket;
my $CNT = 0;
my $LOOP = 0;
my $NAME = "";
my $DEBUG = 0;
# find out your ip-address and change it below:
## my $MYIP = "10.0.0.1";
my $PORT = "4662";
my $Q; my $IP;

$SIG{INT} = \&gotyou;	# catch CTRL-C

sub gotyou {	# CTRL-C received
	$SIG{INT} = \&gotyou;
	printf "\nterminating, %s leechers zapped:\n",$CNT;
	foreach $x (keys %MEM) {
		printf "%s\t%s\n",$x,$MEM{$x};
		}
	printf "cleaning up...\n";
	system("killall tcpkill") == 0 or die "killall tcpkill failed: $?";
	exit 0;
	}

if ($ARGV[0] =~ /\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/) {
	printf "Whitelisting %s...\n",$ARGV[0];
	$MEM{$ARGV[0]} = 1;
	}
elsif($ARGV[0]) {
	printf "This: $ARGV[0] doesn't look like an ip-address, exiting...\n";
	exit 1;
	}

while(1) {	# do untill doomesday

  my @ns=`netstat -tupan`; # get connection data
  $DEBUG and print @ns;

  foreach $line (@ns) {
#	if ($line =~ /^tcp\s+\d+\s+(\d+)\s+$MYIP\S+\s+([\d\.]+?):.*ESTABLISHED.*amule/o) {
	if ($line =~ /^tcp\s+0\s+(\d+)\s+$MYIP:$PORT\s+([\d\.]+?):.*ESTABLISHED.*amule/o) {
		$Q  = $1; # send queue (when it's not zero we are sending out data)
		$IP = $2; # ip-address of the other party
		if ($Q > 0) {
		        $NAME = gethostbyaddr(inet_aton($IP), AF_INET) or $NAME="unkown";
			printf "\nConnected with %s\t%s\n",$IP,$NAME;
			if (!($MEM{$IP})) {
				$CNT++;
				print "New leecher!\n";
				$MEM{$IP}=$NAME;
				printf "zapping downloader %s...\n",$IP;
                		system("tcpkill host $IP &") == 0 or die "system tcpkill host $IP failed: $?";
				}
			}
		} #if $line...
	elsif ($line =~ /^tcp\s+0\s+(\d+)\s+$MYIP:\d+\s+([\d\.]+?):.*ESTABLISHED.*amule/o) {
		$Q  = $1; # send queue (when the leecher does not connect in to our server)
		$IP = $2;
		if ($Q > 5000) {
		        $NAME = gethostbyaddr(inet_aton($IP), AF_INET) or $NAME="unkown";
			printf "\nConnected out to %s\t%s\n",$IP,$NAME;
			if (!($MEM{$IP})) {
				$CNT++;
				print "New leecher!\n";
				$MEM{$IP}=$NAME;
				printf "zapping downloader %s...\n",$IP;
                		system("tcpkill host $IP &") == 0 or die "system tcpkill host $IP failed: $?";
				}
			}
		} # elsif
	elsif ($line =~ /^tcp\s+(\d\d+)\s+\d+\s+$MYIP:\S+\s+([\d\.]+?):.*ESTABLISHED.*amule/o) {
		$Q  = $1; # receive queue
		$IP = $2;
		$Q > 0 and $MEM{$IP}="whitelisted";	# we are receiving from this guy, so lets not block him
		} # elsif
	} #foreach

  printf ".";
  if ($LOOP++ > 40) {
	printf "\n";
	$LOOP=0;
	}
  sleep 10;

  } #while

printf "%s zapped...\n",$CNT;
exit 0;
