mirror of
https://github.com/arduino/Arduino.git
synced 2025-03-14 11:29:26 +01:00
Added method to extract Proxy URI from configuration
This commit is contained in:
parent
6dd8ec8665
commit
0a3370ccc6
@ -36,8 +36,11 @@ import org.junit.Test;
|
|||||||
import java.net.*;
|
import java.net.*;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertFalse;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
public class CustomProxySelectorTest {
|
public class CustomProxySelectorTest {
|
||||||
|
|
||||||
@ -58,6 +61,8 @@ public class CustomProxySelectorTest {
|
|||||||
Proxy proxy = proxySelector.getProxyFor(uri);
|
Proxy proxy = proxySelector.getProxyFor(uri);
|
||||||
|
|
||||||
assertEquals(Proxy.NO_PROXY, proxy);
|
assertEquals(Proxy.NO_PROXY, proxy);
|
||||||
|
Optional<URI> proxyURI = proxySelector.getProxyURIFor(uri);
|
||||||
|
assertFalse(proxyURI.isPresent());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -67,6 +72,8 @@ public class CustomProxySelectorTest {
|
|||||||
Proxy proxy = proxySelector.getProxyFor(uri);
|
Proxy proxy = proxySelector.getProxyFor(uri);
|
||||||
|
|
||||||
assertEquals(ProxySelector.getDefault().select(uri).get(0), proxy);
|
assertEquals(ProxySelector.getDefault().select(uri).get(0), proxy);
|
||||||
|
Optional<URI> proxyURI = proxySelector.getProxyURIFor(uri);
|
||||||
|
assertFalse(proxyURI.isPresent());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -77,6 +84,9 @@ public class CustomProxySelectorTest {
|
|||||||
Proxy proxy = proxySelector.getProxyFor(uri);
|
Proxy proxy = proxySelector.getProxyFor(uri);
|
||||||
|
|
||||||
assertEquals(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("proxy.example.com", 8080)), proxy);
|
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
|
@Test
|
||||||
@ -93,6 +103,10 @@ public class CustomProxySelectorTest {
|
|||||||
PasswordAuthentication authentication = Authenticator.requestPasswordAuthentication(null, 8080, uri.toURL().getProtocol(), "ciao", "");
|
PasswordAuthentication authentication = Authenticator.requestPasswordAuthentication(null, 8080, uri.toURL().getProtocol(), "ciao", "");
|
||||||
assertEquals(authentication.getUserName(), "auto");
|
assertEquals(authentication.getUserName(), "auto");
|
||||||
assertEquals(String.valueOf(authentication.getPassword()), "autopassword");
|
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
|
@Test
|
||||||
@ -103,6 +117,10 @@ public class CustomProxySelectorTest {
|
|||||||
Proxy proxy = proxySelector.getProxyFor(uri);
|
Proxy proxy = proxySelector.getProxyFor(uri);
|
||||||
|
|
||||||
assertEquals(new Proxy(Proxy.Type.SOCKS, new InetSocketAddress("proxy.example.com", 8080)), proxy);
|
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
|
@Test
|
||||||
@ -113,6 +131,8 @@ public class CustomProxySelectorTest {
|
|||||||
Proxy proxy = proxySelector.getProxyFor(uri);
|
Proxy proxy = proxySelector.getProxyFor(uri);
|
||||||
|
|
||||||
assertEquals(Proxy.NO_PROXY, proxy);
|
assertEquals(Proxy.NO_PROXY, proxy);
|
||||||
|
Optional<URI> proxyURI = proxySelector.getProxyURIFor(uri);
|
||||||
|
assertFalse(proxyURI.isPresent());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -123,6 +143,10 @@ public class CustomProxySelectorTest {
|
|||||||
Proxy proxy = proxySelector.getProxyFor(uri);
|
Proxy proxy = proxySelector.getProxyFor(uri);
|
||||||
|
|
||||||
assertEquals(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("4.5.6.7", 8080)), proxy);
|
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
|
@Test
|
||||||
@ -133,6 +157,8 @@ public class CustomProxySelectorTest {
|
|||||||
Proxy proxy = proxySelector.getProxyFor(new URL("http://www.intranet.domain.com/ciao").toURI());
|
Proxy proxy = proxySelector.getProxyFor(new URL("http://www.intranet.domain.com/ciao").toURI());
|
||||||
|
|
||||||
assertEquals(Proxy.NO_PROXY, proxy);
|
assertEquals(Proxy.NO_PROXY, proxy);
|
||||||
|
Optional<URI> proxyURI = proxySelector.getProxyURIFor(new URL("http://www.intranet.domain.com/ciao").toURI());
|
||||||
|
assertFalse(proxyURI.isPresent());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -146,6 +172,10 @@ public class CustomProxySelectorTest {
|
|||||||
Proxy proxy = proxySelector.getProxyFor(uri);
|
Proxy proxy = proxySelector.getProxyFor(uri);
|
||||||
|
|
||||||
assertEquals(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("localhost", 8080)), proxy);
|
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
|
@Test
|
||||||
@ -165,5 +195,9 @@ public class CustomProxySelectorTest {
|
|||||||
PasswordAuthentication authentication = Authenticator.requestPasswordAuthentication(null, 8080, uri.toURL().getProtocol(), "ciao", "");
|
PasswordAuthentication authentication = Authenticator.requestPasswordAuthentication(null, 8080, uri.toURL().getProtocol(), "ciao", "");
|
||||||
assertEquals(authentication.getUserName(), "username");
|
assertEquals(authentication.getUserName(), "username");
|
||||||
assertEquals(String.valueOf(authentication.getPassword()), "pwd");
|
assertEquals(String.valueOf(authentication.getPassword()), "pwd");
|
||||||
|
|
||||||
|
Optional<URI> proxyURI = proxySelector.getProxyURIFor(uri);
|
||||||
|
assertTrue(proxyURI.isPresent());
|
||||||
|
assertEquals("http://username:pwd@localhost:8080/", proxyURI.get().toString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -29,16 +29,33 @@
|
|||||||
|
|
||||||
package cc.arduino.net;
|
package cc.arduino.net;
|
||||||
|
|
||||||
import cc.arduino.Constants;
|
|
||||||
import org.apache.commons.compress.utils.IOUtils;
|
|
||||||
|
|
||||||
import javax.script.*;
|
|
||||||
import java.io.IOException;
|
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.nio.charset.Charset;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Optional;
|
||||||
import java.util.stream.Stream;
|
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 {
|
public class CustomProxySelector {
|
||||||
|
|
||||||
private final Map<String, String> preferences;
|
private final Map<String, String> preferences;
|
||||||
@ -48,6 +65,56 @@ public class CustomProxySelector {
|
|||||||
clearPreviousAuthenticator();
|
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 {
|
public Proxy getProxyFor(URI uri) throws IOException, ScriptException, NoSuchMethodException {
|
||||||
String proxyType = preferences.get(Constants.PREF_PROXY_TYPE);
|
String proxyType = preferences.get(Constants.PREF_PROXY_TYPE);
|
||||||
if (proxyType == null || proxyType.isEmpty()) {
|
if (proxyType == null || proxyType.isEmpty()) {
|
||||||
@ -64,7 +131,7 @@ public class CustomProxySelector {
|
|||||||
return ProxySelector.getDefault().select(uri).get(0);
|
return ProxySelector.getDefault().select(uri).get(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
return pacProxy(pac, uri);
|
return makeProxyFrom(pacProxy(pac, uri));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Constants.PROXY_TYPE_MANUAL.equals(proxyType)) {
|
if (Constants.PROXY_TYPE_MANUAL.equals(proxyType)) {
|
||||||
@ -74,7 +141,7 @@ public class CustomProxySelector {
|
|||||||
throw new IllegalStateException("Unable to understand proxy settings");
|
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));
|
setAuthenticator(preferences.get(Constants.PREF_PROXY_USERNAME), preferences.get(Constants.PREF_PROXY_PASSWORD));
|
||||||
|
|
||||||
URLConnection urlConnection = new URL(pac).openConnection();
|
URLConnection urlConnection = new URL(pac).openConnection();
|
||||||
@ -105,8 +172,7 @@ public class CustomProxySelector {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
nashorn.eval(pacScript);
|
nashorn.eval(pacScript);
|
||||||
String proxyConfigs = callFindProxyForURL(uri, nashorn);
|
return callFindProxyForURL(uri, nashorn);
|
||||||
return makeProxyFrom(proxyConfigs);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private String callFindProxyForURL(URI uri, ScriptEngine nashorn) throws ScriptException, NoSuchMethodException {
|
private String callFindProxyForURL(URI uri, ScriptEngine nashorn) throws ScriptException, NoSuchMethodException {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user