Snippets
Created by
Andrey Chesnakov
last modified
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 | <?php
//
// Site-Moved-Proxy v.0.2 (c) 2015 RuWeb.net
//
// Put theese 4 lines into .htaccess :
// RewriteEngine On
// RewriteCond %{REQUEST_FILENAME} !-f [OR]
// RewriteCond %{REQUEST_FILENAME} !\.(gif|png|jpg|jpeg|bmp|ico|css|js|zip|rar|gz|tgz|bz2|txt|pdf|doc|xls|docx|xlsx|exe)$
// RewriteRule !^.site-moved.php$ .site-moved.php [L]
//
// and specify new IP of your website in variable below:
$ip='127.0.0.1';
$myver='0.2';
$myname='Site-Moved-Proxy';
$timeout=30;
$timeout_connect=5;
if (($t=ini_get('max_execution_time')) && $timeout>=$t) $timeout=$t-1;
if ($_SERVER['SERVER_ADDR']==$ip) { echo "ERROR: new IP same as this server address<br>\n"; exit; }
$_SERVER['PATH_INFO']=$_SERVER['REDIRECT_URL'];
$fp = fsockopen ($ip, 80, $errno, $errstr, $timeout_connect);
if (!$fp) {
echo "$errstr ($errno)<br>\n";
} else {
stream_set_timeout($fp, $timeout);
$path=$_SERVER['PATH_INFO'].($_SERVER['QUERY_STRING']?"?$_SERVER[QUERY_STRING]":'');
$path=preg_replace('/ /','%20',$path);
fputs ($fp, "$_SERVER[REQUEST_METHOD] $path HTTP/1.0\r\n");
$headers = getallheaders();
foreach ($headers as $header => $value) {
# $logheaders.="$header: $value\r\n";
if (preg_match('/^Via$/i',$header)) {
if (preg_match("/ $myname/", $value)) { echo "ERROR: loop detected<br>\n"; exit; }
$value.="$value, $myver $myname";
$via=1;
} elseif (preg_match('/^X-Forwarded-For$/i',$header)) {
$value.="$value, $_SERVER[REMOTE_ADDR]";
$xff=1;
} elseif (preg_match('/^(Proxy-Connection|Content-Length|Connection)$/i',$header)) $header='';
if ($header) fputs ($fp, "$header: $value\r\n");
}
if (!$via) fputs ($fp, "Via: $myver $myname\r\n");
if (!$xff) fputs ($fp, "X-Forwarded-For: $_SERVER[REMOTE_ADDR]\r\n");
fputs ($fp, "Connection: close\r\n");
if ($_SERVER[REQUEST_METHOD]=='POST') {
if (!preg_match('#multipart/form-data;\s+boundary=([\w\-]+)#i',$_SERVER[CONTENT_TYPE],$marr)){
if ($_SERVER[CONTENT_LENGTH]) fputs ($fp, "Content-Length: $_SERVER[CONTENT_LENGTH]\r\n");
fputs ($fp, "\r\n");
$input = fopen('php://input','r');
while (!feof($input)) fputs ($fp,fread($input, 10240));
fclose($input);
} else {
$postdata='';
$boundary=$marr[1];
foreach($_POST as $name=>$data) {
$postdata .= '--'.$boundary."\r\n"
.'Content-Disposition: form-data; name="'.$name.'"'."\r\n\r\n"
.$data."\r\n"; // FIXME: If $data is an array, handle that correctly.
}
foreach($_FILES as $name=>$data) {
if (!is_uploaded_file($data['tmp_name'])) continue;
$postdata .= '--'.$boundary."\r\n"
.'Content-Disposition: form-data; name="'.$name.'";'
.' filename="'.$data['name'].'"'."\r\n";
$postdata .= 'Content-Type: '.$data['type']."\r\n\r\n".file_get_contents($data['tmp_name'])."\r\n";
}
$postdata .= '--'.$boundary."--\r\n";
fputs ($fp, "Content-Length: ".strlen($postdata)."\r\n");
fputs ($fp,"\r\n".$postdata);
}
fflush($fp);
} else {
if ($_SERVER[CONTENT_LENGTH]) fputs ($fp, "Content-Length: $_SERVER[CONTENT_LENGTH]\r\n");
fputs ($fp, "\r\n");
}
while ($line=fgets($fp)) {
if (trim($line)=='') break;
if (preg_match("#^HTTP/1.\d\s+(\d+)#i",$line,$arr)) $status=$arr[1];
elseif (preg_match("/^Content-Encoding:/i", $line)) ini_set('zlib.output_compression', 'Off');
elseif (preg_match("/^Content-Length:/i",$line)) $line='';
elseif (preg_match("/^(Pragma|Cache-Control):/i", $line))
if ($status<300 && preg_match("/no-cache/i", $line)) $line='';
# else $line=preg_replace("/max-age=65536/","max-age=604800", $line);
elseif (preg_match("#^Content-Type:\s+([^/]+)/(\S+)#i", $line,$content_type)) {
if ($content_type[1]!='text') ini_set('zlib.output_compression', 'Off');
}
if ($line) {
header($line);
# $logheaders.=$line;
}
}
while (!feof($fp)) echo fread($fp,10240);
fclose ($fp);
}
#echo "<!-- proxy\n$logheaders\n\n".($postdata?$postdata:file_get_contents('php://input'))."\n-->";
?>
|
Comments (1)
You can clone a snippet to your computer for local editing. Learn more.
Based on DA-Proxy v.0.14