Custom Application Proxy

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
programmingjeff
Forum Commoner
Posts: 26
Joined: Fri Jan 05, 2007 10:56 am

Custom Application Proxy

Post by programmingjeff »

I've been developing a Javascript/Ajax/PHP application for the past 9 months. The application basically has 2 primary functions: generate the primary page, and handle all ajax requests that come from the primary page. I've designed the application so that all requests go through a single file, index.php.

My goal is to keep the application on my own server, and allow my clients to log in remotely. Although they wouldn't mind doing this, they don't want their customers to see that a 3rd party is hosting their data. To avoid this, I've created a small proxy script that my clients can place on their servers. To any user using the proxy script, it seems like that file is the entire application, although it simply reroutes requests to my server.

The functions of the proxy are the following:
1. Redirect all GET requests to the server
2. Redirect data from the server back to the client, including redirects and cookies and body data.


I had this script working properly a while ago, but it doesn't work properly anymore. On some HTML pages that it serves, it inserts random text when there are more than 4 blank lines in a row., and at the beginning, and at the end. Any suggestions on why this would be happening? Any better suggestions?

Code: Select all

<?php

// Name of this script
$name = $_SERVER['PHP_SELF'];

// Path to this script
$thispath = 'http://localhost/';

// Auto-generated password
$password = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';


// Host of the primary application
$host = 'PATH.TO.MY.SERVER.COM';

// Path to the primary application
$path = 'http://'.$host.'/';


$headers = array();

$header = 'GET '.$path.'index.php?__proxypwd='.$password.'&__proxypath='.urlencode($thispath).'&__proxyname='.urlencode($name)
.(isset($_POST['username'])&&isset($_POST['username']) ? '&__proxyusername='.urlencode($_POST['username']).'&__proxypassword='.urlencode($_POST['password']) : '')
.'&'.$_SERVER['QUERY_STRING']." HTTP/1.1\r\nHost: $host\r\n";

$header_lookup = array(	'HTTP_USER_AGENT' => 'User-Agent',
						'HTTP_ACCEPT' => 'Accept',
						'HTTP_ACCEPT_LANGUAGE' => 'Accept-Language',
						'HTTP_ACCEPT_ENCODING' => 'Accept-Encoding',
						'HTTP_ACCEPT_CHARSET' => 'Accept-Charset'
);
foreach($header_lookup as $key => $name)
	if(isset($_SERVER[$key])) $header .= $name.': '.$_SERVER[$key]."\r\n";


// Add cookies
if(count($_COOKIE)) {
	$cookies = array();
	foreach($_COOKIE as $k => $v)
		$cookies[] = $k.'='.urlencode($v);
	$header .= 'Cookie: '.implode('; ',$cookies)."\r\n";
}

$header .= "Connection: Close\r\n\r\n";

// Open the socket and send it the request
$socket = fsockopen( $host, 80, $errno, $errstr);
if(!$socket) die("Socket Error");
fwrite($socket, $header);

// Get the response
$out = '';
while(!feof($socket)) {
	$s = fgets($socket, 128);
	$out .= $s;
}
fclose($socket);

// See if any cookies need to be set
preg_match("/Set-Cookie: ([^\r]+)/", $out, $match);
if(count($match) == 2) {
	$kv = explode('=', $match[1]);
	setcookie($kv[0],$kv[1]);
}


// Relocate
preg_match("/Location: ([^\r]+)/", $out, $match);
if(count($match) == 2) {
	header($match[1]);
	die();
}

// Remove the response headers
echo strchr($out, "\r\n\r\n");

?>


Sample output:

Code: Select all

bo57
<html>
...
</html>
0
Last edited by programmingjeff on Fri Jan 05, 2007 4:21 pm, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

What about using cURL or Snoopy?
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

What about using mod_proxy? (eg: http://www.apachetutor.org/admin/reverseproxies).
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I just realized this was in Critique. Moved to PHP - Code.
programmingjeff
Forum Commoner
Posts: 26
Joined: Fri Jan 05, 2007 10:56 am

Post by programmingjeff »

timvw wrote:What about using mod_proxy? (eg: http://www.apachetutor.org/admin/reverseproxies).
mod_proxy does exactly what I need it to do. The only problem is, how do I configure it on a 3rd party hosting site? Most of them don't allow you to mess around with httpd.conf...



@feyd: I apologize for placing this topic in the wrong category.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Talk to the host to see what they recommend.
Post Reply