summaryrefslogtreecommitdiff
path: root/src/de/blinkt/openvpn/ProxyDetection.java
diff options
context:
space:
mode:
authorArne Schwabe <arne@rfc2549.org>2012-07-30 20:43:49 +0200
committerArne Schwabe <arne@rfc2549.org>2012-07-30 20:43:49 +0200
commitd7ec9395e2f4118a00109a3bffb9fef49bc0f583 (patch)
tree441bc96d698c2fb643a8d8642b66d6bd3586c6ae /src/de/blinkt/openvpn/ProxyDetection.java
parentead513af3e9b408642a7ad41d2dfd2b24d78c665 (diff)
Use system proxy settings for openvpn
Diffstat (limited to 'src/de/blinkt/openvpn/ProxyDetection.java')
-rw-r--r--src/de/blinkt/openvpn/ProxyDetection.java52
1 files changed, 52 insertions, 0 deletions
diff --git a/src/de/blinkt/openvpn/ProxyDetection.java b/src/de/blinkt/openvpn/ProxyDetection.java
new file mode 100644
index 00000000..dfcfbf19
--- /dev/null
+++ b/src/de/blinkt/openvpn/ProxyDetection.java
@@ -0,0 +1,52 @@
+package de.blinkt.openvpn;
+
+import java.net.InetSocketAddress;
+import java.net.MalformedURLException;
+import java.net.Proxy;
+import java.net.ProxySelector;
+import java.net.SocketAddress;
+import java.net.URISyntaxException;
+import java.net.URL;
+import java.util.List;
+
+public class ProxyDetection {
+ static SocketAddress detectProxy(VpnProfile vp) {
+ // Construct a new url with https as protocol
+ try {
+ URL url = new URL(String.format("https://%s:%s",vp.mServerName,vp.mServerPort));
+ Proxy proxy = getFirstProxy(url);
+
+ if(proxy==null)
+ return null;
+ SocketAddress addr = proxy.address();
+ if (addr instanceof InetSocketAddress) {
+ return addr;
+ }
+
+ } catch (MalformedURLException e) {
+ OpenVPN.logError(R.string.getproxy_error,e.getLocalizedMessage());
+ } catch (URISyntaxException e) {
+ OpenVPN.logError(R.string.getproxy_error,e.getLocalizedMessage());
+ }
+ return null;
+ }
+
+ static Proxy getFirstProxy(URL url) throws URISyntaxException {
+ System.setProperty("java.net.useSystemProxies", "true");
+
+ List<Proxy> proxylist = ProxySelector.getDefault().select(url.toURI());
+
+
+ if (proxylist != null) {
+ for (Proxy proxy: proxylist) {
+ SocketAddress addr = proxy.address();
+
+ if (addr != null) {
+ return proxy;
+ }
+ }
+
+ }
+ return null;
+ }
+} \ No newline at end of file