1#!/usr/bin/perl -w
2use HTTP::Proxy qw( :log );
3use HTTP::Proxy::HeaderFilter::simple;
4use MIME::Base64 qw( encode_base64 );
5use strict;
6
7# the encoded user:password pair
8# login:  http
9# passwd: proxy
10my $token = "Basic " . encode_base64( "http:proxy", '' );
11
12# a very simple proxy that requires authentication
13my $proxy = HTTP::Proxy->new(@ARGV);
14
15# the authentication filter
16$proxy->push_filter(
17    request => HTTP::Proxy::HeaderFilter::simple->new(
18        sub {
19            my ( $self, $headers, $request ) = @_;
20
21            # check the token against all credentials
22            my $ok = 0;
23            $_ eq $token && $ok++
24                for $self->proxy->hop_headers->header('Proxy-Authorization');
25
26            # no valid credential
27            if ( !$ok ) {
28                my $response = HTTP::Response->new(407);
29                $response->header(
30                    Proxy_Authenticate => 'Basic realm="HTTP::Proxy"' );
31                $self->proxy->response($response);
32            }
33        }
34    )
35);
36
37$proxy->start;
38
39