| 1 | #proxy-vole |
| 2 | |
| 3 | Proxy Vole is a Java library to auto detect the platform network proxy settings. |
| 4 | Note: This library is a fork of the now dead [proxy-vole](https://code.google.com/p/proxy-vole/) project by Bernd Rosstauscher hosted at Google Code. |
| 5 | |
| 6 | ##Introduction |
| 7 | The library provides some proxy setting search strategies to read the proxy settings from the system config |
| 8 | (Windows, KDE, Gnome, OSX), browser config (Firefox, IE) or environment variables and provides you an ready to use proxy selector. |
| 9 | |
| 10 | ##Why a fork? |
| 11 | * Can't contact Bernd Rosstauscher. |
| 12 | * Google Code is dead by now. |
| 13 | * proxy-vole seems to be dead even longer. Last commit mid 2015. Last release end 2013. |
| 14 | * proxy-vole is not available on any public Maven repository. Needed to change the Maven coordinates and Java package names |
| 15 | to be able to push it to Maven Central on my own. |
| 16 | * I don't like the Windows DLL and usage of JNI. Replaced both by JNA. |
| 17 | |
| 18 | ##Usage |
| 19 | |
| 20 | ###Using the default strategy to find the settings |
| 21 | ```Java |
| 22 | // Use the static factory method getDefaultProxySearch to create a proxy search instance |
| 23 | // configured with the default proxy search strategies for the current environment. |
| 24 | ProxySearch proxySearch = ProxySearch.getDefaultProxySearch(); |
| 25 | |
| 26 | // Invoke the proxy search. This will create a ProxySelector with the detected proxy settings. |
| 27 | ProxySelector proxySelector = proxySearch.getProxySelector(); |
| 28 | |
| 29 | // Install this ProxySelector as default ProxySelector for all connections. |
| 30 | ProxySelector.setDefault(proxySelector); |
| 31 | ``` |
| 32 | |
| 33 | ###Modifying the search strategy |
| 34 | ```Java |
| 35 | // Create a not configured proxy search instance and configure customized proxy search strategies. |
| 36 | ProxySearch proxySearch = new ProxySearch(); |
| 37 | if (PlatformUtil.getCurrentPlattform() == Platform.WIN) { |
| 38 | proxySearch.addStrategy(Strategy.IE); |
| 39 | proxySearch.addStrategy(Strategy.FIREFOX); |
| 40 | proxySearch.addStrategy(Strategy.JAVA); |
| 41 | } else if (PlatformUtil.getCurrentPlattform() == Platform.LINUX) { |
| 42 | proxySearch.addStrategy(Strategy.GNOME); |
| 43 | proxySearch.addStrategy(Strategy.KDE); |
| 44 | proxySearch.addStrategy(Strategy.FIREFOX); |
| 45 | } else { |
| 46 | proxySearch.addStrategy(Strategy.OS_DEFAULT); |
| 47 | } |
| 48 | ``` |
| 49 | |
| 50 | ###Improving PAC performance |
| 51 | When your system uses a proxy automation script (PAC) Javascript is used to determine the actual proxy. |
| 52 | If your program needs to access a lot of HTTP URLs, then this might become a performance bottleneck. |
| 53 | To speed things up a little bit, you can activate a cache that will store already processed URLs. |
| 54 | When a cached URL is accessed the Javascript execution will be skipped and the cached proxy is used. |
| 55 | ```Java |
| 56 | // Use the static factory method getDefaultProxySearch to create a proxy search instance |
| 57 | // configured with the default proxy search strategies for the current environment. |
| 58 | ProxySearch proxySearch = ProxySearch.getDefaultProxySearch(); |
| 59 | |
| 60 | // Cache 20 hosts for up to 10 minutes. This is the default. |
| 61 | proxySearch.setPacCacheSettings(20, 1000*60*5, CacheScope.CACHE_SCOPE_HOST); |
| 62 | ``` |
| 63 | |
| 64 | ###How to handle proxy authentication |
| 65 | Some proxy servers request a login from the user before they will allow any connections. Proxy Vole |
| 66 | has no support to handle this automatically. This needs to be done manually, because there is no way to read |
| 67 | the login and password. These settings are stored encrypted. You need to install an authenticator in your Java |
| 68 | program manually and e.g. ask the user in a dialog to enter the username and password. |
| 69 | ```Java |
| 70 | Authenticator.setDefault(new Authenticator() { |
| 71 | protected PasswordAuthentication getPasswordAuthentication() { |
| 72 | if (getRequestorType() == RequestorType.PROXY) { |
| 73 | return new PasswordAuthentication("proxy-user", "proxy-password".toCharArray()); |
| 74 | } else { |
| 75 | return super.getPasswordAuthentication(); |
| 76 | } |
| 77 | } |
| 78 | }); |
| 79 | ``` |
| 80 | |
| 81 | ###Choose the right proxy |
| 82 | Please be aware a Java ProxySelector returns a list of valid proxies for a given URL and sometimes simply |
| 83 | choosing the first one is not good enough. Very often a check of the supported protocol is neccessary. |
| 84 | |
| 85 | The following code chooses the first HTTP/S proxy. |
| 86 | ```Java |
| 87 | Proxy proxy = Proxy.NO_PROXY; |
| 88 | |
| 89 | // Get list of proxies from default ProxySelector available for given URL |
| 90 | List<Proxy> proxies = null; |
| 91 | if (ProxySelector.getDefault() != null) { |
| 92 | proxies = ProxySelector.getDefault().select(uri); |
| 93 | } |
| 94 | |
| 95 | // Find first proxy for HTTP/S. Any DIRECT proxy in the list returned is only second choice |
| 96 | if (proxies != null) { |
| 97 | loop: for (Proxy p : proxies) { |
| 98 | switch (p.type()) { |
| 99 | case HTTP: |
| 100 | proxy = p; |
| 101 | break loop; |
| 102 | case DIRECT: |
| 103 | proxy = p; |
| 104 | break; |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | ``` |
| 109 | |
| 110 | ###Logging |
| 111 | Proxy Vole does not use Log4J, LogBack or SLF4J to make the library as light weight as possible with no external dependencies. |
| 112 | If you need to know what is going on inside of the library you may want to install a logger. |
| 113 | ```Java |
| 114 | // Register MyLogger instance |
| 115 | Logger.setBackend(new MyLogger()); |
| 116 | ``` |
| 117 | |
| 118 | ###Testing PAC |
| 119 | Testing the PAC parser can be problematic, because the myIPAddress() method returns different results on different machines. |
| 120 | Therefore the system property com.github.markusbernhardt.proxy.pac.overrideLocalIP can be set for unit tests. |
| 121 | It's value will always be used as myIPAddress in all PAC scripts. |
| 122 | ```Java |
| 123 | System.setProperty(PacScriptMethods.OVERRIDE_LOCAL_IP, "123.123.123.123"); |
| 124 | ``` |
| 125 | |
| 126 | ###Proxy Vole Tester |
| 127 | There is also a small GUI to test the different search strategies. Simply start the [jar-with-dependencies](http://search.maven.org/remotecontent?filepath=com/github/markusbernhardt/proxy-vole/1.0.1/proxy-vole-1.0.1-jar-with-dependencies.jar) |
| 128 | or directly the class `com.github.markusbernhardt.proxy.ui.ProxyTester`. |
| 129 | |
| 130 |  |
| 131 | |
| 132 | ##Motivation |
| 133 | Today more and more applications try to take direct advantage of Internet connectivity and web services to bring web content |
| 134 | to your desktop. Java has very good network and Internet connectivity with build in APIs for HTTP, HTTPS, FTP and a web services stack. |
| 135 | But the first thing that you need, to use all this fantastic technology, is a working Internet connection. And this is where Java |
| 136 | lacks a lot in my opinion. When you develop an applet or an Java Webstart application the situation is quite good. The Java |
| 137 | Plugin will use the proxy settings and connections as used by the browser, but for standalone applications the situation is |
| 138 | quite unsatisfactory. You have to ask your users to twiddle with system properties and every Java application has |
| 139 | it's own way of setting proxy configurations. In business environments where you often can find proxy configuration scripts you are stuck. |
| 140 | |
| 141 | ##Current Situation |
| 142 | To set the proxy settings in Java you can use some (documented, but hard to find) system properties on application startup. |
| 143 | At runtime you can use the `ProxySelector` API to configure the proxy settings. Java even comes with a system property to |
| 144 | detect the system proxy settings automatically, but this one is poorly documented and unreliable in its behaviour. |
| 145 | |
| 146 | ##The Solution |
| 147 | Use the Proxy Vole library to provide network connectivity out of the box for your Java application. It provides strategies |
| 148 | for auto detecting the current proxy settings. There are many configurable strategies to choose from. At the moment Proxy Vole |
| 149 | supports the following proxy detection strategies. |
| 150 | |
| 151 | * Read platform settings (Supports: Windows, KDE, Gnome, OSX) |
| 152 | * Read browser setting (Supports: Firefox 3.x+, Internet Explorer; Chrome and Webkit use the platform settings) |
| 153 | * Read environment variables (often used variables on Linux / Unix server systems) |
| 154 | * Auto detection script by using WPAD/PAC (Not all variations supported) |
| 155 |