diff --git a/README.md b/README.md index a53242a..b873f3a 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,10 @@ Dont worry about privacy: all data is stored in your browser offline and your PC 1. Plugin download 2. Filling config.yml. Port and password configuration +3. SSL config +If generated with +keytool -genkey -keyalg RSA -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" +then need to https://localhost:8080/ ## How it works @@ -31,7 +35,7 @@ Server communicate with all connected clients using JSON. The following table sh | Variable |Meaning | |---------------------|-----------------------------------------------------------------------------| -| status |Status code (as integer), representing response type. See table below | +| status |Status code (as integer), representing response type. See listing below* | | statusDescription |Status description (as String) describing status code | | respondsTo |`(Optional)` Original command sent by client which triggered this response| | message |Response content | diff --git a/src/com/mesacarlos/webconsole/WebConsole.java b/src/com/mesacarlos/webconsole/WebConsole.java index 95a7d98..855706e 100644 --- a/src/com/mesacarlos/webconsole/WebConsole.java +++ b/src/com/mesacarlos/webconsole/WebConsole.java @@ -1,32 +1,50 @@ package com.mesacarlos.webconsole; +import java.io.File; +import java.io.FileInputStream; import java.io.IOException; import java.net.InetSocketAddress; +import java.security.KeyManagementException; +import java.security.KeyStore; +import java.security.KeyStoreException; +import java.security.UnrecoverableKeyException; + +import javax.net.ssl.KeyManagerFactory; +import javax.net.ssl.SSLContext; +import javax.net.ssl.TrustManagerFactory; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.core.Filter; +import org.bukkit.Bukkit; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.plugin.java.JavaPlugin; +import org.java_websocket.server.DefaultSSLWebSocketServerFactory; import com.mesacarlos.webconsole.util.LogFilter; import com.mesacarlos.webconsole.websockets.WSServer; public class WebConsole extends JavaPlugin { FileConfiguration config = this.getConfig(); - + // Websocket server and thread private WSServer server; private Thread wsThread; - + @Override public void onEnable() { createConfig(); - startWS(); + try { + startWS(); + } catch (Exception e) { + Bukkit.getLogger().warning("Error occured while starting WebSockets Server."); + e.printStackTrace(); + } + Filter f = new LogFilter(getWSServer()); ((org.apache.logging.log4j.core.Logger) LogManager.getRootLogger()).addFilter(f); } - + @Override public void onDisable() { try { @@ -36,24 +54,59 @@ public class WebConsole extends JavaPlugin { e.printStackTrace(); } } - + /** * Creates configuration file */ private void createConfig() { + // SSL variables + config.addDefault("useSSL", false); + config.addDefault("StoreType", "JKS"); + config.addDefault("KeyStore", "plugins/WebConsole/keystore.jks"); + config.addDefault("StorePassword", "storepassword"); + config.addDefault("KeyPassword", "keypassword"); + + // Connection config variables config.addDefault("host", "localhost"); config.addDefault("port", 8080); config.addDefault("password", 1234); + config.options().copyDefaults(true); saveConfig(); } - + /** - * Start WebSockets server + * Start WebSocket server */ - private void startWS() { - //Start WebSockets server + private void startWS() throws Exception, + KeyStoreException, UnrecoverableKeyException, KeyManagementException { + // Create WebSocket server server = new WSServer(this, new InetSocketAddress(config.getString("host"), config.getInt("port"))); + + if(config.getBoolean("useSSL")) { + // Configure SSL + String STORETYPE = config.getString("StoreType"); + String KEYSTORE = config.getString("KeyStore"); + String STOREPASSWORD = config.getString("StorePassword"); + String KEYPASSWORD = config.getString("KeyPassword"); + + KeyStore ks = KeyStore.getInstance(STORETYPE); + File kf = new File(KEYSTORE); + ks.load(new FileInputStream(kf), STOREPASSWORD.toCharArray()); + + KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509"); + kmf.init(ks, KEYPASSWORD.toCharArray()); + TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509"); + tmf.init(ks); + + SSLContext sslContext = null; + sslContext = SSLContext.getInstance("TLS"); + sslContext.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null); + + server.setWebSocketFactory(new DefaultSSLWebSocketServerFactory(sslContext)); + } + + // Start Server wsThread = new Thread(new Runnable() { @Override public void run() { @@ -62,8 +115,8 @@ public class WebConsole extends JavaPlugin { }); wsThread.start(); } - + public WSServer getWSServer() { - return (WSServer)server; + return (WSServer) server; } } \ No newline at end of file