Skip to content

Commit

Permalink
fix google#3 enable to set SSLContext as an option
Browse files Browse the repository at this point in the history
  • Loading branch information
nkzawa committed Jul 7, 2014
1 parent b1e43ba commit fe2fd44
Show file tree
Hide file tree
Showing 13 changed files with 208 additions and 36 deletions.
4 changes: 4 additions & 0 deletions src/main/java/com/github/nkzawa/engineio/client/Socket.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.github.nkzawa.thread.EventThread;
import org.json.JSONException;

import javax.net.ssl.SSLContext;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.*;
Expand Down Expand Up @@ -117,6 +118,7 @@ public void run() {}
/*package*/ Transport transport;
private Future pingTimeoutTimer;
private Future pingIntervalTimer;
private SSLContext sslContext;

private ReadyState readyState;
private ScheduledExecutorService heartbeatScheduler = Executors.newSingleThreadScheduledExecutor();
Expand Down Expand Up @@ -165,6 +167,7 @@ public Socket(Options opts) {
}

this.secure = opts.secure;
this.sslContext = opts.sslContext;
this.hostname = opts.hostname != null ? opts.hostname : "localhost";
this.port = opts.port != 0 ? opts.port : (this.secure ? 443 : 80);
this.query = opts.query != null ?
Expand Down Expand Up @@ -211,6 +214,7 @@ private Transport createTransport(String name) {
}

Transport.Options opts = new Transport.Options();
opts.sslContext = this.sslContext;
opts.hostname = this.hostname;
opts.port = this.port;
opts.secure = this.secure;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.github.nkzawa.engineio.parser.Parser;
import com.github.nkzawa.thread.EventThread;

import javax.net.ssl.SSLContext;
import java.util.Map;

