Contributed by superfrink
on Nov 30, 2004 at 04:28 UTC
Q&A
> HTTP and FTP clients
Answer: How do I password-protect my HTTP::Proxy daemon? contributed by superfrink
The following includes hints scattered through the thread login & password.
The HTTP::Proxy documentation doesn't explicitly mention "login" or "password",
but it does talk about "authentication".
HTTP::Proxy does not have options to enable a password protection system;
instead, you have to add a "filter" for authentication.
HTTP::Proxy::HeaderFilter::simple can create such a filter.
The HTTP-Proxy distribution includes an example program,
proxy-auth.pl,
which demonstrates how to add a BasicAuth style password to a proxy.
Below is a copy of this program with some customizations to (1) specify the port,
(2) allow any IP address to use the proxy, and (3) disable some headers added by the proxy.
#!/usr/bin/perl -w
use HTTP::Proxy qw( :log );
use HTTP::Proxy::HeaderFilter::simple;
use MIME::Base64 qw( encode_base64 );
use strict;
# the encoded user:password pair
my $token = "Basic " . encode_base64( "warren:12345" );
chomp $token; # grr
# a very simple proxy that requires authentication
# login/password: http/proxy
my $proxy = HTTP::Proxy->new;
$proxy->port( 3128 ); # the classical accessors are here!
$proxy->host( undef ); # allow any IP addresses.
$proxy->via( '' ); # hide proxy presence.
$proxy->x_forwarded_for( '' ); # hide proxy presence.
# $proxy->logmask( shift || NONE );
$proxy->logmask( shift || STATUS );
# the authentication filter
$proxy->push_filter(
request => HTTP::Proxy::HeaderFilter::simple->new(
sub {
my ( $self, $headers, $request ) = @_;
my $auth = $self->proxy->hop_headers->header('Proxy-Author
+ization') || '';
# check the credentials
if ( $auth ne $token ) {
my $response = HTTP::Response->new(407);
$response->header( Proxy_Authenticate => 'Basic realm=
+"HTTP::Proxy"' );
$self->proxy->response($response);
}
}
)
);
$proxy->start;
|
Please (register and) log in if you wish to add an answer
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, 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, 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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.
|
|