Skip to content

Commit a76e052

Browse files
authored
Merge pull request #1070 from marci4/Issue1058
2 parents 2a91fe2 + e1061ae commit a76e052

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

src/main/java/org/java_websocket/client/WebSocketClient.java

+18-7
Original file line numberDiff line numberDiff line change
@@ -461,12 +461,16 @@ public void sendPing() {
461461
public void run() {
462462
InputStream istream;
463463
try {
464-
boolean isNewSocket = false;
465-
if (socketFactory != null) {
464+
boolean upgradeSocketToSSLSocket = false;
465+
// Prioritise a proxy over a socket factory and apply the socketfactory later
466+
if (proxy != Proxy.NO_PROXY) {
467+
socket = new Socket(proxy);
468+
upgradeSocketToSSLSocket = true;
469+
} else if (socketFactory != null) {
466470
socket = socketFactory.createSocket();
467471
} else if (socket == null) {
468472
socket = new Socket(proxy);
469-
isNewSocket = true;
473+
upgradeSocketToSSLSocket = true;
470474
} else if (socket.isClosed()) {
471475
throw new IOException();
472476
}
@@ -480,10 +484,17 @@ public void run() {
480484
}
481485

482486
// if the socket is set by others we don't apply any TLS wrapper
483-
if (isNewSocket && "wss".equals(uri.getScheme())) {
484-
SSLContext sslContext = SSLContext.getInstance("TLSv1.2");
485-
sslContext.init(null, null, null);
486-
SSLSocketFactory factory = sslContext.getSocketFactory();
487+
if (upgradeSocketToSSLSocket && "wss".equals(uri.getScheme())) {
488+
SSLSocketFactory factory;
489+
// Prioritise the provided socketfactory
490+
// Helps when using web debuggers like Fiddler Classic
491+
if (socketFactory != null && (socketFactory instanceof SSLSocketFactory)) {
492+
factory = (SSLSocketFactory) socketFactory;
493+
} else {
494+
SSLContext sslContext = SSLContext.getInstance("TLSv1.2");
495+
sslContext.init(null, null, null);
496+
factory = sslContext.getSocketFactory();
497+
}
487498
socket = factory.createSocket(socket, uri.getHost(), getPort(), true);
488499
}
489500

0 commit comments

Comments
 (0)