Massive update enabling view-only users
This commit is contained in:
@ -13,6 +13,7 @@ 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;
|
||||
@ -76,11 +77,17 @@ public class WebConsole extends JavaPlugin {
|
||||
// Connection config variables
|
||||
config.addDefault("host", "0.0.0.0");
|
||||
config.addDefault("port", 8080);
|
||||
config.addDefault("password", 1234);
|
||||
|
||||
// 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();
|
||||
}
|
||||
|
116
src/es/mesacarlos/webconsole/auth/LoginManager.java
Normal file
116
src/es/mesacarlos/webconsole/auth/LoginManager.java
Normal file
@ -0,0 +1,116 @@
|
||||
package es.mesacarlos.webconsole.auth;
|
||||
|
||||
import java.net.InetSocketAddress;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Map;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
|
||||
import es.mesacarlos.webconsole.WebConsole;
|
||||
|
||||
public class LoginManager {
|
||||
private ArrayList<User> loggedInUsers = new ArrayList<User>();
|
||||
private static LoginManager instance;
|
||||
|
||||
private LoginManager() {}
|
||||
|
||||
public static LoginManager getInstance() {
|
||||
if(instance == null)
|
||||
instance = new LoginManager();
|
||||
return instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Logs user in
|
||||
* @param user User to login
|
||||
*/
|
||||
public void logIn(User user) {
|
||||
loggedInUsers.add(user);
|
||||
}
|
||||
|
||||
/**
|
||||
* Logs user out
|
||||
* @param address User to logout
|
||||
*/
|
||||
public void logOut(InetSocketAddress address) {
|
||||
for(User user : loggedInUsers)
|
||||
if(user.getSocketAddress().equals(address))
|
||||
loggedInUsers.remove(user);
|
||||
}
|
||||
|
||||
public User getUser(InetSocketAddress address) {
|
||||
for(User user : loggedInUsers)
|
||||
if(user.getSocketAddress().equals(address))
|
||||
return user;
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if user is logged in
|
||||
* @param address User to check
|
||||
* @return true if user is logged in, false otherwise
|
||||
*/
|
||||
public boolean isLoggedIn(InetSocketAddress address) {
|
||||
for(User user : loggedInUsers)
|
||||
if(user.getSocketAddress().equals(address))
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve the full logged-in user list
|
||||
* @return list of logged in users
|
||||
*/
|
||||
public ArrayList<User> getLoggedInUsers() {
|
||||
return loggedInUsers;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the provided password corresponds to any admin
|
||||
* @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 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();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the provided password corresponds to any viewer
|
||||
* @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 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();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public UserType isValidUser(String password) {
|
||||
//Check if is an admin
|
||||
String username = isValidAdminPassword(password);
|
||||
if(username != null)
|
||||
return UserType.ADMIN;
|
||||
|
||||
//Check if is a viewer
|
||||
username = isValidViewerPassword(password);
|
||||
if(username != null)
|
||||
return UserType.VIEWER;
|
||||
|
||||
//He is nothing
|
||||
return UserType.UNKNOWN;
|
||||
}
|
||||
}
|
33
src/es/mesacarlos/webconsole/auth/User.java
Normal file
33
src/es/mesacarlos/webconsole/auth/User.java
Normal file
@ -0,0 +1,33 @@
|
||||
package es.mesacarlos.webconsole.auth;
|
||||
|
||||
import java.net.InetSocketAddress;
|
||||
|
||||
import es.mesacarlos.webconsole.util.Internationalization;
|
||||
|
||||
public class User {
|
||||
private String username;
|
||||
private InetSocketAddress socketAddress;
|
||||
private UserType userType;
|
||||
|
||||
public User(InetSocketAddress socketAddress, String username, UserType userType) {
|
||||
this.socketAddress = socketAddress;
|
||||
this.username = username;
|
||||
this.userType = userType;
|
||||
}
|
||||
|
||||
public InetSocketAddress getSocketAddress() {
|
||||
return socketAddress;
|
||||
}
|
||||
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
public UserType getUserType() {
|
||||
return userType;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return Internationalization.getPhrase("user-tostring", username, socketAddress, userType);
|
||||
}
|
||||
}
|
7
src/es/mesacarlos/webconsole/auth/UserType.java
Normal file
7
src/es/mesacarlos/webconsole/auth/UserType.java
Normal file
@ -0,0 +1,7 @@
|
||||
package es.mesacarlos.webconsole.auth;
|
||||
|
||||
public enum UserType {
|
||||
ADMIN,
|
||||
VIEWER,
|
||||
UNKNOWN
|
||||
}
|
@ -1,14 +1,14 @@
|
||||
package es.mesacarlos.webconsole.minecraft;
|
||||
|
||||
import java.net.InetSocketAddress;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
import es.mesacarlos.webconsole.auth.LoginManager;
|
||||
import es.mesacarlos.webconsole.auth.User;
|
||||
import es.mesacarlos.webconsole.util.Internationalization;
|
||||
import es.mesacarlos.webconsole.util.LoginManager;
|
||||
|
||||
public class WebConsoleCommand implements CommandExecutor {
|
||||
private String version;
|
||||
@ -22,16 +22,16 @@ public class WebConsoleCommand implements CommandExecutor {
|
||||
StringBuilder msg = new StringBuilder();
|
||||
|
||||
msg.append(Internationalization.getPhrase("webconsole-version", version) + "\n");
|
||||
ArrayList<InetSocketAddress> connections = LoginManager.getInstance().getLoggedInUsers();
|
||||
ArrayList<User> users = LoginManager.getInstance().getLoggedInUsers();
|
||||
|
||||
if (connections.isEmpty()) {
|
||||
if (users.isEmpty()) {
|
||||
msg.append(Internationalization.getPhrase("webconsole-no-connections"));
|
||||
} else {
|
||||
msg.append(Internationalization.getPhrase("webconsole-active-connections") + "\n");
|
||||
for (int i = 0; i < connections.size(); i++) {
|
||||
InetSocketAddress connection = connections.get(i);
|
||||
msg.append(connection.toString());
|
||||
if(i+1 < connections.size())
|
||||
for (int i = 0; i < users.size(); i++) {
|
||||
User user = users.get(i);
|
||||
msg.append(user.toString());
|
||||
if(i+1 < users.size())
|
||||
msg.append("\n");
|
||||
}
|
||||
}
|
||||
|
@ -1,50 +0,0 @@
|
||||
package es.mesacarlos.webconsole.util;
|
||||
|
||||
import java.net.InetSocketAddress;
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class LoginManager {
|
||||
private ArrayList<InetSocketAddress> loggedInUsers = new ArrayList<InetSocketAddress>();
|
||||
private static LoginManager instance;
|
||||
|
||||
private LoginManager() {}
|
||||
|
||||
public static LoginManager getInstance() {
|
||||
if(instance == null)
|
||||
instance = new LoginManager();
|
||||
return instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Logs user in
|
||||
* @param address User to login
|
||||
*/
|
||||
public void logIn(InetSocketAddress address) {
|
||||
loggedInUsers.add(address);
|
||||
}
|
||||
|
||||
/**
|
||||
* Logs user out
|
||||
* @param address User to logout
|
||||
*/
|
||||
public void logOut(InetSocketAddress address) {
|
||||
loggedInUsers.remove(address);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if user is logged in
|
||||
* @param address User to check
|
||||
* @return true if user is logged in, false otherwise
|
||||
*/
|
||||
public boolean isLoggedIn(InetSocketAddress address) {
|
||||
return loggedInUsers.contains(address);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve the full logged-in user list
|
||||
* @return list of logged in users
|
||||
*/
|
||||
public ArrayList<InetSocketAddress> getLoggedInUsers() {
|
||||
return loggedInUsers;
|
||||
}
|
||||
}
|
@ -11,9 +11,9 @@ 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;
|
||||
import es.mesacarlos.webconsole.util.LoginManager;
|
||||
import es.mesacarlos.webconsole.websocket.command.WSCommandFactory;
|
||||
import es.mesacarlos.webconsole.websocket.command.WSCommand;
|
||||
import es.mesacarlos.webconsole.websocket.response.ConsoleOutput;
|
||||
|
@ -6,6 +6,9 @@ import org.bukkit.Bukkit;
|
||||
import org.bukkit.command.ConsoleCommandSender;
|
||||
import org.java_websocket.WebSocket;
|
||||
|
||||
import es.mesacarlos.webconsole.auth.LoginManager;
|
||||
import es.mesacarlos.webconsole.auth.User;
|
||||
import es.mesacarlos.webconsole.auth.UserType;
|
||||
import es.mesacarlos.webconsole.util.Internationalization;
|
||||
import es.mesacarlos.webconsole.websocket.WSServer;
|
||||
|
||||
@ -13,7 +16,13 @@ public class ExecCommand implements WSCommand {
|
||||
|
||||
@Override
|
||||
public void execute(WSServer wsServer, WebSocket conn, String command) {
|
||||
|
||||
User u = LoginManager.getInstance().getUser(conn.getRemoteSocketAddress());
|
||||
if(u == null || u.getUserType() != UserType.ADMIN) {
|
||||
if(u != null)
|
||||
Bukkit.getLogger().warning(Internationalization.getPhrase("viewer-error-console", u, command));
|
||||
return;
|
||||
}
|
||||
|
||||
Bukkit.getLogger().info(Internationalization.getPhrase("cmd-executed-console", conn.getRemoteSocketAddress(), Internationalization.utf8ToIso(command)));
|
||||
ConsoleCommandSender sender = Bukkit.getServer().getConsoleSender();
|
||||
|
||||
|
@ -3,33 +3,48 @@ package es.mesacarlos.webconsole.websocket.command;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.java_websocket.WebSocket;
|
||||
|
||||
import es.mesacarlos.webconsole.auth.LoginManager;
|
||||
import es.mesacarlos.webconsole.auth.User;
|
||||
import es.mesacarlos.webconsole.auth.UserType;
|
||||
import es.mesacarlos.webconsole.util.Internationalization;
|
||||
import es.mesacarlos.webconsole.util.LoginManager;
|
||||
import es.mesacarlos.webconsole.websocket.WSServer;
|
||||
import es.mesacarlos.webconsole.websocket.response.LoginRequired;
|
||||
import es.mesacarlos.webconsole.websocket.response.Processed;
|
||||
|
||||
public class LogInCommand implements WSCommand {
|
||||
|
||||
|
||||
@Override
|
||||
public void execute(WSServer wsServer, WebSocket conn, String password) {
|
||||
// If user is logged in, then return.
|
||||
if (LoginManager.getInstance().isLoggedIn(conn.getRemoteSocketAddress()))
|
||||
return;
|
||||
|
||||
// Get password from config files
|
||||
String receivedPassword = wsServer.getMainClass().getConfig().getString("password");
|
||||
|
||||
if (receivedPassword.equals(password)) {
|
||||
// Password is correct, logging in
|
||||
LoginManager.getInstance().logIn(conn.getRemoteSocketAddress());
|
||||
wsServer.sendToClient(conn, new Processed(Internationalization.getPhrase("login-sucessful-message"), "LOGIN ********"));
|
||||
Bukkit.getLogger().info(Internationalization.getPhrase("login-sucessful-console", conn.getRemoteSocketAddress()));
|
||||
} else {
|
||||
// Password was incorrect
|
||||
wsServer.sendToClient(conn, new LoginRequired(Internationalization.getPhrase("login-failed-message")));
|
||||
Bukkit.getLogger().info(Internationalization.getPhrase("login-failed-console", conn.getRemoteSocketAddress()));
|
||||
|
||||
//Check user type and login is password is valid
|
||||
switch(LoginManager.getInstance().isValidUser(password)) {
|
||||
case ADMIN:
|
||||
login(wsServer, conn, LoginManager.getInstance().isValidAdminPassword(password), UserType.ADMIN);
|
||||
break;
|
||||
case VIEWER:
|
||||
login(wsServer, conn, LoginManager.getInstance().isValidViewerPassword(password), UserType.VIEWER);
|
||||
break;
|
||||
case UNKNOWN:
|
||||
wsServer.sendToClient(conn, new LoginRequired(Internationalization.getPhrase("login-failed-message")));
|
||||
Bukkit.getLogger().info(Internationalization.getPhrase("login-failed-console", conn.getRemoteSocketAddress()));
|
||||
break;
|
||||
default:
|
||||
wsServer.sendToClient(conn, new LoginRequired(Internationalization.getPhrase("login-failed-message")));
|
||||
Bukkit.getLogger().info(Internationalization.getPhrase("login-failed-console", conn.getRemoteSocketAddress()));
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void login(WSServer wsServer, WebSocket conn, String username, UserType as) {
|
||||
User user = new User(conn.getRemoteSocketAddress(), username, as);
|
||||
LoginManager.getInstance().logIn(user);
|
||||
|
||||
wsServer.sendToClient(conn, new Processed(Internationalization.getPhrase("login-sucessful-message"), "LOGIN ********"));
|
||||
Bukkit.getLogger().info(Internationalization.getPhrase("login-sucessful-console", user.toString()));
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user