Skip to content

Commit c612b7f

Browse files
committed
Added method to extract Proxy URI from configuration
1 parent d3494dc commit c612b7f

File tree

2 files changed

+109
-9
lines changed

2 files changed

+109
-9
lines changed

app/test/cc/arduino/net/CustomProxySelectorTest.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,11 @@
3636
import java.net.*;
3737
import java.util.HashMap;
3838
import java.util.Map;
39+
import java.util.Optional;
3940

4041
import static org.junit.Assert.assertEquals;
42+
import static org.junit.Assert.assertFalse;
43+
import static org.junit.Assert.assertTrue;
4144

4245
public class CustomProxySelectorTest {
4346

@@ -58,6 +61,8 @@ public void testNoProxy() throws Exception {
5861
Proxy proxy = proxySelector.getProxyFor(uri);
5962

6063
assertEquals(Proxy.NO_PROXY, proxy);
64+
Optional<URI> proxyURI = proxySelector.getProxyURIFor(uri);
65+
assertFalse(proxyURI.isPresent());
6166
}
6267

6368
@Test
@@ -67,6 +72,8 @@ public void testSystemProxy() throws Exception {
6772
Proxy proxy = proxySelector.getProxyFor(uri);
6873

6974
assertEquals(ProxySelector.getDefault().select(uri).get(0), proxy);
75+
Optional<URI> proxyURI = proxySelector.getProxyURIFor(uri);
76+
assertFalse(proxyURI.isPresent());
7077
}
7178

7279
@Test
@@ -77,6 +84,9 @@ public void testProxyPACHTTP() throws Exception {
7784
Proxy proxy = proxySelector.getProxyFor(uri);
7885

7986
assertEquals(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("proxy.example.com", 8080)), proxy);
87+
Optional<URI> proxyURI = proxySelector.getProxyURIFor(uri);
88+
assertTrue(proxyURI.isPresent());
89+
assertEquals("http://proxy.example.com:8080/", proxyURI.get().toString());
8090
}
8191

8292
@Test
@@ -93,6 +103,10 @@ public void testProxyPACHTTPWithLogin() throws Exception {
93103
PasswordAuthentication authentication = Authenticator.requestPasswordAuthentication(null, 8080, uri.toURL().getProtocol(), "ciao", "");
94104
assertEquals(authentication.getUserName(), "auto");
95105
assertEquals(String.valueOf(authentication.getPassword()), "autopassword");
106+
107+
Optional<URI> proxyURI = proxySelector.getProxyURIFor(uri);
108+
assertTrue(proxyURI.isPresent());
109+
assertEquals("http://auto:[email protected]:8080/", proxyURI.get().toString());
96110
}
97111

98112
@Test
@@ -103,6 +117,10 @@ public void testProxyPACSOCKS() throws Exception {
103117
Proxy proxy = proxySelector.getProxyFor(uri);
104118

105119
assertEquals(new Proxy(Proxy.Type.SOCKS, new InetSocketAddress("proxy.example.com", 8080)), proxy);
120+
121+
Optional<URI> proxyURI = proxySelector.getProxyURIFor(uri);
122+
assertTrue(proxyURI.isPresent());
123+
assertEquals("socks://proxy.example.com:8080/", proxyURI.get().toString());
106124
}
107125

108126
@Test
@@ -113,6 +131,8 @@ public void testProxyPACDirect() throws Exception {
113131
Proxy proxy = proxySelector.getProxyFor(uri);
114132

115133
assertEquals(Proxy.NO_PROXY, proxy);
134+
Optional<URI> proxyURI = proxySelector.getProxyURIFor(uri);
135+
assertFalse(proxyURI.isPresent());
116136
}
117137

118138
@Test
@@ -123,6 +143,10 @@ public void testProxyPACComplex() throws Exception {
123143
Proxy proxy = proxySelector.getProxyFor(uri);
124144

125145
assertEquals(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("4.5.6.7", 8080)), proxy);
146+
147+
Optional<URI> proxyURI = proxySelector.getProxyURIFor(uri);
148+
assertTrue(proxyURI.isPresent());
149+
assertEquals("http://4.5.6.7:8080/", proxyURI.get().toString());
126150
}
127151

128152
@Test
@@ -133,6 +157,8 @@ public void testProxyPACComplex2() throws Exception {
133157
Proxy proxy = proxySelector.getProxyFor(new URL("http://www.intranet.domain.com/ciao").toURI());
134158

135159
assertEquals(Proxy.NO_PROXY, proxy);
160+
Optional<URI> proxyURI = proxySelector.getProxyURIFor(new URL("http://www.intranet.domain.com/ciao").toURI());
161+
assertFalse(proxyURI.isPresent());
136162
}
137163

138164
@Test
@@ -146,6 +172,10 @@ public void testManualProxy() throws Exception {
146172
Proxy proxy = proxySelector.getProxyFor(uri);
147173

148174
assertEquals(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("localhost", 8080)), proxy);
175+
176+
Optional<URI> proxyURI = proxySelector.getProxyURIFor(uri);
177+
assertTrue(proxyURI.isPresent());
178+
assertEquals("http://localhost:8080/", proxyURI.get().toString());
149179
}
150180

151181
@Test
@@ -165,5 +195,9 @@ public void testManualProxyWithLogin() throws Exception {
165195
PasswordAuthentication authentication = Authenticator.requestPasswordAuthentication(null, 8080, uri.toURL().getProtocol(), "ciao", "");
166196
assertEquals(authentication.getUserName(), "username");
167197
assertEquals(String.valueOf(authentication.getPassword()), "pwd");
198+
199+
Optional<URI> proxyURI = proxySelector.getProxyURIFor(uri);
200+
assertTrue(proxyURI.isPresent());
201+
assertEquals("http://username:pwd@localhost:8080/", proxyURI.get().toString());
168202
}
169203
}

arduino-core/src/cc/arduino/net/CustomProxySelector.java

Lines changed: 75 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,33 @@
2929

3030
package cc.arduino.net;
3131

32-
import cc.arduino.Constants;
33-
import org.apache.commons.compress.utils.IOUtils;
34-
35-
import javax.script.*;
3632
import java.io.IOException;
37-
import java.net.*;
33+
import java.net.Authenticator;
34+
import java.net.HttpURLConnection;
35+
import java.net.InetSocketAddress;
36+
import java.net.MalformedURLException;
37+
import java.net.PasswordAuthentication;
38+
import java.net.Proxy;
39+
import java.net.ProxySelector;
40+
import java.net.URI;
41+
import java.net.URISyntaxException;
42+
import java.net.URL;
43+
import java.net.URLConnection;
3844
import java.nio.charset.Charset;
3945
import java.util.Map;
46+
import java.util.Optional;
4047
import java.util.stream.Stream;
4148

49+
import javax.script.Invocable;
50+
import javax.script.ScriptContext;
51+
import javax.script.ScriptEngine;
52+
import javax.script.ScriptEngineManager;
53+
import javax.script.ScriptException;
54+
55+
import org.apache.commons.compress.utils.IOUtils;
56+
57+
import cc.arduino.Constants;
58+
4259
public class CustomProxySelector {
4360

4461
private final Map<String, String> preferences;
@@ -48,6 +65,56 @@ public CustomProxySelector(Map<String, String> preferences) {
4865
clearPreviousAuthenticator();
4966
}
5067

68+
public Optional<URI> getProxyURIFor(URI uri) throws URISyntaxException {
69+
String auth = "";
70+
String user = preferences.getOrDefault(Constants.PREF_PROXY_USERNAME, "");
71+
if (!user.isEmpty()) {
72+
String pass = preferences.getOrDefault(Constants.PREF_PROXY_PASSWORD, "");
73+
auth = user + ":" + pass + "@";
74+
}
75+
String host, port, proto;
76+
77+
switch (preferences.get(Constants.PREF_PROXY_TYPE)) {
78+
default:
79+
return Optional.empty();
80+
81+
case Constants.PROXY_TYPE_NONE:
82+
return Optional.empty();
83+
84+
case Constants.PROXY_TYPE_MANUAL:
85+
host = preferences.get(Constants.PREF_PROXY_MANUAL_HOSTNAME);
86+
port = preferences.get(Constants.PREF_PROXY_MANUAL_PORT);
87+
proto = preferences.get(Constants.PREF_PROXY_MANUAL_TYPE).toLowerCase();
88+
break;
89+
90+
case Constants.PROXY_TYPE_AUTO:
91+
String pac = preferences.getOrDefault(Constants.PREF_PROXY_PAC_URL, "");
92+
if (pac.isEmpty()) {
93+
return Optional.empty();
94+
}
95+
96+
try {
97+
String proxyConfigs = pacProxy(pac, uri);
98+
System.out.println(proxyConfigs);
99+
String proxyConfig = proxyConfigs.split(";")[0];
100+
if (proxyConfig.startsWith("DIRECT")) {
101+
return Optional.empty();
102+
}
103+
proto = proxyConfig.startsWith("PROXY ") ? "http" : "socks";
104+
proxyConfig = proxyConfig.substring(6);
105+
String[] hostPort = proxyConfig.split(":");
106+
host = hostPort[0];
107+
port = hostPort[1];
108+
} catch (Exception e) {
109+
e.printStackTrace();
110+
return Optional.empty();
111+
}
112+
break;
113+
}
114+
115+
return Optional.of(new URI(proto + "://" + auth + host + ":" + port + "/"));
116+
}
117+
51118
public Proxy getProxyFor(URI uri) throws IOException, ScriptException, NoSuchMethodException {
52119
String proxyType = preferences.get(Constants.PREF_PROXY_TYPE);
53120
if (proxyType == null || proxyType.isEmpty()) {
@@ -64,7 +131,7 @@ public Proxy getProxyFor(URI uri) throws IOException, ScriptException, NoSuchMet
64131
return ProxySelector.getDefault().select(uri).get(0);
65132
}
66133

67-
return pacProxy(pac, uri);
134+
return makeProxyFrom(pacProxy(pac, uri));
68135
}
69136

70137
if (Constants.PROXY_TYPE_MANUAL.equals(proxyType)) {
@@ -74,7 +141,7 @@ public Proxy getProxyFor(URI uri) throws IOException, ScriptException, NoSuchMet
74141
throw new IllegalStateException("Unable to understand proxy settings");
75142
}
76143

77-
private Proxy pacProxy(String pac, URI uri) throws IOException, ScriptException, NoSuchMethodException {
144+
private String pacProxy(String pac, URI uri) throws IOException, ScriptException, NoSuchMethodException {
78145
setAuthenticator(preferences.get(Constants.PREF_PROXY_USERNAME), preferences.get(Constants.PREF_PROXY_PASSWORD));
79146

80147
URLConnection urlConnection = new URL(pac).openConnection();
@@ -105,8 +172,7 @@ private Proxy pacProxy(String pac, URI uri) throws IOException, ScriptException,
105172
}
106173
});
107174
nashorn.eval(pacScript);
108-
String proxyConfigs = callFindProxyForURL(uri, nashorn);
109-
return makeProxyFrom(proxyConfigs);
175+
return callFindProxyForURL(uri, nashorn);
110176
}
111177

112178
private String callFindProxyForURL(URI uri, ScriptEngine nashorn) throws ScriptException, NoSuchMethodException {

0 commit comments

Comments
 (0)