-   Linux - Newbie (
-   -   black hole proxy perl script (

thegeekster 12-17-2003 02:58 AM

black hole proxy perl script
How do I invoke a perl scipt on startup? Is it merely a matter of adding it to the rc.local (Slackware) startup file, or is there more to it? And once it's started, how would I check to see which port it is listening on?

I like using John LoVerso's no-ads.pac for filtering unwanted sites from the intenet, which routes such request to what he calls a black hole proxy. This proxy can be nothing more than a localhost port (default is localhost:3421) that is unused and works fine for most of the major browsers, except Opera seems to be unable to use the no-ads file as is. I think it needs an proxy that responds to such requests instead of merely timing out.

John has a noproxy script which has instructions for seting it up, and also mentions a perl script called written by Sean Burke which I would like to try out.. However, this perl script doesn't have any instructions a n00b can use for implementing it.

Any help much appreciated :)

ToniT 12-17-2003 03:36 AM

1. yes, just add it to your rc.local, there is nothing more in it.
2. lsof -i

thegeekster 12-17-2003 04:30 AM

Great..........thanx, ToniT :)

thegeekster 12-17-2003 09:14 PM

doesn't work from rc.local
Well, that perl script didn't work by just entering it in rc.local, or even just invoking it from a command line. I tried it without args and with args. It will accept port numbers as args, but all it does is.........nothing. Doesn't even return the command prompt, but just hangs forever.

So I tried using it from inetd.conf, like the instructions in the noproxy script and substituting the path, like so:

3421        stream        tcp        nowait        nobody        /usr/local/bin/        noproxy
Then finding the PID for inetd with the 'ps -x' command so i can restart inetd with the kill command:

kill -HUP <PID of inetd>
This seemed to work, but Opera still doesn't like the no-ads.pac autoconfig proxy file (it stills tends to hang for quite a while). Oh well, guess I'll have to stick with using Privoxy with Opera for filtering ads.......

ToniT 12-17-2003 09:28 PM

How do you separate a case when a program 'does nothing' from a case when a program 'waits for a connection to the port' or from a case when a program 'waits for input from the stdin'? ;)

(Well, either by looking the source or checking with lsof -i, but you didn't mention using it in this particular case)

thegeekster 12-18-2003 05:18 AM

Actually, ToniT, you were a big help for me.....I learned about the lsof command, and after readng the man pages, got a better idea on its uses.......Usually, this is all it takes for me, a pointer in the right direction and then I can go from there. :)

I did read the source file for the perl script, but it didn't tell me much, as you can see (not commented), and I'm not familiar with the perl language:

# Time-stamp: "2002-02-22 14:01:52 MST"
# Black-hole proxy server.

use strict;
use IO::Socket qw(: DEFAULT :crlf);
use constant MY_PORT => 9123;
use constant DEBUG => 0;

my $image_type = 'image/gif';
my $image = "$ENV{'HOME'}/public_html/null.gif";

    if(PrivoxyWindowOpen(IN, "<$image")) {
        local $/;
        $image = join '',
          "HTTP/1.0 200 OK", CRLF,
          "Content-Type: ", $image_type, CRLF, CRLF,
    } else {
        # Default image:
        $image = join '',
          "HTTP/1.0 200 OK", CRLF,
          "Content-Type: ", $image_type, CRLF, CRLF,
          # 8x8 image: box with a border.
          "\x40\x40\x40", # Inside box color
          "\x00\x00\x00", # Outside border color
          # Actual gif data:

my $quit = 0;
$SIG{'INT'} = sub {$quit = 1};

my $sock = IO::Socket::INET->new(
  Listen => 20,        LocalPort => shift(@ARGV)|| MY_PORT,
  Timeout => 60 * 60,  Reuse => 1,
) or die "Can't create listening socket: $!\n";

DEBUG and warn "Waiting for connections...\n";

my($session, $peer, $port);
while(!$quit) {
  next unless my $session = $sock->accept;
  if(DEBUG) {
    $peer = gethostbyaddr($session->peeraddr, AF_INET)
    || $session->peerhost;
    $port = $session->peerport;
    warn "Connection from [$peer,$port] at ", scalar(localtime), "\n";

  print $session $image;
  DEBUG and print " (Closed)\n";

DEBUG and print STDERR "Byebye\n";
exit 0;

I opened up the no-ads.pac file again (which is very well commented) and reread the section on black hole proxies and noticed that this perl program is a 'wait' style script, which I interpret to mean that when invoked it will wait for input. But how do I use it, I kept asking myself? Here's the relevant part I'm talking about:

//  b. A simple, blackhole server
//      When needed, I run a simple "server" at port 3421 that denies
//      all requests.  Some options you can use for this:
//      - On Windows, you can try Larry Wang's black-hole proxy program:
//        I can not vouch that his binaries are virus free, but he does
//        offer the source code.
//      - I use this shell script on UNIX; it is invoked via inetd.
//        /usr/local/lib/noproxy:
//          #!/bin/sh
//          read a
//          read b
//          echo HTTP/1.0 501 No Ads Accepted
//          echo ""
//          exit
//        Add this line to inetd.conf ('kill -HUP' inetd afterwards):
//          3421 stream tcp nowait nobody /usr/local/lib/noproxy noproxy
//        This simple script doesn't work on Linux because of the
//        (IMHO) broken way it's TCP stack works.  See the bottom of
// for a complete copy
//        of the `noproxy' shell script.
//        If always exec'ing a shell was expensive on your computer
//        (it isn't on mine), then you could use a "wait"-style Perl
//        script that would accept() incoming connections.
//      - Sean Burke has a black-hole proxy written in Perl script:
//        (This is a standalone server, not run from inetd).

So I'm guessing this to mean that it should be invoked in the variable defined by the no-ads.pac file, like so:

// ***
// *** Update the next line with the correct hostname: port
// *** of your blackhole proxy server, if needed.
// ***
// *** (If you use IE, this is already fine)
// ***
// var blackhole = "PROXY";    // Original entry
 var blackhole = "PROXY";

But I'm only guessing here, and at this point I've determined that Opera just doesn't like no-ads.pac, period. However, it would be nice to know how to properly use that perl script, since the noproxy shell script written by John LoVerso doesn't work as well on Linux like it does on Unix.

So I'm still looking into the matter, and I've still got a long way to go in figuring out the Linux/Unix way of how things get done, but you did help me a lot, ToniT :)

PS: I had to edit the code sections in a couple of places, namely putting a space between any colon-letter combo, to get rid of any smiley faces ;)

All times are GMT -5. The time now is 01:21 PM.