-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
158 lines (141 loc) · 5.05 KB
/
Copy pathindex.php
File metadata and controls
158 lines (141 loc) · 5.05 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);
define('DEBUG', 0);
$request = substr($_SERVER['REQUEST_URI'], 1);
$myhost=$_SERVER['HTTP_HOST'];
if (!$request) {
print "<!DOCTYPE html><html lang=\"en\"><head><title>Privacy Cache</title></head><body><h1>Privacy Cache</h1><p>The Privacy Cache is a proxy that can be prepended to almost all GET requests. The Privacy Cache tries to filter out requests to websites belonging to known trackers and proxy them so to disable any tracking.</p><p><b>Disclaimer:</b> Both the DNTCache and the PrivacyCache are very much alpha software, which means it's not suitable for deployment.</p></body></html>\n";
exit(0);
}
$valid_url=filter_var($request, FILTER_VALIDATE_URL, FILTER_FLAG_SCHEME_REQUIRED| FILTER_FLAG_HOST_REQUIRED);
if (!$valid_url) {
header('Vary: Referer');
header('Cache-Control: max-age=86400');
if (array_key_exists('HTTP_REFERER', $_SERVER)) {
$referrer=$_SERVER['HTTP_REFERER'];
if (preg_match("/https?:\/\/${myhost}\/(https?):\/\/([^\/]+)/", $referrer, $matches)) {
$scheme=$matches[1];
$host=$matches[2];
$request="$scheme://$host/$request";
$valid_url=filter_var($request, FILTER_VALIDATE_URL, FILTER_FLAG_SCHEME_REQUIRED| FILTER_FLAG_HOST_REQUIRED);
if ($valid_url) {
http_response_code(307);
header("Location: https://${myhost}/${valid_url}");
exit(0);
}
}
}
http_response_code(404);
print "<html><body><h1>'${request}' does not meet formal requirements to be a valid URL.</body></html>\n";
exit(0);
}
$urlc=parse_url($valid_url);
$scheme=$urlc['scheme'];
$host=$urlc['host'];
function ip_in_range( $ip, $range ) {
if ( strpos( $range, '/' ) == false ) {
$range .= '/32';
}
// $range is in IP/CIDR format eg 127.0.0.1/24
list( $range, $netmask ) = explode( '/', $range, 2 );
$range_decimal = ip2long( $range );
$ip_decimal = ip2long( $ip );
$wildcard_decimal = pow( 2, ( 32 - $netmask ) ) - 1;
$netmask_decimal = ~ $wildcard_decimal;
return ( ( $ip_decimal & $netmask_decimal ) == ( $range_decimal & $netmask_decimal ) );
}
function deniedHost($host) {
if ($host == 'localhost')
return true;
if (filter_var($host, FILTER_VALIDATE_IP)) {
if (ip_in_range($host, '127.0.0.1/24'))
return true;
if (ip_in_range($host, '10.0.0.1/8'))
return true;
if (ip_in_range($host, '172.16.0.1/12'))
return true;
if (ip_in_range($host, '192.168.0.1/16'))
return true;
}
return false;
}
if (deniedHost($host)) {
http_response_code(403);
print "<!DOCTYPE html><html lang=\"en\"><head><title>Privacy Cache</title></head><body><h1>Access denied</h1><p>You don't wanna go there</p>";
exit(0);
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $request);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
$r = curl_exec($ch);
curl_close($ch);
$r = explode("\n", $r);
if (DEBUG) {
http_response_code(501);
header('Content-type: text/plain');
}
header('access-control-allow-origin: *');
$n=count($r);
$headers=array();
$headers['']=$r[0];
//print($r[0]);
$status=explode(" ", $r[0])[1];
for ($i=1;$i < $n && strlen($r[$i])>1;$i++) {
$tmp = explode(":", $r[$i]);
$key=strtolower($tmp[0]);
$value=implode(":", array_slice ($tmp, 1));
$headers[$key]=$value;
if ($key == "content-type") {
$content_type=explode(";",$value);
for ($j=0;$j < count($content_type);$j++) {
$content_type[$j]=$request=ltrim($content_type[$j], ' ');
}
}
}
if (DEBUG)
print_r($content_type);
if (DEBUG)
print_r($headers);
$pt_headers=array('content-type', 'expires','date','cache-control','vary', 'location');
if (DEBUG)
print $status."\n";
else
http_response_code($status);
foreach ($pt_headers as $pt) {
if (array_key_exists($pt, $headers)) {
if (DEBUG)
print ("$pt: $headers[$pt]");
else {
if ($pt == 'location') {
$loc=ltrim($headers['location'], ' ');
header ("location: https://${myhost}/$loc");
}
else
header ("$pt: $headers[$pt]");
}
}
}
$body=implode("\n", array_slice ($r , $i+1));
$editTypes = array("text/css", "text/html", 'application/javascript','text/javascript');
$lut=array('' => $scheme.':', 'http:' => 'http:', 'https:' => 'https:');
$patterns=array(
'/(http:|https:|)(\/\/.*?\.googleapis\.com)/e',
'/(http:|https:|)(\/\/.*?\.gstatic\.com)/e',
'/(http:|https:|)(\/\/.*?\.squarespace\.com)/e',
);
if (in_array ( $content_type[0] , $editTypes)) {
//$body=preg_replace($patterns, "//${myhost}/$1", $body);
$body=preg_replace($patterns, '"//${myhost}/{$lut[\'$1\']}$2"', $body, -1, $count_replacements);
//$body=str_replace("//fonts.googleapis.com", "//${myhost}/https://fonts.googleapis.com", $body);
}
if (! isset($count_replacements))
$count_replacements=0;
header("X-Replacements: $count_replacements");
print($body);
//print "<html><body><h1>Support for caching is not yet implemented</h1></body></html>\n";
//http_response_code(307);
//header("Location: ${valid_url}");
?>