From f177d1fe2d81fb9301e36d5f8a88b1b7f94bd092 Mon Sep 17 00:00:00 2001 From: Carlos <28845529+mesacarlos@users.noreply.github.com> Date: Thu, 25 Jun 2020 16:53:10 +0200 Subject: [PATCH] Code refactor --- src/es/mesacarlos/webconsole/WebConsole.java | 53 ++------ .../webconsole/auth/PasswordManager.java | 37 ++---- .../webconsole/config/ConfigManager.java | 122 ++++++++++++++++++ .../webconsole/config/UserData.java | 19 +++ .../webconsole/websocket/WSServer.java | 13 +- .../websocket/command/ExecCommand.java | 5 +- .../websocket/command/LogInCommand.java | 4 +- 7 files changed, 168 insertions(+), 85 deletions(-) create mode 100644 src/es/mesacarlos/webconsole/config/ConfigManager.java create mode 100644 src/es/mesacarlos/webconsole/config/UserData.java diff --git a/src/es/mesacarlos/webconsole/WebConsole.java b/src/es/mesacarlos/webconsole/WebConsole.java index 6fafc25..7d31445 100644 --- a/src/es/mesacarlos/webconsole/WebConsole.java +++ b/src/es/mesacarlos/webconsole/WebConsole.java @@ -3,7 +3,6 @@ package es.mesacarlos.webconsole; import java.io.File; import java.io.FileInputStream; import java.io.IOException; -import java.net.InetSocketAddress; import java.security.KeyStore; import javax.net.ssl.KeyManagerFactory; @@ -13,29 +12,24 @@ 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.ConfigurationSection; -import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.plugin.java.JavaPlugin; import org.java_websocket.server.DefaultSSLWebSocketServerFactory; +import es.mesacarlos.webconsole.config.ConfigManager; import es.mesacarlos.webconsole.minecraft.WebConsoleCommand; import es.mesacarlos.webconsole.util.Internationalization; import es.mesacarlos.webconsole.util.LogFilter; import es.mesacarlos.webconsole.websocket.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(); - //Change language to user-specified language. - Internationalization.setCurrentLocale(config.getString("language")); + Internationalization.setCurrentLocale(ConfigManager.getInstance().getLanguage()); //Start WebSocket Server try { @@ -63,48 +57,19 @@ public class WebConsole extends JavaPlugin { } } - /** - * 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", "0.0.0.0"); - config.addDefault("port", 8080); - - // Language config - config.addDefault("language", "en"); - - if(config.getConfigurationSection("passwords") == null) { - ConfigurationSection passwordsSection = config.createSection("passwords"); - ConfigurationSection adminPasswordSection = passwordsSection.createSection("admin"); - adminPasswordSection.addDefault("user1", "mySecurePassword"); - passwordsSection.createSection("viewer"); - } - - config.options().copyDefaults(true); - saveConfig(); - } - /** * Start WebSocket server */ private void startWS() throws Exception { // Create WebSocket server - server = new WSServer(this, new InetSocketAddress(config.getString("host"), config.getInt("port"))); + server = new WSServer(ConfigManager.getInstance().getSocketAdress()); - if(config.getBoolean("useSSL")) { + if(ConfigManager.getInstance().isSslEnabled()) { // Configure SSL - String STORETYPE = config.getString("StoreType"); - String KEYSTORE = config.getString("KeyStore"); - String STOREPASSWORD = config.getString("StorePassword"); - String KEYPASSWORD = config.getString("KeyPassword"); + String STORETYPE = ConfigManager.getInstance().getStoreType(); + String KEYSTORE = ConfigManager.getInstance().getKeyStore(); + String STOREPASSWORD = ConfigManager.getInstance().getStorePassword(); + String KEYPASSWORD = ConfigManager.getInstance().getKeyPassword(); KeyStore ks = KeyStore.getInstance(STORETYPE); File kf = new File(KEYSTORE); @@ -133,6 +98,6 @@ public class WebConsole extends JavaPlugin { } public WSServer getWSServer() { - return (WSServer) server; + return server; } } \ No newline at end of file diff --git a/src/es/mesacarlos/webconsole/auth/PasswordManager.java b/src/es/mesacarlos/webconsole/auth/PasswordManager.java index bbbc4c9..6ae87d4 100644 --- a/src/es/mesacarlos/webconsole/auth/PasswordManager.java +++ b/src/es/mesacarlos/webconsole/auth/PasswordManager.java @@ -1,10 +1,7 @@ package es.mesacarlos.webconsole.auth; -import java.util.Map; - -import org.bukkit.Bukkit; - -import es.mesacarlos.webconsole.WebConsole; +import es.mesacarlos.webconsole.config.ConfigManager; +import es.mesacarlos.webconsole.config.UserData; public class PasswordManager { @@ -15,12 +12,12 @@ public class PasswordManager { */ public static UserType isValidUser(String password) { //Check if is an admin - String username = isValidAdminPassword(password); + String username = getAdminUsernameFromPassword(password); if(username != null) return UserType.ADMIN; //Check if is a viewer - username = isValidViewerPassword(password); + username = getViewerUsernameFromPassword(password); if(username != null) return UserType.VIEWER; @@ -33,15 +30,10 @@ public class PasswordManager { * @param password Provided password * @return Name of the user if password corresponds to a valid admin, null if is a viewer or an invalid password */ - public static String isValidAdminPassword(String password) { - WebConsole plugin = (WebConsole)Bukkit.getPluginManager().getPlugin("WebConsole"); - Map passwords = plugin.getConfig().getConfigurationSection("passwords").getConfigurationSection("admin").getValues(false); - - for(Map.Entry entry : passwords.entrySet()) { - String pwd = (String)entry.getValue(); - if(pwd.equals(password)) - return entry.getKey(); - } + public static String getAdminUsernameFromPassword(String password) { + for(UserData ud : ConfigManager.getInstance().getAdmins()) + if(ud.getPassword().equals(password)) + return ud.getUsername(); return null; } @@ -50,15 +42,10 @@ public class PasswordManager { * @param password Provided password * @return Name of the user if password corresponds to a valid viewer, null if is a admin or invalid password */ - public static String isValidViewerPassword(String password) { - WebConsole plugin = (WebConsole)Bukkit.getPluginManager().getPlugin("WebConsole"); - Map passwords = plugin.getConfig().getConfigurationSection("passwords").getConfigurationSection("viewer").getValues(false); - - for(Map.Entry entry : passwords.entrySet()) { - String pwd = (String)entry.getValue(); - if(pwd.equals(password)) - return entry.getKey(); - } + public static String getViewerUsernameFromPassword(String password) { + for(UserData ud : ConfigManager.getInstance().getViewers()) + if(ud.getPassword().equals(password)) + return ud.getUsername(); return null; } diff --git a/src/es/mesacarlos/webconsole/config/ConfigManager.java b/src/es/mesacarlos/webconsole/config/ConfigManager.java new file mode 100644 index 0000000..3797862 --- /dev/null +++ b/src/es/mesacarlos/webconsole/config/ConfigManager.java @@ -0,0 +1,122 @@ +package es.mesacarlos.webconsole.config; + +import java.net.InetSocketAddress; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +import org.bukkit.Bukkit; +import org.bukkit.configuration.ConfigurationSection; +import org.bukkit.configuration.file.FileConfiguration; + +import es.mesacarlos.webconsole.WebConsole; + +public class ConfigManager { + private static ConfigManager instance; + private WebConsole plugin = (WebConsole) Bukkit.getPluginManager().getPlugin("WebConsole"); + private FileConfiguration config = plugin.getConfig(); + + private ConfigManager() { + loadConfig(); + } + + public static ConfigManager getInstance() { + if(instance == null) + instance = new ConfigManager(); + return instance; + } + + /** + * Create configuration file or load it if already exist + */ + private void loadConfig() { + // 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", "0.0.0.0"); + config.addDefault("port", 8080); + + // Language config + config.addDefault("language", "en"); + + if(config.getConfigurationSection("passwords") == null) { + ConfigurationSection passwordsSection = config.createSection("passwords"); + ConfigurationSection adminPasswordSection = passwordsSection.createSection("admin"); + adminPasswordSection.addDefault("user1", "mySecurePassword"); + passwordsSection.createSection("viewer"); + } + + config.options().copyDefaults(true); + plugin.saveConfig(); + } + + public boolean isSslEnabled() { + return config.getBoolean("useSSL"); + } + + public String getStoreType() { + return config.getString("StoreType"); + } + + public String getKeyStore() { + return config.getString("KeyStore"); + } + + public String getStorePassword() { + return config.getString("StorePassword"); + } + + public String getKeyPassword() { + return config.getString("KeyPassword"); + } + + /** + * Get host and port fields already as InetSocketAddress + * @return InetSocketAddress created from the fields host and port of config.yml + */ + public InetSocketAddress getSocketAdress() { + return new InetSocketAddress(config.getString("host"), config.getInt("port")); + } + + /** + * Language code from config.yml + * @return language code + */ + public String getLanguage() { + return config.getString("language"); + } + + /** + * Get all admins as a UserData list + * @return list of admin users + */ + public List getAdmins() { + Map passwords = plugin.getConfig().getConfigurationSection("passwords").getConfigurationSection("admin").getValues(false); + List adminUsers = new ArrayList(); + + for(Map.Entry entry : passwords.entrySet()) + adminUsers.add(new UserData(entry.getKey(), entry.getValue().toString())); + + return adminUsers; + } + + /** + * Get all viewers as a UserData list + * @return list of viewer users + */ + public List getViewers() { + Map passwords = plugin.getConfig().getConfigurationSection("passwords").getConfigurationSection("viewer").getValues(false); + List viewerUsers = new ArrayList(); + + for(Map.Entry entry : passwords.entrySet()) + viewerUsers.add(new UserData(entry.getKey(), entry.getValue().toString())); + + return viewerUsers; + } + +} \ No newline at end of file diff --git a/src/es/mesacarlos/webconsole/config/UserData.java b/src/es/mesacarlos/webconsole/config/UserData.java new file mode 100644 index 0000000..8253bc3 --- /dev/null +++ b/src/es/mesacarlos/webconsole/config/UserData.java @@ -0,0 +1,19 @@ +package es.mesacarlos.webconsole.config; + +public class UserData { + private String username; + private String password; + + public UserData(String username, String password) { + this.username = username; + this.password = password; + } + + public String getUsername() { + return username; + } + + public String getPassword() { + return password; + } +} \ No newline at end of file diff --git a/src/es/mesacarlos/webconsole/websocket/WSServer.java b/src/es/mesacarlos/webconsole/websocket/WSServer.java index b73eabb..c466bd8 100644 --- a/src/es/mesacarlos/webconsole/websocket/WSServer.java +++ b/src/es/mesacarlos/webconsole/websocket/WSServer.java @@ -10,7 +10,6 @@ import org.java_websocket.exceptions.WebsocketNotConnectedException; import org.java_websocket.handshake.ClientHandshake; import org.java_websocket.server.WebSocketServer; -import es.mesacarlos.webconsole.WebConsole; import es.mesacarlos.webconsole.auth.LoginManager; import es.mesacarlos.webconsole.util.DateTimeUtils; import es.mesacarlos.webconsole.util.Internationalization; @@ -24,11 +23,9 @@ import es.mesacarlos.webconsole.websocket.response.UnknownCommand; public class WSServer extends WebSocketServer { private HashMap commands = WSCommandFactory.getCommandsHashMap(); - private WebConsole plugin; - public WSServer(WebConsole plugin, InetSocketAddress address) { + public WSServer(InetSocketAddress address) { super(address); - this.plugin = plugin; } @Override @@ -83,14 +80,6 @@ public class WSServer extends WebSocketServer { Bukkit.getLogger().info(Internationalization.getPhrase("started-websocket")); } - /** - * Returns main class - * @return Main plugin class - */ - public WebConsole getMainClass() { - return plugin; - } - /** * Sends the message to all connected AND logged-in users */ diff --git a/src/es/mesacarlos/webconsole/websocket/command/ExecCommand.java b/src/es/mesacarlos/webconsole/websocket/command/ExecCommand.java index 9348ccd..6be39e8 100644 --- a/src/es/mesacarlos/webconsole/websocket/command/ExecCommand.java +++ b/src/es/mesacarlos/webconsole/websocket/command/ExecCommand.java @@ -6,6 +6,7 @@ import org.bukkit.Bukkit; import org.bukkit.command.ConsoleCommandSender; import org.java_websocket.WebSocket; +import es.mesacarlos.webconsole.WebConsole; import es.mesacarlos.webconsole.auth.LoginManager; import es.mesacarlos.webconsole.auth.User; import es.mesacarlos.webconsole.auth.UserType; @@ -25,11 +26,11 @@ public class ExecCommand implements WSCommand { Bukkit.getLogger().info(Internationalization.getPhrase("cmd-executed-console", conn.getRemoteSocketAddress(), Internationalization.utf8ToIso(command))); ConsoleCommandSender sender = Bukkit.getServer().getConsoleSender(); - + WebConsole plugin = (WebConsole) Bukkit.getPluginManager().getPlugin("WebConsole"); try { @SuppressWarnings("unused") boolean success = Bukkit.getScheduler() - .callSyncMethod(wsServer.getMainClass(), () -> Bukkit.dispatchCommand(sender, command)).get(); + .callSyncMethod(plugin, () -> Bukkit.dispatchCommand(sender, command)).get(); } catch (InterruptedException | ExecutionException e) { e.printStackTrace(); } diff --git a/src/es/mesacarlos/webconsole/websocket/command/LogInCommand.java b/src/es/mesacarlos/webconsole/websocket/command/LogInCommand.java index d650e77..2665ed9 100644 --- a/src/es/mesacarlos/webconsole/websocket/command/LogInCommand.java +++ b/src/es/mesacarlos/webconsole/websocket/command/LogInCommand.java @@ -23,10 +23,10 @@ public class LogInCommand implements WSCommand { //Check user type and login is password is valid switch(PasswordManager.isValidUser(password)) { case ADMIN: - login(wsServer, conn, PasswordManager.isValidAdminPassword(password), UserType.ADMIN); + login(wsServer, conn, PasswordManager.getAdminUsernameFromPassword(password), UserType.ADMIN); break; case VIEWER: - login(wsServer, conn, PasswordManager.isValidViewerPassword(password), UserType.VIEWER); + login(wsServer, conn, PasswordManager.getViewerUsernameFromPassword(password), UserType.VIEWER); break; default: wsServer.sendToClient(conn, new LoginRequired(Internationalization.getPhrase("login-failed-message")));