From e9568c0a5c6ddd963c5fb944cdd3b32fa1479eb6 Mon Sep 17 00:00:00 2001 From: James Bunton Date: Sat, 23 Jun 2007 22:15:21 +1000 Subject: [PATCH] Initial checkin --- path.php | 7 +++ proxy.php | 139 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 146 insertions(+) create mode 100644 path.php create mode 100644 proxy.php diff --git a/path.php b/path.php new file mode 100644 index 0000000..a17cd8b --- /dev/null +++ b/path.php @@ -0,0 +1,7 @@ + diff --git a/proxy.php b/proxy.php new file mode 100644 index 0000000..fdbf912 --- /dev/null +++ b/proxy.php @@ -0,0 +1,139 @@ +"; + print_r($_GET); + print_r($_POST); + print_r($_COOKIE); + print_r($_SERVER); + print_r(apache_request_headers()); + print ""; + die(); +} + +function debug($msg) { + $fp = fopen("/tmp/debug.log", "a"); + fwrite($fp, "===\n"); + fwrite($fp, $msg); + fwrite($fp, "\n"); + fclose($fp); +} + +function getUrl($url) { + if(isset($_SERVER["PATH_INFO"])) { + $url .= $_SERVER["PATH_INFO"]; + } + $url = parse_url($url); + if(!isset($url["port"])) { + $url["port"] = 80; + if($url["scheme"] == "https") { + $url["port"] = 443; + } + } + if(strlen($_SERVER["QUERY_STRING"]) > 0) { + $url["path"] = $url["path"] . "?" . $_SERVER["QUERY_STRING"]; + } + return $url; +} + +function generateRequest($rmeth, $host, $path) { + $crlf = "\r\n"; + + $req = $rmeth . " " . $path . " HTTP/1.0" . $crlf; + + $headers = apache_request_headers(); + if(isset($headers["Host"])) { + $req .= "Host: " . $headers["Host"] . $crlf; + } + if(isset($headers["Cookie"])) { + $req .= "Cookie: " . $headers["Cookie"] . $crlf; + } + if(isset($headers["Cookie2"])) { + $req .= "Cookie2: " . $headers["Cookie2"] . $crlf; + } + if(isset($headers["Referer"])) { + $req .= "Referer: " . $headers["Referer"] . $crlf; + } + if(isset($headers["Content-Length"])) { + $req .= "Content-Length: " . $headers["Content-Length"] . $crlf; + } + if(isset($headers["Content-Type"])) { + $val = $headers["Content-Type"]; + $val = str_replace("PHPHACK", "", $val); + $req .= "Content-Type: " . $headers["Content-Type"] . $crlf; + } + + $req .= $crlf; + + return $req; +} + +function doProxy($scheme, $rmeth, $host, $port, $path) +{ + $req = generateRequest($rmeth, $host, $path); + + // Make the HTTP request + $fp = fsockopen(($scheme == 'https' ? 'ssl://' : '') . $host, $port); + if(!$fp) { + die("Could not connect to internal server!"); + } + fwrite($fp, $req); + if($rmeth == "POST") { + $fpi = fopen("php://input", "rb"); + while(!feof($fpi)) { + fwrite($fp, fread($fpi, 1024)); + } + fclose($fpi); + } + + $headers = array(); + $mode = 0; + while(is_resource($fp) && $fp && !feof($fp)) { + // Parsing headers + if($mode == 0) { + // Grab a line + $line = fgets($fp); + + if(strlen(trim($line)) != 0) { + array_push($headers, $line); + } else { + $mode = 1; + foreach($headers as $header) { + header($header); + } + } + } + // Body data! + else { + myPassThru($fp); + } + } + + fclose($fp); +} + +function myPassThru($fp) { + $data = ""; + while(!feof($fp)) { + $data .= fread($fp, 1024); + } + $data = str_replace("enctype=\"multipart/form-data\"", "enctype=\"PHPHACKmultipart/form-data\"", $data); + print $data; +} + +function main($basePath) { + ///debugAndDie(); + + $u = getUrl($basePath); + $rmeth = $_SERVER["REQUEST_METHOD"]; + doProxy($u["scheme"], $rmeth, $u["host"], $u["port"], $u["path"]); +} + +// Run! +if(!isset($PROXY)) { + die("No forwarding available!"); +} + +main($PROXY); + +?> -- 2.39.2