From 0a3370ccc6b9578c910e3b6c93b5a4164c3036e3 Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Thu, 16 Apr 2020 22:09:39 +0200 Subject: [PATCH] Added method to extract Proxy URI from configuration --- .../arduino/net/CustomProxySelectorTest.java | 34 ++++++++ .../cc/arduino/net/CustomProxySelector.java | 84 +++++++++++++++++-- 2 files changed, 109 insertions(+), 9 deletions(-) diff --git a/app/test/cc/arduino/net/CustomProxySelectorTest.java b/app/test/cc/arduino/net/CustomProxySelectorTest.java index 411f1aa23..65087e114 100644 --- a/app/test/cc/arduino/net/CustomProxySelectorTest.java +++ b/app/test/cc/arduino/net/CustomProxySelectorTest.java @@ -36,8 +36,11 @@ import org.junit.Test; import java.net.*; import java.util.HashMap; import java.util.Map; +import java.util.Optional; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; public class CustomProxySelectorTest { @@ -58,6 +61,8 @@ public class CustomProxySelectorTest { Proxy proxy = proxySelector.getProxyFor(uri); assertEquals(Proxy.NO_PROXY, proxy); + Optional proxyURI = proxySelector.getProxyURIFor(uri); + assertFalse(proxyURI.isPresent()); } @Test @@ -67,6 +72,8 @@ public class CustomProxySelectorTest { Proxy proxy = proxySelector.getProxyFor(uri); assertEquals(ProxySelector.getDefault().select(uri).get(0), proxy); + Optional proxyURI = proxySelector.getProxyURIFor(uri); + assertFalse(proxyURI.isPresent()); } @Test @@ -77,6 +84,9 @@ public class CustomProxySelectorTest { Proxy proxy = proxySelector.getProxyFor(uri); assertEquals(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("proxy.example.com", 8080)), proxy); + Optional proxyURI = proxySelector.getProxyURIFor(uri); + assertTrue(proxyURI.isPresent()); + assertEquals("http://proxy.example.com:8080/", proxyURI.get().toString()); } @Test @@ -93,6 +103,10 @@ public class CustomProxySelectorTest { PasswordAuthentication authentication = Authenticator.requestPasswordAuthentication(null, 8080, uri.toURL().getProtocol(), "ciao", ""); assertEquals(authentication.getUserName(), "auto"); assertEquals(String.valueOf(authentication.getPassword()), "autopassword"); + + Optional proxyURI = proxySelector.getProxyURIFor(uri); + assertTrue(proxyURI.isPresent()); + assertEquals("http://auto:autopassword@proxy.example.com:8080/", proxyURI.get().toString()); } @Test @@ -103,6 +117,10 @@ public class CustomProxySelectorTest { Proxy proxy = proxySelector.getProxyFor(uri); assertEquals(new Proxy(Proxy.Type.SOCKS, new InetSocketAddress("proxy.example.com", 8080)), proxy); + + Optional proxyURI = proxySelector.getProxyURIFor(uri); + assertTrue(proxyURI.isPresent()); + assertEquals("socks://proxy.example.com:8080/", proxyURI.get().toString()); } @Test @@ -113,6 +131,8 @@ public class CustomProxySelectorTest { Proxy proxy = proxySelector.getProxyFor(uri); assertEquals(Proxy.NO_PROXY, proxy); + Optional proxyURI = proxySelector.getProxyURIFor(uri); + assertFalse(proxyURI.isPresent()); } @Test @@ -123,6 +143,10 @@ public class CustomProxySelectorTest { Proxy proxy = proxySelector.getProxyFor(uri); assertEquals(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("4.5.6.7", 8080)), proxy); + + Optional proxyURI = proxySelector.getProxyURIFor(uri); + assertTrue(proxyURI.isPresent()); + assertEquals("http://4.5.6.7:8080/", proxyURI.get().toString()); } @Test @@ -133,6 +157,8 @@ public class CustomProxySelectorTest { Proxy proxy = proxySelector.getProxyFor(new URL("http://www.intranet.domain.com/ciao").toURI()); assertEquals(Proxy.NO_PROXY, proxy); + Optional proxyURI = proxySelector.getProxyURIFor(new URL("http://www.intranet.domain.com/ciao").toURI()); + assertFalse(proxyURI.isPresent()); } @Test @@ -146,6 +172,10 @@ public class CustomProxySelectorTest { Proxy proxy = proxySelector.getProxyFor(uri); assertEquals(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("localhost", 8080)), proxy); + + Optional proxyURI = proxySelector.getProxyURIFor(uri); + assertTrue(proxyURI.isPresent()); + assertEquals("http://localhost:8080/", proxyURI.get().toString()); } @Test @@ -165,5 +195,9 @@ public class CustomProxySelectorTest { PasswordAuthentication authentication = Authenticator.requestPasswordAuthentication(null, 8080, uri.toURL().getProtocol(), "ciao", ""); assertEquals(authentication.getUserName(), "username"); assertEquals(String.valueOf(authentication.getPassword()), "pwd"); + + Optional proxyURI = proxySelector.getProxyURIFor(uri); + assertTrue(proxyURI.isPresent()); + assertEquals("http://username:pwd@localhost:8080/", proxyURI.get().toString()); } } diff --git a/arduino-core/src/cc/arduino/net/CustomProxySelector.java b/arduino-core/src/cc/arduino/net/CustomProxySelector.java index 32a0894e8..b96094ae8 100644 --- a/arduino-core/src/cc/arduino/net/CustomProxySelector.java +++ b/arduino-core/src/cc/arduino/net/CustomProxySelector.java @@ -29,16 +29,33 @@ package cc.arduino.net; -import cc.arduino.Constants; -import org.apache.commons.compress.utils.IOUtils; - -import javax.script.*; import java.io.IOException; -import java.net.*; +import java.net.Authenticator; +import java.net.HttpURLConnection; +import java.net.InetSocketAddress; +import java.net.MalformedURLException; +import java.net.PasswordAuthentication; +import java.net.Proxy; +import java.net.ProxySelector; +import java.net.URI; +import java.net.URISyntaxException; +import java.net.URL; +import java.net.URLConnection; import java.nio.charset.Charset; import java.util.Map; +import java.util.Optional; import java.util.stream.Stream; +import javax.script.Invocable; +import javax.script.ScriptContext; +import javax.script.ScriptEngine; +import javax.script.ScriptEngineManager; +import javax.script.ScriptException; + +import org.apache.commons.compress.utils.IOUtils; + +import cc.arduino.Constants; + public class CustomProxySelector { private final Map preferences; @@ -48,6 +65,56 @@ public class CustomProxySelector { clearPreviousAuthenticator(); } + public Optional getProxyURIFor(URI uri) throws URISyntaxException { + String auth = ""; + String user = preferences.getOrDefault(Constants.PREF_PROXY_USERNAME, ""); + if (!user.isEmpty()) { + String pass = preferences.getOrDefault(Constants.PREF_PROXY_PASSWORD, ""); + auth = user + ":" + pass + "@"; + } + String host, port, proto; + + switch (preferences.get(Constants.PREF_PROXY_TYPE)) { + default: + return Optional.empty(); + + case Constants.PROXY_TYPE_NONE: + return Optional.empty(); + + case Constants.PROXY_TYPE_MANUAL: + host = preferences.get(Constants.PREF_PROXY_MANUAL_HOSTNAME); + port = preferences.get(Constants.PREF_PROXY_MANUAL_PORT); + proto = preferences.get(Constants.PREF_PROXY_MANUAL_TYPE).toLowerCase(); + break; + + case Constants.PROXY_TYPE_AUTO: + String pac = preferences.getOrDefault(Constants.PREF_PROXY_PAC_URL, ""); + if (pac.isEmpty()) { + return Optional.empty(); + } + + try { + String proxyConfigs = pacProxy(pac, uri); + System.out.println(proxyConfigs); + String proxyConfig = proxyConfigs.split(";")[0]; + if (proxyConfig.startsWith("DIRECT")) { + return Optional.empty(); + } + proto = proxyConfig.startsWith("PROXY ") ? "http" : "socks"; + proxyConfig = proxyConfig.substring(6); + String[] hostPort = proxyConfig.split(":"); + host = hostPort[0]; + port = hostPort[1]; + } catch (Exception e) { + e.printStackTrace(); + return Optional.empty(); + } + break; + } + + return Optional.of(new URI(proto + "://" + auth + host + ":" + port + "/")); + } + public Proxy getProxyFor(URI uri) throws IOException, ScriptException, NoSuchMethodException { String proxyType = preferences.get(Constants.PREF_PROXY_TYPE); if (proxyType == null || proxyType.isEmpty()) { @@ -64,7 +131,7 @@ public class CustomProxySelector { return ProxySelector.getDefault().select(uri).get(0); } - return pacProxy(pac, uri); + return makeProxyFrom(pacProxy(pac, uri)); } if (Constants.PROXY_TYPE_MANUAL.equals(proxyType)) { @@ -74,7 +141,7 @@ public class CustomProxySelector { throw new IllegalStateException("Unable to understand proxy settings"); } - private Proxy pacProxy(String pac, URI uri) throws IOException, ScriptException, NoSuchMethodException { + private String pacProxy(String pac, URI uri) throws IOException, ScriptException, NoSuchMethodException { setAuthenticator(preferences.get(Constants.PREF_PROXY_USERNAME), preferences.get(Constants.PREF_PROXY_PASSWORD)); URLConnection urlConnection = new URL(pac).openConnection(); @@ -105,8 +172,7 @@ public class CustomProxySelector { } }); nashorn.eval(pacScript); - String proxyConfigs = callFindProxyForURL(uri, nashorn); - return makeProxyFrom(proxyConfigs); + return callFindProxyForURL(uri, nashorn); } private String callFindProxyForURL(URI uri, ScriptEngine nashorn) throws ScriptException, NoSuchMethodException {