Skip to content

Commit c0b9dc8

Browse files
committed
Make sure we can't get into a proxy selector loop
I haven't actually seen this happen (real requests seem to be sent with socket://... URLs for the proxy step) but it seems quite possible, so it's better to protect against it.
1 parent 042f6f5 commit c0b9dc8

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

src/main/java/tech/httptoolkit/javaagent/advice/OverrideAllProxySelectionAdvice.java

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,21 @@ public static void selectProxy(
1717
) {
1818
String scheme = uri.getScheme();
1919

20-
// For HTTP URIs only, we override all proxy selection globally to select our proxy instead:
21-
if (scheme.equals("http") || scheme.equals("https")) {
20+
boolean isHttp = scheme.equals("http") || scheme.equals("https");
21+
22+
// We read from our custom variables, since we can't access HttpProxyAgent from a bootstrapped
23+
// class, and we use namespaced properties to make this extra reliable:
24+
String proxyHost = System.getProperty("tech.httptoolkit.proxyHost");
25+
int proxyPort = Integer.parseInt(System.getProperty("tech.httptoolkit.proxyPort"));
26+
27+
boolean isRequestToProxy = uri.getHost().equals(proxyHost) && uri.getPort() == proxyPort;
28+
29+
// For HTTP URIs going elsewhere, we override all proxy selection globally to go via our proxy:
30+
if (isHttp && !isRequestToProxy) {
2231
returnedProxies = Collections.singletonList(
23-
new Proxy(Proxy.Type.HTTP, new InetSocketAddress(
24-
// We read from our custom variables, since we can't access HttpProxyAgent from a bootstrapped
25-
// class, and we use namespaced properties to make this extra reliable:
26-
System.getProperty("tech.httptoolkit.proxyHost"),
27-
Integer.parseInt(System.getProperty("tech.httptoolkit.proxyPort"))
28-
))
32+
new Proxy(Proxy.Type.HTTP,
33+
new InetSocketAddress(proxyHost, proxyPort)
34+
)
2935
);
3036
}
3137
}

0 commit comments

Comments
 (0)