Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/arduino/Arduino.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCristian Maglie <c.maglie@arduino.cc>2020-04-16 23:09:39 +0300
committerCristian Maglie <c.maglie@arduino.cc>2020-07-24 13:37:53 +0300
commit0a3370ccc6b9578c910e3b6c93b5a4164c3036e3 (patch)
tree41b8a768d0c41d7e9d10a53b6fe6e9f50b09aeab
parent6dd8ec8665f8d055a9307547b403fa13c1333873 (diff)
Added method to extract Proxy URI from configuration
-rw-r--r--app/test/cc/arduino/net/CustomProxySelectorTest.java34
-rw-r--r--arduino-core/src/cc/arduino/net/CustomProxySelector.java84
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<URI> 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<URI> 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<URI> 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<URI> 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<URI> 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<URI> 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<URI> 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<URI> 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<URI> 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<URI> 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<String, String> preferences;
@@ -48,6 +65,56 @@ public class CustomProxySelector {
clearPreviousAuthenticator();
}
+ public Optional<URI> 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 {