http://qs321.pair.com?node_id=208968

I needed a quick script to provide 'Anti-Leeching' for some of my files. (Leeching is a method of stealing someone else's bandwidth by linking to their files instead of downloading the files to your own site.)

After several iterations, I finally have something that works pretty well for my .zip files.

Just put the hostname and the IP in the @HOSTS array for all your hosts that will use this script. As merlyn mentioned in one of his articles, everyone has a drawer where they keep miscellaneous items, this one belongs in your virtual drawer for that time when you need a quick anti-leech sub.

--
hiseldl
What time is it? It's Camel Time!

Update: changed grep pattern because of merlyn's comment about reverse DNS. Thanks merlyn!

#!/usr/bin/perl -w use strict; use CGI qw/:standard/; $|=1; my $DIR = param('d') || './'; # default dir my $FILE = param('f') || 'xyz.zip'; # default file my @HOSTS = ( 'www.myotherhost.com', '10.0.0.2', 'www.myhost.com', '10.0.0.1', ); print header('text/plain'), "No access to $FILE" unless DownloadFile($DIR, $FILE, \@HOSTS); exit 0; sub DownloadFile { my ($dir, $filename, $hosts) = @_; my $remote = remote_host(); #### the following is bad because it is not an exact #### match nor is it anchored #### return(0) unless grep /$remote/, @$hosts; # the following suggested by [merlyn] return 0 unless grep $remote eq $_, @$hosts; my $filesize = -s "$dir/$filename"; # print full header print "Content-disposition: filename=$filename\n"; print "Content-Length: $filesize\n"; print "Content-Type: application/octet-stream\n\n"; # open in binmode open(READ,$filename) || die; binmode READ; # stream it out binmode STDOUT; while (<READ>) { print; } close(READ); # should always return true return(1); }