<?php
error_reporting(E_ALL);
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false)
echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
$result = socket_connect($socket, "92.51.77.126", 1080); // socks ip
if ($result === false)
echo "socket_connect() failed.\nReason: ($result) " . socket_strerror(socket_last_error($socket)) . "\n";
$in = "\x04\x02";
$in .= "\x00\x50"; // http proxy port
$in .= "\xc0\xf0\x2e\x7e"; // http proxy ip address
$in .= "\x00";
socket_write($socket, $in, strlen($in));
sleep(1);
$in = "HEAD / HTTP/1.1\r\n";
$in .= "Host: www.example.com\r\n";
$in .= "Connection: Close\r\n\r\n";
socket_write($socket, $in, strlen($in));
$out = '';
while ($out = socket_read($socket, 2048)) {
echo $out;
}
socket_close($socket);
?>
I'd like to bind a socks 4 proxy server to connect an another proxy. This example the secound proxy is a http proxy. When connect the first is normally connected the server answer is Granted, but the communication at this time is finished. How continued the communication this example?
You are lucky that you get a 'Granted' reply - I do not get any reply from SOCKS server in OpenSSH, it just closes the connection...
As I understand the SOCKS4 description, the answer for type 2 (BIND) request contains a port number and an IP address; when a connection arrives to them, your program will be able to read next status packet, which tells about the connection. There is a limited time (seems 2 minutes) of waiting for the connection. This is mainly for an FTP client - an FTP server needs back connection to the client and the BIND allows the connection to be passed.
If you want to initiate a connection from your side, use type 1 (CONNECT) request. Also, make sure the port and the IP address in the request are correct - seems in your code they are 80 and 192.240.30.126.
Related
I am trying to make a simple PHP, javascript websocket test project and I ran into the issue where PHP would not continue the while(true) loop once I call the socket accept function. It waits for an actual connection before continuing the loop. Is there a way to call this asynchronously so the while loop can continue? Or is there a way to immediately accept more incoming connections after one is accepted?
Here is my code
<?php
error_reporting(E_ALL);
/* Allow the script to hang around waiting for connections.
set_time_limit(0);
/* Turn on implicit output flushing so we see what we're getting
as it comes in.
ob_implicit_flush();*/
$address = 'localhost';
$port = 8080;
$clients = [];
$server = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_set_option($server, SOL_SOCKET, SO_REUSEADDR, 1);
socket_bind($server, $address, $port);
socket_listen($server);
/*$client = acceptConn($server);
$clients[0] = $client;*/
// Send WebSocket handshake headers.
$newclient = socket_accept($server);
handshake($server, $newclient);
$clients[count($clients)] = $newclient;
function handshake($server, $client){
$request = socket_read($client, 5000);
preg_match('#Sec-WebSocket-Key: (.*)\r\n#', $request, $matches);
$key = base64_encode(pack(
'H*',
sha1($matches[1] . '258EAFA5-E914-47DA-95CA-C5AB0DC85B11')
));
$headers = "HTTP/1.1 101 Switching Protocols\r\n";
$headers .= "Upgrade: websocket\r\n";
$headers .= "Connection: Upgrade\r\n";
$headers .= "Sec-WebSocket-Version: 13\r\n";
$headers .= "Sec-WebSocket-Accept: $key\r\n\r\n";
socket_write($client, $headers, strlen($headers));
}
// Send messages into WebSocket in a loop.
do {
sleep(1);
//echo "next";
$content = 'Now: ' . time();
$response = chr(129) . chr(strlen($content)) . $content;
foreach($clients as $key => $item){
if(socket_write($item, $response) != 17){
socket_close($item);
unset($clients[$key]);
continue;
//echo json_encode($clients);
}
continue;
}
/*echo count($clients);
$newclient = socket_accept($server);
echo gettype($newclient);
$clients[count($clients)] = $client;*/
}while (true);
?>
I found the solution.
To anyone who was wondering what it was, use:
socket_set_nonblock($socketservervariable);
to make the server not wait for a client to connect before proceeding. That creates a new error though. When doing the client handshake the socket_read wanted a socket but it got the boolean value of false when the socket_accept function didn't detect a client connecting. So this was my solution to that:
$newclient = socket_accept($server);
if($newclient != false){
handshake($server, $newclient);
$clients[count($clients)] = $newclient;
echo "\n\nNew client connected!\n\n";
}
and that solved all of my problems.
i was want to connect to my fingerprint device useing TCP/IP
i was setting ip address in device,
my php code:
<?php
$fp = fsockopen("192.168.1.211", 80, $errno, $errstr, 30);
if (!$fp) {
echo "$errstr ($errno)<br />\n";
} else {
$out = "GET / HTTP/1.1\r\n";
$out .= "Host: www.example.com\r\n";
$out .= "Connection: Close\r\n\r\n";
fwrite($fp, $out);
while (!feof($fp)) {
echo fgets($fp, 128);
}
fclose($fp);
}
?>
when i try to connect, i get error connection refused . but ping to device is work, i don't know where is the problem,
My fingerprint device specification:
Vendor :Solution
type : x101-c
i hope somebody can help and solve my problem,,
i really stack now....
but ping to device is work
Ping uses ICMP packets not TCP. If you're getting a connection refused then the (TCP) port is firewalled.
I am newbie in using PHP Websocket.
Here is my problem:
I have already successful running PHP websocket and creating a simple chat application(web application of course). In receiving client data. All of the clients will receive the data. How could I send data to a specified client or maybe to a several clients(not all clients).
I learn it at here
<?php
// Create a new socket
$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
// An example list of IP addresses owned by the computer
$sourceips['kevin'] = '127.0.0.1';
$sourceips['madcoder'] = '127.0.0.2';
// Bind the source address
socket_bind($sock, $sourceips['madcoder']);
// Connect to destination address
socket_connect($sock, '127.0.0.1', 80);
// Write
$request = 'GET / HTTP/1.1' . "\r\n" .
'Host: example.com' . "\r\n\r\n";
socket_write($sock, $request);
// Close
socket_close($sock);
?>
SOURCE: http://php.net/manual/en/function.socket-bind.php
I have a simple whois script
if($conn = fsockopen ($whois_server, 43)) {
fputs($conn, $domain."\r\n");
while(!feof($conn)) {
$output .= fgets($conn, 128);
}
fclose($conn);
return $output;
}
$whois_server = whois.afilias.info; //whois server for info domains
but I want to run in through proxy. So I need to connect to proxy -> then connect to whois server -> and then make the request. Something like this?
$fp = fsockopen($ip,$port);
fputs($fp, "CONNECT $whois_server:43\r\n $domain\r\n");
But it doesn't work, I don't know if i'm making the second connection right.
Sending your request to the proxy should do what you like:
<?php
$proxy = "1.2.3.4"; // proxy
$port = 8080; // proxy port
$fp = fsockopen($proxy,$port); // connect to proxy
fputs($fp, "CONNECT $whois_server:43\r\n $domain\r\n");
$data="";
while (!feof($fp)) $data.=fgets($fp,1024);
fclose($fp);
var_dump($data);
?>
Rather than doing that I would recommend you using CURL in combination with:
curl_setopt($ch, CURLOPT_PROXY, 1.2.3.4);
I'm trying to set up a socket server in php that stays open. This example taken from php.net will close after it receives connection...even after I comment out socket_close($spawn)
<?
// set some variables
$host = "192.168.1.109";
$port = 1234;
// don't timeout!
set_time_limit(0);
// create socket
$socket = socket_create(AF_INET, SOCK_STREAM, 0) 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");
// 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 = $input . "\n";
$output = strrev($input) . "\n";
echo $input;
socket_write($spawn, $output, strlen ($output)) or die("Could not write
output\n");
// close sockets
//socket_close($spawn);
//socket_close($socket);
?>
and here is the code for the client connecting...
<?php
$fp = fsockopen("192.168.1.109", 1234, $errno, $errstr, 30);
if (!$fp) {
echo "$errstr ($errno)<br />\n";
} else {
//$out = "testing";
$out = "GET / HTTP/1.1\r\n";
$out .= "Host: 127.0.0.1\r\n";
$out .= "Connection: Close\r\n\r\n";
$out .= "testing\n";
fwrite($fp, $out);
while (!feof($fp)) {
echo fgets($fp, 128);
}
fclose($fp);
//exit();
}
//exit;
?>
socket_read without the O_NONBLOCK flag (see socket_set_nonblock) is a blocking operation, so it will wait there until it receives something.
As soon as something is received, the rest of the script continues, and exits, as there is no loop in place to do the next read. (i.e: in a server it's usual to do a while(true){} loop)
You need to wrap the accept in a loop or something. It closes because the script execution has ended.
You could do something like this:
while ($spawn = socket_accept($socket)) {
//do stuff
}