Set HostnameVerifier as an Options
This commit is contained in:
@@ -10,6 +10,7 @@ import com.github.nkzawa.parseqs.ParseQS;
|
||||
import com.github.nkzawa.thread.EventThread;
|
||||
import org.json.JSONException;
|
||||
|
||||
import javax.net.ssl.HostnameVerifier;
|
||||
import javax.net.ssl.SSLContext;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
@@ -121,6 +122,7 @@ public class Socket extends Emitter {
|
||||
private Future pingTimeoutTimer;
|
||||
private Future pingIntervalTimer;
|
||||
private SSLContext sslContext;
|
||||
private HostnameVerifier hostnameVerifier;
|
||||
|
||||
private ReadyState readyState;
|
||||
private ScheduledExecutorService heartbeatScheduler;
|
||||
@@ -197,6 +199,7 @@ public class Socket extends Emitter {
|
||||
opts.transports : new String[]{Polling.NAME, WebSocket.NAME}));
|
||||
this.policyPort = opts.policyPort != 0 ? opts.policyPort : 843;
|
||||
this.rememberUpgrade = opts.rememberUpgrade;
|
||||
this.hostnameVerifier = opts.hostnameVerifier;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -254,6 +257,7 @@ public class Socket extends Emitter {
|
||||
opts.timestampParam = this.timestampParam;
|
||||
opts.policyPort = this.policyPort;
|
||||
opts.socket = this;
|
||||
opts.hostnameVerifier = this.hostnameVerifier;
|
||||
|
||||
Transport transport;
|
||||
if (WebSocket.NAME.equals(name)) {
|
||||
|
||||
@@ -6,6 +6,7 @@ import com.github.nkzawa.engineio.parser.Packet;
|
||||
import com.github.nkzawa.engineio.parser.Parser;
|
||||
import com.github.nkzawa.thread.EventThread;
|
||||
|
||||
import javax.net.ssl.HostnameVerifier;
|
||||
import javax.net.ssl.SSLContext;
|
||||
import java.util.Map;
|
||||
|
||||
@@ -42,6 +43,7 @@ public abstract class Transport extends Emitter {
|
||||
protected String timestampParam;
|
||||
protected SSLContext sslContext;
|
||||
protected Socket socket;
|
||||
protected HostnameVerifier hostnameVerifier;
|
||||
|
||||
protected ReadyState readyState;
|
||||
|
||||
@@ -55,6 +57,7 @@ public abstract class Transport extends Emitter {
|
||||
this.timestampRequests = opts.timestampRequests;
|
||||
this.sslContext = opts.sslContext;
|
||||
this.socket = opts.socket;
|
||||
this.hostnameVerifier = opts.hostnameVerifier;
|
||||
}
|
||||
|
||||
protected Transport onError(String msg, Exception desc) {
|
||||
@@ -144,6 +147,7 @@ public abstract class Transport extends Emitter {
|
||||
public int policyPort = -1;
|
||||
public Map<String, String> query;
|
||||
public SSLContext sslContext;
|
||||
public HostnameVerifier hostnameVerifier;
|
||||
protected Socket socket;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -53,6 +53,9 @@ public class WebSocket extends Transport {
|
||||
SSLSocketFactory factory = sslContext.getSocketFactory();// (SSLSocketFactory) SSLSocketFactory.getDefault();
|
||||
client.setSslSocketFactory(factory);
|
||||
}
|
||||
if (this.hostnameVerifier != null) {
|
||||
client.setHostnameVerifier(this.hostnameVerifier);
|
||||
}
|
||||
Request.Builder builder = new Request.Builder().url(uri());
|
||||
for (Map.Entry<String, String> entry : headers.entrySet()) {
|
||||
builder.addHeader(entry.getKey(), entry.getValue());
|
||||
|
||||
@@ -6,6 +6,7 @@ import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.JUnit4;
|
||||
|
||||
import javax.net.ssl.HostnameVerifier;
|
||||
import javax.net.ssl.KeyManagerFactory;
|
||||
import javax.net.ssl.SSLContext;
|
||||
import javax.net.ssl.TrustManagerFactory;
|
||||
@@ -23,15 +24,11 @@ import static org.junit.Assert.assertThat;
|
||||
@RunWith(JUnit4.class)
|
||||
public class SSLConnectionTest extends Connection {
|
||||
|
||||
static {
|
||||
// for test on localhost
|
||||
javax.net.ssl.HttpsURLConnection.setDefaultHostnameVerifier(
|
||||
new javax.net.ssl.HostnameVerifier(){
|
||||
static HostnameVerifier hostnameVerifier = new javax.net.ssl.HostnameVerifier(){
|
||||
public boolean verify(String hostname, javax.net.ssl.SSLSession sslSession) {
|
||||
return hostname.equals("localhost");
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
private Socket socket;
|
||||
|
||||
@@ -74,6 +71,7 @@ public class SSLConnectionTest extends Connection {
|
||||
|
||||
Socket.Options opts = createOptions();
|
||||
opts.sslContext = createSSLContext();
|
||||
opts.hostnameVerifier = SSLConnectionTest.hostnameVerifier;
|
||||
socket = new Socket(opts);
|
||||
socket.on(Socket.EVENT_OPEN, new Emitter.Listener() {
|
||||
@Override
|
||||
@@ -98,6 +96,7 @@ public class SSLConnectionTest extends Connection {
|
||||
|
||||
Socket.Options opts = createOptions();
|
||||
opts.sslContext = createSSLContext();
|
||||
opts.hostnameVerifier = SSLConnectionTest.hostnameVerifier;
|
||||
socket = new Socket(opts);
|
||||
socket.on(Socket.EVENT_OPEN, new Emitter.Listener() {
|
||||
@Override
|
||||
@@ -127,7 +126,9 @@ public class SSLConnectionTest extends Connection {
|
||||
final BlockingQueue<Object> values = new LinkedBlockingQueue<Object>();
|
||||
|
||||
Socket.setDefaultSSLContext(createSSLContext());
|
||||
socket = new Socket(createOptions());
|
||||
Socket.Options opts = createOptions();
|
||||
opts.hostnameVerifier = SSLConnectionTest.hostnameVerifier;
|
||||
socket = new Socket(opts);
|
||||
socket.on(Socket.EVENT_OPEN, new Emitter.Listener() {
|
||||
@Override
|
||||
public void call(Object... args) {
|
||||
|
||||
Reference in New Issue
Block a user