mgproxysocket.cpp

来自「一款LINUX下的下载软件」· C++ 代码 · 共 756 行 · 第 1/2 页

CPP
756
字号
	}
	
	
	switch(m_ProxyVersion)
	{
		case V4:
			return DoV4Connect(server,port);
		case V4A:
			return DoV4AConnect(server,port);
		case V5:
			return DoV5Connect(server,port);	
		case AUTOVERSION:		
		default:
			if( DoV5Connect(server,port)) {
				m_ProxyVersion=V5;
				return true;
			}else if(DoV4AConnect(server,port)) {
				m_ProxyVersion=V4A;
				return true;
			}else {
				if(DoV4Connect(server,port))
				{
					m_ProxyVersion=V4;
					return true;
				}
				else
				{
					return false;
				}
			}
		
	}
 
}
*/

bool CMgProxySocket::DoV5Connect( const char* server , int port )
{

    unsigned char buf[ 512 ];
    buf[ 0 ] = 0x05;
    buf[ 1 ] = 0x01; //connect
    buf[ 2 ] = 0x00; //reserve
    buf[ 3 ] = 0x03;
    buf[ 4 ] = strlen( server );
    memcpy( buf + 5, server, strlen( server ) );
    int pos = 5 + buf[ 4 ];
    short sport = htons( ( short ) port );
    memcpy( buf + pos, &sport, 2 );
    pos += 2;

    if ( !Send( buf, pos ) )
    {
        m_nLastError += ( 0x1A << 8 );
        return false;
    }

    int nret = Read( buf, 512 );

    if ( nret <= 0 )
    {
        m_nLastError += ( 0x1B << 8 );
        return false;
    }

    if ( nret < 10 )
    {
        m_nLastError = ( 0x0F << 8 );
        return false;
    }

    if ( buf[ 0 ] != 0x05 || buf[ 2 ] != 0x00 )
    {
        m_nLastError = ( 0x10 << 8 );
        return false;
    }

    /*
    # X'00' success
    # X'01' fail
    # X'02' not allow
    # X'03' net unreach
    # X'04' host unreach
    # X'05' connect refuse
    # X'06' TTL timeout
    # X'07' not support command
    # X'08' not support address
    # X'09' – X'FF' undef
    */
    if ( buf[ 1 ] == 0 )
    {
        return true;
    }
    else if ( buf[ 1 ] == 0x01 )
    {
        m_nLastError = ( 0x11 << 8 );
        return false;
    }
    else if ( buf[ 1 ] == 0x02 )
    {
        m_nLastError = ( 0x12 << 8 );
        return false;
    }
    else if ( buf[ 1 ] == 0x03 )
    {
        m_nLastError = ( 0x13 << 8 );
        return false;
    }
    else if ( buf[ 1 ] == 0x04 )
    {
        m_nLastError = ( 0x14 << 8 );
        return false;
    }
    else if ( buf[ 1 ] == 0x05 )
    {
        m_nLastError = ( 0x15 << 8 );
        return false;
    }
    else if ( buf[ 1 ] == 0x06 )
    {
        m_nLastError = ( 0x16 << 8 );
        return false;
    }
    else if ( buf[ 1 ] == 0x07 )
    {
        m_nLastError = ( 0x17 << 8 );
        return false;
    }
    else if ( buf[ 1 ] == 0x08 )
    {
        m_nLastError = ( 0x18 << 8 );
        return false;
    }
    else
    {
        m_nLastError = ( 0x19 << 8 );
        return false;
    }

    return false;
}

bool CMgProxySocket::DoV4Connect( const char* server , int port )
{ //resolv ip locally
    /*
    		+----+----+----+----+----+----+----+----+----+----+....+----+
    		| VN | CD | DSTPORT |      DSTIP        | USERID       |NULL|
    		+----+----+----+----+----+----+----+----+----+----+....+----+
    bytes:	   1    1      2              4           variable       1
    	
    	
    		+----+----+----+----+----+----+----+----+
    		| VN | CD | DSTPORT |      DSTIP        |
    		+----+----+----+----+----+----+----+----+
    bytes:	   1    1      2              4
     
    VN is the version of the reply code and should be 0. CD is the result
    code with one of the following values:
     
    	90: request granted
    	91: request rejected or failed
    	92: request rejected becasue SOCKS server cannot connect to
    	    identd on the client
    	93: request rejected because the client program and identd
    	    report different user-ids.	
    */


    unsigned char buf[ 256 ];

    struct hostent *hp;
    hp = gethostbyname( server );

    if ( !hp )
    {
        m_nLastError = 0x02; //??
        return false;
    }

    if ( hp->h_addrtype != AF_INET )
    { //neither AF_INET nor AF_INET6
        m_nLastError = 0x03; //??
        return false;
    }

    short iport = htons( ( short ) port );

    buf[ 0 ] = 0x04;
    buf[ 1 ] = 0x01;
    memcpy( buf + 2, &iport, 2 );
    memcpy( buf + 4, ( void* ) ( hp->h_addr ), 4 );
    memcpy( buf + 8, m_ProxyUser.c_str(), m_ProxyUser.length() );
    buf[ 8 + m_ProxyUser.length() ] = 0;

    if ( !Send( buf, 8 + m_ProxyUser.length() + 1 ) )
    {
        return false;
    }

    int nret = Read( buf, 256 );

    if ( nret != 8 )
    {
        return false;
    }

    if ( buf[ 0 ] != 0 )
    {
        return false;

    }

    if ( buf[ 1 ] == 90 )
    {
        return true;
    }
    else if ( buf[ 1 ] == 91 )
    {
        return false;
    }
    else if ( buf[ 1 ] == 92 )
    {
        return false;
    }
    else if ( buf[ 1 ] == 93 )
    {
        return false;
    }

    return false;
}

