| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Apache::DNAT; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 1 |  |  | 1 |  | 674 | use strict; | 
|  | 1 |  |  |  |  | 3 |  | 
|  | 1 |  |  |  |  | 26 |  | 
| 4 | 1 |  |  | 1 |  | 408 | use Socket qw(sockaddr_in inet_aton inet_ntoa); | 
|  | 1 |  |  |  |  | 2547 |  | 
|  | 1 |  |  |  |  | 440 |  | 
| 5 | 1 |  |  | 1 |  | 227 | eval q{use Apache::Connection; use Apache::Constants qw(DECLINED OK MOVED);}; | 
|  | 0 |  |  |  |  |  |  | 
|  | 0 |  |  |  |  |  |  | 
| 6 |  |  |  |  |  |  |  | 
| 7 |  |  |  |  |  |  | sub handler { | 
| 8 | 0 |  |  | 0 | 0 |  | my $r = shift; | 
| 9 | 0 |  |  |  |  |  | my $c = $r->connection; | 
| 10 | 0 |  |  |  |  |  | my $old_remote_addr = $c->remote_addr; | 
| 11 | 0 |  |  |  |  |  | my ($old_port, $old_addr) = sockaddr_in($old_remote_addr); | 
| 12 | 0 |  |  |  |  |  | $old_addr = inet_ntoa $old_addr; | 
| 13 | 0 | 0 |  |  |  |  | if ($old_addr =~ /^(127|10|192.168|172\.(1[6-9]|2\d|3[01]))\./) { | 
| 14 |  |  |  |  |  |  | # Martian IP so it is safe | 
| 15 | 0 |  |  |  |  |  | my $headers = $r->headers_in; | 
| 16 | 0 |  |  |  |  |  | my $new_addr = $headers->{"remote-addr"}; | 
| 17 | 0 |  |  |  |  |  | my $new_port = $headers->{"remote-port"}; | 
| 18 | 0 | 0 | 0 |  |  |  | if ($new_addr && $new_port) { | 
| 19 | 0 |  |  |  |  |  | delete $headers->{"remote-addr"}; | 
| 20 | 0 |  |  |  |  |  | delete $headers->{"remote-port"}; | 
| 21 | 0 |  |  |  |  |  | $c->remote_addr(scalar sockaddr_in($new_port, inet_aton($new_addr))); | 
| 22 | 0 |  |  |  |  |  | $c->remote_ip($new_addr); | 
| 23 |  |  |  |  |  |  | } | 
| 24 |  |  |  |  |  |  | } | 
| 25 |  |  |  |  |  |  |  | 
| 26 |  |  |  |  |  |  | # Now pretend like I didn't do anything. | 
| 27 | 0 |  |  |  |  |  | return DECLINED(); | 
| 28 |  |  |  |  |  |  | } | 
| 29 |  |  |  |  |  |  |  | 
| 30 |  |  |  |  |  |  |  | 
| 31 |  |  |  |  |  |  | sub UnPort { | 
| 32 | 0 |  |  | 0 | 0 |  | my $r = shift; | 
| 33 | 0 |  |  |  |  |  | my $type = $r->content_type; | 
| 34 | 0 | 0 | 0 |  |  |  | if ($type && $type eq "httpd/unix-directory") { | 
| 35 | 0 |  |  |  |  |  | my $path = $r->uri; | 
| 36 | 0 | 0 |  |  |  |  | if ($path !~ m%/$%) { | 
| 37 |  |  |  |  |  |  | # Make sure the non-canonical bouncer routine runs | 
| 38 | 0 |  |  |  |  |  | $r->handler("perl-script"); | 
| 39 | 0 |  |  |  |  |  | $r->push_handlers(PerlHandler => \&directory_bounce); | 
| 40 |  |  |  |  |  |  | } | 
| 41 |  |  |  |  |  |  | } | 
| 42 | 0 |  |  |  |  |  | return OK(); | 
| 43 |  |  |  |  |  |  | } | 
| 44 |  |  |  |  |  |  |  | 
| 45 |  |  |  |  |  |  | sub directory_bounce { | 
| 46 | 0 |  |  | 0 | 0 |  | my $r = shift; | 
| 47 | 0 | 0 |  |  |  |  | my $proto = $r->subprocess_env("https")?"https":"http"; | 
| 48 | 0 |  | 0 |  |  |  | my $host = $r->header_in("host") || $r->hostname || $r->server->server_hostname; | 
| 49 | 0 |  |  |  |  |  | my $path = $r->uri; | 
| 50 | 0 |  |  |  |  |  | my $query = $r->args; | 
| 51 |  |  |  |  |  |  |  | 
| 52 | 0 |  |  |  |  |  | my $url = "$proto://$host$path/"; | 
| 53 | 0 | 0 |  |  |  |  | $url .= "?$query" if length $query; | 
| 54 |  |  |  |  |  |  |  | 
| 55 | 0 |  |  |  |  |  | $r->status(MOVED()); | 
| 56 | 0 |  |  |  |  |  | $r->content_type("text/html"); | 
| 57 | 0 |  |  |  |  |  | $r->header_out(Location => $url); | 
| 58 | 0 |  |  |  |  |  | $r->send_http_header; | 
| 59 |  |  |  |  |  |  |  | 
| 60 | 0 | 0 |  |  |  |  | return OK() if $r->header_only; | 
| 61 | 0 |  |  |  |  |  | $r->print("Moved here\n"); | 
| 62 | 0 |  |  |  |  |  | return OK(); | 
| 63 |  |  |  |  |  |  | } | 
| 64 |  |  |  |  |  |  |  | 
| 65 |  |  |  |  |  |  | 1; | 
| 66 |  |  |  |  |  |  | __END__ |