common/src/main/java/HTTPClient/SocksClient.java
/**
     * Does the protocol exchange for a version 5 SOCKS connection.
     * (rfc-1928)
     */
Does the protocol exchange for a version 5 SOCKS connection.
Does the protocol exchange for a version 5 SOCKS connection .
private void v5ProtExchg(InputStream inp, OutputStream out, String host,
			     int port)
	throws SocksException, IOException
    {
	int                   version;
	ByteArrayOutputStream buffer = new ByteArrayOutputStream(100);
	Log.write(Log.SOCKS, "Socks: Beginning V5 Protocol Exchange for host "
			     + host + ":" + port);
	Log.write(Log.SOCKS, "Socks: Sending authentication request; methods"
			     + " No-Authentication, Username/Password");
	buffer.reset();
	buffer.write(5);		
	buffer.write(2);		
	buffer.write(NO_AUTH);		
	buffer.write(USERPWD);		
	buffer.writeTo(out);
	version = inp.read();
	if (version == -1)
	    throw new SocksException("Connection refused by server");
	else if (version != 5)
	    throw new SocksException("Received invalid version: " + version +
				     "; expected: 5");
	int method = inp.read();
	Log.write(Log.SOCKS, "Socks: Received response; version: " + version +
			     "; method: " + method);
	switch(method)
	{
	    case NO_AUTH:
		break;
	    case GSSAPI:
		negotiate_gssapi(inp, out);
		break;
	    case USERPWD:
		negotiate_userpwd(inp, out);
		break;
	    case NO_ACC:
		throw new SocksException("Server unwilling to accept any " +
					 "standard authentication methods");
	    default:
		throw new SocksException("Cannot handle authentication method "
					 + method);
	}
	Log.write(Log.SOCKS, "Socks: Sending connect request");
	buffer.reset();
	buffer.write(5);				
	buffer.write(CONNECT);				
	buffer.write(0);				
	buffer.write(DMNAME);				
	buffer.write(host.length() & 0xff);		
	buffer.write(host.getBytes("8859_1"));		
	buffer.write((port >> 8) & 0xff);		
	buffer.write(port & 0xff);
	buffer.writeTo(out);
	version = inp.read();
	if (version != 5)
	    throw new SocksException("Received invalid version: " + version +
				     "; expected: 5");
	int sts = inp.read();
	Log.write(Log.SOCKS, "Socks: Received response; version: " + version +
			     "; status: " + sts);
	switch (sts)
	{
	    case 0:	
		break;
	    case 1:
		throw new SocksException("General SOCKS server failure");
	    case 2:
		throw new SocksException("Connection not allowed");
	    case 3:
		throw new SocksException("Network unreachable");
	    case 4:
		throw new SocksException("Host unreachable");
	    case 5:
		throw new SocksException("Connection refused");
	    case 6:
		throw new SocksException("TTL expired");
	    case 7:
		throw new SocksException("Command not supported");
	    case 8:
		throw new SocksException("Address type not supported");
	    default:
		throw new SocksException("Unknown reply received from server: "
					 + sts);
	}
	inp.read();			
	int atype = inp.read(),		
	    alen;			
	switch(atype)
	{
	    case IP_V6:
		alen = 16;
		break;
	    case IP_V4:
		alen = 4;
		break;
	    case DMNAME:
		alen = inp.read();
		break;
	    default:
		throw new SocksException("Invalid address type received from" +
					 " server: "+atype);
	}
	byte[] skip = new byte[alen+2];		
	int rcvd = 0,
	    tot  = 0;
	while (tot < skip.length  &&
		(rcvd = inp.read(skip, 0, skip.length-tot)) != -1)
	    tot += rcvd;
    }
private void v5ProtExchg ( InputStream inp , OutputStream out , String host , int port ) throws SocksException , IOException { int version ; ByteArrayOutputStream buffer = new ByteArrayOutputStream ( 100 ) ; Log . write ( Log . SOCKS , "Socks: Beginning V5 Protocol Exchange for host " + host + ":" + port ) ; Log . write ( Log . SOCKS , "Socks: Sending authentication request; methods" + " No-Authentication, Username/Password" ) ; buffer . reset ( ) ; buffer . write ( 5 ) ; buffer . write ( 2 ) ; buffer . write ( NO_AUTH ) ; buffer . write ( USERPWD ) ; buffer . writeTo ( out ) ; version = inp . read ( ) ; if ( version == - 1 ) throw new SocksException ( "Connection refused by server" ) ; else if ( version != 5 ) throw new SocksException ( "Received invalid version: " + version + "; expected: 5" ) ; int method = inp . read ( ) ; Log . write ( Log . SOCKS , "Socks: Received response; version: " + version + "; method: " + method ) ; switch ( method ) { case NO_AUTH : break ; case GSSAPI : negotiate_gssapi ( inp , out ) ; break ; case USERPWD : negotiate_userpwd ( inp , out ) ; break ; case NO_ACC : throw new SocksException ( "Server unwilling to accept any " + "standard authentication methods" ) ; default : throw new SocksException ( "Cannot handle authentication method " + method ) ; } Log . write ( Log . SOCKS , "Socks: Sending connect request" ) ; buffer . reset ( ) ; buffer . write ( 5 ) ; buffer . write ( CONNECT ) ; buffer . write ( 0 ) ; buffer . write ( DMNAME ) ; buffer . write ( host . length ( ) & 0xff ) ; buffer . write ( host . getBytes ( "8859_1" ) ) ; buffer . write ( ( port >> 8 ) & 0xff ) ; buffer . write ( port & 0xff ) ; buffer . writeTo ( out ) ; version = inp . read ( ) ; if ( version != 5 ) throw new SocksException ( "Received invalid version: " + version + "; expected: 5" ) ; int sts = inp . read ( ) ; Log . write ( Log . SOCKS , "Socks: Received response; version: " + version + "; status: " + sts ) ; switch ( sts ) { case 0 : break ; case 1 : throw new SocksException ( "General SOCKS server failure" ) ; case 2 : throw new SocksException ( "Connection not allowed" ) ; case 3 : throw new SocksException ( "Network unreachable" ) ; case 4 : throw new SocksException ( "Host unreachable" ) ; case 5 : throw new SocksException ( "Connection refused" ) ; case 6 : throw new SocksException ( "TTL expired" ) ; case 7 : throw new SocksException ( "Command not supported" ) ; case 8 : throw new SocksException ( "Address type not supported" ) ; default : throw new SocksException ( "Unknown reply received from server: " + sts ) ; } inp . read ( ) ; int atype = inp . read ( ) , alen ; switch ( atype ) { case IP_V6 : alen = 16 ; break ; case IP_V4 : alen = 4 ; break ; case DMNAME : alen = inp . read ( ) ; break ; default : throw new SocksException ( "Invalid address type received from" + " server: " + atype ) ; } byte [ ] skip = new byte [ alen + 2 ] ; 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 5 SOCKS connection.
Does the protocol exchange for a version 5 SOCKS connection .
// send version 5 verification methods // version // number of verification methods // method: no authentication // method: username/password //buffer.write(GSSAPI);		// method: gssapi // receive servers repsonse // enter sub-negotiation for authentication // send version 5 request // version // command // reserved - must be 0 // address type // address length // address // port // read response // succeeded // Reserved // address type // address length // skip address + port