PHP SSL stream_socket_client 不会使用创建的 $context [英] PHP SSL stream_socket_client won't use created $context

查看:38
本文介绍了PHP SSL stream_socket_client 不会使用创建的 $context的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很失望.我正在连接到 ssl 服务器,并且直接连接运行良好,但是当我尝试添加流上下文以使用代理或袜子 5 时,套接字不会使用它并且无论如何都可以很好地直接连接到这些 ssl://服务器,我我通过观察 127.0.0.1 代理服务器日志来检查 - 甚至没有连接尝试.另外,我可以使用socks5://http代理选项将流包装到socks5服务器中吗?

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);

推荐答案

我找到了正确的方法.这完美地连接了socks5服务器.

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 );

这篇关于PHP SSL stream_socket_client 不会使用创建的 $context的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