diff options
Diffstat (limited to 'src/main/java/org/openslx/network')
5 files changed, 254 insertions, 0 deletions
diff --git a/src/main/java/org/openslx/network/ProxyConfiguration.java b/src/main/java/org/openslx/network/ProxyConfiguration.java new file mode 100644 index 0000000..cfdcdb9 --- /dev/null +++ b/src/main/java/org/openslx/network/ProxyConfiguration.java @@ -0,0 +1,62 @@ +package org.openslx.network; + +import java.net.Authenticator; +import java.net.InetSocketAddress; +import java.net.Proxy; +import java.net.ProxySelector; + +import org.apache.log4j.Logger; + +import com.btr.proxy.search.wpad.WpadProxySearchStrategy; +import com.btr.proxy.util.ProxyException; + +/** + * Class for configuring proxy settings system wide, if necessary. + * + * @author bjoern + * + */ +public class ProxyConfiguration +{ + private static final Logger log = Logger.getLogger( ProxyConfiguration.class ); + + public static void configProxy() + { + // Configuring proxy settings. First read options from config file. + String proxyConfiguration = ProxyProperties.getProxyConf(); + if ( ( proxyConfiguration.equals( "AUTO" ) ) || ( proxyConfiguration.equals( "" ) ) ) { + log.info( "Configuring proxy settings automatically..." ); + // Configuring proxy settings automatically. + WpadProxySearchStrategy wPSS = new WpadProxySearchStrategy(); + try { + ProxySelector pS = wPSS.getProxySelector(); + ProxySelector.setDefault( pS ); + } catch ( ProxyException e ) { + log.error( "Setting proxy configuration automatically failed.", e ); + } + } else if ( proxyConfiguration.equals( "YES" ) ) { + // Take the proxy settings from config file. + // First check if one of the following necessary options might not be set. + if ( ProxyProperties.checkProxySettings() ) { + String proxyAddress = ProxyProperties.getProxyAddress(); + int proxyPort = ProxyProperties.getProxyPort(); + + // Configure proxy. + Proxy proxy = new Proxy( Proxy.Type.SOCKS, new InetSocketAddress( proxyAddress, proxyPort ) ); + StaticProxySelector sPS = new StaticProxySelector( proxy ); + ProxySelector.setDefault( sPS ); + + if ( ! ( ProxyProperties.getProxyUsername().equals( "" ) ) && ! ( ProxyProperties.getProxyPassword().equals( "" ) ) ) { + log.info( "Configuring proxy settings manually WITH authentication..." ); + // use Proxy with authentication. + String proxyUname = ProxyProperties.getProxyUsername(); + String proxyPass = ProxyProperties.getProxyPassword(); + + // Set authentication. + StaticProxyAuthenticator sPA = new StaticProxyAuthenticator( proxyUname, proxyPass ); + Authenticator.setDefault( sPA ); + } + } + } + } +} diff --git a/src/main/java/org/openslx/network/ProxyProperties.java b/src/main/java/org/openslx/network/ProxyProperties.java new file mode 100644 index 0000000..6675f3a --- /dev/null +++ b/src/main/java/org/openslx/network/ProxyProperties.java @@ -0,0 +1,73 @@ +package org.openslx.network; + +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStreamReader; +import java.nio.charset.StandardCharsets; +import java.util.Properties; + +import org.apache.log4j.Logger; +import org.openslx.util.Util; + +public class ProxyProperties +{ + private static Logger log = Logger.getLogger( ProxyProperties.class ); + private static final Properties properties = new Properties(); + + // Getting the proxy settings from config file stored in + // "/opt/openslx/proxy/conf". + public static String getProxyConf() + { + return properties.getProperty( "PROXY_CONF" ); + } + + public static String getProxyAddress() + { + return properties.getProperty( "PROXY_ADDR" ); + } + + public static String getProxyUsername() + { + return properties.getProperty( "PROXY_USERNAME" ); + } + + public static String getProxyPassword() + { + return properties.getProperty( "PROXY_PASSWORD" ); + } + + // Integers // + public static int getProxyPort() + { + return Util.tryToParseInt( properties.getProperty( "PROXY_PORT" ) ); + } + + /** + * Load properties + */ + static { + InputStreamReader stream = null; + try { + // Load all entries of the config file into properties + stream = new InputStreamReader( + new FileInputStream("/opt/openslx/proxy/config"), StandardCharsets.UTF_8); + properties.load(stream); + stream.close(); + } catch (IOException e) { + log.error("Could not load proxy properties from '/opt/openslx/proxy/conf'. Exiting."); + System.exit( 2 ); + } finally { + Util.streamClose( stream ); + } + } + + /** + * Check proxy settings for being not empty. + * @return + */ + public static boolean checkProxySettings() { + return ( + (getProxyAddress() != "") && + (getProxyPort() != 0)); + } +} diff --git a/src/main/java/org/openslx/network/StaticProxyAuthenticator.java b/src/main/java/org/openslx/network/StaticProxyAuthenticator.java new file mode 100644 index 0000000..a11f1e1 --- /dev/null +++ b/src/main/java/org/openslx/network/StaticProxyAuthenticator.java @@ -0,0 +1,21 @@ +package org.openslx.network; + +import java.net.Authenticator; +import java.net.PasswordAuthentication; + +public class StaticProxyAuthenticator extends Authenticator +{ + private final String username, password; + + public StaticProxyAuthenticator( String username, String password ) + { + this.username = username; + this.password = password; + } + + protected PasswordAuthentication getPasswordAuthentication() + { + return new PasswordAuthentication( + this.username, this.password.toCharArray() ); + } +} diff --git a/src/main/java/org/openslx/network/StaticProxySelector.java b/src/main/java/org/openslx/network/StaticProxySelector.java new file mode 100644 index 0000000..4dc12e9 --- /dev/null +++ b/src/main/java/org/openslx/network/StaticProxySelector.java @@ -0,0 +1,84 @@ +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.List; + +import org.apache.log4j.Logger; + +public class StaticProxySelector extends ProxySelector +{ + private static Logger log = Logger.getLogger( StaticProxySelector.class ); + + private final Proxy proxy; + + 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<Proxy> select( URI uri ) + { + List<Proxy> proxyList = new ArrayList<Proxy>(); + String host = uri.getHost(); + + log.info( "Connect to: " + host ); + + List<NetworkInterface> nWI = getNetworkInterfaces(); + + if ( nWI != null ) { + // iterate over network interfaces and check for InetAddresses. + for ( int i = 0; i < nWI.size(); ++i ) { + Enumeration<InetAddress> 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 ); + } + } + } + } else if ( ! ( 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<NetworkInterface> 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; + } + +} diff --git a/src/main/java/org/openslx/network/config.example b/src/main/java/org/openslx/network/config.example new file mode 100644 index 0000000..522dfdd --- /dev/null +++ b/src/main/java/org/openslx/network/config.example @@ -0,0 +1,14 @@ +# proxy configuration: +# 3 options for proxy configuration: AUTO = autmatically, YES, NO. If nothing is +# set, the default automatically will be choosen. +PROXY_CONF=AUTO +# If PROXY_CONF = YES some more information for connection are necessary: +# PROXY_ADDR = destination addres +PROXY_ADDR=1.2.3.4 + +# PROXY_PORT = port for proxy connection +PROXY_PORT=1080 + +# Username and password are optional, if empty, it is assumed that not necessary: +PROXY_USERNAME=test +PROXY_PASSWORD=password |
