kazak has asked for the wisdom of the Perl Monks concerning the following question:

Hi 2 all. I'm trying to implement some sort of repeater for my needs. As a test appliance it should detect google captcha request in response on search request. I mean if request is:

http://www.google.com/search?=perlmonks

and search engine will respond

http://www.google.com/sorry/?continue=http://www.google.com/search%3Fq%3Fperlmonks

Custom proxy must:

1. Detect this response by "/sorry/"

2. Turn it into initial request again :

http://www.google.com/search?=perlmonks

3. Redirect client on

http://www.google.com/search?=perlmonks

I'm new to perl, so this code may contain mistakes:
#!/usr/bin/perl use strict; use warnings; use HTTP::Proxy qw( :log ); use HTTP::Proxy::HeaderFilter::simple; use LWP::UserAgent; my $ua = LWP::UserAgent->new(); $ua->proxy(['http'],'http://127.0.0.1:29999'); $ua->timeout(10); $ua->agent('Mozilla/5.0 (Windows NT 6.1) AppleWebKit/534.24 (KHTML, li +ke Gecko) Chrome/11.0.696.60 Safari/534.24'); #open ( LOGFILE, ">>", "/var/log/repeater.log"); my $fname = "/var/log/repeater.log"; open my $logfh, ">>", $fname or die "couldn't open '$fname': $!"; my $proxy = HTTP::Proxy->new( port => '38374', agent => $ua, logfh => $logfh, @ARGV ); #HTTP::Proxy->new(@ARGV); ### <------- Is there any method that will +allow to pass @ARGV or something else if new() method was used earlie +r. $proxy->logmask( ALL ); $proxy->push_filter( # host => 'google.com', # only apply to this domain response => HTTP::Proxy::HeaderFilter::simple->new( sub { my ( $s +elf, $headers, $response ) = @_; # skip non redirects return if $response->code !~ /^3/; # pick up location my $location = $headers->header('Location'); # find bad redirections if ( $location =~ m{\/sorry.*} ) { # change the redirect my $new_location = $location ; $new_location =~ s/.*(\/sorry\/\?continue=.*)/$1/gx ; $new_location =~ s/\/sorry\/\?continue=//; $headers->header( Location => $new_location ); # print some logging information $self->proxy->log( ALL, LOCATION => "$location => $new_location" ); } } ) ); $proxy->start;
The thing is, I've allready used new() method for some parameters, but now I need to pass @ARGV. I can't finish this script due to a lack of experience but I really need this thing to work :(. Thanks in advance.

Replies are listed 'Best First'.
Re: Unfinished custom proxy
by Eliya (Vicar) on Jan 08, 2012 at 11:28 UTC
    The thing is, I've allready used new() method for some parameters, but now I need to pass @ARGV.

    Normally, objects provide setters that allow you to control their state or behavior.  When you call ->new(...) (the constructor) another time, a new object will be created.  Why not simply pass the additional parameters on the first call?  Can you elaborate on why you need to pass @ARGV?

      Well I just don't know how to do it correctly. While I defined port, agent,...etc, I used a hash with predefined key names like port, agent, logfh, but I could find any method for @ARGV, like params, arguments or whatever, may be it sound stupid :) for pro guys, but this is my reason I thought that with my skills I can either pass it by default with new(), or I should use something that I don't know yet. BTW my only guess was:
      open ( LOGFILE, ">>", "/var/log/repeater.log"); my $proxy = HTTP::Proxy->new( port => '38374', agent => $ua, logfh => <LOGFILE>, @ARGV );
      Can I do such a thing ?

        You can do that as long as @ARGV (which I suppose you're using here to refer to arguments in the wider sense) contains key-value pairs that are acceptable with a HTTP::Proxy constructor call — which is why I asked what exactly those arguments are supposed to be or do.

        Now it's exiting with:
        telnet 127.0.0.1 38374 Trying 127.0.0.1... Can't use an undefined value as a symbol reference at HTTP/Proxy.pm li +ne 765. Can't use an undefined value as a symbol reference at HTTP/Proxy.pm li +ne 765. Connected to localhost.localdomain (127.0.0.1). Escape character is '^]'. Connection closed by foreign host. [1]+ Exit 29 ./proxy1.pl
Re: Unfinished custom proxy
by Anonymous Monk on Jan 08, 2012 at 11:38 UTC

    It is not gonna happen, you can't fool google, once it throws up the CAPTCHA screen, you have to answer the captcha

      It worked :) But there were two things:

      1)Worked, but in semi manual way.

      2) With google it's just a proof of concept , real script will be used for some other things.

        I tried to fix this, but I still got
        Now it's exiting with: telnet 127.0.0.1 38374 Trying 127.0.0.1... Can't use an undefined value as a symbol reference at HTTP/Proxy.pm l +i +ne 765. Connected to localhost.localdomain (127.0.0.1). Escape character is +'^]'. Connection closed by foreign host. [1]+ Exit 29 ./proxy1.pl
        If I try to connect on port 38374
Re: Unfinished custom proxy
by choroba (Cardinal) on Jan 08, 2012 at 13:53 UTC
      Sorry for such late answer. I wanted to use a push_filter as an argument for HTTP::Proxy object.