FileDocCategorySizeDatePackage
AEProxyAddressMapperImpl.javaAPI DocAzureus 3.0.3.45007Thu Jan 11 10:20:24 GMT 2007com.aelitis.azureus.core.proxy.impl

AEProxyAddressMapperImpl

public class AEProxyAddressMapperImpl extends Object implements com.aelitis.azureus.core.proxy.AEProxyAddressMapper
author
parg

Fields Summary
protected static com.aelitis.azureus.core.proxy.AEProxyAddressMapper
singleton
protected boolean
enabled
protected String
prefix
protected long
next_value
protected Map
map
protected Map
reverse_map
protected org.gudy.azureus2.core3.util.AEMonitor
this_mon
Constructors Summary
protected AEProxyAddressMapperImpl()

			
	
	
	
	    if ( 	COConfigurationManager.getBooleanParameter("Enable.Proxy") &&
	    		COConfigurationManager.getBooleanParameter("Enable.SOCKS")){
	    	
	    	String	host = COConfigurationManager.getStringParameter("Proxy.Host");
	
	    	try{
		    	if ( 	host.length() > 0 &&
		    			InetAddress.getByName(host).isLoopbackAddress()){
		    		
		    		enabled	= true;
		    		
		    		byte[]	b = new byte[120];
		    		
		    		for (int i=0;i<b.length;i++){
		    			
		    			b[i] = (byte)(Math.random()*256);
		    		}
		    		
		    		prefix = ByteFormatter.encodeString( b );
		    	}
	    	}catch( Throwable e ){
	    		
	    		Debug.printStackTrace(e);
	    	}
	    }
	
Methods Summary
public java.lang.Stringexternalise(java.lang.String address)

		if ( !enabled || address.length() < 255 ){
			
			return( address );
		}
		
		String	target = (String)map.get( address );
		
		if ( target == null ){
			
			target = address;
		}
		
		// System.out.println( "AEProxyAddressMapper: externalise " + address + " -> " + target );
		
		return( target );
	
public java.net.URLexternalise(java.net.URL url)

		if ( !enabled ){
			
			return( url );
		}
		
		String	host	= url.getHost();
		
		if ( host.length() < 255 ){
			
			return( url );
		}
		
		String	new_host = externalise( host );
		
		String	url_str = url.toString();
		
		int	pos = url_str.indexOf( host );
		
		if ( pos == -1 ){
			
			Debug.out( "inconsistent url '" + url_str + "' / '" + host + "'" );
			
			return( url );
		}
		
		String 	new_url_str = url_str.substring(0,pos) +
				new_host + url_str.substring(pos+host.length());
		
		try{
			return( new URL( new_url_str ));
			
		}catch( MalformedURLException e ){
			
			Debug.printStackTrace(e);
			
			return( url );
		}
	
public static com.aelitis.azureus.core.proxy.AEProxyAddressMappergetSingleton()

	
	  
	
	
		return( singleton );
	
public java.lang.Stringinternalise(java.lang.String address)

		if ( !enabled ){
			
			return( address );
		}
		
		if ( address.length() < 256 ){
			
			return( address );
		}
		
		String	target;
		
		try{
			this_mon.enter();
			
			target = (String)reverse_map.get( address );
			
			if ( target == null ){
				
				target = prefix + (next_value++);
			
				while( target.length() < 255 ){
				
					target += "0";
				}
				
				map.put( target, address );
				
				reverse_map.put( address, target );
			}
		}finally{
			
			this_mon.exit();
		}
		
		// System.out.println( "AEProxyAddressMapper: internalise " + address + " -> " + target );
		
		return( target );
	
public java.net.URLinternalise(java.net.URL url)

		if ( !enabled ){
			
			return( url );
		}
		
		String	host = url.getHost();
		
		if ( host.length() < 256 ){
			
			return( url );
		}
		
		String	new_host = internalise( host );
		
		String	url_str = url.toString();
		
		int	pos = url_str.indexOf( host );
		
		if ( pos == -1 ){
			
			Debug.out( "inconsistent url '" + url_str + "' / '" + host + "'" );
			
			return( url );
		}
		
		String 	new_url_str = url_str.substring(0,pos) +
				new_host + url_str.substring(pos+host.length());
		
		try{
			return( new URL( new_url_str ));
			
		}catch( MalformedURLException e ){
			
			Debug.printStackTrace(e);
			
			return( url );
		}