|
| 1 | +// @formatter:off |
| 2 | + |
| 3 | +import java.io.File; |
| 4 | +import java.io.FileInputStream; |
| 5 | +import java.net.InetAddress; |
| 6 | +import java.net.InetSocketAddress; |
| 7 | +import java.net.Socket; |
| 8 | +import java.nio.channels.SocketChannel; |
| 9 | +import java.security.KeyStore; |
| 10 | +import java.util.List; |
| 11 | + |
| 12 | +import javax.net.ssl.KeyManagerFactory; |
| 13 | +import javax.net.ssl.SSLContext; |
| 14 | +import javax.net.ssl.SSLEngine; |
| 15 | +import javax.net.ssl.TrustManagerFactory; |
| 16 | + |
| 17 | +import org.java_websocket.SSLSocketChannel; |
| 18 | +import org.java_websocket.WebSocket; |
| 19 | +import org.java_websocket.WebSocketAdapter; |
| 20 | +import org.java_websocket.WebSocketImpl; |
| 21 | +import org.java_websocket.drafts.Draft; |
| 22 | +import org.java_websocket.handshake.ClientHandshake; |
| 23 | +import org.java_websocket.server.WebSocketServer; |
| 24 | + |
| 25 | +/* |
| 26 | + * Create the appropriate websocket server. |
| 27 | + */ |
| 28 | +public class SSLServer implements WebSocketServer.WebSocketServerFactory |
| 29 | +{ |
| 30 | + private static final String STORETYPE = "JKS"; |
| 31 | + private static final String KEYSTORE = "keystore.jks"; |
| 32 | + private static final String STOREPASSWORD = "storepassword"; |
| 33 | + private static final String KEYPASSWORD = "keypassword"; |
| 34 | + |
| 35 | + public static void main(String[] args) throws Exception |
| 36 | + { |
| 37 | + new SSLServer(); |
| 38 | + } |
| 39 | + |
| 40 | + private SSLContext sslContext; |
| 41 | + |
| 42 | + void loadFromFile() throws Exception |
| 43 | + { |
| 44 | + // load up the key store |
| 45 | + KeyStore ks = KeyStore.getInstance(STORETYPE); |
| 46 | + File kf = new File(KEYSTORE); |
| 47 | + ks.load(new FileInputStream(kf), STOREPASSWORD.toCharArray()); |
| 48 | + |
| 49 | + KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509"); |
| 50 | + kmf.init(ks, KEYPASSWORD.toCharArray()); |
| 51 | + TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509"); |
| 52 | + tmf.init(ks); |
| 53 | + |
| 54 | + sslContext = SSLContext.getInstance("TLS"); |
| 55 | + sslContext.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null); |
| 56 | + } |
| 57 | + |
| 58 | + /* |
| 59 | + * Keystore with certificate created like so (in JKS format): |
| 60 | + * |
| 61 | + 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" |
| 62 | + */ |
| 63 | + SSLServer() throws Exception |
| 64 | + { |
| 65 | + sslContext = null; |
| 66 | + loadFromFile(); |
| 67 | + |
| 68 | + // create the web socket server |
| 69 | + WebSocketSource wsgateway = new WebSocketSource(8001, InetAddress.getByName("127.0.0.1")); |
| 70 | + wsgateway.setWebSocketFactory(this); |
| 71 | + wsgateway.start(); |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + public WebSocketImpl createWebSocket( WebSocketAdapter a, Draft d, Socket c ) { |
| 76 | + |
| 77 | + return new WebSocketImpl( a, d, c ); |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public WebSocketImpl createWebSocket( WebSocketAdapter a, List<Draft> d, Socket s ) { |
| 82 | + if(sslContext != null) try{ |
| 83 | + SSLEngine e = sslContext.createSSLEngine(); |
| 84 | + e.setUseClientMode(false); |
| 85 | + return new WebSocketImpl( a, d, s ); |
| 86 | + } catch ( Exception e1 ) { |
| 87 | + } |
| 88 | + return new WebSocketImpl( a, d, s ); |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + public SocketChannel wrapChannel( SocketChannel c ) { |
| 93 | + if( sslContext != null ) |
| 94 | + try { |
| 95 | + SSLEngine e = sslContext.createSSLEngine(); |
| 96 | + e.setUseClientMode( false ); |
| 97 | + new SSLSocketChannel( c, e ); |
| 98 | + } catch ( Exception e1 ) { |
| 99 | + } |
| 100 | + |
| 101 | + return c; |
| 102 | + } |
| 103 | + |
| 104 | + class WebSocketSource extends WebSocketServer |
| 105 | + { |
| 106 | + private WebSocket handle; |
| 107 | + WebSocketSource(int port, InetAddress addr) |
| 108 | + { |
| 109 | + super(new InetSocketAddress(addr, port)); |
| 110 | + handle = null; |
| 111 | + } |
| 112 | + |
| 113 | + @Override |
| 114 | + public void onClose(WebSocket arg0, int arg1, String arg2, boolean arg3) |
| 115 | + { |
| 116 | + System.err.println("---------------------------->Closed"); |
| 117 | + if(arg0 == handle) handle = null; |
| 118 | + } |
| 119 | + |
| 120 | + @Override |
| 121 | + public void onError(WebSocket arg0, Exception arg1) { |
| 122 | + // TODO Auto-generated method stub |
| 123 | + } |
| 124 | + |
| 125 | + @Override |
| 126 | + public void onMessage(WebSocket arg0, String arg1) |
| 127 | + { |
| 128 | + if(arg0 != handle){ |
| 129 | + arg0.close(org.java_websocket.framing.CloseFrame.NORMAL); |
| 130 | + return; |
| 131 | + } |
| 132 | + |
| 133 | + System.out.println("--------->["+arg1+"]"); |
| 134 | + } |
| 135 | + |
| 136 | + @Override |
| 137 | + public void onOpen(WebSocket arg0, ClientHandshake arg1) |
| 138 | + { |
| 139 | + // nothing to see just yet |
| 140 | + if(handle == null){ |
| 141 | + handle = arg0; |
| 142 | + }else if(handle != arg0){ |
| 143 | + arg0.close(org.java_websocket.framing.CloseFrame.NORMAL); |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + void done() |
| 148 | + { |
| 149 | + if(handle != null) handle.close(org.java_websocket.framing.CloseFrame.NORMAL); |
| 150 | + } |
| 151 | + } |
| 152 | +} |
| 153 | + |
| 154 | +//@formatter:on |
0 commit comments