之前写过Nginx反代方法的,那么DirectAdmin行不行呢,当然可以,首先新建一个.htaccess文件,目的是重定向所有请求.
Header unset Upgrade
<ifModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L]
</ifModule>
然后在PHP中分别针对两种请求进行转发.
<?php
// 拼接URL
function GetUrl()
{
$s = !isset($_SERVER['HTTPS']) ? '' : ($_SERVER['HTTPS'] == 'on') ? 's' : '';
$protocol = strtolower($_SERVER['SERVER_PROTOCOL']);
$protocol = substr($protocol,0,strpos($protocol,'/')).$s.'://';
$port = ($_SERVER['SERVER_PORT'] == 80) ? '' : ':'.$_SERVER['SERVER_PORT'];
$server_name = isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'].$port : getenv('SERVER_NAME').$port;
$host = isset($_SERVER['HTTP_HOST']) ? strtolower($_SERVER['HTTP_HOST']) : $server_name;
$GetUrl=$protocol.$host.$_SERVER['REQUEST_URI'];
}
// 遍历请求头
function HeaderCallback($str){
return strtoupper($str[0]);
}
function GetClientHeader(){
$headers=array();
foreach($_SERVER as $k=>$v){
if(strpos($k,'HTTP_')===0){
$k=strtolower(preg_replace('/^HTTP/', '', $k));
$k=preg_replace_callback('/_\w/','header_callback',$k);
$k=preg_replace('/^_/','',$k);
$k=str_replace('_','-',$k);
if($k=='Host') continue;
$headers[]="$k:$v";
}
}
return $headers;
}
function ParseHeader($ret){
list($headerstr,$ret)=explode("\r\n\r\n",$ret, 2);
$ret=array($headerstr,$ret);
if(preg_match('/^HTTP\/1\.1 \d{3}/', $ret)){
$ret=ParseHeader($ret);
}
return $ret;
}
$_REQUEST['url'] = GetUrl();
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://github.com/'.$_SERVER['REQUEST_URI']);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
if(!empty($_SERVER['HTTP_REFERER']))
curl_setopt($ch,CURLOPT_REFERER,$_SERVER['HTTP_REFERER']);
$headers=GetClientHeader();
curl_setopt($ch,CURLOPT_HTTPHEADER,$headers) ;
if( $_SERVER['REQUEST_METHOD']=='POST' )
{
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, file_get_contents('php://input'));
}
$ret = curl_exec($ch);
list($headerstr,$ret)=ParseHeader($ret);
$headarr= explode("\r\n", $headerstr);
foreach($headarr as $h){
if(strlen($h)>0){
if(strpos($h,'Content-Length')!==false) continue;
if(strpos($h,'Transfer-Encoding')!==false) continue;
if(strpos($h,'Connection')!==false) continue;
if(strpos($h,'HTTP/1.1 100 Continue')!==false) continue;
if(strpos($h,'Upgrade')!==false) continue;
if(strpos($h,'Location')){
header(str_replace('github.com',$_SERVER['SERVER_NAME'],$h));
}else{
header($h);
}
}
}
// 返回内容
echo $ret;
curl_close($ch);
当然缺点也很大,基本上能Clone多大的库,取决于你的DA资源限制(比如限制死的很容易Limit resources错误.)
部署截图.
速度明显有差距
而且当我想第二次再试,就已经触发资源限制,可能因为我这个虚拟主机太便宜吧.
而且因为每次是抓后存到内存再发回,所以暂存过程也占用很多内存.