landysaccount Posted September 5, 2010 Share Posted September 5, 2010 Hello. This is a follow up on a previous thread. I'm trying to redirect a socket connection via browser to another script or page but, I'm having a lot of problems trying to do it. I don't know if it can be done. Please guide me to where I can get some input on it. I've google about it and nothing exactly... The socket file looks like this: $host = "172.16.0.3"; $port = "200"; // don't timeout! set_time_limit(0); // create socket $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP ) or die("Could not create socket\n"); // bind socket to port $result = socket_bind($socket, $host, $port) or die("Could not bind to socket\n"); // start listening for connections $result = socket_listen($socket, 3) or die("Could not set up socket listener\n"); while( true ){ // accept incoming connections // spawn another socket to handle communication $spawn = socket_accept($socket) or die("Could not accept incoming connection\n"); // read client input $input = socket_read($spawn, 1024) or die("Could not read input\n"); // clean up input string $input = trim($input); // reverse client input and send back $output = strrev($input) . "\n"; header('Location: http://www.cnn.com/'); $output = "hello there.. you're connected"; socket_write($spawn, $output, strlen ($output)) or die("Could not write output\n"); } // close sockets socket_close($spawn); socket_close($socket); ?> I put in the browser's addr bar: http://172.16.0.3:200 and get "hello there.. you're connected"... Can it be done?? Any suggestions? Thanks in advanced for your help. Quote Link to comment https://forums.phpfreaks.com/topic/212565-redirect-connection-with-sockets/ Share on other sites More sharing options...
landysaccount Posted September 6, 2010 Author Share Posted September 6, 2010 I think I got a socket redirect the client to a different page like this: $output = '<meta http-equiv="content-type" content="text/html; charset=windows-1250" />' . '<meta name="generator" content="PSPad editor, www.pspad.com" />' . '<meta http-equiv="Expires" content="Tue, 20 Aug 1996 14:25:27 GMT" />' . '<meta http-equiv="Cache-Control" content="no-cache; must-revalidate" />'. '<meta HTTP-EQUIV="REFRESH" content="0; url=http://www.cnn.com">'; socket_write($spawn, $output, strlen ($output)) or die("Could not write output\n"); Quote Link to comment https://forums.phpfreaks.com/topic/212565-redirect-connection-with-sockets/#findComment-1107709 Share on other sites More sharing options...
landysaccount Posted September 6, 2010 Author Share Posted September 6, 2010 After testing, I notice it works with IE8, and Mozilla but, not with Chrome. I just get this as plain text on the brower: <meta http-equiv="content-type" content="text/html; charset=windows-1250" /><meta name="generator" content="PSPad editor, www.pspad.com" /><meta http-equiv="Expires" content="Tue, 20 Aug 1996 14:25:27 GMT" /><meta http-equiv="Cache-Control" content="no-cache; must-revalidate" /><meta HTTP-EQUIV="REFRESH" content="0; url=http://www.cnn.com"> Don't know why? Quote Link to comment https://forums.phpfreaks.com/topic/212565-redirect-connection-with-sockets/#findComment-1107712 Share on other sites More sharing options...
landysaccount Posted September 6, 2010 Author Share Posted September 6, 2010 I thought I almost had this resolved but I'm hitting a wall. I'm currently testing doing a redirect with iptables to the port the socket script/daemon is listening on. This script will be doing user authentication, add some iptables rules, and saving some info to db. Now, I would like to know the address and uri request the user was trying to go to before it was redirected. I can't get it. Please guide me. Thanks for your time. Quote Link to comment https://forums.phpfreaks.com/topic/212565-redirect-connection-with-sockets/#findComment-1107729 Share on other sites More sharing options...
landysaccount Posted September 6, 2010 Author Share Posted September 6, 2010 Thanks for replying. I'm running this daemon/server listening on a certain port waiting for iptables to redirect traffic to it: iptables -t nat -A PREROUTING -i eth0 -p tcp \ -s 172.16.0.0/16 --sport $UNPRIVPORTS --dport 80 \ -j REDIRECT --to-port $daemonport Now, the daemon recieves this request and redirects (or maybe do authentication on its own) to another script in the same box that will do the authentication. Based on the credentials it would add an iptables rule to allow the client to the internet and to the page the user intended to before the redirection. Now, the problem is how can I get the intended address when iptables redirects it to the daemon? I've tried: '<meta HTTP-EQUIV="REFRESH" content="0; url=http://172.16.0.1/index2.php?requested=' .$_SERVER["QUERY_STRING"] . '&referer='. $_SERVER["HTTP_REFERER"] . '&uri='. $_SERVER["REQUEST_URI"] . '">'; and none of these are set. There might be a trick to get it. Your help is appreciated. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/212565-redirect-connection-with-sockets/#findComment-1107926 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.