侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

PHP反向代理类代码

php  /  管理员 发布于 7年前   192

改自PHP Reverse Proxy PRP,修改了原版中的一些错误,支持了文件上传以及上传文件类型识别,支持指定IP,自适应SAE环境。

使用方法

port="8080";$proxy->host="";//$proxy->ip="1.1.1.1";$proxy->forward_path="";$proxy->connect();$proxy->output();?>

源代码

version="PHP Reverse Proxy (PRP) 1.0";  $this->port="8080";  $this->host="127.0.0.1";  $this->ip="";  $this->content="";  $this->forward_path="";  $this->path="";  $this->content_type="";  $this->user_agent="";  $this->http_code="";  $this->XFF="";  $this->request_method="GET";  $this->IMS=false;  $this->cacheTime=72000;  $this->lastModified=gmdate("D, d M Y H:i:s",time()-72000)." GMT";  $this->cookie="";  $this->XRequestedWith = "";  $this->authorization = ""; } function translateURL($serverName) {  $this->path=$this->forward_path.$_SERVER['REQUEST_URI'];  if(IS_SAE)   return $this->translateServer($serverName).$this->path;  if($_SERVER['QUERY_STRING']=="")   return $this->translateServer($serverName).$this->path;  else  return $this->translateServer($serverName).$this->path."?".$_SERVER['QUERY_STRING']; } function translateServer($serverName) {  $s = empty($_SERVER["HTTPS"]) ? ''   : ($_SERVER["HTTPS"] == "on") ? "s"   : "";  $protocol = $this->left(strtolower($_SERVER["SERVER_PROTOCOL"]), "/").$s;  if($this->port=="")    return $protocol."://".$serverName;  else   return $protocol."://".$serverName.":".$this->port; } function left($s1, $s2) {  return substr($s1, 0, strpos($s1, $s2)); } function preConnect(){  $this->user_agent=$_SERVER['HTTP_USER_AGENT'];  $this->request_method=$_SERVER['REQUEST_METHOD'];  $tempCookie="";  foreach ($_COOKIE as $i => $value) {   $tempCookie=$tempCookie." $i=$_COOKIE[$i];";  }  $this->cookie=$tempCookie;  if(empty($_SERVER['HTTP_X_FORWARDED_FOR'])){   $this->XFF=$_SERVER['REMOTE_ADDR'];  } else {   $this->XFF=$_SERVER['HTTP_X_FORWARDED_FOR'].", ".$_SERVER['REMOTE_ADDR'];  }  } function connect(){  if(empty($_SERVER['HTTP_IF_MODIFIED_SINCE'])){   $this->preConnect();   $ch=curl_init();   if($this->request_method=="POST"){    curl_setopt($ch, CURLOPT_POST,1);     $postData = array();    $filePost = false;    $uploadPath = 'uploads/';    if (IS_SAE)      $uploadPath = SAE_TMP_PATH;     if(count($_FILES)>0){      if(!is_writable($uploadPath)){        die('You cannot upload to the specified directory, please CHMOD it to 777.');      }      foreach($_FILES as $key => $fileArray){         copy($fileArray["tmp_name"], $uploadPath . $fileArray["name"]);        $proxyLocation = "@" . $uploadPath . $fileArray["name"] . ";type=" . $fileArray["type"];        $postData = array($key => $proxyLocation);        $filePost = true;      }    }     foreach($_POST as $key => $value){      if(!is_array($value)){     $postData[$key] = $value;      }      else{     $postData[$key] = serialize($value);      }    }     if(!$filePost){      //$postData = http_build_query($postData);      $postString = "";      $firstLoop = true;      foreach($postData as $key => $value){      $parameterItem = urlencode($key)."=".urlencode($value);      if($firstLoop){     $postString .= $parameterItem;      }      else{     $postString .= "&".$parameterItem;      }      $firstLoop = false;       }      $postData = $postString;    }     //echo print_r($postData);     //curl_setopt($ch, CURLOPT_VERBOSE, 0);    //curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);    //curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible;)");    $this->sendPost = $postData;    //var_dump(file_exists(str_replace('@','',$postData['imgfile'])));exit;    curl_setopt($ch, CURLOPT_POSTFIELDS,$postData);    //curl_setopt($ch, CURLOPT_POSTFIELDS,file_get_contents($proxyLocation));    //curl_setopt($ch, CURLOPT_POSTFIELDS,file_get_contents("php://input"));   }    //gets rid of mulitple ? in URL   $translateURL = $this->translateURL(($this->ip)?$this->ip:$this->host);   if(substr_count($translateURL, "?")>1){     $firstPos = strpos($translateURL, "?", 0);     $secondPos = strpos($translateURL, "?", $firstPos + 1);     $translateURL = substr($translateURL, 0, $secondPos);   }    curl_setopt($ch,CURLOPT_URL,$translateURL);    $proxyHeaders = array(     "X-Forwarded-For: ".$this->XFF,     "User-Agent: ".$this->user_agent,     "Host: ".$this->host   );    if(strlen($this->XRequestedWith)>1){     $proxyHeaders[] = "X-Requested-With: ".$this->XRequestedWith;     //echo print_r($proxyHeaders);   }    curl_setopt($ch,CURLOPT_HTTPHEADER, $proxyHeaders);    if($this->cookie!=""){    curl_setopt($ch,CURLOPT_COOKIE,$this->cookie);   }   curl_setopt($ch,CURLOPT_FOLLOWLOCATION,false);    curl_setopt($ch,CURLOPT_AUTOREFERER,true);    curl_setopt($ch,CURLOPT_HEADER,true);   curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);    $output=curl_exec($ch);   $info = curl_getinfo( $ch );   curl_close($ch);   $this->postConnect($info,$output);  }else {   $this->lastModified=$_SERVER['HTTP_IF_MODIFIED_SINCE'];   $this->IMS=true;  } } function postConnect($info,$output){  $this->content_type=$info["content_type"];  $this->http_code=$info['http_code'];  //var_dump($info);exit;  if(!empty($info['last_modified'])){   $this->lastModified=$info['last_modified'];  }  $this->resultHeader=substr($output,0,$info['header_size']);  $content = substr($output,$info['header_size']);   if($this->http_code=='200'){   $this->content=$content;  }elseif( ($this->http_code=='302' || $this->http_code=='301') && isset($info['redirect_url'])){   $redirect_url = str_replace($this->host,$_SERVER['HTTP_HOST'],$info['redirect_url']);   if (IS_SAE)     $redirect_url = str_replace('http://fetchurl.sae.sina.com.cn/','',$info['redirect_url']);   header("Location: $redirect_url");   exit;  }elseif($this->http_code=='404'){   header("HTTP/1.1 404 Not Found");   exit("HTTP/1.1 404 Not Found");  }elseif($this->http_code=='500'){   header('HTTP/1.1 500 Internal Server Error');   exit("HTTP/1.1 500 Internal Server Error");  }else{   exit("HTTP/1.1 ".$this->http_code." Internal Server Error");  } }  function output(){  $currentTimeString=gmdate("D, d M Y H:i:s",time());  $expiredTime=gmdate("D, d M Y H:i:s",(time()+$this->cacheTime));   $doOriginalHeaders = true;  if($doOriginalHeaders){    if($this->IMS){     header("HTTP/1.1 304 Not Modified");     header("Date: Wed, $currentTimeString GMT");     header("Last-Modified: $this->lastModified");     header("Server: $this->version");    }else{      header("HTTP/1.1 200 OK");     header("Date: Wed, $currentTimeString GMT");     header("Content-Type: ".$this->content_type);     header("Last-Modified: $this->lastModified");     header("Cache-Control: max-age=$this->cacheTime");     header("Expires: $expiredTime GMT");     header("Server: $this->version");     preg_match("/Set-Cookie:[^\n]*/i",$this->resultHeader,$result);     foreach($result as $i=>$value){      header($result[$i]);     }     preg_match("/Content-Encoding:[^\n]*/i",$this->resultHeader,$result);     foreach($result as $i=>$value){      //header($result[$i]);     }     preg_match("/Transfer-Encoding:[^\n]*/i",$this->resultHeader,$result);     foreach($result as $i=>$value){      //header($result[$i]);     }     echo($this->content);     /*     if(stristr($this->content, "error")){    echo print_r($this->sendPost);     }     */    }  }  else{    $headerString = $this->resultHeader; //string     $headerArray = explode("\n", $headerString);    foreach($headerArray as $privHeader){   header($privHeader);    }     if(stristr($headerString, "Transfer-encoding: chunked")){   flush();   ob_flush();   $i = 0;   $maxLen = strlen($this->content);    while($i < $maxLen){     $endChar = $i + self::chunkSize;     if($endChar >= $maxLen){    $endChar = $maxLen - 1;     }     $chunk = substr($this->content, $i, $endChar);     $this->dump_chunk($chunk);     flush();     ob_flush();     $i = $i + $endChar;   }    }    else{    echo($this->content);    }     //echo "header: ".print_r($headerArray);    //header($this->resultHeader);  }  }   function dump_chunk($chunk) {   echo sprintf("%x\r\n", strlen($chunk));   echo $chunk;   echo "\r\n"; }   function getOutsideHeaders(){   $headers = array();   foreach ($_SERVER as $name => $value){   if (substr($name, 0, 5) == 'HTTP_') {     $name = str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))));     $headers[$name] = $value;   }elseif ($name == "CONTENT_TYPE") {     $headers["Content-Type"] = $value;   }elseif ($name == "CONTENT_LENGTH") {     $headers["Content-Length"] = $value;   }elseif(stristr($name, "X-Requested-With")) {     $headers["X-Requested-With"] = $value;    $this->XRequestedWith = $value;  }   }     //echo print_r($headers);    $this->outsideHeaders = $headers;   return $headers; }  }?>

您可能感兴趣的文章:

  • php采集中国代理服务器网的方法
  • php在线代理转向代码
  • php设计模式 Proxy (代理模式)
  • php中通过虚代理实现延迟加载的实现代码
  • PHP 反射机制实现动态代理的代码
  • php使用curl并发减少后端访问时间的方法分析
  • PHP使用curl模拟post上传及接收文件的方法
  • PHP Curl模拟登录微信公众平台、新浪微博实例代码
  • php curl抓取网页的介绍和推广及使用CURL抓取淘宝页面集成方法
  • php使用curl通过代理获取数据的实现方法


  • 上一条:
    php中将一段数据存到一个txt文件中并显示其内容
    下一条:
    php获取apk包信息的方法
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • PHP 8.4 Alpha 1现已发布!(0个评论)
    • 用Time Warden监控PHP中的代码处理时间(0个评论)
    • 在PHP中使用array_pop + yield实现读取超大型目录功能示例(0个评论)
    • Property Hooks RFC在PHP 8.4中越来越接近现实(0个评论)
    • 近期文章
    • 智能合约Solidity学习CryptoZombie第三课:组建僵尸军队(高级Solidity理论)(0个评论)
    • 智能合约Solidity学习CryptoZombie第二课:让你的僵尸猎食(0个评论)
    • 智能合约Solidity学习CryptoZombie第一课:生成一只你的僵尸(0个评论)
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2016-11
    • 2017-06
    • 2017-07
    • 2017-08
    • 2017-09
    • 2017-11
    • 2017-12
    • 2018-01
    • 2018-02
    • 2018-03
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2020-07
    • 2020-09
    • 2021-02
    • 2021-03
    • 2021-04
    • 2021-05
    • 2021-06
    • 2021-07
    • 2021-08
    • 2021-09
    • 2021-10
    • 2021-11
    • 2021-12
    • 2022-01
    • 2022-02
    • 2022-05
    • 2022-06
    • 2022-07
    • 2022-08
    • 2022-09
    • 2022-10
    • 2022-11
    • 2022-12
    • 2023-01
    • 2023-02
    • 2023-03
    • 2023-04
    • 2023-05
    • 2023-06
    • 2023-07
    • 2023-08
    • 2023-09
    • 2023-10
    • 2023-11
    • 2023-12
    • 2024-01
    • 2024-02
    • 2024-03
    • 2024-04
    • 2024-05
    • 2024-06
    • 2024-07
    • 2024-09
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客