cleanup output
[HTTP-Proxy-Archive.git] / proxy.pl
index 5c076b1..ec6e344 100755 (executable)
--- a/proxy.pl
+++ b/proxy.pl
@@ -9,14 +9,18 @@ use HTTP::Proxy::BodyFilter::save;
 use HTTP::Proxy::BodyFilter::simple;
 use HTTP::Proxy::HeaderFilter::simple;
 
-our $info = {
-       debug => 1,
-       direct => [ qw/
-imageserver.ebscohost.com
-       / ],
-};
 use Data::Dump qw(dump);
 
+sub var_save {
+       my ( $dir, $name, $value ) = @_;
+       $value ||= "\n";
+       mkdir "var/$dir" unless -e "var/$dir";
+       open(my $fh, '>>', "var/$dir/$name") || die $!;
+       print $fh $value;
+       close($fh);
+}
+
+
 #
 # logger.pl
 #
@@ -89,7 +93,13 @@ my $get_filter = HTTP::Proxy::HeaderFilter::simple->new(
             print_headers( $req, @clt_hdr );
         }
         print STDOUT $message->status_line, "\n";
-        print_headers( $message, @srv_hdr );
+       print_headers( $message, @srv_hdr );
+
+       if ( my $cookie = $message->header( 'Set-Cookie' ) ) {
+               my $host = $req->uri->host;
+               warn "COOKIE: $cookie from $host\n";
+               var_save 'cookie' => $host;
+       }
     }
 );
 
@@ -136,10 +146,11 @@ else {
 my $saved;
 $proxy->push_filter(
     # you should probably restrict this to certain hosts as well
-    path => qr/\.pdf$/,
+    path => qr/\.pdf\b/,
     mime => 'application/pdf',
     # save the PDF
     response => HTTP::Proxy::BodyFilter::save->new(
+       timestamp => 1,
         template => "%f",
         prefix   => 'pdf'
     ),
@@ -149,34 +160,81 @@ $proxy->push_filter(
             my ( $self, $message ) = @_;    # for information, saorge
             $saved = 0;
         },
-        filter => sub {
-            my ( $self, $dataref, $message, $protocol, $buffer ) = @_;
-            $$dataref = $saved++ ? "" 
-              : sprintf '<p>Saving PDF file. Go <a href="%s">back</a></p>',
-                        $message->request->header('referer');
-        }
+#        filter => sub {
+#            my ( $self, $dataref, $message, $protocol, $buffer ) = @_;
+#            $$dataref = $saved++ ? "" 
+#              : sprintf '<p>Saving PDF file. Go <a href="%s">back</a></p>',
+#                        $message->request->header('referer');
+#        }
     ),
     # change the response Content-Type
     response => HTTP::Proxy::HeaderFilter::simple->new(
         sub {
             my ( $self, $headers, $response ) = @_;
-            $headers->content_type('text/html');
+#            $headers->content_type('text/html');
         }
     ),
 );
 
+#
+# proxy-auth.pl
+#
+
+
+use HTTP::Proxy qw( :log );
+use MIME::Base64 qw( encode_base64 );
+
+# the encoded user:password pair
+# login:  http
+# passwd: proxy
+my $token = "Basic " . encode_base64( "http:proxy", '' );
+
+# the authentication filter
+$proxy->push_filter(
+    request => HTTP::Proxy::HeaderFilter::simple->new(
+        sub {
+            my ( $self, $headers, $request ) = @_;
+
+            # check the token against all credentials
+            my $ok = 0;
+            $_ eq $token && $ok++
+                for $self->proxy->hop_headers->header('Proxy-Authorization');
+
+            # no valid credential
+            if ( !$ok ) {
+                my $response = HTTP::Response->new(407);
+                $response->header(
+                    Proxy_Authenticate => 'Basic realm="HTTP::Proxy"' );
+                $self->proxy->response($response);
+            }
+        }
+    )
+);
+
+
 #
 # admin interface
 #
 
+sub debug_on { -e 'var/debug' }
+sub debug_dump { -e 'var/debug' && warn "## ", dump( @_ ) }
+
 my $admin_filter = HTTP::Proxy::HeaderFilter::simple->new( sub {
    my ( $self, $headers, $message ) = @_;
-warn "XXX ", $headers->header('x-forwarded-for'), ' ', $message->uri, "\n";
+warn "\nXXX [", $headers->header('x-forwarded-for'), '] ', $message->uri, "\n";
 
-       return unless $message->uri->host eq $proxy->host;
+       print $message->headers_as_string if debug_on;
 
-       $info->{debug} = not $info->{debug} if $message->uri->query =~ m{debug};
-       warn "## ", dump( $headers, $message ) if $info->{debug};
+       my $host = $message->uri->host;
+       var_save 'hits' => $host;
+       return unless $host eq $proxy->host;
+
+       if ( my $q = $message->uri->query ) {
+               if ( $q =~ m{debug} ) {
+                       -e 'var/debug' ? unlink 'var/debug' : open(my $touch,'>','var/debug');
+               }
+       }
+       debug_dump( $headers, $message );
 
        my $host_port = $proxy->host . ':' . $proxy->port;
 
@@ -194,11 +252,15 @@ function FindProxyForURL(url, host) {
        if (shExpMatch(url, "*.gif")) return "DIRECT";
        if (shExpMatch(url, "*.png")) return "DIRECT";
        if (shExpMatch(url, "*.ico")) return "DIRECT";
+       if (shExpMatch(url, "*.jpg")) return "DIRECT";
  
 //      if (isInNet(host, "10.0.0.0",  "255.255.248.0"))    {
 //             return "PROXY fastproxy.example.com:8080";
 //     }
+
+       // we don't want to see this traffic! 
+       if (shExpMatch(url, "*.google.*")) return "DIRECT";
+
        return "PROXY $host_port; DIRECT";
 }
 
@@ -212,9 +274,17 @@ function FindProxyForURL(url, host) {
 
 <h1>HTTP Proxy Archive</h1>
 
-<a href="http://$host_port/proxy.pac">proxy.pac</a>
+<div style="background: #ff0; padding: 1em;">
+
+Copy following url into automatic proxy configuration and enable it:
+<p>
+<a href="http://$host_port/proxy.pac">http://$host_port/proxy.pac</a>
+
+</div>
 
-       | . '<pre>' . dump($info) . '</pre>' );
+       | 
+       . qq|<a href=/>/</a> <a href="?debug">debug</a>|
+       );
 
        $self->proxy->response( $res );
 } );