简体   繁体   中英

Socks 4 bind in php example

<?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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM