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); }
|
---|
Replies are listed 'Best First'. | |
---|---|
•Re: anti leech CGI
by merlyn (Sage) on Oct 30, 2002 at 07:06 UTC | |
by zigdon (Deacon) on Oct 30, 2002 at 13:24 UTC | |
by merlyn (Sage) on Oct 30, 2002 at 15:41 UTC | |
by hiseldl (Priest) on Oct 30, 2002 at 15:42 UTC | |
by merlyn (Sage) on Oct 30, 2002 at 15:56 UTC | |
Re: (Funny) anti leech CGI
by shotgunefx (Parson) on Oct 30, 2002 at 10:10 UTC | |
Re: anti leech CGI
by erasei (Pilgrim) on Oct 30, 2002 at 14:21 UTC | |
by hiseldl (Priest) on Oct 30, 2002 at 15:08 UTC | |
Re: anti leech CGI
by hacker (Priest) on Oct 30, 2002 at 14:54 UTC | |
by hiseldl (Priest) on Oct 30, 2002 at 15:13 UTC |
Back to
Cool Uses for Perl