直接上代码
<?php
ignore_user_abort(true);
ob_start();
ob_clean();
$init = init();
function init()
{
$init = null;
$init['server'] = 'www.gua64.com';
$init['method'] = strtolower(@$_SERVER['REQUEST_METHOD']);
$init['host'] = explode(':', @$_SERVER['HTTP_HOST'])[0];
$init['url'] = @$_SERVER['REQUEST_URI'];
return $init;
}
function getdata()
{
global $init;
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'https://' . $init['server'] . $init['url']);
curl_setopt($curl, CURLOPT_HEADER, FALSE);
curl_setopt($curl, CURLOPT_TIMEOUT, 9);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_ENCODING, '');
if ($init['method'] == 'post')
{
curl_setopt($curl, CURLOPT_POST, TRUE);
curl_setopt($curl, CURLOPT_POSTFIELDS, @file_get_contents('php://input'));
}
$body = curl_exec($curl);
curl_close($curl);
return $body;
}
/*********************
如果环境不支持 CURL 或者抓取失败可以使用 GET_FILE_CONTENTS 方法代替
function getdata()
{
global $init;
$content = @file_get_contents('php://input');
$header = @stream_context_create(['http' => ['method' => strtoupper($init['method']), 'content' => $content]]);
$body = @file_get_contents('https://' . $init['server'] . $init['url'], false, $header);
return $body;
}
*********************/
$htmls = getdata();
$htmls = str_replace($init['server'], $init['host'], $htmls);
if (is_numeric(strpos($init['url'], '.ico'))) header('Content-type: image/x-icon');
else if (is_numeric(strpos($init['url'], '.css'))) header('Content-type: text/css');
else if (is_numeric(strpos($init['url'], '.js'))) header('Content-type: application/x-javascript');
else if (is_numeric(strpos($init['url'], '.jpg'))) header('Content-type: image/jpeg');
else if (is_numeric(strpos($init['url'], '.png'))) header('Content-type: image/png');
echo $htmls;
ob_flush();
flush();
?>
配套.htaccess文件
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . / [L]
</IfModule>
配套nginx伪静态
if (!-f $request_filename){
set $rule_0 1$rule_0;
}
if ($rule_0 = "1"){
rewrite ^/ /index.php last;
}
web.config
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="a" stopProcessing="true">
<match url="." ignoreCase="false" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
</conditions>
<action type="Rewrite" url="/" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
2020.09.11更新
另推荐7ghost,有后台的反代php源码
https://github.com/shadoweb/7ghost
已修复大部分问题