create var dir if needed
[HTTP-Proxy-Archive.git] / proxy.pl
index a2b5546..f6fc12b 100755 (executable)
--- a/proxy.pl
+++ b/proxy.pl
@@ -14,6 +14,7 @@ use Data::Dump qw(dump);
 sub var_save {
        my ( $dir, $name, $value ) = @_;
        $value ||= "\n";
+       mkdir 'var' unless -e 'var';
        mkdir "var/$dir" unless -e "var/$dir";
        open(my $fh, '>>', "var/$dir/$name") || die $!;
        print $fh $value;
@@ -146,10 +147,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'
     ),
@@ -175,6 +177,42 @@ $proxy->push_filter(
     ),
 );
 
+#
+# 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
 #
@@ -184,7 +222,7 @@ 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";
 
        print $message->headers_as_string if debug_on;
 
@@ -217,6 +255,9 @@ function FindProxyForURL(url, host) {
        if (shExpMatch(url, "*.ico")) return "DIRECT";
        if (shExpMatch(url, "*.jpg")) return "DIRECT";
  
+       if (isInNet(host, "127.0.0.0",  "255.0.0.0"))
+               return "DIRECT";
+
 //      if (isInNet(host, "10.0.0.0",  "255.255.248.0"))    {
 //             return "PROXY fastproxy.example.com:8080";
 //     }