Mercurial > hg > anonet-resdb
comparison doc/www.powerfulproxy.com/public_html/do_it.php @ 501:4d73737f40ff draft
new version of powerfulproxy from atiti
author | Nick <nick@somerandomnick.ano> |
---|---|
date | Wed, 11 May 2011 05:11:58 +0000 |
parents | 6da2470e59b5 |
children | 60ece78f8b36 |
comparison
equal
deleted
inserted
replaced
500:0e5b572cc83a | 501:4d73737f40ff |
---|---|
1 <?php | 1 <?php |
2 // Copyright Atiti, 2011 | 2 // Copyright Atiti, 2011 |
3 // Version 0.1-2 | 3 // Version 0.1-2 |
4 // Heavily modified by Ivo <Ivo@UCIS.nl> | |
4 | 5 |
5 // Where are we at? | 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); | |
12 $path = implode('/', $pall); | |
13 array_pop($pall); | |
14 $rp = implode('/', $pall); | |
15 | |
16 /* CONFIGURATION */ | |
6 $SERVICEURL = "http://powerfulproxy.com/do_it.php/"; | 17 $SERVICEURL = "http://powerfulproxy.com/do_it.php/"; |
7 // Do da request | 18 |
8 function get_url($url, $data) { | 19 $REWRITE_CONTENT_TYPES = array('text/html', 'text/xml', 'text/plain'); |
9 if (!$url) { | 20 $REWRITE_PATTERNS = array( |
10 echo "Invalid use!"; | 21 /* Rewrite complete http/https URLs, enable one of the tree, and no more! */ |
11 die; | 22 // '@(https?)://(([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@i' => $SERVICEURL.'$1/$2', |
23 // '@(src|href|action)\s*=\s*(\'|")(https?)://([^\'"]*)\2@i' => '$1=$2'.$SERVICEURL.'$3/$4$2', | |
24 '@(<[^>]*)(src|href|action)\s*=\s*(\'|")(https?)://([^\'"]*)\3@i' => '$1$2=$3'.$SERVICEURL.'$4/$5$3', | |
25 /* Rewrite URLs relative to site root, enable one of the tree, and no more! */ | |
26 // '@(src|href|action)\s*=\s*(\'|")/([^\'"]*)\2@i' => '$1=$2'.$SERVICEURL.$proto.'/'.$host.'/$3$2', | |
27 '@(<[^>]*)(src|href|action)\s*=\s*(\'|")/([^\'"]*)\3@i' => '$1$2=$3'.$SERVICEURL.$proto.'/'.$host.'/$4$3', | |
28 ); | |
29 $CURL_OPTIONS = array( | |
30 CURLOPT_USERAGENT => "AnoNet proxy", | |
31 CURLOPT_AUTOREFERER => TRUE, | |
32 CURLOPT_CONNECTTIMEOUT => 15, | |
33 CURLOPT_TIMEOUT => 28, | |
34 CURLOPT_MAXREDIRS => 10, | |
35 CURLOPT_FAILONERROR => FALSE, | |
36 CURLOPT_HEADER => 1, | |
37 CURLOPT_FOLLOWLOCATION => FALSE, | |
38 // CURLOPT_INTERFACE => '0.0.0.0', | |
39 // CURLOPT_PROXY => "http://b.polipo.srn.ano:8000/", | |
40 // CURLOPT_PROXYUSERPWD => 'username:password', | |
41 ); | |
42 /* END OF CONFIGURATION */ | |
43 | |
44 $url = $proto."://".$host."/".$path; | |
45 if (isset($_SERVER['QUERY_STRING']) && strlen($_SERVER['QUERY_STRING'])) $url .= "?".$_SERVER['QUERY_STRING']; | |
46 $ch = curl_init($url); | |
47 curl_setopt_array($ch, $CURL_OPTIONS); | |
48 curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); | |
49 //curl_setopt($ch, CURLOPT_HEADER, FALSE); | |
50 if (count($_POST)) { | |
51 curl_setopt($ch, CURLOPT_POST, TRUE); | |
52 curl_setopt($ch, CURLOPT_POSTFIELDS, $_POST); | |
53 } | |
54 $response = curl_exec($ch); | |
55 list($header, $data) = explode("\r\n\r\n", $response, 2); | |
56 if ($error = curl_error($ch)) die('CURL ERROR: '.$error); | |
57 $info = curl_getinfo($ch); | |
58 | |
59 header('Status: '.$info['http_code']); | |
60 header('Content-Type: '.$info['content_type']); | |
61 | |
62 $redirurl = ""; | |
63 if ($info['http_code'] === 301) { | |
64 $headers = explode("\r\n", $header); | |
65 foreach($headers as $h) { | |
66 $cur_header = explode(": ", $h); | |
67 if ($cur_header[0] == "Location") { | |
68 $redirurl = preg_replace('@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@', $SERVICEURL.str_replace("http://", "http/", "$1"), $cur_header[1]); | |
69 $redirurl = str_replace(".php/http://", ".php/http/", $redirurl); | |
70 header('Location: '.$redirurl); | |
71 } | |
12 } | 72 } |
13 $options = array( | 73 } else { |
14 CURLOPT_RETURNTRANSFER => true, // return web page | 74 if (in_array(strtok($info['content_type'], ';'), $REWRITE_CONTENT_TYPES)) $data = preg_replace(array_keys($REWRITE_PATTERNS), array_values($REWRITE_PATTERNS), $data, -1, $count); |
15 CURLOPT_HEADER => false, // don't return headers | |
16 CURLOPT_FOLLOWLOCATION => true, // follow redirects | |
17 CURLOPT_ENCODING => "", // handle all encodings | |
18 CURLOPT_USERAGENT => "AnoNet proxy", // who am i | |
19 CURLOPT_AUTOREFERER => true, // set referer on redirect | |
20 CURLOPT_CONNECTTIMEOUT => 15, // timeout on connect | |
21 CURLOPT_TIMEOUT => 28, // timeout on response | |
22 CURLOPT_MAXREDIRS => 10, // stop after 10 redirects | |
23 CURLOPT_FAILONERROR => true, | |
24 // CURLOPT_PROXY => "http://b.polipo.srn.ano:8000/", | |
25 ); | |
26 $ch = curl_init ($url); | |
27 curl_setopt_array( $ch, $options ); | |
28 $fields_string = ""; | |
29 if (count($data)) { | |
30 foreach($data as $key=>$value) { $fields_string .= $key.'='.$value.'&'; } | |
31 rtrim($fields_string,'&'); | |
32 curl_setopt($ch, CURLOPT_POST, count($data)); | |
33 curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string); | |
34 } | |
35 $ret = curl_exec ($ch); | |
36 if ($error = curl_error($ch)) | |
37 echo 'ERROR: ',$error; | |
38 $info = curl_getinfo($ch); | |
39 return array("data"=>$ret,"info"=>$info); | |
40 } | 75 } |
41 // Rewrite relative paths | |
42 function rewriteRelative($html, $base) { | |
43 $server = preg_replace('@^([^\:]*)://([^/*]*)(/|$).*@', '\1://\2/', $base); | |
44 $html = preg_replace('@\<([^>]*) (href|src)="/([^"]*)"@i', '<\1 \2="' . $server . '\3"', $html); | |
45 $html = preg_replace('@\<([^>]*) (href|src)="(([^\:"])*|([^"]*:[^/"].*))"@i', '<\1 \2="' . $base . '\3"', $html); | |
46 return $html; | |
47 } | |
48 if (isset($_SERVER["PATH_INFO"])) | |
49 $p = $_SERVER["PATH_INFO"]; | |
50 if (isset($_SERVER["QUERY_STRING"])) | |
51 $q = $_SERVER["QUERY_STRING"]; | |
52 $postdata = $_POST; | |
53 $pall = explode("/", $p); | |
54 if (count($pall) <= 1) { | |
55 echo "Wrong host format? or smtg."; | |
56 die; | |
57 } | |
58 $proto = $pall[1]; | |
59 $host = $pall[2]; | |
60 unset($pall[0]); | |
61 unset($pall[1]); | |
62 unset($pall[2]); | |
63 $path = implode("/", $pall); | |
64 // Figure out relative paths | |
65 $pi = pathinfo($path); | |
66 if ($pi) { | |
67 $rp = @$pi["dirname"]; | |
68 } else | |
69 $rp = ""; | |
70 if (!$rp) | |
71 $rp = $path; | |
72 // Construct request url | |
73 $geturl = $proto."://".$host."/".$path; | |
74 if ($q) | |
75 $geturl .= "?".$q; // Append query string | |
76 | 76 |
77 $d = get_url($geturl, $postdata); | 77 header('Content-Length: '.strlen($data)); |
78 $data = $d["data"]; | 78 echo $data; |
79 $ct = $d["info"]["content_type"]; | |
80 $ct_s = explode(";", $ct); | |
81 $found = false; | |
82 $match_ct = array("text/html", "text/xml", "text/plain"); | |
83 foreach($match_ct as $m) { | |
84 if ($ct_s[0] == $m) | |
85 $found = true; | |
86 } | |
87 if ($found) { // Only rewrite for proper content | |
88 $ret = rewriteRelative($data, $proto."://".$host."/".$rp."/"); | |
89 $ret = preg_replace('@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@', $SERVICEURL.str_replace("http://", "http/", "$1"), $ret); | |
90 $ret = str_replace(".php/http://", ".php/http/", $ret); | |
91 $ret = str_replace(".php/https://", ".php/https/", $ret); | |
92 | |
93 $ret = str_replace("../", "", $ret); | |
94 $items = Array("/src='\/(.*)'/", "/src=\"\/(.*)\"/", "/href='\/(.*)'/", "/href=\"\/(.*)\"/"); | |
95 $ret = preg_replace($items, "src='".$SERVICEURL.$proto."/".$host."/$1'", $ret); | |
96 $ret = preg_replace("/action=\"\/\"/i", "action=\"".$SERVICEURL.$proto."/".$host."/\"", $ret); | |
97 | |
98 } else | |
99 $ret = ""; | |
100 // Output da shit | |
101 header("Content-Type: ".$ct); | |
102 if (strlen($ret) == 0) | |
103 echo $data; | |
104 else | |
105 echo $ret; | |
106 | |
107 ?> | 79 ?> |