LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 12-17-2003, 01:58 AM   #1
thegeekster
Member
 
Registered: Dec 2003
Location: USA (Pacific coast)
Distribution: Vector 5.8-SOHO, FreeBSD 6.2
Posts: 513

Rep: Reputation: 33
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 black_hole_http_server.pl 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
 
Old 12-17-2003, 02:36 AM   #2
ToniT
Senior Member
 
Registered: Oct 2003
Location: Zurich, Switzerland
Distribution: Debian/unstable
Posts: 1,357

Rep: Reputation: 47
1. yes, just add it to your rc.local, there is nothing more in it.
2. lsof -i
 
Old 12-17-2003, 03:30 AM   #3
thegeekster
Member
 
Registered: Dec 2003
Location: USA (Pacific coast)
Distribution: Vector 5.8-SOHO, FreeBSD 6.2
Posts: 513

Original Poster
Rep: Reputation: 33
Great..........thanx, ToniT
 
Old 12-17-2003, 08:14 PM   #4
thegeekster
Member
 
Registered: Dec 2003
Location: USA (Pacific coast)
Distribution: Vector 5.8-SOHO, FreeBSD 6.2
Posts: 513

Original Poster
Rep: Reputation: 33
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:
Code:
3421	stream	tcp	nowait	nobody	/usr/local/bin/black_hole_http_server.pl	noproxy
Then finding the PID for inetd with the 'ps -x' command so i can restart inetd with the kill command:
Code:
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.......
 
Old 12-17-2003, 08:28 PM   #5
ToniT
Senior Member
 
Registered: Oct 2003
Location: Zurich, Switzerland
Distribution: Debian/unstable
Posts: 1,357

Rep: Reputation: 47
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)
 
Old 12-18-2003, 04:18 AM   #6
thegeekster
Member
 
Registered: Dec 2003
Location: USA (Pacific coast)
Distribution: Vector 5.8-SOHO, FreeBSD 6.2
Posts: 513

Original Poster
Rep: Reputation: 33
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:
Code:
#!/usr/bin/perl
# Time-stamp: "2002-02-22 14:01:52 MST"
#
# Black-hole proxy server.  sburke@cpan.org
#

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")) {
	binmode(IN);
	local $/;
	$image = join '',
	  "HTTP/1.0 200 OK", CRLF,
	  "Content-Type: ", $image_type, CRLF, CRLF,
	  <IN>;
	close(IN);
    } else {
        # Default image:
	$image = join '',
	  "HTTP/1.0 200 OK", CRLF,
	  "Content-Type: ", $image_type, CRLF, CRLF,
	  # 8x8 image: box with a border.
          "\x47\x49\x46\x38\x37\x61\x08\x00\x08\x00\xb3",
          "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
          "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
          "\x40\x40\x40", # Inside box color
          "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
          "\x00\x00\x00\x00\x00\x00\x00\x00\x00",
          "\x00\x00\x00", # Outside border color
          # Actual gif data:
          "\x2c\x00\x00\x00\x00\x08\x00\x08\x00\x00\x04\x0e\xf0\xc9\x49",
          "\xeb\x3c\xf8\xd0\xac\x6f\xde\x9f\x25\x52\x11\x00\x3b",
        ;
    }
}
#-----------------------------------------------------------------------------

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

  #select($session);
  #++$|;
  #select(STDOUT);
  
  print $session $image;
  close($session);
  DEBUG and print " (Closed)\n";
}

DEBUG and print STDERR "Byebye\n";
close($sock);
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:
Code:
//  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:
//
//          http://leisuresuit10.tripod.com/BlackHoleProxy/
//
//        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
//        http://www.schooner.com/~loverso/no-ads/ 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:
//
//        http://www.speech.cs.cmu.edu/~sburke...http_server.pl
//        (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:
Code:
// ***
// *** 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 127.0.0.1:3421";    // Original entry
 var blackhole = "PROXY 127.0.0.1:3421/usr/local/bin/blackhole_proxy.pl";
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

Last edited by thegeekster; 12-18-2003 at 04:22 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
perl shell proxy setup Garp Linux - Networking 2 10-05-2005 05:23 PM
Converting a Windows Perl script to a Linux Perl script. rubbercash Programming 2 07-19-2004 10:22 AM
Printing problems - strange black hole lynxgogo Linux - General 0 01-19-2004 02:43 PM
how to find the pid of a perl script from shell script toovato Linux - General 1 12-19-2003 06:25 PM
Including methods from a perl script into another perl script gene_gEnie Programming 3 01-31-2002 05:03 AM


All times are GMT -5. The time now is 02:38 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration