|
1 | 1 | package tech.httptoolkit.javaagent.advice.apacheclient; |
2 | 2 |
|
3 | 3 | import net.bytebuddy.asm.Advice; |
| 4 | +import net.bytebuddy.implementation.bytecode.assign.Assigner; |
4 | 5 | import org.apache.http.HttpHost; |
5 | 6 | import org.apache.http.conn.routing.HttpRoute; |
6 | | -import tech.httptoolkit.javaagent.HttpProxyAgent; |
| 7 | + |
| 8 | +import java.net.*; |
7 | 9 |
|
8 | 10 | public class ApacheV4ReturnProxyRouteAdvice { |
9 | 11 | @Advice.OnMethodExit |
10 | 12 | public static void determineRoute( |
11 | | - @Advice.Return(readOnly = false) HttpRoute returnValue |
| 13 | + // We type this dynamically, because in some cases (notably Gradle) we seemingly can't reach the |
| 14 | + // HttpRoute type from ByteBuddy, only at runtime. |
| 15 | + @Advice.Return(readOnly = false, typing = Assigner.Typing.DYNAMIC) Object returnValue |
12 | 16 | ) { |
| 17 | + HttpRoute existingValue = (HttpRoute) returnValue; |
| 18 | + // We guarantee that the default proxy selector is always our own. This ensures that we can |
| 19 | + // always grab the proxy URL without needing to access our injected classes. |
| 20 | + Proxy proxy = ProxySelector.getDefault().select(URI.create("https://example.com")).get(0); |
| 21 | + InetSocketAddress proxyAddress = (InetSocketAddress) proxy.address(); |
| 22 | + |
13 | 23 | returnValue = new HttpRoute( |
14 | | - returnValue.getTargetHost(), |
15 | | - returnValue.getLocalAddress(), |
16 | | - new HttpHost( |
17 | | - HttpProxyAgent.getAgentProxyHost(), |
18 | | - HttpProxyAgent.getAgentProxyPort() |
19 | | - ), |
20 | | - returnValue.isSecure() |
| 24 | + existingValue.getTargetHost(), |
| 25 | + existingValue.getLocalAddress(), |
| 26 | + new HttpHost(proxyAddress.getHostString(), proxyAddress.getPort()), |
| 27 | + existingValue.isSecure() |
21 | 28 | ); |
22 | 29 | } |
23 | 30 | } |
0 commit comments