= 4.1.0 required */ /* port to listen on */ $port=8000; /*favorite editor - give full path if not in $PATH*/ $editor = "vi"; /* create socket*/ $socket=socket_create(AF_INET, SOCK_STREAM, 0); /* bind socket to port*/ socket_bind($socket, "127.0.0.1", $port) or die ('Cannot bind'); /* begin listening */ socket_listen($socket); print "Configure your browser's proxy setting to point to 127.0.0.1 port $port\n\n"; flush(); /*while loop - keep accepting incoming connections*/ while (true) { /* accept request*/ $incoming = socket_accept($socket); $data = ""; $checkonce=1; $ispost=0; /*this loop is stores the browser request (GET or POST) into $data*/ while(($incoming_data = socket_read($incoming, 2048))!=false) { /*append $data with $incoming_data at every iteration*/ $data .= $incoming_data; /*check if request is POST. No need to do this more than once*/ if($checkonce) { if(strtolower(substr($data,0,4))=="post") $ispost=1; $checkonce=0; } $pos=strpos($data,"\r\n\r\n"); /*if there is \r\n\r\n in $data*/ if($pos!=false) { /*if this isn't a POST request, then the browser is done sending the request*/ if($ispost==0) { break; } /*otherwise, the data section of the POST request will follow after \r\n\r\n*/ else { /* find value of Content-Length: */ list($pre_length,$rest_header)=explode("Content-Length: ",$data,2); list($content_length,$remaining)=explode("\r\n",$rest_header,2); if(is_numeric($content_length)==true) { list($header,$rest)=explode("\r\n\r\n",$data); /*remaining length of data to be read is the difference between the value of Content-Length and the amount of data that has already been read after \r\n\r\n*/ $remaining_n=$content_length-strlen($rest); if($remaining_n>=0) { $incoming_data=socket_read($incoming,$remaining_n); $data.=$incoming_data; break; } } } } } /*create temporary file in /tmp and write contents of incoming data to it*/ $filename = "/tmp/proxy_".rand(); $file_handle = fopen ($filename,"w"); if(!$file_handle) die ("Unable to create $filename for writing."); fputs($file_handle,$data); fclose($file_handle); /* get value of URL submitted by browser into the $url variable*/ list($request,$url,$http)=explode(" ",$data,3); /*open in editor only if the request is POST*/ /*NOTE: Remove the if condition if you want to intercept GET requests also*/ if($ispost==1) { passthru ("$editor $filename > /dev/tty < /dev/tty"); } /*after the user is done editing the request, re-read the temporary file*/ $file_handle = fopen ($filename,"r"); if(!$file_handle) die ("Unable to open $filename for reading."); $data=""; /*store data in file into $data*/ do { $databuf = fread($file_handle,4096); if(strlen($databuf)==0) break; $data .= $databuf; }while (true); fclose($file_handle); /*remove temp file*/ unlink($filename); /*get the request(GET or POST), the target url, and the rest of the request*/ list($request,$url,$rest_header)=explode(" ",$data,3); /*replace HTTP/1.x with HTTP/1.0. This way we can avoid the Keep-Alive requests*/ list($http_ver,$rest_rest_header)=explode("\r\n",$rest_header,2); $rest_header="HTTP/1.0\r\n".$rest_rest_header; print "\n\nURL: $url\n"; flush(); /*$url should contain the string http://x so it should be greater than 7 in length*/ if(strlen($url)<=7) { print "Unknown request from web browser.\n"; flush(); close($incoming); continue; } /*change $url to $host, ie strip everything after the /*/ list($host,$rest)=explode("/",substr($url,7),2); /*is this a POST request?*/ if($ispost) { /*re-calculate length from data obtained in temporary file*/ list($realheader,$realdata)=explode("\r\n\r\n",$data,2); list($pre_length,$rest_header)=explode("Content-Length: ",$rest_header,2); list($actual_length,$rest_rest_header)=explode("\r\n",$rest_header,2); $rest=" /".$rest." ".$pre_length."Content-Length: ".(strlen($realdata))."\r\n".$rest_rest_header; $data=$request.$rest; } else $data=$request." /".$rest." ".$rest_header; /*connect to target host*/ print "Connectiong to $host.."; flush(); $fsocket=fsockopen($host,80); if(!$fsocket) { print "Could not connect to $host\n"; flush(); close($incoming); continue; } print "Connected!..Writing request..."; flush(); /*Remove Proxy-Connection: line*/ list($pre,$proxy_connection)=explode("Proxy-Connection: ",$data,2); list($until_eol,$rest)=explode("\r\n",$proxy_connection,2); $data=$pre.$rest; /*write outgoing request*/ fwrite($fsocket,$data,strlen($data)); $response=""; print "Reading Response..."; flush(); /*read response from server*/ while (!feof($fsocket)) { $response_buf=fread($fsocket,1024); $response.=$response_buf; } fclose($fsocket); /*send resposne from server to the browser*/ socket_write($incoming,$response,strlen($response)); /*ok we are done here, close browser connection*/ socket_close($incoming); print "Done serving $url\n"; flush(); } ?>