From 9b1c96111e9060975400c0f8f17fc4ec0279845f Mon Sep 17 00:00:00 2001 From: Björn Hagemeister Date: Wed, 12 Nov 2014 15:41:38 +0100 Subject: Moved whole proxy settings configuration to master-sync-shared. Plus adapted class to this changes. --- pom.xml | 6 ++ .../org/openslx/network/ProxyConfiguration.java | 109 +++++++++------------ .../java/org/openslx/network/ProxyProperties.java | 73 ++++++++++++++ 3 files changed, 128 insertions(+), 60 deletions(-) create mode 100644 src/main/java/org/openslx/network/ProxyProperties.java diff --git a/pom.xml b/pom.xml index e2c3301..60fa968 100644 --- a/pom.xml +++ b/pom.xml @@ -107,5 +107,11 @@ [1.5.8,1.7.5] compile + + com.googlecode.vestige + proxy_vole + 0.0.3-SNAPSHOT + + diff --git a/src/main/java/org/openslx/network/ProxyConfiguration.java b/src/main/java/org/openslx/network/ProxyConfiguration.java index 5a97b62..cfdcdb9 100644 --- a/src/main/java/org/openslx/network/ProxyConfiguration.java +++ b/src/main/java/org/openslx/network/ProxyConfiguration.java @@ -1,73 +1,62 @@ 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 java.net.Authenticator; +import java.net.InetSocketAddress; +import java.net.Proxy; +import java.net.ProxySelector; import org.apache.log4j.Logger; -import org.openslx.util.Util; +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 Logger log = Logger.getLogger( ProxyConfiguration.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" ); - } + private static final Logger log = Logger.getLogger( ProxyConfiguration.class ); - public static String getProxyPassword() + public static void configProxy() { - return properties.getProperty( "PROXY_PASSWORD" ); - } - - // Integers // - public static int getProxyPort() - { - return Util.tryToParseInt( properties.getProperty( "PROXY_PORT" ) ); - } + // 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(); - /** - * 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 ); + // 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 ); + } + } } } - - /** - * 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/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)); + } +} -- cgit v1.2.3-55-g7522