Skip to content

Commit 042f6f5

Browse files
committed
Use SSLContext.getDefault() more now that it's guaranteed fixed
Without this, some of the existing advice logic needs to be able to reach HttpProxyAgent, which fails in some environments. This avoids that entirely, making runtime logic everywhere simpler & more reliable. Also starts removing unnecessary extra try/catches, as it appears that bytebuddy handles this for us by default anyway.
1 parent 26d45c9 commit 042f6f5

File tree

4 files changed

+21
-22
lines changed

4 files changed

+21
-22
lines changed
Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
package tech.httptoolkit.javaagent.advice;
22

33
import net.bytebuddy.asm.Advice;
4-
import tech.httptoolkit.javaagent.HttpProxyAgent;
5-
64
import javax.net.ssl.SSLContext;
5+
import java.security.NoSuchAlgorithmException;
76

87
public class OverrideSslContextFieldAdvice {
98

109
@Advice.OnMethodEnter
1110
public static void beforeMethod(
1211
@Advice.FieldValue(value = "sslContext", readOnly = false) SSLContext sslContextField
13-
) {
14-
sslContextField = HttpProxyAgent.getInterceptedSslContext();
12+
) throws NoSuchAlgorithmException {
13+
sslContextField = SSLContext.getDefault();
1514
}
1615

1716
}

src/main/java/tech/httptoolkit/javaagent/advice/apacheclient/ApacheSetSslSocketFactoryAdvice.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
package tech.httptoolkit.javaagent.advice.apacheclient;
22

33
import net.bytebuddy.asm.Advice;
4-
import tech.httptoolkit.javaagent.HttpProxyAgent;
54

5+
import javax.net.ssl.SSLContext;
66
import java.lang.reflect.Field;
7+
import java.security.NoSuchAlgorithmException;
78
import java.util.Arrays;
89

910
public class ApacheSetSslSocketFactoryAdvice {
1011

1112
@Advice.OnMethodEnter
12-
public static void beforeCreateSocket(@Advice.This Object thisFactory) {
13+
public static void beforeCreateSocket(@Advice.This Object thisFactory) throws Exception {
1314
// Before creating the socket - replace the SSL context so the new socket trusts us.
1415

1516
boolean intercepted = false;
@@ -22,12 +23,8 @@ public static void beforeCreateSocket(@Advice.This Object thisFactory) {
2223
field.setAccessible(true);
2324

2425
// Overwrite the socket factory with our own:
25-
try {
26-
field.set(thisFactory, HttpProxyAgent.getInterceptedSslContext().getSocketFactory());
27-
intercepted = true;
28-
} catch (IllegalAccessException e) {
29-
throw new RuntimeException("Could not intercept Apache HttpClient HTTPS sockets");
30-
}
26+
field.set(thisFactory, SSLContext.getDefault().getSocketFactory());
27+
intercepted = true;
3128
} catch (NoSuchFieldException ignored) { }
3229
}
3330

src/main/java/tech/httptoolkit/javaagent/advice/jettyclient/JettyReturnSslContextFactoryV10Advice.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,17 @@
22

33
import net.bytebuddy.asm.Advice;
44
import org.eclipse.jetty.util.ssl.SslContextFactory;
5-
import tech.httptoolkit.javaagent.HttpProxyAgent;
5+
6+
import javax.net.ssl.SSLContext;
67

78
public class JettyReturnSslContextFactoryV10Advice {
89
@Advice.OnMethodExit
9-
public static void getSslContextFactory(@Advice.Return(readOnly = false) SslContextFactory.Client returnValue) {
10+
public static void getSslContextFactory(
11+
@Advice.Return(readOnly = false) SslContextFactory.Client returnValue
12+
) throws Exception {
1013
SslContextFactory.Client sslFactory = new SslContextFactory.Client();
11-
sslFactory.setSslContext(HttpProxyAgent.getInterceptedSslContext());
12-
try {
13-
sslFactory.start();
14-
} catch (Exception e) {
15-
throw new RuntimeException(e);
16-
}
14+
sslFactory.setSslContext(SSLContext.getDefault());
15+
sslFactory.start();
1716

1817
returnValue = sslFactory;
1918
}

src/main/java/tech/httptoolkit/javaagent/advice/jettyclient/JettyReturnSslContextFactoryV9Advice.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,16 @@
44
import org.eclipse.jetty.util.ssl.SslContextFactory;
55
import tech.httptoolkit.javaagent.HttpProxyAgent;
66

7+
import javax.net.ssl.SSLContext;
8+
79
public class JettyReturnSslContextFactoryV9Advice {
810

911
@Advice.OnMethodExit
10-
public static void getSslContextFactory(@Advice.Return(readOnly = false) SslContextFactory returnValue) {
12+
public static void getSslContextFactory(
13+
@Advice.Return(readOnly = false) SslContextFactory returnValue
14+
) throws Exception {
1115
SslContextFactory sslFactory = new JettyV9StubContextFactory();
12-
sslFactory.setSslContext(HttpProxyAgent.getInterceptedSslContext());
16+
sslFactory.setSslContext(SSLContext.getDefault());
1317
try {
1418
sslFactory.start();
1519
} catch (Exception e) {

0 commit comments

Comments
 (0)