SecureWebSockets available from now on

This commit is contained in:
Carlos 2019-08-11 16:26:41 +02:00
parent b58d97fc90
commit 5d1a193a39
2 changed files with 69 additions and 12 deletions

View File

@ -10,6 +10,10 @@ Dont worry about privacy: all data is stored in your browser offline and your PC
1. Plugin download 1. Plugin download
2. Filling config.yml. Port and password configuration 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 ## How it works
@ -31,7 +35,7 @@ Server communicate with all connected clients using JSON. The following table sh
| Variable |Meaning | | 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 | | statusDescription |Status description (as String) describing status code |
| respondsTo |`(Optional)` Original command sent by client which triggered this response| | respondsTo |`(Optional)` Original command sent by client which triggered this response|
| message |Response content | | message |Response content |

View File

@ -1,12 +1,24 @@
package com.mesacarlos.webconsole; package com.mesacarlos.webconsole;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.net.InetSocketAddress; 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.LogManager;
import org.apache.logging.log4j.core.Filter; import org.apache.logging.log4j.core.Filter;
import org.bukkit.Bukkit;
import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import org.java_websocket.server.DefaultSSLWebSocketServerFactory;
import com.mesacarlos.webconsole.util.LogFilter; import com.mesacarlos.webconsole.util.LogFilter;
import com.mesacarlos.webconsole.websockets.WSServer; import com.mesacarlos.webconsole.websockets.WSServer;
@ -21,7 +33,13 @@ public class WebConsole extends JavaPlugin {
@Override @Override
public void onEnable() { public void onEnable() {
createConfig(); createConfig();
try {
startWS(); startWS();
} catch (Exception e) {
Bukkit.getLogger().warning("Error occured while starting WebSockets Server.");
e.printStackTrace();
}
Filter f = new LogFilter(getWSServer()); Filter f = new LogFilter(getWSServer());
((org.apache.logging.log4j.core.Logger) LogManager.getRootLogger()).addFilter(f); ((org.apache.logging.log4j.core.Logger) LogManager.getRootLogger()).addFilter(f);
@ -41,19 +59,54 @@ public class WebConsole extends JavaPlugin {
* Creates configuration file * Creates configuration file
*/ */
private void createConfig() { 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("host", "localhost");
config.addDefault("port", 8080); config.addDefault("port", 8080);
config.addDefault("password", 1234); config.addDefault("password", 1234);
config.options().copyDefaults(true); config.options().copyDefaults(true);
saveConfig(); saveConfig();
} }
/** /**
* Start WebSockets server * Start WebSocket server
*/ */
private void startWS() { private void startWS() throws Exception,
//Start WebSockets server KeyStoreException, UnrecoverableKeyException, KeyManagementException {
// Create WebSocket server
server = new WSServer(this, new InetSocketAddress(config.getString("host"), config.getInt("port"))); 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() { wsThread = new Thread(new Runnable() {
@Override @Override
public void run() { public void run() {
@ -64,6 +117,6 @@ public class WebConsole extends JavaPlugin {
} }
public WSServer getWSServer() { public WSServer getWSServer() {
return (WSServer)server; return (WSServer) server;
} }
} }