• 用PHP实现反向代理服务器


    Part1:代码实例

      1 <?php 
      2 set_time_limit(60);
      3 if( !defined('__DIR__') )
      4 {
      5   define('__DIR__',dirname(__FILE__)) ;
      6 }
      7 
      8 $_REQUEST['url'] =gtRootUrl();
      9 //改成网站正式服务器ip
     10 $ip= '127.0.0.1';
     11 $aAccess = curl_init() ;
     12 // --------------------
     13 // set URL and other appropriate options
     14 curl_setopt($aAccess, CURLOPT_URL, $_REQUEST['url']);
     15 curl_setopt($aAccess, CURLOPT_HEADER, true);
     16 curl_setopt($aAccess, CURLOPT_RETURNTRANSFER, true);
     17 curl_setopt($aAccess, CURLOPT_FOLLOWLOCATION, false);
     18 curl_setopt($aAccess, CURLOPT_SSL_VERIFYPEER, false);  
     19 curl_setopt($aAccess, CURLOPT_SSL_VERIFYHOST, false);  
     20 curl_setopt($aAccess, CURLOPT_TIMEOUT, 60);
     21 curl_setopt($aAccess, CURLOPT_BINARYTRANSFER, true);
     22 //curl_setopt($aAccess, CURLOPT_HTTPPROXYTUNNEL, 0);
     23 curl_setopt($aAccess,CURLOPT_PROXY,$ip.':80');
     24 //curl_setopt($aAccess,CURLOPT_PROXY,'127.0.0.1:8888');
     25 
     26 if(!empty($_SERVER['HTTP_REFERER']))
     27     curl_setopt($aAccess,CURLOPT_REFERER,$_SERVER['HTTP_REFERER']) ;
     28 
     29 
     30 
     31 $headers=get_client_header();
     32 curl_setopt($aAccess,CURLOPT_HTTPHEADER,$headers) ;
     33 
     34 if( $_SERVER['REQUEST_METHOD']=='POST' )
     35 {
     36     curl_setopt($aAccess, CURLOPT_POST, 1);
     37     curl_setopt($aAccess, CURLOPT_POSTFIELDS, http_build_query($_POST));
     38 }
     39 
     40 
     41 // grab URL and pass it to the browser
     42 
     43 $sResponse = curl_exec($aAccess);
     44 list($headerstr,$sResponse)=parseHeader($sResponse);
     45 $headarr= explode("
    ", $headerstr);
     46 foreach($headarr as $h){
     47     if(strlen($h)>0){
     48         if(strpos($h,'Content-Length')!==false) continue;
     49         if(strpos($h,'Transfer-Encoding')!==false) continue;
     50         if(strpos($h,'Connection')!==false) continue;
     51         if(strpos($h,'HTTP/1.1 100 Continue')!==false) continue;
     52         header($h);
     53     }
     54 }
     55 
     56 function replace_html_path($arrMatche)
     57 {    
     58     $sPath = makeUrl($arrMatche[4]) ;
     59     if( strtolower($arrMatche[1])=='img' )
     60     {
     61         $sPath.= '&bin=1' ;
     62     }
     63     
     64     return "<{$arrMatche[1]} {$arrMatche[2]} {$arrMatche[3]}="{$sPath}"" ;
     65 }
     66 
     67 function get_client_header(){
     68     $headers=array();
     69     foreach($_SERVER as $k=>$v){
     70         if(strpos($k,'HTTP_')===0){
     71             $k=strtolower(preg_replace('/^HTTP/', '', $k));
     72             $k=preg_replace_callback('/_w/','header_callback',$k);
     73             $k=preg_replace('/^_/','',$k);
     74             $k=str_replace('_','-',$k);
     75             if($k=='Host') continue;
     76             $headers[]="$k:$v";
     77         }
     78     }
     79     return $headers;
     80 }
     81 
     82 function header_callback($str){
     83     return strtoupper($str[0]);
     84 }
     85 
     86 function parseHeader($sResponse){
     87     list($headerstr,$sResponse)=explode("
    
    ",$sResponse, 2);
     88     $ret=array($headerstr,$sResponse);
     89     if(preg_match('/^HTTP/1.1 d{3}/', $sResponse)){
     90         $ret=parseHeader($sResponse);
     91     }
     92     return $ret;
     93 }
     94 
     95 function gtRootUrl()
     96 {
     97 //缓存结果,同一个request不重复计算
     98  static $gtrooturl;
     99  if(empty($gtrooturl)){
    100     // Protocol
    101     $s = !isset($_SERVER['HTTPS']) ? '' : ($_SERVER['HTTPS'] == 'on') ? 's' : '';
    102     $protocol = strtolower($_SERVER['SERVER_PROTOCOL']);
    103     $protocol = substr($protocol,0,strpos($protocol,'/')).$s.'://';
    104     // Port
    105     $port = ($_SERVER['SERVER_PORT'] == 80) ? '' : ':'.$_SERVER['SERVER_PORT'];
    106     // Server name
    107     $server_name = isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'].$port : getenv('SERVER_NAME').$port;
    108     // Host
    109     $host = isset($_SERVER['HTTP_HOST']) ? strtolower($_SERVER['HTTP_HOST']) : $server_name;
    110 
    111      $gtrooturl=$protocol.$host.$_SERVER['REQUEST_URI'];
    112     }
    113         return $gtrooturl;
    114 }
    115 
    116 // close cURL resource, and free up system resources
    117 curl_close($aAccess);
    118 echo $sResponse ;
    View Code 

    思想参考原网址

    https://my.oschina.net/jamesren/blog/668495

  • 相关阅读:
    yocto/bitbake 学习资源
    QEMU/KVM学习资源
    ubuntu 中创建和删除用户
    git 重命名本地和远程分支
    Ubuntu 上搭建 FTP 服务器
    gdb 常见用法
    git log 显示与特定文件相关的 commit 信息
    基于 qemu system mode 运行 arm 程序
    基于 qemu user mode 运行 aarch64 程序
    checking in(airport)
  • 原文地址:https://www.cnblogs.com/jasonxu19900827/p/7810006.html
Copyright © 2020-2023  润新知