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