Skip to content

Commit 98f4a3c

Browse files
committed
cleaned up SSLExample
1 parent b44340a commit 98f4a3c

File tree

3 files changed

+83
-147
lines changed

3 files changed

+83
-147
lines changed

example/ChatServer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public ChatServer( InetSocketAddress address ) {
2424

2525
@Override
2626
public void onOpen( WebSocket conn, ClientHandshake handshake ) {
27-
this.sendToAll( "new connection" );
27+
this.sendToAll( "new connection: " + handshake.getResourceDescriptor() );
2828
System.out.println( conn + " entered the room!" );
2929
}
3030

example/SSLExample.java

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import java.io.File;
2+
import java.io.FileInputStream;
3+
import java.io.IOException;
4+
import java.net.Socket;
5+
import java.nio.channels.ByteChannel;
6+
import java.nio.channels.SocketChannel;
7+
import java.security.KeyStore;
8+
import java.util.List;
9+
10+
import javax.net.ssl.KeyManagerFactory;
11+
import javax.net.ssl.SSLContext;
12+
import javax.net.ssl.SSLEngine;
13+
import javax.net.ssl.TrustManagerFactory;
14+
15+
import org.java_websocket.SSLSocketChannel;
16+
import org.java_websocket.WebSocket;
17+
import org.java_websocket.WebSocketAdapter;
18+
import org.java_websocket.WebSocketImpl;
19+
import org.java_websocket.drafts.Draft;
20+
import org.java_websocket.server.WebSocketServer;
21+
22+
public class SSLExample {
23+
24+
/*
25+
* Keystore with certificate created like so (in JKS format):
26+
*
27+
*keytool -genkey -validity 3650 -keystore "keystore.jks" -storepass "storepassword" -keypass "keypassword" -alias "default" -dname "CN=127.0.0.1, OU=MyOrgUnit, O=MyOrg, L=MyCity, S=MyRegion, C=MyCountry"
28+
*/
29+
public static void main( String[] args ) throws Exception {
30+
WebSocket.DEBUG = true;
31+
32+
ChatServer chatserver = new ChatServer( 8887 );
33+
34+
// load up the key store
35+
String STORETYPE = "JKS";
36+
String KEYSTORE = "keystore.jks";
37+
String STOREPASSWORD = "storepassword";
38+
String KEYPASSWORD = "keypassword";
39+
40+
KeyStore ks = KeyStore.getInstance( STORETYPE );
41+
File kf = new File( KEYSTORE );
42+
ks.load( new FileInputStream( kf ), STOREPASSWORD.toCharArray() );
43+
44+
KeyManagerFactory kmf = KeyManagerFactory.getInstance( "SunX509" );
45+
kmf.init( ks, KEYPASSWORD.toCharArray() );
46+
TrustManagerFactory tmf = TrustManagerFactory.getInstance( "SunX509" );
47+
tmf.init( ks );
48+
49+
SSLContext sslContext = null;
50+
sslContext = SSLContext.getInstance( "TLS" );
51+
sslContext.init( kmf.getKeyManagers(), tmf.getTrustManagers(), null );
52+
53+
chatserver.setWebSocketFactory( new SSLWebSocketServerFactory( sslContext ) );
54+
55+
chatserver.start();
56+
57+
}
58+
}
59+
60+
class SSLWebSocketServerFactory implements WebSocketServer.WebSocketServerFactory {
61+
private SSLContext sslcontext;
62+
SSLWebSocketServerFactory( SSLContext sslContext ) {
63+
this.sslcontext = sslContext;
64+
}
65+
66+
@Override
67+
public ByteChannel wrapChannel( SocketChannel c ) throws IOException {
68+
SSLEngine e = sslcontext.createSSLEngine();
69+
e.setUseClientMode( false );
70+
return new SSLSocketChannel( c, e );
71+
}
72+
73+
@Override
74+
public WebSocketImpl createWebSocket( WebSocketAdapter a, Draft d, Socket c ) {
75+
return new WebSocketImpl( a, d, c );
76+
}
77+
78+
@Override
79+
public WebSocketImpl createWebSocket( WebSocketAdapter a, List<Draft> d, Socket s ) {
80+
return new WebSocketImpl( a, d, s );
81+
}
82+
}

example/SSLServer.java

Lines changed: 0 additions & 146 deletions
This file was deleted.

0 commit comments

Comments
 (0)