public abstract class Transport extends Emitter {
Expand Down Expand Up @@ -39,6 +40,7 @@ public String toString() {
protected String path;
protected String hostname;
protected String timestampParam;
protected SSLContext sslContext;
protected Socket socket;

protected ReadyState readyState;
Expand All @@ -51,6 +53,7 @@ public Transport(Options opts) {
this.query = opts.query;
this.timestampParam = opts.timestampParam;
this.timestampRequests = opts.timestampRequests;
this.sslContext = opts.sslContext;
this.socket = opts.socket;
}

Expand Down Expand Up @@ -140,6 +143,7 @@ public static class Options {
public int port;
public int policyPort;
public Map<String, String> query;
public SSLContext sslContext;
protected Socket socket;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import com.github.nkzawa.emitter.Emitter;
import com.github.nkzawa.thread.EventThread;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
Expand Down Expand Up @@ -36,6 +38,7 @@ protected Request request(Request.Options opts) {
opts = new Request.Options();
}
opts.uri = this.uri();
opts.sslContext = this.sslContext;

Request req = new Request(opts);

Expand Down Expand Up @@ -141,15 +144,17 @@ public static class Request extends Emitter {

private static final ExecutorService xhrService = Executors.newCachedThreadPool();

String method;
String uri;
byte[] data;
HttpURLConnection xhr;
private String method;
private String uri;
private byte[] data;
private SSLContext sslContext;
private HttpURLConnection xhr;

public Request(Options opts) {
this.method = opts.method != null ? opts.method : "GET";
this.uri = opts.uri;
this.data = opts.data;
this.sslContext = opts.sslContext;
}

public void create() {
Expand All @@ -164,6 +169,10 @@ public void create() {
return;
}

if (xhr instanceof HttpsURLConnection && this.sslContext != null) {
((HttpsURLConnection)xhr).setSSLSocketFactory(this.sslContext.getSocketFactory());
}

Map<String, String> headers = new TreeMap<String, String>(String.CASE_INSENSITIVE_ORDER);

if ("POST".equals(this.method)) {
Expand Down Expand Up @@ -293,6 +302,7 @@ public static class Options {
public String uri;
public String method;
public byte[] data;
public SSLContext sslContext;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.github.nkzawa.engineio.parser.Parser;
import com.github.nkzawa.parseqs.ParseQS;
import com.github.nkzawa.thread.EventThread;
import org.java_websocket.client.DefaultSSLWebSocketClientFactory;
import org.java_websocket.client.WebSocketClient;
import org.java_websocket.drafts.Draft_17;
import org.java_websocket.handshake.ServerHandshake;
Expand Down Expand Up @@ -93,6 +94,9 @@ public void run() {
});
}
};
if (this.sslContext != null) {
this.ws.setWebSocketFactory(new DefaultSSLWebSocketClientFactory(this.sslContext));
}
this.ws.connect();
} catch (URISyntaxException e) {
throw new RuntimeException(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public void call(Object... args) {
}

@Test(timeout = TIMEOUT)
public void receiveBinaryDataAndMultiplebyteUTF8String() throws InterruptedException {
public void receiveBinaryDataAndMultibyteUTF8String() throws InterruptedException {
final Semaphore semaphore = new Semaphore(0);
final byte[] binaryData = new byte[5];
for (int i = 0; i < binaryData.length; i++) {
Expand Down
12 changes: 11 additions & 1 deletion src/test/java/com/github/nkzawa/engineio/client/Connection.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public void startServer() throws IOException, InterruptedException {

final CountDownLatch latch = new CountDownLatch(1);
serverProcess = Runtime.getRuntime().exec(
"node src/test/resources/index.js " + PORT, new String[] {"DEBUG=engine*"});
"node src/test/resources/server.js", createEnv());
serverService = Executors.newCachedThreadPool();
serverOutout = serverService.submit(new Runnable() {
@Override
Expand Down Expand Up @@ -70,4 +70,14 @@ public void stopServer() throws InterruptedException {
serverService.shutdown();
serverService.awaitTermination(3000, TimeUnit.MILLISECONDS);
}

Socket.Options createOptions() {
Socket.Options opts = new Socket.Options();
opts.port = PORT;
return opts;
}

String[] createEnv() {
return new String[] {"DEBUG=engine*", "PORT=" + PORT};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ public class ConnectionTest extends Connection {
public void connectToLocalhost() throws InterruptedException {
final Semaphore semaphore = new Semaphore(0);

Socket.Options opts = new Socket.Options();
opts.port = PORT;
socket = new Socket(opts);
socket = new Socket(createOptions());
socket.on(Socket.EVENT_OPEN, new Emitter.Listener() {
@Override
public void call(Object... args) {
Expand All @@ -45,9 +43,7 @@ public void call(Object... args) {
public void receiveMultibyteUTF8StringsWithPolling() throws InterruptedException {
final Semaphore semaphore = new Semaphore(0);

Socket.Options opts = new Socket.Options();
opts.port = PORT;
socket = new Socket(opts);
socket = new Socket(createOptions());
socket.on(Socket.EVENT_OPEN, new Emitter.Listener() {
@Override
public void call(Object... args) {
Expand All @@ -71,9 +67,7 @@ public void call(Object... args) {
public void receiveEmoji() throws InterruptedException {
final Semaphore semaphore = new Semaphore(0);

Socket.Options opts = new Socket.Options();
opts.port = PORT;
socket = new Socket(opts);
socket = new Socket(createOptions());
socket.on(Socket.EVENT_OPEN, new Emitter.Listener() {
@Override
public void call(Object... args) {
Expand All @@ -97,9 +91,7 @@ public void call(Object... args) {
public void notSendPacketsIfSocketCloses() throws InterruptedException {
final Semaphore semaphore = new Semaphore(0);

Socket.Options opts = new Socket.Options();
opts.port = PORT;
socket = new Socket(opts);
socket = new Socket(createOptions());
socket.on(Socket.EVENT_OPEN, new Emitter.Listener() {
@Override
public void call(Object... args) {
Expand Down
122 changes: 122 additions & 0 deletions src/test/java/com/github/nkzawa/engineio/client/SSLConnectionTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package com.github.nkzawa.engineio.client;

import com.github.nkzawa.emitter.Emitter;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManagerFactory;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.security.KeyStore;
import java.util.concurrent.CountDownLatch;

import static org.hamcrest.CoreMatchers.is;
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(){
public boolean verify(String hostname, javax.net.ssl.SSLSession sslSession) {
return hostname.equals("localhost");
}
});
}

private Socket socket;

@Override
Socket.Options createOptions() {
Socket.Options opts = super.createOptions();
opts.secure = true;
return opts;
}

@Override
String[] createEnv() {
return new String[] {"DEBUG=engine*", "PORT=" + PORT, "SSL=1"};
}

SSLContext createSSLContext() throws GeneralSecurityException, IOException {
KeyStore ks = KeyStore.getInstance("JKS");
File file = new File("src/test/resources/keystore.jks");
ks.load(new FileInputStream(file), "password".toCharArray());

KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
kmf.init(ks, "password".toCharArray());

TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
tmf.init(ks);

SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
return sslContext;
}

@Test(timeout = TIMEOUT)
public void connect() throws Exception {
final CountDownLatch latch = new CountDownLatch(1);

Socket.Options opts = createOptions();
opts.sslContext = createSSLContext();
socket = new Socket(opts);
socket.on(Socket.EVENT_OPEN, new Emitter.Listener() {
@Override
public void call(Object... args) {
socket.on(Socket.EVENT_MESSAGE, new Emitter.Listener() {
@Override
public void call(Object... args) {
assertThat((String)args[0], is("hi"));
socket.close();
latch.countDown();
}
});
}
}).on("error", new Emitter.Listener() {
@Override
public void call(Object... args) {
((Exception)args[0]).printStackTrace();
}
});
socket.open();
latch.await();
}

@Test(timeout = TIMEOUT)
public void upgrade() throws Exception {
final CountDownLatch latch = new CountDownLatch(1);

Socket.Options opts = createOptions();
opts.sslContext = createSSLContext();
socket = new Socket(opts);
socket.on(Socket.EVENT_OPEN, new Emitter.Listener() {
@Override
public void call(Object... args) {
socket.on(Socket.EVENT_UPGRADE, new Emitter.Listener() {
@Override
public void call(Object... args) {
socket.send("hi");
socket.on(Socket.EVENT_MESSAGE, new Emitter.Listener() {
@Override
public void call(Object... args) {
assertThat((String) args[0], is("hi"));
socket.close();
latch.countDown();
}
});
}
});
}
});
socket.open();
latch.await();
}
}
Loading

0 comments on commit fe2fd44

Please sign in to comment.