453
|
1 <?php |
|
2 // Copyright Atiti, 2011 |
|
3 // Version 0.1-2 |
501
|
4 // Heavily modified by Ivo <Ivo@UCIS.nl> |
453
|
5 |
501
|
6 if (!isset($_SERVER['PATH_INFO'])) die('PATH_INFO is not set'); |
|
7 $pall = explode("/", $_SERVER['PATH_INFO']); |
|
8 if (count($pall) <= 1) die('Unexpected path format'); |
|
9 array_shift($pall); |
|
10 $proto = array_shift($pall); |
|
11 $host = array_shift($pall); |
502
|
12 $hostparts = explode('.', $host); |
504
|
13 if (long2ip(ip2long($host))===$host) { |
503
|
14 if ($hostparts[0]!='1') die('Bad IP'); |
|
15 } elseif (!preg_match("/ano|ntwrk$/",array_pop($hostparts))) die('Bad host'); |
501
|
16 $path = implode('/', $pall); |
|
17 array_pop($pall); |
|
18 $rp = implode('/', $pall); |
|
19 |
|
20 /* CONFIGURATION */ |
453
|
21 $SERVICEURL = "http://powerfulproxy.com/do_it.php/"; |
501
|
22 |
|
23 $REWRITE_CONTENT_TYPES = array('text/html', 'text/xml', 'text/plain'); |
|
24 $REWRITE_PATTERNS = array( |
|
25 /* Rewrite complete http/https URLs, enable one of the tree, and no more! */ |
|
26 // '@(https?)://(([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@i' => $SERVICEURL.'$1/$2', |
|
27 // '@(src|href|action)\s*=\s*(\'|")(https?)://([^\'"]*)\2@i' => '$1=$2'.$SERVICEURL.'$3/$4$2', |
|
28 '@(<[^>]*)(src|href|action)\s*=\s*(\'|")(https?)://([^\'"]*)\3@i' => '$1$2=$3'.$SERVICEURL.'$4/$5$3', |
|
29 /* Rewrite URLs relative to site root, enable one of the tree, and no more! */ |
|
30 // '@(src|href|action)\s*=\s*(\'|")/([^\'"]*)\2@i' => '$1=$2'.$SERVICEURL.$proto.'/'.$host.'/$3$2', |
|
31 '@(<[^>]*)(src|href|action)\s*=\s*(\'|")/([^\'"]*)\3@i' => '$1$2=$3'.$SERVICEURL.$proto.'/'.$host.'/$4$3', |
|
32 ); |
|
33 $CURL_OPTIONS = array( |
|
34 CURLOPT_USERAGENT => "AnoNet proxy", |
|
35 CURLOPT_AUTOREFERER => TRUE, |
|
36 CURLOPT_CONNECTTIMEOUT => 15, |
|
37 CURLOPT_TIMEOUT => 28, |
|
38 CURLOPT_MAXREDIRS => 10, |
|
39 CURLOPT_FAILONERROR => FALSE, |
|
40 CURLOPT_HEADER => 1, |
|
41 CURLOPT_FOLLOWLOCATION => FALSE, |
|
42 // CURLOPT_INTERFACE => '0.0.0.0', |
|
43 // CURLOPT_PROXY => "http://b.polipo.srn.ano:8000/", |
|
44 // CURLOPT_PROXYUSERPWD => 'username:password', |
|
45 ); |
|
46 /* END OF CONFIGURATION */ |
|
47 |
|
48 $url = $proto."://".$host."/".$path; |
|
49 if (isset($_SERVER['QUERY_STRING']) && strlen($_SERVER['QUERY_STRING'])) $url .= "?".$_SERVER['QUERY_STRING']; |
|
50 $ch = curl_init($url); |
|
51 curl_setopt_array($ch, $CURL_OPTIONS); |
|
52 curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); |
|
53 //curl_setopt($ch, CURLOPT_HEADER, FALSE); |
|
54 if (count($_POST)) { |
|
55 curl_setopt($ch, CURLOPT_POST, TRUE); |
|
56 curl_setopt($ch, CURLOPT_POSTFIELDS, $_POST); |
453
|
57 } |
501
|
58 $response = curl_exec($ch); |
|
59 list($header, $data) = explode("\r\n\r\n", $response, 2); |
|
60 if ($error = curl_error($ch)) die('CURL ERROR: '.$error); |
|
61 $info = curl_getinfo($ch); |
|
62 |
|
63 header('Status: '.$info['http_code']); |
|
64 header('Content-Type: '.$info['content_type']); |
453
|
65 |
501
|
66 $redirurl = ""; |
|
67 if ($info['http_code'] === 301) { |
|
68 $headers = explode("\r\n", $header); |
|
69 foreach($headers as $h) { |
|
70 $cur_header = explode(": ", $h); |
|
71 if ($cur_header[0] == "Location") { |
|
72 $redirurl = preg_replace('@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@', $SERVICEURL.str_replace("http://", "http/", "$1"), $cur_header[1]); |
|
73 $redirurl = str_replace(".php/http://", ".php/http/", $redirurl); |
|
74 header('Location: '.$redirurl); |
|
75 } |
|
76 } |
|
77 } else { |
|
78 if (in_array(strtok($info['content_type'], ';'), $REWRITE_CONTENT_TYPES)) $data = preg_replace(array_keys($REWRITE_PATTERNS), array_values($REWRITE_PATTERNS), $data, -1, $count); |
453
|
79 } |
|
80 |
501
|
81 header('Content-Length: '.strlen($data)); |
|
82 echo $data; |
453
|
83 ?> |