./c0de/php/rproxy.txt

0.
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.
62.
63.
64.
65.
66.
67.
68.
69.
70.
71.
72.
73.
74.
75.
76.
77.
78.
79.
80.
81.
82.
83.
84.
85.
86.
87.
88.
89.
90.
91.
92.
93.
94.
95.
96.
97.
98.
99.
100.
101.
102.
103.
104.
105.
106.
107.
108.
109.
110.
111.
112.
113.
114.
115.
116.
117.
118.
119.
120.
121.
122.
123.
124.
125.
126.
127.
128.
129.
130.
131.
132.
133.
134.
135.
136.
137.
138.
139.
140.
141.
142.
143.
144.
145.
146.
147.
148.
149.
150.
151.
152.
153.
154.
155.
156.
157.
158.
159.
160.
161.
162.
163.
164.
165.
166.
167.
168.
169.
170.
171.
172.
173.
174.
175.
176.
177.
178.
179.
180.
181.
182.
183.
184.
185.
186.
187.
188.
189.
190.
191.
192.
193.
194.
195.
196.
197.
198.
199.
200.
201.
202.
203.
204.
205.
206.
207.
<?php
/*
 *
 * Name: RProxy
 * Credits: charles "real" F. <charlesfol[at]hotmail.fr>
 * Date: 26/04/08
 *
 * RProxy permits you to get an HIGH ANONYMOUS HTTP proxy
 * with a host that just supports an apache webserver.
 *
 *
 *       +-> Local Socket -> PHP file -+
 *       |                             |
 *     Client                      Remote Host
 *       |                             |
 *       +- Local Socket <- PHP File <-+
 *
 * When the local listening socket receives an http request,
 * it sends it to the remote PHP file, which execute it.
 * Then the PHP file sends the response to the local socket,
 * which transmit it to the client.
 *
 * The advantage of RProxy is that it's usable everywhere
 * and really discret :)
 * It just needs an apache server to work, because a PHPfile
 * exec your HTTP Request for you.
 *
 * howto:
 * 1. Host this script on a remote host, like a free.fr FTP
 * (eg http://server.com/proxy.php)
 * 2. Launch the script, on your own computer, in CLI:
 *  $ php proxy.php http://server.com/proxy.php
 *  RProxy ready, listening on port 8888
 * 3. Now use this proxy like a normal one, localhost:8888.
 *
 * I made a very precisely commented code, expecting you to
 * understand it.
 *
 *
 * [ Done ]
 *
 * 26/03: Boundary mode is now supported.
 *        No matter  the request size,  because
 *        it's now splited into pieces.
 *        ($header_max var)
 * 23/04: Modified  a little Local Socket code.
 *        PHP did not like  \r\n  concatenation
 *        but I don't know why.
 *        Remote Host headers are now used, in-
 *        stead of PHP file's headers.
 * 09/06: A little update to support FF post.
 *
 *
 */
 
/* Local Socket Configuration */
$port		= 8888; # Port of your local listening socket
$max_conn	= 10;	# Maximum number of connections to your local socket
$header_max	= 1000; # Max header size.
 
#
# Part #1 [] Local Socket
#
 
