Jump to content

Socket Forwarder/Proxy


NathanR70

Recommended Posts

Hi all, I've am working on what seemed to be a simple PHP sockets task however I am running up against a wall and missing something here. The task goal is a simple socket forwarder/proxy of sorts where I point a browser proxy to the PHP CLI script listening on a local IP/port and catch all browser requests within the script and simply forward those requests onto the appropriate target server as specified in the GET/POST/HEAD/etc request captured from the browser. I then grab the target server reply and forward it back to the browser via the still open browser request socket. I am running into some blocking and/or timing issues I think although I may be wrong.

 

 

Any assistance/ideas are greatly appreciated.

 

-N

 

 

This PHP CLI script is running on Windows 7 with the following version info:

PHP 5.3.1 (cli) (built: Nov 19 2009 09:49:51)

Copyright © 1997-2009 The PHP Group

Zend Engine v2.3.0, Copyright © 1998-2009 Zend Technologies

 

 

 


error_reporting(0);
set_time_limit(0);

$listen_addr = '192.168.2.105';
$listen_port = 8080;

function msg($txt, $id = "msg") { print "[".$id."]: ".$txt."\r\n"; }

if (!($p_socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP))) msg("Failed to create proxy/listener socket", "p_socket"); else msg("Created proxy/listener socket", "p_socket: ".$p_socket);
if (!socket_set_option($p_socket, SOL_SOCKET, SO_REUSEADDR, 1)) msg("Failed to set socket option SO_REUSEADDR", "p_socket"); else msg("Socket option SO_REUSEADDR set", "p_socket: ".$p_socket);
//if (!socket_set_nonblock($p_socket)) msg("Failed to set socket to non-blocking", "p_socket"); else msg("Socket set to non-blocking", "p_socket: ".$p_socket); 
if (!socket_bind($p_socket, $listen_addr, $listen_port)) msg("Failed to bind socket to listener address/port", "p_socket"); else msg("Socket bound to listener address/port", "p_socket: ".$p_socket);
if (!socket_listen($p_socket)) msg("Failed to set socket to listen", "p_socket"); else msg("Socket set to listen", "p_socket: ".$p_socket);




do {

$c_socket = socket_accept($p_socket);
msg("Client connected", "c_socket");

msg("Setting non-block", "c_socket");
socket_set_nonblock($c_socket);

msg("Beginning read from client", "c_socket");
while( $client_buffer = socket_read($c_socket, 2048) ) { $client_request .= $client_buffer; }

msg("Done reading from client", "c_socket");
msg("Data read from client:\r\n----------\r\n".$client_request."----------\r\n", "c_socket");

preg_match("/^Host: (.*?)$/m", $client_request, $host_match);
$s_ip = gethostbyname(($host = trim($host_match[1])));

msg("Creating socket for connection to remote server");
$s_socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
msg("Socket created for connecting to remote server", "s_socket");

msg("Connecting to remote server", "s_socket");
socket_connect($s_socket, $s_ip, 80);
msg("Connected to remote server", "s_socket");

msg("Setting non-block", "s_socket");
socket_set_nonblock($s_socket);

msg("Forwarding request to remote server", "s_socket");
$b = socket_write($s_socket, $client_request);
msg("Sent ".$b." bytes to remote server", "s_socket");

msg("Beginning read from remote server", "s_socket");
while( $server_buffer = socket_read($s_socket, 2048) ) { $server_reply .= $server_buffer; }
msg("Done reading ".strlen($server_reply)." bytes from remote server", "s_socket");

msg("Closing socket", "s_socket");
socket_close($s_socket);
msg("Socket closed", "s_socket");

msg("Sending the server response to client", "c_socket");
socket_write($c_socket, $server_reply, strlen($server_reply));
msg("Server response sent to client", "c_socket");

msg("Closing socket", "c_socket");
socket_close($c_socket);
msg("Socket closed", "c_socket");

} while(true);

 

Link to comment
https://forums.phpfreaks.com/topic/194216-socket-forwarderproxy/
Share on other sites

Sorry about that, I don't know where my brain was.

 

Currently I am reading 0 bytes from the target server and obviously not delivering anything to the browser. But I have periodically overcome reading 0 bytes from the server by turning blocking on/off for the server socket.

 

I have read several documents/resources regarding socket blocking and I thought I had an understanding of it. I understand it to mean that with blocking enabled a PHP socket function (accept, read, write?) will block/pause the entire script until it returns and a PHP socket function where the socket is non-blocking will return immediately regardless of whether it has completed a read/write/accept/etc.

 

I see benefits and downsides both ways. For what I am attempting I would imagine that blocking might be ok because I simply want to slurp up the browser request and pass it to the target server, slurp up it's reply and pass it back to the browser. I am struggling though with determining (blocking enabled) when the browser is done giving me a request and further when server is done giving me back data. With blocking on I can't seem to get out of the block no matter what or how I test.

 

Am I misunderstanding socket blocking or missing a key point?

 

-N

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.