From aa8d4bb45bc251cc1f01b80095dc60485a242115 Mon Sep 17 00:00:00 2001 From: Björn Hagemeister Date: Tue, 11 Nov 2014 15:37:17 +0100 Subject: Handles check for connection to own IP - address more generall. --- .../org/openslx/network/StaticProxySelector.java | 46 ++++++++++++++++++---- 1 file 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 proxyList = new ArrayList(); 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 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 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( (NetworkInterface)e.nextElement() ); + } + return retList; + } + } -- cgit v1.2.3-55-g7522