summaryrefslogtreecommitdiff
path: root/doc/www.powerfulproxy.com/public_html/do_it.php
blob: 48c128e5269ae20f11c325e7cbd4ad74d50eeec9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php
// Copyright Atiti, 2011
// Version 0.1-2
// Heavily modified by Ivo <Ivo@UCIS.nl>

if (!isset($_SERVER['PATH_INFO'])) die('PATH_INFO is not set');
$pall = explode("/", $_SERVER['PATH_INFO']);
if (count($pall) <= 1) die('Unexpected path format');
array_shift($pall);
$proto = array_shift($pall);
$host = array_shift($pall);
$hostparts = explode('.', $host);
if (long2ip(ip2long($host))===$host) {
 if ($hostparts[0]!='1') die('Bad IP');
} elseif (!preg_match("/ano|ntwrk$/",array_pop($hostparts))) die('Bad host');
$path = implode('/', $pall);
array_pop($pall);
$rp = implode('/', $pall);

/* CONFIGURATION */
$SERVICEURL = "http://powerfulproxy.com/do_it.php/";

$REWRITE_CONTENT_TYPES = array('text/html', 'text/xml', 'text/plain');
$REWRITE_PATTERNS = array(
/* Rewrite complete http/https URLs, enable one of the tree, and no more! */
//	'@(https?)://(([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@i' => $SERVICEURL.'$1/$2',
//	'@(src|href|action)\s*=\s*(\'|")(https?)://([^\'"]*)\2@i' => '$1=$2'.$SERVICEURL.'$3/$4$2',
	'@(<[^>]*)(src|href|action)\s*=\s*(\'|")(https?)://([^\'"]*)\3@i' => '$1$2=$3'.$SERVICEURL.'$4/$5$3',
/* Rewrite URLs relative to site root, enable one of the tree, and no more! */
//	'@(src|href|action)\s*=\s*(\'|")/([^\'"]*)\2@i' => '$1=$2'.$SERVICEURL.$proto.'/'.$host.'/$3$2',
	'@(<[^>]*)(src|href|action)\s*=\s*(\'|")/([^\'"]*)\3@i' => '$1$2=$3'.$SERVICEURL.$proto.'/'.$host.'/$4$3',
);
if (file_exists("do_it.inc")) include "do_it.inc";
if (!isset($CURL_OPTIONS)) $CURL_OPTIONS = array(
//	CURLOPT_USERAGENT	=> "AnoNet proxy",
	CURLOPT_USERAGENT	=> $_SERVER["HTTP_USER_AGENT"]." AnoNetProxy",
	CURLOPT_AUTOREFERER	=> TRUE,
	CURLOPT_REFERER		=> $_SERVER["HTTP_REFERER"],
	CURLOPT_CONNECTTIMEOUT	=> 15,
	CURLOPT_TIMEOUT		=> 28,
	CURLOPT_MAXREDIRS	=> 10,
	CURLOPT_FAILONERROR	=> FALSE,
	CURLOPT_HEADER		=> 1,
	CURLOPT_FOLLOWLOCATION	=> FALSE,
//	CURLOPT_INTERFACE	=> '0.0.0.0',
	CURLOPT_PROXY		=> "http://b.polipo.srn.ano:8000/",
//	CURLOPT_PROXYUSERPWD	=> 'username:password',
);
/* END OF CONFIGURATION */

$url = $proto."://".$host."/".$path;
if (isset($_SERVER['QUERY_STRING']) && strlen($_SERVER['QUERY_STRING'])) $url .= "?".$_SERVER['QUERY_STRING'];
$ch = curl_init($url);
curl_setopt_array($ch, $CURL_OPTIONS);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
//curl_setopt($ch, CURLOPT_HEADER, FALSE);
if (count($_POST)) {
	curl_setopt($ch, CURLOPT_POST, TRUE);
	curl_setopt($ch, CURLOPT_POSTFIELDS, $_POST);
}
$response = curl_exec($ch);
list($header, $data) = explode("\r\n\r\n", $response, 2); 
if ($error = curl_error($ch)) die('CURL ERROR: '.$error);
$info = curl_getinfo($ch);

header('Status: '.$info['http_code']);
header('Content-Type: '.$info['content_type']);

$redirurl = "";
if ($info['http_code'] === 301) {
	$headers = explode("\r\n", $header);
	foreach($headers as $h) {
		$cur_header = explode(": ", $h);
		if ($cur_header[0] == "Location") {
			$redirurl = preg_replace('@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@', $SERVICEURL.str_replace("http://", "http/", "$1"), $cur_header[1]);
			$redirurl = str_replace(".php/http://", ".php/http/", $redirurl);
			header('Location: '.$redirurl);
		}
	}
} else {
	if (in_array(strtok($info['content_type'], ';'), $REWRITE_CONTENT_TYPES)) $data = preg_replace(array_keys($REWRITE_PATTERNS), array_values($REWRITE_PATTERNS), $data, -1, $count);
}

header('Content-Length: '.strlen($data));
echo $data;
?>