diff options
author | Björn Hagemeister | 2014-11-11 15:37:17 +0100 |
---|---|---|
committer | Björn Hagemeister | 2014-11-11 15:37:17 +0100 |
commit | aa8d4bb45bc251cc1f01b80095dc60485a242115 (patch) | |
tree | d69925990a8baf61817fadbd93a45c8bcee448a8 /src/main/java/org/openslx/network | |
parent | Added ProxySelector Class for connecting to masterserver via proxy if necessary. (diff) | |
download | master-sync-shared-aa8d4bb45bc251cc1f01b80095dc60485a242115.tar.gz master-sync-shared-aa8d4bb45bc251cc1f01b80095dc60485a242115.tar.xz master-sync-shared-aa8d4bb45bc251cc1f01b80095dc60485a242115.zip |
Handles check for connection to own IP - address more generall.
Diffstat (limited to 'src/main/java/org/openslx/network')
-rw-r--r-- | src/main/java/org/openslx/network/StaticProxySelector.java | 46 |
1 files changed, 38 insertions, 8 deletions
diff --git a/src/main/java/org/openslx/network/StaticProxySelector.java b/src/main/java/org/openslx/network/StaticProxySelector.java index 28332f6..de872d9 100644 --- a/src/main/java/org/openslx/network/StaticProxySelector.java +++ b/src/main/java/org/openslx/network/StaticProxySelector.java @@ -1,12 +1,15 @@ package org.openslx.network; import java.io.IOException; -import java.net.InetSocketAddress; +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.List; import org.apache.log4j.Logger; @@ -34,17 +37,44 @@ public class StaticProxySelector extends ProxySelector List<Proxy> proxyList = new ArrayList<Proxy>(); String host = uri.getHost(); - log.info( "Host: " + host ); + log.info( "Connect to: " + host ); - // If host equals localhost return empty list. - if ( ! ( host.startsWith( "127." ) ) && ! ( host.equals( "localhost" ) ) ) { - // log.info("host.startsWith(127.): " + host.startsWith( "127." )); - // log.info( "host.equals(localhost): " + host.equals("localhost")); - log.info( "Adding proxy to proxyList" ); - proxyList.add( this.proxy ); + List<NetworkInterface> nWI = getNetworkInterfaces(); + + // iterate over network interfaces and check for InetAddresses. + for ( int i = 0; i < nWI.size(); ++i ) { + Enumeration e = nWI.get( i ).getInetAddresses(); + // iterate over InetAddresses of current interface. + while ( e.hasMoreElements() ) { + InetAddress address = (InetAddress)e.nextElement(); + // Add proxy to list, if host do not equals to address. + if ( ! ( host.equals( address ) ) && + ! ( host.startsWith( "127." ) ) && + ! ( host.equals( "localhost" ) ) ) { + proxyList.add( this.proxy ); + } + } } // log.info( "proxyList: " + proxyList.toString() ); return proxyList; } + // Getting ArrayList with all NetworkInterfaces. + private ArrayList<NetworkInterface> getNetworkInterfaces() + { + ArrayList<NetworkInterface> retList = new ArrayList<NetworkInterface>(); + Enumeration e = null; + try { + e = NetworkInterface.getNetworkInterfaces(); + } catch ( SocketException e1 ) { + // TODO Auto-generated catch block + e1.printStackTrace(); + return null; + } + while ( e.hasMoreElements() ) { + retList.add( (NetworkInterface)e.nextElement() ); + } + return retList; + } + } |