开发者

PHP HTTP Proxy Server

开发者 https://www.devze.com 2023-02-22 07:49 出处:网络
Does anyone know of a PHP script that runs as a proxy server (WITHOUT using a web server like Apache)? I am looking for a PHP based one so I can 开发者_开发技巧edit/modify the traffic that goes throug

Does anyone know of a PHP script that runs as a proxy server (WITHOUT using a web server like Apache)? I am looking for a PHP based one so I can 开发者_开发技巧edit/modify the traffic that goes through it (for security reasons). The closest thing I've found is philtron (http://philtron.sourceforge.net/), but this seems to be an outdated project and no longer works with PHP5.


You can use Nanoweb, a web server implemented in PHP. It comes with mod_proxy. Though manipulating the traffic will require a few more code changes. It wasn't intended for that. OTOH it's the most HTTP/1.1 compliant PHP solution you will find.


I'm not a big PHP expert and I'm sure one can implement webs server in PHP but that's not how it is done. If you don't want to use Apache try using nginx (lightweight, easy to change, etc).

Nevertheless I think your requirements are weird. It's like saying you want to write an HTML page in Assembly language. Doable but not the best way. If you need something like a proxy that can sniff on traffic you should use haproxy and modify it to your needs.


I too needed an HTTP proxy written in PHP, so that I can integrate it into a project that needs to interact with a 3rd party server that didn't implement CORS.

Here's my result. The script is tested with PHP-7.4.

<?php
 $target = substr($_SERVER["REQUEST_URI"], strlen($_SERVER["SCRIPT_NAME"]));
 #$url = "http://localhost:8008$target"; 
 $url = "https://example.com/$target"; 
 $req = curl_init();
 curl_setopt($req, CURLOPT_URL, $url);

 $hopbyhop = [];
 
 function handler_header($req, $header)
 {
   global $hopbyhop;
   header($header);
   $matches = [];
   if( preg_match('/^connection:(.*)$/i', $header, $matches) )
   {
     $hopts = explode(",", $matches[1]);
     foreach( $hopts as $hopt )
     {
       $ho = trim($hopt);
       $hopbyhop[] = $ho;
     }
   }
   return strlen($header);
 }

 function header_readycb()
 {
   global $hopbyhop;
   foreach( $hopbyhop as $hhdr )
   {
     header_remove($hhdr);
   }
 }

 header_register_callback("header_readycb");
 curl_setopt($req, CURLOPT_HEADERFUNCTION, "handler_header");
 curl_setopt($req, CURLOPT_CUSTOMREQUEST, $_SERVER["REQUEST_METHOD"]);

 $hdr = [];

 $prefix = "HTTP_";
 $conn = "HTTP_CONNECTION";
 $connopt = $_SERVER[$conn] ?? "";
 $connopts = explode(",", $connopt);
 foreach( $connopts as &$opt )
 {
   $opt = trim($opt);
   $opt = strtoupper($opt);
   $opt = preg_replace('/[^[:alnum:]]/', "_", $opt);
 }

 foreach( $_SERVER as $k => $v )
 {
   if( substr($k, 0, strlen($prefix)) !== $prefix ) continue;
   if( $k === "HTTP_HOST" ) continue;

   $i = 0;
   for($i=0; $i<count($connopts); $i++)
     if( "HTTP_".$connopts[$i] === $k ) break;
   if( $i < count($connopts) ) continue;

   $hdr[] = strtr(substr($k, strlen($prefix)), "_", "-").": $v";
 }

 curl_setopt($req, CURLOPT_HTTPHEADER, $hdr);

 curl_exec($req);

You can register different callbacks using curl_setopt to capture and modify the request and the response.

0

精彩评论

暂无评论...
验证码 换一张
取 消