Code refactor
This commit is contained in:
parent
45c3584061
commit
f177d1fe2d
@ -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;
|
||||
}
|
||||
}
|
@ -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<String, Object> passwords = plugin.getConfig().getConfigurationSection("passwords").getConfigurationSection("admin").getValues(false);
|
||||
|
||||
for(Map.Entry<String, Object> 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<String, Object> passwords = plugin.getConfig().getConfigurationSection("passwords").getConfigurationSection("viewer").getValues(false);
|
||||
|
||||
for(Map.Entry<String, Object> 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;
|
||||
}
|
||||
|
||||
|
122
src/es/mesacarlos/webconsole/config/ConfigManager.java
Normal file
122
src/es/mesacarlos/webconsole/config/ConfigManager.java
Normal file
@ -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<UserData> getAdmins() {
|
||||
Map<String, Object> passwords = plugin.getConfig().getConfigurationSection("passwords").getConfigurationSection("admin").getValues(false);
|
||||
List<UserData> adminUsers = new ArrayList<UserData>();
|
||||
|
||||
for(Map.Entry<String, Object> 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<UserData> getViewers() {
|
||||
Map<String, Object> passwords = plugin.getConfig().getConfigurationSection("passwords").getConfigurationSection("viewer").getValues(false);
|
||||
List<UserData> viewerUsers = new ArrayList<UserData>();
|
||||
|
||||
for(Map.Entry<String, Object> entry : passwords.entrySet())
|
||||
viewerUsers.add(new UserData(entry.getKey(), entry.getValue().toString()));
|
||||
|
||||
return viewerUsers;
|
||||
}
|
||||
|
||||
}
|
19
src/es/mesacarlos/webconsole/config/UserData.java
Normal file
19
src/es/mesacarlos/webconsole/config/UserData.java
Normal file
@ -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;
|
||||
}
|
||||
}
|
@ -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<String, WSCommand> 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
|
||||
*/
|
||||
|
@ -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();
|
||||
}
|
||||
|
@ -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")));
|
||||
|
Loading…
x
Reference in New Issue
Block a user