common/src/main/java/HTTPClient/SocksClient.java
/**
     * Does the protocol exchange for a version 4 SOCKS connection.
     */
Does the protocol exchange for a version 4 SOCKS connection.
Does the protocol exchange for a version 4 SOCKS connection .
private void v4ProtExchg(InputStream inp, OutputStream out, String host,
			     int port)
	throws SocksException, IOException
    {
	ByteArrayOutputStream buffer = new ByteArrayOutputStream(100);
	Log.write(Log.SOCKS, "Socks: Beginning V4 Protocol Exchange for host "
			     + host + ":" + port);
	byte[] addr = { 0, 0, 0, 42 };
	if (!v4A)
	{
	    try
		{ addr = InetAddress.getByName(host).getAddress(); }
	    catch (UnknownHostException uhe)
		{ v4A = true; }
	    catch (SecurityException se)
		{ v4A = true; }
	    if (v4A)
		Log.write(Log.SOCKS, "Socks: Switching to version 4A");
	}
	if (user == null)	
	{
	    String user_str;
	    try
		{ user_str = System.getProperty("user.name", ""); }
	    catch (SecurityException se)
		{ user_str = "";	/* try it anyway */ }
	    byte[] tmp = user_str.getBytes();
	    user = new byte[tmp.length+1];
	    System.arraycopy(tmp, 0, user, 0, tmp.length);
	    user[user_str.length()] = 0;	
	}
	Log.write(Log.SOCKS, "Socks: Sending connect request for user " +
			     new String(user, 0, user.length-1));
	buffer.reset();
	buffer.write(4);				
	buffer.write(CONNECT);				
	buffer.write((port >> 8) & 0xff);		
	buffer.write(port & 0xff);
	buffer.write(addr);				
	buffer.write(user);				
	if (v4A)
	{
	    buffer.write(host.getBytes("8859_1"));	
	    buffer.write(0);				
	}
	buffer.writeTo(out);
	int version = inp.read();
	if (version == -1)
	    throw new SocksException("Connection refused by server");
	else if (version == 4)	
	    Log.write(Log.SOCKS, "Socks: Warning: received version 4 " +
				 "instead of 0");
	else if (version != 0)
	    throw new SocksException("Received invalid version: " + version +
				     "; expected: 0");
	int sts = inp.read();
	Log.write(Log.SOCKS, "Socks: Received response; version: " + version +
			     "; status: " + sts);
	switch (sts)
	{
	    case 90:	
		break;
	    case 91:	
		throw new SocksException("Connection request rejected");
	    case 92:	: can't connect to identd
		throw new SocksException("Connection request rejected: " +
					 "can't connect to identd");
	    case 93:	: identd reports diff uid
		throw new SocksException("Connection request rejected: " +
					 "identd reports different user-id " +
					 "from "+
					 new String(user, 0, user.length-1));
	    default:	
		throw new SocksException("Connection request rejected: " +
					 "unknown error " + sts);
	}
	byte[] skip = new byte[2+4];		
	int rcvd = 0,
	    tot  = 0;
	while (tot < skip.length  &&
		(rcvd = inp.read(skip, 0, skip.length-tot)) != -1)
	    tot += rcvd;
    }
private void v4ProtExchg ( InputStream inp , OutputStream out , String host , int port ) throws SocksException , IOException { ByteArrayOutputStream buffer = new ByteArrayOutputStream ( 100 ) ; Log . write ( Log . SOCKS , "Socks: Beginning V4 Protocol Exchange for host " + host + ":" + port ) ; byte [ ] addr = { 0 , 0 , 0 , 42 } ; if ( ! v4A ) { try { addr = InetAddress . getByName ( host ) . getAddress ( ) ; } catch ( UnknownHostException uhe ) { v4A = true ; } catch ( SecurityException se ) { v4A = true ; } if ( v4A ) Log . write ( Log . SOCKS , "Socks: Switching to version 4A" ) ; } if ( user == null ) { String user_str ; try { user_str = System . getProperty ( "user.name" , "" ) ; } catch ( SecurityException se ) { user_str = "" ; /* try it anyway */ } byte [ ] tmp = user_str . getBytes ( ) ; user = new byte [ tmp . length + 1 ] ; System . arraycopy ( tmp , 0 , user , 0 , tmp . length ) ; user [ user_str . length ( ) ] = 0 ; } Log . write ( Log . SOCKS , "Socks: Sending connect request for user " + new String ( user , 0 , user . length - 1 ) ) ; buffer . reset ( ) ; buffer . write ( 4 ) ; buffer . write ( CONNECT ) ; buffer . write ( ( port >> 8 ) & 0xff ) ; buffer . write ( port & 0xff ) ; buffer . write ( addr ) ; buffer . write ( user ) ; if ( v4A ) { buffer . write ( host . getBytes ( "8859_1" ) ) ; buffer . write ( 0 ) ; } buffer . writeTo ( out ) ; int version = inp . read ( ) ; if ( version == - 1 ) throw new SocksException ( "Connection refused by server" ) ; else if ( version == 4 ) Log . write ( Log . SOCKS , "Socks: Warning: received version 4 " + "instead of 0" ) ; else if ( version != 0 ) throw new SocksException ( "Received invalid version: " + version + "; expected: 0" ) ; int sts = inp . read ( ) ; Log . write ( Log . SOCKS , "Socks: Received response; version: " + version + "; status: " + sts ) ; switch ( sts ) { case 90 : break ; case 91 : throw new SocksException ( "Connection request rejected" ) ; case 92 : throw new SocksException ( "Connection request rejected: " + "can't connect to identd" ) ; case 93 : throw new SocksException ( "Connection request rejected: " + "identd reports different user-id " + "from " + new String ( user , 0 , user . length - 1 ) ) ; default : throw new SocksException ( "Connection request rejected: " + "unknown error " + sts ) ; } byte [ ] skip = new byte [ 2 + 4 ] ; int rcvd = 0 , tot = 0 ; while ( tot < skip . length && ( rcvd = inp . read ( skip , 0 , skip . length - tot ) ) != - 1 ) tot += rcvd ; }
Does the protocol exchange for a version 4 SOCKS connection.
Does the protocol exchange for a version 4 SOCKS connection .
// get ip addr and user name // if we can't translate, let's try the server // I see no reason not to cache this // 0-terminated string // send version 4 request // version // command // port // address // user // host name // terminating 0 // read response // not all socks4 servers are correct... // request granted // request rejected // request rejected: can't connect to identd // request rejected: identd reports diff uid // unknown status // skip port + address