I'm trying to create one transparent HTTP proxy. It's purpose is to stay between the browser and the web server and be invisible. Here is the code I'm using. Unfortunately it's not working very well. When I open the web page (referenced by $base_uri
) there are different results depending on that whether I've opened it using the browser only or the browser and the proxy. I'm trying it on a web site which is returning all kinds of responses including "transfer-encoding: chunked
" (so I guess may be the problem could be there?!). I think that there could be also problems with the cookies but I don't know how to solve them (if any...).
#!/usr/bin/perl
use strict;
use HTTP::Daemon;
use LWP::UserAgent;
use HTTP::Cookies;
my $cookie_jar = HTTP::Cookies->new();
my $ua = LWP::UserAgent->new( max_redirect => 0, env_proxy => 0,keep_alive => 1, timeout => 30, agent => "Mozilla/4.76 [en] (Win98; U)");
my $d = HTTP::Daemon->new(
LocalHost => "localhost", # remove this to listen from other machines
# (i.e. open-relay... be careful of spammers!)
LocalPort => 33331
) || die;
print "[Proxy URL:", $d->url, "]\n";
fork(); fork(); fork(); # 2^3 = 8 processes
$ua->cookie_jar($cookie_jar);
my $base_uri = 'http://example.com/';
while (my $c = $d->accept) {
while (my $request = $c->get_request) {
my $uri = $base_uri . $request->uri->as_string();
my $method = $request->method;
my $req = HTTP::Request->new($method, $uri);
$request->uri($uri);
print "[[ $method >> $uri ]]\n";
my $response = $ua->simple_request($request);
$c->send_response( $response );
}
$c->close;
undef($c);
}
Thank you in advance!