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(
156 # send a HTML message instead
157 response => HTTP::Proxy::BodyFilter::simple->new(
159 my ( $self, $message ) = @_; # for information, saorge
163 # my ( $self, $dataref, $message, $protocol, $buffer ) = @_;
164 # $$dataref = $saved++ ? ""
165 # : sprintf '<p>Saving PDF file. Go <a href="%s">back</a></p>',
166 # $message->request->header('referer');
169 # change the response Content-Type
170 response => HTTP::Proxy::HeaderFilter::simple->new(
172 my ( $self, $headers, $response ) = @_;
173 # $headers->content_type('text/html');
182 sub debug_on { -e 'var/debug' }
183 sub debug_dump { -e 'var/debug' && warn "## ", dump( @_ ) }
185 my $admin_filter = HTTP::Proxy::HeaderFilter::simple->new( sub {
186 my ( $self, $headers, $message ) = @_;
187 warn "XXX [", $headers->header('x-forwarded-for'), '] ', $message->uri, "\n";
189 print $message->headers_as_string if debug_on;
191 my $host = $message->uri->host;
192 var_save 'hits' => $host;
193 return unless $host eq $proxy->host;
195 if ( my $q = $message->uri->query ) {
196 if ( $q =~ m{debug} ) {
197 -e 'var/debug' ? unlink 'var/debug' : open(my $touch,'>','var/debug');
200 debug_dump( $headers, $message );
202 my $host_port = $proxy->host . ':' . $proxy->port;
204 my $res = HTTP::Response->new( 200 );
206 if ( $message->uri->path =~ m/(proxy.pac|wpad.dat)/ ) {
207 $res->content_type('application/x-ns-proxy-autoconfig');
210 function FindProxyForURL(url, host) {
211 // if (shExpMatch(url, "*.example.com:*/*")) {return "DIRECT";}
213 if (shExpMatch(url, "*.js")) return "DIRECT";
214 if (shExpMatch(url, "*.css")) return "DIRECT";
215 if (shExpMatch(url, "*.gif")) return "DIRECT";
216 if (shExpMatch(url, "*.png")) return "DIRECT";
217 if (shExpMatch(url, "*.ico")) return "DIRECT";
218 if (shExpMatch(url, "*.jpg")) return "DIRECT";
220 // if (isInNet(host, "10.0.0.0", "255.255.248.0")) {
221 // return "PROXY fastproxy.example.com:8080";
224 // we don't want to see this traffic!
225 if (shExpMatch(url, "*.google.*")) return "DIRECT";
227 return "PROXY $host_port; DIRECT";
231 $self->proxy->response( $res );
235 $res->content_type('text/html');
238 <h1>HTTP Proxy Archive</h1>
240 <div style="background: #ff0; padding: 1em;">
242 Copy following url into automatic proxy configuration and enable it:
244 <a href="http://$host_port/proxy.pac">http://$host_port/proxy.pac</a>
249 . qq|<a href=/>/</a> <a href="?debug">debug</a>|
252 $self->proxy->response( $res );
254 $proxy->push_filter( request => $admin_filter );
261 warn "listen on host ", $proxy->host, " port ", $proxy->port, "\n";