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);
}
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.