mirror of
https://github.com/GeoffreyFrogeye/phroxyp.git
synced 2024-11-23 19:56:02 +01:00
Proxy now callable as an object
Well, "object"...
This commit is contained in:
parent
b287949ead
commit
bc30ea6f88
56
proxy.php
56
proxy.php
|
@ -1,23 +1,18 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
// PROXY
|
class Proxy {
|
||||||
// Config
|
public function __construct($serv, $port = 80, $root = '/', $localRoot = '') {
|
||||||
$serv = 'google.com'; // TODO External file
|
// Functions
|
||||||
$port = 80;
|
function str_replace_once($search, $replace, $subject) { // TODO Credit
|
||||||
$root = '';
|
|
||||||
$localRoot = '';
|
|
||||||
|
|
||||||
// Functions
|
|
||||||
function str_replace_once($search, $replace, $subject) { // TODO Credit
|
|
||||||
$pos = strpos($subject, $search);
|
$pos = strpos($subject, $search);
|
||||||
if ($pos === false) {
|
if ($pos === false) {
|
||||||
return $subject;
|
return $subject;
|
||||||
}
|
}
|
||||||
|
|
||||||
return substr($subject, 0, $pos) . $replace . substr($subject, $pos + strlen($search));
|
return substr($subject, 0, $pos) . $replace . substr($subject, $pos + strlen($search));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!function_exists('getallheaders')) { // TODO Credit
|
if (!function_exists('getallheaders')) { // TODO Credit
|
||||||
function getallheaders() {
|
function getallheaders() {
|
||||||
if (!is_array($_SERVER)) {
|
if (!is_array($_SERVER)) {
|
||||||
return array();
|
return array();
|
||||||
|
@ -31,26 +26,26 @@ if (!function_exists('getallheaders')) { // TODO Credit
|
||||||
}
|
}
|
||||||
return $headers;
|
return $headers;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Target determination
|
// Target determination
|
||||||
$metd = $_SERVER['REQUEST_METHOD'];
|
$metd = $_SERVER['REQUEST_METHOD'];
|
||||||
$reqp = $_SERVER['REQUEST_URI'];
|
$reqp = $_SERVER['REQUEST_URI'];
|
||||||
$reqp = str_replace_once($localRoot, '', $reqp); // TODO Reliable method
|
$reqp = str_replace_once($localRoot, '', $reqp); // TODO Reliable method
|
||||||
|
|
||||||
// Preparing request headers
|
// Preparing request headers
|
||||||
$reqHeds = "$metd $root$reqp HTTP/1.1\r\n";
|
$reqHeds = "$metd $root$reqp HTTP/1.1\r\n";
|
||||||
$reqHeds .= "Host: $serv:$port\r\n";
|
$reqHeds .= "Host: $serv:$port\r\n";
|
||||||
|
|
||||||
// Converting client request headers to server request headers
|
// Converting client request headers to server request headers
|
||||||
$reqsHedsC = getallheaders();
|
$reqsHedsC = getallheaders();
|
||||||
foreach ($reqsHedsC as $name => $content) {
|
foreach ($reqsHedsC as $name => $content) {
|
||||||
if ($name != 'Host' && $name != 'Connection') { // TODO More analysis
|
if ($name != 'Host' && $name != 'Connection') { // TODO More analysis
|
||||||
$reqHeds .= "$name: $content\r\n";
|
$reqHeds .= "$name: $content\r\n";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($metd == 'POST') { // TODO Other with data methods
|
if ($metd == 'POST') { // TODO Other with data methods
|
||||||
if (isset($_POST['payload'])) {
|
if (isset($_POST['payload'])) {
|
||||||
$postData = stripslashes($_POST['payload']);
|
$postData = stripslashes($_POST['payload']);
|
||||||
$reqHeds .= "Content-Length: ".strlen($postData)."\r\n";
|
$reqHeds .= "Content-Length: ".strlen($postData)."\r\n";
|
||||||
|
@ -64,15 +59,15 @@ if ($metd == 'POST') { // TODO Other with data methods
|
||||||
$postinfo = rtrim($postinfo, '&');
|
$postinfo = rtrim($postinfo, '&');
|
||||||
$reqHeds .= "\r\n" . $postinfo;
|
$reqHeds .= "\r\n" . $postinfo;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
$reqHeds .= "Connection: Close\r\n\r\n";
|
$reqHeds .= "Connection: Close\r\n\r\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
$fp = fsockopen($serv, $port, $errno, $errstr, 30);
|
$fp = fsockopen($serv, $port, $errno, $errstr, 30);
|
||||||
if (!$fp) { // TODO ErrorCode, ErrorDocument
|
if (!$fp) { // TODO ErrorCode, ErrorDocument
|
||||||
echo "Couldn't connect to server\n<br/>$errstr ($errno)<br />\n";
|
echo "Couldn't connect to server\n<br/>$errstr ($errno)<br />\n";
|
||||||
} else {
|
} else {
|
||||||
// Sending request
|
// Sending request
|
||||||
fwrite($fp, $reqHeds);
|
fwrite($fp, $reqHeds);
|
||||||
$resBuf = '';
|
$resBuf = '';
|
||||||
|
@ -96,5 +91,8 @@ if (!$fp) { // TODO ErrorCode, ErrorDocument
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fclose($fp);
|
fclose($fp);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
?>
|
?>
|
||||||
|
|
Loading…
Reference in a new issue