⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 aesocksproxyconnectionimpl.java

📁 这是一个基于java编写的torrent的P2P源码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
				
				return( false );
				
			}else if ( len == -1 ){
				
				throw( new IOException( "read channel shutdown" ));
			}
			
			
			if ( buffer.hasRemaining()){
				
				return( true );
			}
			
			buffer.flip();
			
			int	num_methods	= buffer.get();
			
			new proxyStateV5Methods(num_methods);
			
			return( true );
		}
	}
	
	protected class
	proxyStateV5Methods
		extends AESocksProxyState
	{
		
		protected
		proxyStateV5Methods(
			int		methods )
		{
			super( AESocksProxyConnectionImpl.this );
			
			connection.setReadState( this );

			buffer	= ByteBuffer.allocate(methods);
		}
		
		protected boolean
		readSupport(
			SocketChannel 		sc )
		
			throws IOException
		{
			int	len = sc.read( buffer );
			
			if ( len == 0 ){
				
				return( false );
				
			}else if ( len == -1 ){
				
				throw( new IOException( "read channel shutdown" ));
			}
			
			
			if ( buffer.hasRemaining()){
				
				return( true );
			}
			
				// we just ignore actual method values
			
			new proxyStateV5MethodsReply();
			
			return( true );
		}
	}
	
	protected class
	proxyStateV5MethodsReply
		extends AESocksProxyState
	{
		
		protected
		proxyStateV5MethodsReply()
		
			throws IOException
		{
			super( AESocksProxyConnectionImpl.this );
			
			new proxyStateV5Request();
			
			connection.setWriteState( this );
			
			buffer	= ByteBuffer.wrap(new byte[]{(byte)5,(byte)0});
			
			write( source_channel );
		}
		
		protected boolean
		writeSupport(
			SocketChannel 		sc )
		
			throws IOException
		{
			int len = sc.write( buffer );
			
			if ( buffer.hasRemaining()){
				
				connection.requestWriteSelect( sc );
			}
			
			return( len > 0 );
		}
	}
	
	/*
    +----+-----+-------+------+----------+----------+
    |VER | CMD |  RSV  | ATYP | DST.ADDR | DST.PORT |
    +----+-----+-------+------+----------+----------+
    | 1  |  1  | X'00' |  1   | Variable |    2     |
    +----+-----+-------+------+----------+----------+

		Where:

	          o  VER    protocol version: X'05'
	          o  CMD
	             o  CONNECT X'01'
	             o  BIND X'02'
	             o  UDP ASSOCIATE X'03'
	          o  RSV    RESERVED
	          o  ATYP   address type of following address
	             o  IP V4 address: X'01'
	             o  DOMAINNAME: X'03'
	             o  IP V6 address: X'04'
	          o  DST.ADDR       desired destination address
	          o  DST.PORT desired destination port in network octet
	             order
	             */
	
	protected class
	proxyStateV5Request
		extends AESocksProxyState
	{
		
		protected
		proxyStateV5Request()
		{
			super( AESocksProxyConnectionImpl.this );
			
			connection.setReadState( this );
			
			buffer	= ByteBuffer.allocate(4);
		}
		
		protected boolean
		readSupport(
			SocketChannel 		sc )
		
			throws IOException
		{
			int	len = sc.read( buffer );
			
			if ( len == 0 ){
				
				return( false );
				
			}else if ( len == -1 ){
				
				throw( new IOException( "read channel shutdown" ));
			}
			
			
			if ( buffer.hasRemaining()){
				
				return( true );
			}
			
			buffer.flip();
			
			buffer.get();		// version
			
			int	command			= buffer.get();
			
			buffer.get();		// reserved
			
			int address_type	= buffer.get();
			
			if ( command != 1 ){
				
				throw( new IOException( "V5: Only connect supported"));
			}
			
			if ( address_type == 1 ){
			
				new proxyStateV5RequestIP();
				
			}else if ( address_type == 3 ){
				
				new proxyStateV5RequestDNS();
				
			}else{
				
				throw( new IOException( "V5: Unsupported address type" ));
			}
			
			return( true );
		}
	}
	
	protected class
	proxyStateV5RequestIP
		extends AESocksProxyState
	{
		
		protected
		proxyStateV5RequestIP()
		{
			super( AESocksProxyConnectionImpl.this );
			
			connection.setReadState( this );
			
			buffer	= ByteBuffer.allocate(4);
		}
		
		protected boolean
		readSupport(
			SocketChannel 		sc )
		
			throws IOException
		{
			int	len = sc.read( buffer );
			
			if ( len == 0 ){
				
				return( false );
				
			}else if ( len == -1 ){
				
				throw( new IOException( "read channel shutdown" ));
			}
			
			
			if ( buffer.hasRemaining()){
				
				return( true );
			}
			
			buffer.flip();
			
			byte[]	bytes = new byte[4];
			
			buffer.get( bytes );
			
			InetAddress inet_address = InetAddress.getByAddress( bytes );
			
			new proxyStateV5RequestPort( "", inet_address );
			
			return( true );
		}
	}
	
	protected class
	proxyStateV5RequestDNS
		extends AESocksProxyState
	{
		boolean	got_length	= false;
		
		protected
		proxyStateV5RequestDNS()
		{
			super( AESocksProxyConnectionImpl.this );
			
			connection.setReadState( this );
			
			buffer	= ByteBuffer.allocate(1);
		}
		
		protected boolean
		readSupport(
			final SocketChannel 		sc )
		
			throws IOException
		{
			int	len = sc.read( buffer );
			
			if ( len == 0 ){
				
				return( false );
				
			}else if ( len == -1 ){
				
				throw( new IOException( "read channel shutdown" ));
			}
			
			
			if ( buffer.hasRemaining()){
				
				return( true );
			}
			
			buffer.flip();
			
			if ( !got_length){
				
				int	length = ((int)buffer.get()) & 0xff;
				
				buffer = ByteBuffer.allocate( length );
				
				got_length	= true;
				
			}else{
				
				String dns_address = "";
				
				while( buffer.hasRemaining()){
				
					dns_address += (char)buffer.get();
				}
					
				if ( disable_dns_lookups ){
				
					new proxyStateV5RequestPort( dns_address, null );
				
				}else{
					
					final String	f_dns_address	= dns_address;
					
					connection.cancelReadSelect( sc );
					
					HostNameToIPResolver.addResolverRequest(
						dns_address,
						new HostNameToIPResolverListener()
						{
							public void
							hostNameResolutionComplete(
								InetAddress	address )
							{
								new proxyStateV5RequestPort( f_dns_address, address);
									
								connection.requestReadSelect( sc );
							}
						});
				}
			}
			
			return( true );
		}
	}
	
	protected class
	proxyStateV5RequestPort
		extends AESocksProxyState
	{
		protected String		unresolved_address;
		protected InetAddress	address;
		
		protected
		proxyStateV5RequestPort(
			String			_unresolved_address,
			InetAddress		_address )
		{
			super( AESocksProxyConnectionImpl.this );
			
			unresolved_address	= _unresolved_address;
			address				= _address;
			
			connection.setReadState( this );
			
			buffer	= ByteBuffer.allocate(2);
		}
		
		protected boolean
		readSupport(
			SocketChannel 		sc )
		
			throws IOException
		{
			int	len = sc.read( buffer );
			
			if ( len == 0 ){
				
				return( false );
				
			}else if ( len == -1 ){
				
				throw( new IOException( "read channel shutdown" ));
			}
			
			
			if ( buffer.hasRemaining()){
				
				return( true );
			}
			
			buffer.flip();
						
			int	port = (((int)buffer.get() & 0xff) << 8 ) + ((int)buffer.get() & 0xff);
			
			socks_version	= 5;
			
			plugable_connection.connect( new AESocksProxyAddressImpl( unresolved_address, address, port ));
			
			return( true );
		}
	}
	
	/*
    +----+-----+-------+------+----------+----------+
    |VER | REP |  RSV  | ATYP | BND.ADDR | BND.PORT |
    +----+-----+-------+------+----------+----------+
    | 1  |  1  | X'00' |  1   | Variable |    2     |
    +----+-----+-------+------+----------+----------+

 Where:

      o  VER    protocol version: X'05'
      o  REP    Reply field:
         o  X'00' succeeded
         o  X'01' general SOCKS server failure
         o  X'02' connection not allowed by ruleset
         o  X'03' Network unreachable
         o  X'04' Host unreachable
         o  X'05' Connection refused
         o  X'06' TTL expired
         o  X'07' Command not supported
         o  X'08' Address type not supported
         o  X'09' to X'FF' unassigned
      o  RSV    RESERVED
      o  ATYP   address type of following address

         o  IP V4 address: X'01'
         o  DOMAINNAME: X'03'
         o  IP V6 address: X'04'
      o  BND.ADDR       server bound address
      o  BND.PORT       server bound port in network octet order
      */
	
	
	protected class
	proxyStateV5Reply
		extends AESocksProxyState
	{
		protected
		proxyStateV5Reply()
		
			throws IOException
		{		
			super( AESocksProxyConnectionImpl.this );
			
			connection.setWriteState( this );
			
			byte[]	addr = plugable_connection.getLocalAddress().getAddress();
			
			int		port = plugable_connection.getLocalPort();
			
			buffer	= ByteBuffer.wrap(
					new byte[]{(byte)5,(byte)0,(byte)0,(byte)1,
								addr[0],addr[1],addr[2],addr[3],
								(byte)((port>>8)&0xff), (byte)(port&0xff)});
					
			
			write( source_channel );
		}
		
		protected boolean
		writeSupport(
			SocketChannel 		sc )
		
			throws IOException
		{
			int	len = sc.write( buffer );
			
			if ( buffer.hasRemaining()){
				
				connection.requestWriteSelect( sc );
				
			}else{
	
				plugable_connection.relayData();
			}
			
			return( len > 0 );
		}
	}
	
	public void
	connected()
	
		throws IOException
	{
		if ( socks_version == 4 ){
			
			new proxyStateV4Reply();
			
		}else{
			
			new proxyStateV5Reply();
		}
	}

	
	protected class
	proxyStateClose
		extends AESocksProxyState
	{
		protected
		proxyStateClose()
		
			throws IOException
		{	
			super( AESocksProxyConnectionImpl.this );
			
			connection.close();
			
			connection.setReadState( null);
			connection.setWriteState( null);
			connection.setConnectState( null);
		}
	}
}

⌨️ 快捷键说明

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