pg has asked for the wisdom of the Perl Monks concerning the following question:
I wrote this for myself, to stop those garbages on internet and speed up, by not forwarding certain requests.
It works and satisfied my needs, however core dumps once a while. Can anyone spot anything, or just pure guess? Your help is greatly apprecaited.
use IO::Socket::INET; use threads; use strict; use warnings; my $banned_type = { "cab" => 1, "class" => 1, "dat" => 1, "exe" => 1, "gif" => 1, "js" => 1, "swf" => 1 }; my $banned_site = { "ad.doubleclick.net" => 1, "search-itnow.com" => 1, "www.ftstock.com" => 1, "www.newshub.com" => 1 }; use constant RES_400 => "HTTP/1.1 400 Bad Request\r\n\r\n"; use constant LISTEN_SIZE => 20; use constant LISTEN_PORT => 8080; use constant MAX_REQ_SIZE => 5000; my $browser_listener = new IO::Socket::INET(Proto => "tcp", LocalAddr => "localhost", LocalPort => LISTEN_PORT, Resue => 1, Listen => LISTEN_SIZE) || die "Failed to initialize browser listener socket +\n"; print "Internet filter started\n"; while (1) { my $browser = $browser_listener->accept(); if ($browser) { my $req; sysread($browser, $req, MAX_REQ_SIZE); my $host = ($req =~ m/Host:\s*(.*?)\r/)[0]; my ($method, $page) = ($req =~ m/^(.*?)\s+(.*?)\s/); print "Received request for [$host, $page]\n"; if ($method eq "GET") { if ($host && $page) { if (is_banned_site($host) || is_banned_type($page)) { print "[$host, $page] is banned\n"; print $browser RES_400; close($browser); } else { threads->create(\&process_one_req, $browser, $req, + $host); } } else { open(BUG, ">bug.txt"); print BUG $req; close(BUG); close($browser); } } else { threads->create(\&process_one_req, $browser, $req, $host); } } else { print "Bad accept, skip\n"; } } sub process_one_req { my ($browser, $req, $host) = @_; my $remote = new IO::Socket::INET(Proto => "tcp", PeerAddr => $hos +t, PeerPort => 80); if ($remote) { print $remote $req; my $chunk; print $browser $chunk while (sysread($remote, $chunk, 10000)); close($remote); undef($remote); } else { print $browser RES_400; } close($browser); undef($browser); } sub is_banned_site { my $site = shift; return 1 if (exists($banned_site->{$site})); if ($site =~ m/offeroptimizer/) { return 1; } if ($site =~ m/revenue.net$/) { return 1; } if ($site =~ m/popupsponsor.com$/) { return 1; } return 0; } sub is_banned_type { return (exists($banned_type->{(split(/\./, shift))[-1]})) ? 1 : 0; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: another core dump
by BrowserUk (Patriarch) on Oct 25, 2003 at 05:09 UTC | |
by pg (Canon) on Oct 25, 2003 at 05:33 UTC | |
by BrowserUk (Patriarch) on Oct 25, 2003 at 06:07 UTC | |
|
Re: another core dump
by pg (Canon) on Oct 25, 2003 at 07:26 UTC |