package org.openslx.network; import java.io.IOException; import java.net.InetAddress; import java.net.NetworkInterface; import java.net.Proxy; import java.net.ProxySelector; import java.net.SocketAddress; import java.net.SocketException; import java.net.URI; import java.util.ArrayList; import java.util.Enumeration; import java.util.HashSet; import java.util.List; import java.util.Set; import org.apache.log4j.Logger; public class StaticProxySelector extends ProxySelector { private static Logger log = Logger.getLogger( StaticProxySelector.class ); private final Proxy proxy; private Set localAddresses = null; private long nextAddressGet = 0; public StaticProxySelector( Proxy proxy ) { this.proxy = proxy; } @Override public void connectFailed( URI uri, SocketAddress sa, IOException ioe ) { // Just one fix proxy. So no code is necessary here for deactivating proxy. } @Override public List select( URI uri ) { List proxyList = new ArrayList(); String host = uri.getHost(); if ( host == null ) // Host not set? Well, we can only guess then, so try to use the proxy return proxyList; host = host.replaceFirst( "%\\d+$", "" ); if ( host.equals( "localhost" ) || host.startsWith( "127." ) || host.startsWith( "::1" ) || host.startsWith( "0:0:0:0:0:0:0:1" ) ) // Localhost = no proxy return proxyList; final Set addrs; synchronized ( this ) { addrs = getLocalAddresses(); } if ( !addrs.contains( host ) ) { proxyList.add( this.proxy ); } return proxyList; } /** * Get all local (IP) addresses * * @return */ private Set getLocalAddresses() { long now = System.currentTimeMillis(); if ( now < nextAddressGet ) return localAddresses; nextAddressGet = now + 60000; List interfaces = getNetworkInterfaces(); if ( interfaces == null ) return localAddresses; // Fallback on last known data // iterate over network interfaces and get all addresses Set addrs = new HashSet<>(); for ( NetworkInterface iface : interfaces ) { Enumeration e = iface.getInetAddresses(); // iterate over InetAddresses of current interface while ( e.hasMoreElements() ) { addrs.add( e.nextElement().getHostAddress().replaceFirst( "%\\d+$", "" ) ); } } synchronized ( this ) { localAddresses = addrs; } return localAddresses; } /** * Get a list of all local network interfaces * * @return */ private List getNetworkInterfaces() { ArrayList retList = new ArrayList(); Enumeration e = null; try { e = NetworkInterface.getNetworkInterfaces(); } catch ( SocketException e1 ) { // TODO Auto-generated catch block e1.printStackTrace(); return null; } while ( e.hasMoreElements() ) { retList.add( e.nextElement() ); } return retList; } }