diff options
Diffstat (limited to 'src/main/java/com/btr/proxy/search/desktop/win')
4 files changed, 379 insertions, 0 deletions
diff --git a/src/main/java/com/btr/proxy/search/desktop/win/DLLManager.java b/src/main/java/com/btr/proxy/search/desktop/win/DLLManager.java new file mode 100644 index 0000000..8d67984 --- /dev/null +++ b/src/main/java/com/btr/proxy/search/desktop/win/DLLManager.java @@ -0,0 +1,171 @@ +package com.btr.proxy.search.desktop.win; + +import java.io.Closeable; +import java.io.File; +import java.io.FileFilter; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +import com.btr.proxy.util.Logger; +import com.btr.proxy.util.Logger.LogLevel; + +/***************************************************************************** + * This class provides some helper methods to work with the dll + * extracting /loading for windows. + * + * @author Bernd Rosstauscher (proxyvole@rosstauscher.de) Copyright 2009 + ****************************************************************************/ + +final class DLLManager { + + private static final class TempDLLFileFilter implements FileFilter { + public boolean accept(File pathname) { + String name = pathname.getName(); + return pathname.isFile() && + name.startsWith(TEMP_FILE_PREFIX) && + name.endsWith(DLL_EXTENSION); + } + } + + public static final String LIB_DIR_OVERRIDE = "proxy_vole_lib_dir"; + + static final String TEMP_FILE_PREFIX = "proxy_vole"; + static final String DLL_EXTENSION = ".dll"; + static String LIB_NAME_BASE = "proxy_util_"; + static final String DEFAULT_LIB_FOLDER = "lib"; + + /************************************************************************* + * Find the location of the native code dll file. + * @return the File pointing to the dll. + * @throws IOException on IO error. + ************************************************************************/ + + static File findLibFile() throws IOException { + String libName = buildLibName(); + File libFile = getOverrideLibFile(libName); + if (libFile == null || libFile.exists() == false) { + libFile = getDefaultLibFile(libName); + } + if (libFile == null || libFile.exists() == false) { + libFile = extractToTempFile(libName); + } + return libFile; + } + + /************************************************************************* + * Delete old temp files that may be there because they were extracted + * from the jar but could not be deleted on VM shutdown because they + * are still locked by windows. + * This is here to prevent a lot of temp dll files on disk. + ************************************************************************/ + + static void cleanupTempFiles() { + try { + String tempFolder = System.getProperty("java.io.tmpdir"); + if (tempFolder == null || tempFolder.trim().length() == 0) { + return; + } + File fldr = new File(tempFolder); + File[] oldFiles = fldr.listFiles(new TempDLLFileFilter()); + if (oldFiles == null) { + return; + } + for (File tmp : oldFiles) { + tmp.delete(); + } + } catch (Exception e) { + Logger.log(DLLManager.class, LogLevel.DEBUG, "Error cleaning up temporary dll files. ", e); + } + } + + /************************************************************************* + * @param libName + * @return + ************************************************************************/ + + private static File getDefaultLibFile(String libName) { + return new File(DEFAULT_LIB_FOLDER, libName); + } + + /************************************************************************* + * Gets the file name that was overriden via system property. + * @param libName + * @return the file, null if it is not existing. + ************************************************************************/ + + private static File getOverrideLibFile(String libName) { + String libDir = System.getProperty(LIB_DIR_OVERRIDE); + if (libDir == null || libDir.trim().length() == 0) { + return null; + } + return new File(libDir, libName); + } + + /************************************************************************* + * @param libName + * @return + * @throws IOException + * @throws FileNotFoundException + ************************************************************************/ + + static File extractToTempFile(String libName) throws IOException { + InputStream source = Win32ProxyUtils.class.getResourceAsStream("/"+DEFAULT_LIB_FOLDER+"/"+libName); + File tempFile = File.createTempFile(TEMP_FILE_PREFIX, DLL_EXTENSION); + tempFile.deleteOnExit(); + FileOutputStream destination = new FileOutputStream(tempFile); + copy(source, destination); + return tempFile; + } + + /************************************************************************* + * @param c a closeable to cleanup ignoring all errors. + ************************************************************************/ + + private static void closeStream(Closeable c) { + try { + c.close(); + } catch (IOException e) { + // Ignore cleanup errors + } + } + + /************************************************************************* + * Copies the content from source to destination. + * @param source + * @param dest + * @throws IOException + ************************************************************************/ + + static void copy(InputStream source, OutputStream dest) + throws IOException { + try { + byte[] buffer = new byte[1024]; + int read = 0; + while (read >= 0) { + dest.write(buffer, 0, read); + read = source.read(buffer); + } + dest.flush(); + } finally { + closeStream(source); + closeStream(dest); + } + } + + /************************************************************************* + * @return the name of the dll valid for the current architecture. + ************************************************************************/ + + private static String buildLibName() { + String arch = "w32"; + if(!System.getProperty("os.arch").equals("x86") ) { + arch = System.getProperty("os.arch"); + } + return LIB_NAME_BASE + arch + DLL_EXTENSION; + } + +} + diff --git a/src/main/java/com/btr/proxy/search/desktop/win/Win32IESettings.java b/src/main/java/com/btr/proxy/search/desktop/win/Win32IESettings.java new file mode 100644 index 0000000..ef91a22 --- /dev/null +++ b/src/main/java/com/btr/proxy/search/desktop/win/Win32IESettings.java @@ -0,0 +1,68 @@ +package com.btr.proxy.search.desktop.win;
+
+/*****************************************************************************
+ * Proxy settings container used for the native methods.
+ * Will contain the Internet Explorer proxy settings as reported by windows
+ * WinHTTP API.
+ *
+ * @author Bernd Rosstauscher (proxyvole@rosstauscher.de) Copyright 2009
+ ****************************************************************************/
+
+public class Win32IESettings {
+
+ private boolean autoDetect;
+ private String autoConfigUrl;
+ private String proxy;
+ private String proxyBypass;
+
+ /*************************************************************************
+ * Constructor
+ * @param autoDetect flag is autodetect is active or not.
+ * @param autoConfigUrl the URL for a PAC script
+ * @param proxy the proxy server selected
+ * @param proxyBypass the proxy bypass address list.
+ ************************************************************************/
+
+ public Win32IESettings(boolean autoDetect, String autoConfigUrl, String proxy, String proxyBypass) {
+ super();
+ this.autoDetect = autoDetect;
+ this.autoConfigUrl = autoConfigUrl;
+ this.proxy = proxy;
+ this.proxyBypass = proxyBypass;
+ }
+
+ /*************************************************************************
+ * @return Returns the autoDetect.
+ ************************************************************************/
+
+ public boolean isAutoDetect() {
+ return this.autoDetect;
+ }
+
+ /*************************************************************************
+ * @return Returns the autoConfigUrl.
+ ************************************************************************/
+
+ public String getAutoConfigUrl() {
+ return this.autoConfigUrl;
+ }
+
+ /*************************************************************************
+ * @return Returns the proxy.
+ ************************************************************************/
+
+ public String getProxy() {
+ return this.proxy;
+ }
+
+ /*************************************************************************
+ * @return Returns the proxyBypass.
+ ************************************************************************/
+
+ public String getProxyBypass() {
+ return this.proxyBypass;
+ }
+
+
+}
+
diff --git a/src/main/java/com/btr/proxy/search/desktop/win/Win32ProxyUtils.java b/src/main/java/com/btr/proxy/search/desktop/win/Win32ProxyUtils.java new file mode 100644 index 0000000..fb1f0b3 --- /dev/null +++ b/src/main/java/com/btr/proxy/search/desktop/win/Win32ProxyUtils.java @@ -0,0 +1,88 @@ +package com.btr.proxy.search.desktop.win;
+
+import java.io.File;
+import java.io.IOException;
+
+/*****************************************************************************
+ * Defines the native methods used for windows to extract some system information.
+ * <p>
+ * This class will need some native code from the library proxy_util_"arch".dll.
+ * To load this library we use a three step algorithm as following:
+ * </p><P>
+ * First check the System property "proxy_vole_lib_dir" if it is set and
+ * it points to a folder where the dll is found than the dll from this
+ * folder is loaded as e.g. <i>"proxy_vole_lib_dir"/proxy_util_w32.dll</i>
+ * </p><p>
+ * Second we try to load the dll from the subfolder <i>lib</i> if that one exists.<br>
+ * Finally if we are inside of a jar file we need to extract the dll file
+ * to a temp-file because windows can not load dlls from a jar
+ * directly. This is a hack but it may work.
+ * </p><p>
+ * Please note that the file is named Win32ProxyUtils but has now also support
+ * for x64 architecture.
+ * </p>
+ * @author Bernd Rosstauscher (proxyvole@rosstauscher.de) Copyright 2009
+ ****************************************************************************/
+
+public class Win32ProxyUtils {
+
+ public static final int WINHTTP_AUTO_DETECT_TYPE_DHCP = 0x00000001;
+ public static final int WINHTTP_AUTO_DETECT_TYPE_DNS_A = 0x00000002;
+
+
+ // Code for loading the windows native dll
+ static {
+ try {
+ File libFile = DLLManager.findLibFile();
+ System.load(libFile.getAbsolutePath());
+ DLLManager.cleanupTempFiles();
+ } catch (IOException e) {
+ throw new RuntimeException("Error loading dll"+e.getMessage(), e);
+ }
+ }
+
+ /*************************************************************************
+ * Constructor
+ ************************************************************************/
+
+ public Win32ProxyUtils() {
+ super();
+ }
+
+ /*************************************************************************
+ * WinHTTP method to detect an PAC URL.
+ * @param mode the mode to use.
+ * @return the PAC URL, null if none was found.
+ ************************************************************************/
+
+ public native String winHttpDetectAutoProxyConfigUrl(int mode);
+
+ /*************************************************************************
+ * Gets the default windows proxy settings.
+ * The returned string will have the following format.
+ * TYPE PROXY | BYPASSLIST
+ * <p>
+ * e.g. DIRECT myproxy.mycompany.com:8080 | *.mycompany.com, localhost
+ * </p>
+ * @return a string containing all info, null if not found.
+ ************************************************************************/
+ // TODO Not implemented correctly in DLL yet.
+ native String winHttpGetDefaultProxyConfiguration();
+
+ /*************************************************************************
+ * Extracts the Internet Explorer proxy settings from the Windows system.
+ * @return a data structure containing all details, null on fail.
+ ************************************************************************/
+
+ public native Win32IESettings winHttpGetIEProxyConfigForCurrentUser();
+
+ /*************************************************************************
+ * Extracts the Internet Explorer proxy settings from the Windows system.
+ * @return a data structure containing all details, null on fail.
+ ************************************************************************/
+
+ public native String readUserHomedir();
+
+
+}
+
diff --git a/src/main/java/com/btr/proxy/search/desktop/win/WinProxySearchStrategy.java b/src/main/java/com/btr/proxy/search/desktop/win/WinProxySearchStrategy.java new file mode 100644 index 0000000..1f26505 --- /dev/null +++ b/src/main/java/com/btr/proxy/search/desktop/win/WinProxySearchStrategy.java @@ -0,0 +1,52 @@ +package com.btr.proxy.search.desktop.win; + +import java.net.ProxySelector; + +import com.btr.proxy.search.ProxySearchStrategy; +import com.btr.proxy.search.browser.ie.IEProxySearchStrategy; +import com.btr.proxy.util.ProxyException; + +/***************************************************************************** + * Extracts the proxy settings from the windows registry. + * This will read the windows system proxy settings. + * + * @author Bernd Rosstauscher (proxyvole@rosstauscher.de) Copyright 2009 + ****************************************************************************/ + +public class WinProxySearchStrategy implements ProxySearchStrategy { + + /************************************************************************* + * Constructor + ************************************************************************/ + + public WinProxySearchStrategy() { + super(); + } + + /************************************************************************* + * getProxySelector + * @see com.btr.proxy.search.ProxySearchStrategy#getProxySelector() + ************************************************************************/ + + public ProxySelector getProxySelector() throws ProxyException { + // TODO Rossi 08.05.2009 Implement this by using Win API calls. + // new Win32ProxyUtils().winHttpGetDefaultProxyConfiguration() + // Current fallback is to use the IE settings. This is better + // because the registry settings are most of the time not set. + // Some Windows server installations may use it though. + return new IEProxySearchStrategy().getProxySelector(); + } + + /************************************************************************* + * Loads the settings. + * @return a WinIESettings object containing all proxy settings. + ************************************************************************/ + + public Win32IESettings readSettings() { + return new IEProxySearchStrategy().readSettings(); + } + + + + +} |