简体   繁体   English

PHP套接字包数据

[英]PHP socket pack data

<?php

$desthost = "imap.gmail.com";
$port     = 993;
$conflag  = STREAM_CLIENT_CONNECT;

try
{
    $socket = stream_socket_client( "tcp://host:port", $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 );

    var_dump(unpack("C5", $server_buffer));


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

This code connect to proxy and connect to imap via proxy. 此代码连接到代理,并通过代理连接到imap。 I can't understand how this pack works ? 我不明白这个包是如何工作的?

what does mean fwrite( $socket, pack( "C3", 0x05, 0x01, 0x00 ) ); 什么是fwrite($ socket,pack(“ C3”,0x05,0x01,0x00)); ?

and what does mean next code ? 下一个代码是什么意思?

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

I think it's "CONNECT host:port" ? 我认为它是“ CONNECT host:port”? Can somebody please explain me about that ? 有人可以向我解释一下吗? When I try to send command "0000001 LOGIN login pass" imap returns me error bad syntax. 当我尝试发送命令“ 0000001 LOGIN login pass”时,imap向我返回错误的错误语法。 Seems I should pack this command or something like this. 似乎我应该打包此命令或类似的东西。

There is an overview of the SOCKS protocol at https://en.wikipedia.org/wiki/SOCKS . https://en.wikipedia.org/wiki/SOCKS上有SOCKS协议的概述。

This is some dirty code to quickly connect to a SOCKS proxy. 这是一些肮脏的代码,可以快速连接到SOCKS代理。

fwrite( $socket, pack( "C3", 0x05, 0x01, 0x00 ) ); means sends the bytes 5, 1, and 0. 表示发送字节5、1和0。

In an initial connection request this means "SOCKS version 5 ", 1 authentication method supported, and that one authentication method is No authentication . 在初始连接请求中,这表示“ SOCKS版本5 ”,支持1身份验证方法,而其中一种身份验证方法为“ No authentication

if ( $server_status == pack( "C2", 0x05, 0x00 ) ) is checking that the server responded with "Socks version 5 , Use No Authentication ". if ( $server_status == pack( "C2", 0x05, 0x00 ) )正在检查服务器是否响应“ Socks版本5 ,不使用No Authentication ”。

fwrite( $socket, pack( "C5", 0x05, 0x01, 0x00, 0x03, strlen( $desthost ) ) . $desthost . pack( "n", $port ) ); is sending a connection request: 正在发送连接请求:

  • 5 (SOCKS version 5) 5(SOCKS版本5)
  • 1 (TCP connection request) 1(TCP连接请求)
  • 0 (Reserved) 0(保留)
  • 3 (Use a domain name) 3(使用域名)
  • length of domain name 域名length
  • The domain name 域名
  • port number in network byte order 网络字节顺序的端口号

if ( ord( $server_buffer[0] ) == 5 && ord( $server_buffer[1] ) == 0 && ord( $server_buffer[2] ) == 0 ) is checking the server response. if ( ord( $server_buffer[0] ) == 5 && ord( $server_buffer[1] ) == 0 && ord( $server_buffer[2] ) == 0 )正在检查服务器响应。

  • 5 Socks version 5 5袜子版本5
  • 0 Request granted 0请求被批准
  • 0 Reserved byte. 0保留字节。

There are more fields but it's ignoring them, and hoping that they fit exactly into 10 bytes, which may not be true if it returns back the domain address or an IPv6 address. 还有更多字段,但是它忽略了它们,希望它们完全适合10个字节,如果它返回域地址或IPv6地址,则可能不正确。

At this point, the connection to the remote server is established. 此时,与远程服务器的连接已建立。 It then upgrades it to TLS, and should otherwise work as expected. 然后将其升级到TLS,否则应可以按预期工作。

For your IMAP commands after this point, make sure they end in "\\r\\n". 对于此后的IMAP命令,请确保它们以“ \\ r \\ n”结尾。 You don't actually show your code for this, so we can't help debug it. 您实际上并没有显示您的代码,因此我们无法帮助对其进行调试。

SOCKS5 is formally specified in RFC1928 . SOCKS5在RFC1928中正式指定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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