bool CMgProxySocket::DoV4AConnect( const char* server , int port )
{


    unsigned char buf[ 256 ];

    buf[ 0 ] = 0x04;
    buf[ 1 ] = 0x01;
    short iport = htons( ( short ) port );
    memcpy( buf + 2, &iport, 2 );
    buf[ 4 ] = buf[ 5 ] = buf[ 6 ] = 0;
    buf[ 7 ] = 0x01;
    memcpy( buf + 8, m_ProxyUser.c_str(), m_ProxyUser.length() );
    buf[ 8 + m_ProxyUser.length() ] = 0;
    memcpy( buf + 8 + m_ProxyUser.length() + 1, server, strlen( server ) );
    buf[ 8 + m_ProxyUser.length() + 1 + strlen( server ) ] = 0;

    if ( !Send( buf, 8 + m_ProxyUser.length() + 1 + strlen( server ) + 1 ) )
    {
        return false;
    }

    int nret = Read( buf, 256 );

    if ( nret != 8 )
    {
        return false;
    }

    if ( buf[ 0 ] != 0 )
    {
        return false;
    }

    if ( buf[ 1 ] == 90 )
    {
        return true;
    }
    else if ( buf[ 1 ] == 91 )
    {
        return false;
    }
    else if ( buf[ 1 ] == 92 )
    {
        return false;
    }
    else if ( buf[ 1 ] == 93 )
    {
        return false;
    }

    return false;
}

bool CMgProxySocket::DoV5Login()
{
    unsigned char init[ 4 ] = {0x05, 0x02, 0x00, 0x02};

    if ( !Send( init, 4 ) )
    {
        m_nLastError = ( 0x03 << 8 );
        return false;
    }

    unsigned char buf[ 256 ];

    int nret = Read( buf, 256 );

    if ( nret != 2 )
    {
        m_nLastError = ( 0x04 << 8 );
        return false;
    }

    if ( buf[ 0 ] != 0x05 )
    {
        m_nLastError = ( 0x05 << 8 );
        return false;
    }

    if ( buf[ 1 ] == 0xFF )
    {
        m_nLastError = ( 0x06 << 8 );
        return false;
    }

    if ( buf[ 1 ] != 0 && buf[ 1 ] != 1 && buf[ 1 ] != 2 )
    {
        m_nLastError = ( 0x07 << 8 );
        return false;
    }

    if ( buf[ 1 ] == 0 )
    { //ok
        return true;
    }
    else if ( buf[ 1 ] == 1 )
    { //GSSAPI
        m_nLastError = ( 0x08 << 8 );
        return false;
    }
    else
    { //u/p
        int tl = 0;
        buf[ 0 ] = 0x01;
        tl++;
        buf[ tl ] = m_ProxyUser.length();
        tl++;
        memcpy( buf + tl, m_ProxyUser.c_str(), m_ProxyUser.length() );
        tl += m_ProxyUser.length();
        buf[ tl ] = m_ProxyPass.length();
        tl++;
        memcpy( buf + tl, m_ProxyPass.c_str(), m_ProxyPass.length() );
        tl += m_ProxyPass.length();

        if ( !Send( buf, tl ) )
        {
            m_nLastError += ( 0x09 << 8 );
            return false;
        }

        if ( Read( buf, 256 ) != 2 )
        {
            m_nLastError = ( 0x0A << 8 );
            return false;
        }

        if ( buf[ 0 ] != 0x01 )
        {
            m_nLastError = ( 0x0B << 8 );
            return false;
        }

        if ( buf[ 1 ] != 0x00 )
        {
            m_nLastError = ( 0x0C << 8 );
            return false;
        }

        return true;
    }

    return false;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?