# Client -> HTTP Request -> Local Socket -> Remote PHP file -> Local Socket -> HTTP Response -> Client
if(isset($argc) && $argc>1)
{
	$url = $argv[1];
 
	$handle = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
	socket_bind($handle, '127.0.0.1', $port);
	socket_listen($handle,$max_conn);
 
	print "RProxy ready, listening on port $port";
 
	while(TRUE)
	{
		$packet = '';
 
		# Client -> HTTP Request -> Local Socket
		# The Local Socket receives an HTTP Request from Client
 
		if(!$_client = socket_accept($handle)) exit("socket_accept(): error ($handle).");
 
		$client_request = '';
 
		while( (eregi("Content-type.*boundary",$client_request) && !eregi("--[0-9]+--\r?\n",$client_request)) 
		    || (eregi("^POST",$client_request) && !ereg("\r\n\r\n.+",$client_request))
		    || !ereg("\r\n\r\n",$client_request) )
			{ $client_request .= socket_read($_client, 2048, PHP_BINARY_READ); }
 
		preg_match("#Host: +([^\t\r\n]+)#i",$client_request,$client_host) or exit();
		$client_host = $client_host[1];
 
		print "\n".d()." -> $client_host ".strlen($client_request);
 
		# Local Socket -> Remote PHP file
		# The Local Socket sends the HTTP Request to the PHP file
 
		$infos      = parse_url($url);
		$phpf_host  = $infos['host'];
		$phpf_port  = isset($infos['port']) ? $infos['port'] : 80;
 
		$total = str_split(base64_encode($client_request),$header_max);
 
		$request  = "GET $url HTTP/1.1\r\n";
		$request .= "Host: $phpf_host\r\n";
		$request .= "User-Agent: Mozilla Firefox 5.0\r\n";
 
		for($i=0;$i<sizeof($total);$i++)
			$request .= "HTTP-Request-$i: ".$total[$i]."\r\n";
 
		$request .= "Connection: Close\r\n";
		$request .= "\r\n";
 
		# Remote PHP file -> Local Socket
		# The Local Socket receives the HTTP Response from the PHP file
 
		$_phpf = fsockopen($phpf_host,80);
		fputs($_phpf,$request);
 
		# Local Socket -> Client
		# The HTTP Response is transmitted by the Local Socket to the Client
 
		$http_response = '';
		while(!feof($_phpf)) $http_response .= fgets($_phpf);
		fclose($_phpf);
 
 
		# Remove HTTP Headers from PHP file's HTTP Response
		$code = explode("\r\n\r\n",$http_response);
		$http_response = '';
		for($i=1;$i<sizeof($code);$i++)
			$http_response .= $code[$i]."\r\n\r\n";
 
		$http_response = preg_replace("#^.*[\r\n]*HTTP#i","HTTP",$http_response);
		$http_response = preg_replace('#0[\r\n]*$#','',$http_response);
 
		socket_write($_client,$http_response,strlen($http_response));
 
		print "\n".d()." <- $client_host ".strlen($http_response);
 
		socket_close($_client);
	}
 
 
	exit();
}
 
#
# Part #2 [] Remote PHP File
#
 
if(!isset($_SERVER['HTTP_HTTP_REQUEST_0']))
{
	header("Location: http://google.com/");
	exit();
}
 
# Local Socket -> Remote PHP file -> Remote Host -> Remote PHP file -> Local Socket
 
# Local Socket -> Remote PHP file
# The PHP File receive the HTTP Request he must do
 
 
$client_request = '';
for($i=0;isset($_SERVER["HTTP_HTTP_REQUEST_$i"]);$i++)
	$client_request .= $_SERVER["HTTP_HTTP_REQUEST_$i"];
 
$client_request = base64_decode($client_request);
 
preg_match("#Host: +([^\t\r\n]+)#i",$client_request,$rhostname) or exit();
$rhostname = $rhostname[1];
 
# Clear client request
 
$clearheaders = array('Keep-Alive','Proxy-Connection','Connection');
for($i=0;$i<sizeof($clearheaders);$i++) $clearheaders[$i] = '#'.$clearheaders[$i].':.+\r\n#i';
 
$client_request = preg_replace($clearheaders,'',$client_request);
$client_request = preg_replace("#(Host:.+\r\n)#i","$1Connection: close\r\n",$client_request);
 
# Remote PHP file -> Remote Host
# The PHP file sends the HTTP Request
 
$_rhost = fsockopen($rhostname,80);
fputs($_rhost,$client_request);
 
# Remote Host -> Remote PHP file
# The PHP file receives the HTTP Response
 
$rhost_response = '';
while(!feof($_rhost)) $rhost_response .= fgets($_rhost);
fclose($_rhost);
 
# Remote PHP file -> Local Socket
# The PHP file displays the HTTP Response,
# which is recovered by the Local Socket
 
print $rhost_response;
 
function d()
{
	return date("H:i:s");
}
 
?>

Back