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); }

In reply to anti leech CGI by hiseldl

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.