3 # http proxy for archives
7 use HTTP::Proxy qw ( :log );
8 use HTTP::Proxy::BodyFilter::save;
9 use HTTP::Proxy::BodyFilter::simple;
10 use HTTP::Proxy::HeaderFilter::simple;
12 use Data::Dump qw(dump);
15 my ( $dir, $name, $value ) = @_;
17 mkdir "var/$dir" unless -e "var/$dir";
18 open(my $fh, '>>', "var/$dir/$name") || die $!;
28 use CGI::Util qw( unescape );
32 # get the command-line parameters
39 my $args = '(' . join( '|', keys %args ) . ')';
40 for ( my $i = 0 ; $i < @ARGV ; $i += 2 ) {
41 if ( $ARGV[$i] =~ /$args/o ) {
42 if ( ref $args{$1} ) {
43 push @{ $args{$1} }, $ARGV[ $i + 1 ];
46 $args{$1} = $ARGV[ $i + 1 ];
48 splice( @ARGV, $i, 2 );
54 # the headers we want to see
56 qw( Content-Type Set-Cookie Set-Cookie2 WWW-Authenticate Location ),
60 ( qw( Cookie Cookie2 Referer Referrer Authorization ), @{ $args{header} } );
62 # NOTE: Body request filters always receive the request body in one pass
63 my $post_filter = HTTP::Proxy::BodyFilter::simple->new(
64 begin => sub { $_[0]->{binary} = 0; },
66 my ( $self, $dataref, $message, $protocol, $buffer ) = @_;
67 print STDOUT "\n", $message->method, " ", $message->uri, "\n";
68 print_headers( $message, @clt_hdr );
70 if ( $self->{binary} || $$dataref =~ /\0/ ) {
72 print STDOUT " (not printing binary data)\n";
76 # this is from CGI.pm, method parse_params()
77 my (@pairs) = split( /[&;]/, $$dataref );
79 my ( $param, $value ) = split( '=', $_, 2 );
80 $param = unescape($param);
81 $value = unescape($value);
82 printf STDOUT " %-20s => %s\n", $param, $value;
87 my $get_filter = HTTP::Proxy::HeaderFilter::simple->new(
89 my ( $self, $headers, $message ) = @_;
90 my $req = $message->request;
91 if ( $req->method ne 'POST' ) {
92 print STDOUT "\n", $req->method, " ", $req->uri, "\n";
93 print_headers( $req, @clt_hdr );
95 print STDOUT $message->status_line, "\n";
96 print_headers( $message, @srv_hdr );
98 if ( my $cookie = $message->header( 'Set-Cookie' ) ) {
99 my $host = $req->uri->host;
100 warn "COOKIE: $cookie from $host\n";
101 var_save 'cookie' => $host;
109 if ( $message->header($h) ) {
110 print STDOUT " $h: $_\n" for ( $message->header($h) );
115 # create and start the proxy
116 my $proxy = HTTP::Proxy->new(@ARGV);
118 # if we want to look at SOME sites
119 if (@{$args{peek}}) {
120 for (@{$args{peek}}) {
124 request => $post_filter
128 response => $get_filter,
133 # otherwise, peek at all sites
137 request => $post_filter
139 $proxy->push_filter( response => $get_filter, mime => $args{mime} );
148 # you should probably restrict this to certain hosts as well
150 mime => 'application/pdf',
152 response => HTTP::Proxy::BodyFilter::save->new(
157 # send a HTML message instead
158 response => HTTP::Proxy::BodyFilter::simple->new(
160 my ( $self, $message ) = @_; # for information, saorge
164 # my ( $self, $dataref, $message, $protocol, $buffer ) = @_;
165 # $$dataref = $saved++ ? ""
166 # : sprintf '<p>Saving PDF file. Go <a href="%s">back</a></p>',
167 # $message->request->header('referer');
170 # change the response Content-Type
171 response => HTTP::Proxy::HeaderFilter::simple->new(
173 my ( $self, $headers, $response ) = @_;
174 # $headers->content_type('text/html');
184 use HTTP::Proxy qw( :log );
185 use MIME::Base64 qw( encode_base64 );
187 # the encoded user:password pair
190 my $token = "Basic " . encode_base64( "http:proxy", '' );
192 # the authentication filter
194 request => HTTP::Proxy::HeaderFilter::simple->new(
196 my ( $self, $headers, $request ) = @_;
198 # check the token against all credentials
200 $_ eq $token && $ok++
201 for $self->proxy->hop_headers->header('Proxy-Authorization');
203 # no valid credential
205 my $response = HTTP::Response->new(407);
207 Proxy_Authenticate => 'Basic realm="HTTP::Proxy"' );
208 $self->proxy->response($response);
219 sub debug_on { -e 'var/debug' }
220 sub debug_dump { -e 'var/debug' && warn "## ", dump( @_ ) }
222 my $admin_filter = HTTP::Proxy::HeaderFilter::simple->new( sub {
223 my ( $self, $headers, $message ) = @_;
224 warn "XXX [", $headers->header('x-forwarded-for'), '] ', $message->uri, "\n";
226 print $message->headers_as_string if debug_on;
228 my $host = $message->uri->host;
229 var_save 'hits' => $host;
230 return unless $host eq $proxy->host;
232 if ( my $q = $message->uri->query ) {
233 if ( $q =~ m{debug} ) {
234 -e 'var/debug' ? unlink 'var/debug' : open(my $touch,'>','var/debug');
237 debug_dump( $headers, $message );
239 my $host_port = $proxy->host . ':' . $proxy->port;
241 my $res = HTTP::Response->new( 200 );
243 if ( $message->uri->path =~ m/(proxy.pac|wpad.dat)/ ) {
244 $res->content_type('application/x-ns-proxy-autoconfig');
247 function FindProxyForURL(url, host) {
248 // if (shExpMatch(url, "*.example.com:*/*")) {return "DIRECT";}
250 if (shExpMatch(url, "*.js")) return "DIRECT";
251 if (shExpMatch(url, "*.css")) return "DIRECT";
252 if (shExpMatch(url, "*.gif")) return "DIRECT";
253 if (shExpMatch(url, "*.png")) return "DIRECT";
254 if (shExpMatch(url, "*.ico")) return "DIRECT";
255 if (shExpMatch(url, "*.jpg")) return "DIRECT";
257 // if (isInNet(host, "10.0.0.0", "255.255.248.0")) {
258 // return "PROXY fastproxy.example.com:8080";
261 // we don't want to see this traffic!
262 if (shExpMatch(url, "*.google.*")) return "DIRECT";
264 return "PROXY $host_port; DIRECT";
268 $self->proxy->response( $res );
272 $res->content_type('text/html');
275 <h1>HTTP Proxy Archive</h1>
277 <div style="background: #ff0; padding: 1em;">
279 Copy following url into automatic proxy configuration and enable it:
281 <a href="http://$host_port/proxy.pac">http://$host_port/proxy.pac</a>
286 . qq|<a href=/>/</a> <a href="?debug">debug</a>|
289 $self->proxy->response( $res );
291 $proxy->push_filter( request => $admin_filter );
298 warn "listen on host ", $proxy->host, " port ", $proxy->port, "\n";