annotate doc/www.powerfulproxy.com/public_html/do_it.php @ 502:60ece78f8b36 draft

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