Код:
<?php
# GAE-based webproxy server. V.1
# License: CC0 1.0
$host = "kinozal.tv"; # translated host
$host_scheme = "http"; # protocol type: "http" or "https"
# banning bots
if (strpos($_SERVER['HTTP_USER_AGENT'], 'http://')) die();
# decode name of subdomain
$request = rawurldecode($_SERVER['REQUEST_URI']);
$subdomain = '';
$a = explode('/', $request, 3);
if (count($a) > 2 && '.' == substr($a[1], -1)) {
$subdomain = $a[1];
$request = '/' . $a[2];
}
# translate browser headers
$headers = '';
foreach ($_SERVER as $name => $value) {
$a = explode('_', $name);
if (count($a) < 2 || $a[0] != 'HTTP' || $a[1] == 'X') continue;
array_shift($a);
$name = strtolower(implode('-', $a));
$headers .= $name . ': ' . str_replace($_SERVER['HTTP_HOST'], $host, $value) . "rn";
}
# send req to host
$context = stream_context_create([
'http' => [
'ignore_errors' => true,
'follow_location' => false,
'method' => $_SERVER['REQUEST_METHOD'],
'header' => $headers,
'timeout' => 60,
'content' => http_build_query($_POST)
]
]);
$url = $host_scheme .'://' . $subdomain . $host . $request;
$result = @file_get_contents($url, false, $context);
if (!isset($http_response_header) || !is_array($http_response_header)) {
die('Proxy error');
}
# respond headers to browser
foreach ($http_response_header as $header_line) {
$a = explode(':', $header_line, 2);
if (count($a) < 2) {
header($header_line, true);
continue;
}
$key = trim($a[0]);
$value = replace_host(trim($a[1]));
switch (strtolower($key)) {
case 'set-cookie':
$value = explode(';', $value)[0];
break;
case 'content-type':
$c_type = strtolower(explode('/', $value, 2)[0]);
}
header($key . ': ' . $value, false);
}
# update text content
echo (!isset($c_type) || $c_type != 'text') ? $result : replace_host($result);
function replace_host($text) {
global $host;
return preg_replace_callback(
'/(http:|https:|)(//)([a-z0-9-.]+.|)'. preg_quote($host). '/is',
function ($m) {
$t = $_SERVER['HTTPS'] == 'on' ? 'https://' : 'http://';
$t .= $_SERVER['HTTP_HOST'] . ($m[3] ? '/' . $m[3] : '');
return $t;
},
$text);
}
# end of file