Hi moks, first I wan to say thanks to merlyn, especially for his web techniques page. I founded there anonymous proxy which using HTTP::Daemon and LWP::UserAgent to do, what you suppose from proxy :-) But unfortunately I wasn't able to get it to work (win2k, ActiveState Perl 5.6.1 b631, last version of modules availables on CPAN)
For easier debugging I added some "logtofile" procedure and change code slightly. It seems, that there is something wrong with parent and child proceses. Whole proxy do something like this:

Master process started
1. connection
1. child forked
1. child froze !!!!
....
2. conection
2. child forked
2. child froze !!!!
....
3. connection
1. child wake up !!!!
1. child do what we need
1. child stops (due to some error?)
3. child forked
....
4. connection
2. child wake up !!!!
2. child do what we need
2. child stops (due to some error?)
C R A S H !!!!

I am including my code and logfile. Please if you have any idea what s wrong let me know.

Thank you

code sample:

#!/home/merlyn/bin/perl #use strict; use LWP::UserAgent; use HTTP::Cookies; $ENV{PATH} = join ":", qw(/usr/ucb /bin /usr/bin); $|++; $log_file_path = "proxy.log"; $log = 2; ## Copyright (c) 1996 by Randal L. Schwartz ## This program is free software; you can redistribute it ## and/or modify it under the same terms as Perl itself. ## Anonymous HTTP proxy (handles http:, gopher:, ftp:) ## requires LWP 5.04 or later my $HOST = "10.0.0.186"; my $PORT = "8008"; my $REFERER = "http://www.domain.com"; sub prefix { my $now = localtime; join "", map { "[$now] [${$}] $_\n" } split /\n/, join "", @_; } $SIG{__WARN__} = sub { warn prefix @_ }; $SIG{__DIE__} = sub { die prefix @_ }; $SIG{CLD} = $SIG{CHLD} = sub { wait; }; #my $AGENT; # global user agent (for efficienc +y) #BEGIN { # use LWP::UserAgent; # @MyAgent::ISA = qw(LWP::UserAgent); # set inheritance # $AGENT = MyAgent->new; # $AGENT->agent("anon/0.07"); # $AGENT->env_proxy; #} #my $cookie_jar = HTTP::Cookies->new; my $ua = LWP::UserAgent->new; #$ua->cookie_jar($cookie_jar); sub LWP::UserAgent::redirect_ok { 0 } # redirects should pass throu +gh { ### MAIN ### use HTTP::Daemon; my $master = new HTTP::Daemon LocalAddr => $HOST, LocalPort => $PORT; warn "set your proxy to <URL:", $master->url, ">"; my $slave; &handle_connection($slave) while $slave = $master->accept; exit 0; } ### END MAIN ### sub handle_connection { &logging( 2, "Handling conection" ); my $connection = shift; # HTTP::Daemon::ClientConn my $pid = fork(); if ($pid) { # spawn OK, and I'm the parent close $connection; return; } ## spawn failed, or I'm a good child &logging( 2, "I am the child procces" ); my $request = $connection->get_request; &logging( 2, "Creating Request object from browser call" ); if (defined($request)) { &logging( 2, "Fetching request" ); my $response = &fetch_request($request); &logging( 2, "Sending response" ); $connection->send_response($response); &logging( 2, "Response sended, closing connection" ); close $connection; } else { warn "request not defined"; } exit 0 if defined $pid; # exit if I'm a good child with a g +ood parent } sub fetch_request { my $request = shift; # HTTP::Request use HTTP::Response; my $url = $request->url; &logging( 2, "Validating URL" ); if ($url->scheme !~ /^(http|gopher|ftp)$/) { my $res = HTTP::Response->new(403, "Forbidden"); $res->content("bad scheme: @{[$url->scheme]}\n"); $res; } elsif (not $url->rel->netloc) { my $res = HTTP::Response->new(403, "Forbidden"); $res->content("relative URL not permitted\n"); $res; } else { &fetch_validated_request($request); } } sub fetch_validated_request { # return HTTP::Response my $request = shift; # HTTP::Request #print $request; ## uses global $AGENT ## warn "orig request: <<<", $request->headers_as_string, ">>>"; &logging( 2, "Removing header information" ); $request->remove_header(qw(User-Agent From Referer Cookie)); ## warn "anon request: <<<", $request->headers_as_string, ">>>"; &logging( 2, "Requesting page" ); my $response = $ua->request($request); ## warn "orig response: <<<", $response->headers_as_string, ">>>" +; &logging( 2, "Removing cookie" ); $response->remove_header(qw(Set-Cookie)); ## warn "anon response: <<<", $response->headers_as_string, ">>>" +; $response; } # ------------------------------------------------------------------ +------- # logging sub logging { my ( $time, $i ); if ( @_[0] <= $log ) { if ( not open(LOG, ">>".$log_file_path) ) { &produce_error( 7 ); die; } $time = localtime(time); if ( $file_lock ) { flock(LOG, 2); } print LOG "$time \t [${$}] \t @_[1] \r\n"; close(LOG); } }

logfile:

refresh pressed Wed May 29 12:07:31 2002 [720] Handling conection Wed May 29 12:07:32 2002 [-1180] I am the child procces refresh pressed Wed May 29 12:08:40 2002 [720] Handling conection Wed May 29 12:08:40 2002 [-1484] I am the child procces refresh pressed Wed May 29 12:08:50 2002 [720] Handling conection Wed May 29 12:08:51 2002 [-1180] Creating Request object from bro +wser call Wed May 29 12:08:51 2002 [-1180] Fetching request Wed May 29 12:08:51 2002 [-1180] Validating URL Wed May 29 12:08:52 2002 [-1448] I am the child procces refresh pressed Wed May 29 12:09:26 2002 [720] Handling conection Wed May 29 12:09:26 2002 [-1484] Creating Request object from bro +wser call Wed May 29 12:09:26 2002 [-1484] Fetching request Wed May 29 12:09:26 2002 [-1484] Validating URL CRASH!!!! --------------------------- Wed May 29 12:11:19 2002 [1276] Handling conection Wed May 29 12:11:19 2002 [-1156] I am the child procces refresh pressed Wed May 29 12:11:37 2002 [1276] Handling conection Wed May 29 12:11:38 2002 [-1528] I am the child procces refresh pressed Wed May 29 12:13:22 2002 [1276] Handling conection Wed May 29 12:13:23 2002 [-1156] Creating Request object fro +m browser call Wed May 29 12:13:23 2002 [-1156] Fetching request Wed May 29 12:13:23 2002 [-1156] Validating URL Wed May 29 12:13:23 2002 [-1284] I am the child procces refresh pressed Wed May 29 12:13:40 2002 [1276] Handling conection CRASH!!!!

Li Tin O've Weedle
mad Tsort's philosopher


In reply to Merlyn Proxy sever fail on win32? by LiTinOveWeedle

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.