2

I'm totally disappointed. I am connecting to a ssl servers, and direct connections working well, but when I am trying to add stream context to use proxy or socks5, socket won't use it and connecting pretty well directly to these ssl:// server anyway, I am checking by watching 127.0.0.1 proxy server log - there weren't even connection attempts. Also, could I wrap stream into socks5 server using socks5:// http proxy option?

$ctx = stream_context_create( array(
                      "http" => array(
                          "timeout"         => 15,
                          "proxy"           => "tcp://127.0.0.1:3128",
                          "request_fulluri" => TRUE,
                       ),
                      "ssl"  => array(
                          "SNI_enabled" => FALSE,
                      )
              ) );

try
{
    $socket = stream_socket_client( "ssl://google.com:443", 
        $errno, $errstr, 15, STREAM_CLIENT_CONNECT, $ctx );
}
catch ( Exception $e )
{
    die( $e->getMessage() );
}

if ( $socket === FALSE )
{
    echo "bad socket";
}

fwrite( $socket, "GET /\n" );
echo fread( $socket, 8192 );

// Here I am connected DIRECTLY, not thru proxy. WHY ???

// But this call succesfully uses context
echo file_get_contents("https://google.com", 0, $ctx);

1 Answer 1

6

I've found the right way. This connects thru socks5 servers perfectly.

$desthost = "google.com";
$port     = 443;
$conflag  = STREAM_CLIENT_CONNECT;

try
{
    $socket = stream_socket_client( "tcp://127.0.0.1:1080", $errno, $errstr, 15, $conflag );

    fwrite( $socket, pack( "C3", 0x05, 0x01, 0x00 ) );
    $server_status = fread( $socket, 2048 );
    if ( $server_status == pack( "C2", 0x05, 0x00 ) )
    {
        // Connection succeeded
    }
    else
    {
        die( "SOCKS Server does not support this version and/or authentication method of SOCKS.\r\n" );
    }

    fwrite( $socket, pack( "C5", 0x05, 0x01, 0x00, 0x03, strlen( $desthost ) ) . $desthost . pack( "n", $port ) );
    $server_buffer = fread( $socket, 10 );

    if ( ord( $server_buffer[0] ) == 5 && ord( $server_buffer[1] ) == 0 && ord( $server_buffer[2] ) == 0 )
    {
        // Connection succeeded
    }
    else
    {
        die( "The SOCKS server failed to connect to the specificed host and port. ( " . $desthost . ":" . $port . " )\r\n" );
    }

    stream_socket_enable_crypto( $socket, TRUE, STREAM_CRYPTO_METHOD_SSLv23_CLIENT );
}
catch ( Exception $e )
{
    die( $e->getMessage() );
}

if ( $socket === FALSE )
{
    die( "bad socket" );
}

fwrite( $socket, "GET /\n" );
echo fread( $socket, 8192 );
Sign up to request clarification or add additional context in comments.

2 Comments

Your answer is very very useful for me!! Thank you a lot! I am trying to work with imap via proxy, using php socket. Your code works perfect. But now I am trying to LOGIN into the account. I use the following code fwrite($socket, "00000001 LOGIN login pass\r\n"); But I get GET BAD Command syntax error. sc=UPE29Z0CW4Y1_220825_66j I thing I need send the data in the another format. Something like pack. But I don't know how it works. Can you help me and explain ? I would be very thankful !
There is no LOGIN http method defined in RFC. You should construct own get/post request with Auth header, something like this: fwrite($socket, "POST /auth\r\nContent-Length: 100\r\nAuthorization: Basic: aashdklashdjkh\r\n\r\n" );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.