First commit
This commit is contained in:
69
src/com/mesacarlos/webconsole/WebConsole.java
Normal file
69
src/com/mesacarlos/webconsole/WebConsole.java
Normal file
@ -0,0 +1,69 @@
|
||||
package com.mesacarlos.webconsole;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.InetSocketAddress;
|
||||
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.core.Filter;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
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();
|
||||
|
||||
Filter f = new LogFilter(getWSServer());
|
||||
((org.apache.logging.log4j.core.Logger) LogManager.getRootLogger()).addFilter(f);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDisable() {
|
||||
try {
|
||||
server.stop();
|
||||
wsThread = null;
|
||||
} catch (IOException | InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates configuration file
|
||||
*/
|
||||
private void createConfig() {
|
||||
config.addDefault("host", "localhost");
|
||||
config.addDefault("port", 8080);
|
||||
config.addDefault("password", 1234);
|
||||
config.options().copyDefaults(true);
|
||||
saveConfig();
|
||||
}
|
||||
|
||||
/**
|
||||
* Start WebSockets server
|
||||
*/
|
||||
private void startWS() {
|
||||
//Start WebSockets server
|
||||
server = new WSServer(this, new InetSocketAddress(config.getString("host"), config.getInt("port")));
|
||||
wsThread = new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
server.run();
|
||||
}
|
||||
});
|
||||
wsThread.start();
|
||||
}
|
||||
|
||||
public WSServer getWSServer() {
|
||||
return (WSServer)server;
|
||||
}
|
||||
}
|
13
src/com/mesacarlos/webconsole/command/CommandFactory.java
Normal file
13
src/com/mesacarlos/webconsole/command/CommandFactory.java
Normal file
@ -0,0 +1,13 @@
|
||||
package com.mesacarlos.webconsole.command;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
public class CommandFactory {
|
||||
|
||||
public static HashMap<String, WSCommand> getCommandsHashMap() {
|
||||
HashMap<String, WSCommand> commands = new HashMap<String, WSCommand>();
|
||||
commands.put("LOGIN", new LogInCommand());
|
||||
commands.put("EXEC", new ExecuteCmdCommand());
|
||||
return commands;
|
||||
}
|
||||
}
|
27
src/com/mesacarlos/webconsole/command/ExecuteCmdCommand.java
Normal file
27
src/com/mesacarlos/webconsole/command/ExecuteCmdCommand.java
Normal file
@ -0,0 +1,27 @@
|
||||
package com.mesacarlos.webconsole.command;
|
||||
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.command.ConsoleCommandSender;
|
||||
import org.java_websocket.WebSocket;
|
||||
|
||||
import com.mesacarlos.webconsole.websockets.WSServer;
|
||||
|
||||
public class ExecuteCmdCommand implements WSCommand{
|
||||
|
||||
@Override
|
||||
public void execute(WSServer wsServer, WebSocket conn, String command) {
|
||||
ConsoleCommandSender sender = Bukkit.getServer().getConsoleSender();
|
||||
|
||||
try {
|
||||
@SuppressWarnings("unused")
|
||||
boolean success = Bukkit.getScheduler().callSyncMethod( wsServer.getMainClass(), () -> Bukkit.dispatchCommand( sender, command ) ).get();
|
||||
} catch (InterruptedException | ExecutionException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
31
src/com/mesacarlos/webconsole/command/LogInCommand.java
Normal file
31
src/com/mesacarlos/webconsole/command/LogInCommand.java
Normal file
@ -0,0 +1,31 @@
|
||||
package com.mesacarlos.webconsole.command;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.java_websocket.WebSocket;
|
||||
|
||||
import com.mesacarlos.webconsole.util.LoginManager;
|
||||
import com.mesacarlos.webconsole.websockets.WSServer;
|
||||
|
||||
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().getAddress().toString()))
|
||||
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().getAddress().toString());
|
||||
conn.send("200 Logged In");
|
||||
Bukkit.getLogger().info("[WebConsole] Successfully logged in from " + conn.getRemoteSocketAddress());
|
||||
}else {
|
||||
conn.send("403 Forbidden");
|
||||
Bukkit.getLogger().info("[WebConsole] Password incorrect while login from " + conn.getRemoteSocketAddress());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
9
src/com/mesacarlos/webconsole/command/WSCommand.java
Normal file
9
src/com/mesacarlos/webconsole/command/WSCommand.java
Normal file
@ -0,0 +1,9 @@
|
||||
package com.mesacarlos.webconsole.command;
|
||||
|
||||
import org.java_websocket.WebSocket;
|
||||
|
||||
import com.mesacarlos.webconsole.websockets.WSServer;
|
||||
|
||||
public interface WSCommand {
|
||||
void execute(WSServer wsServer, WebSocket conn, String params);
|
||||
}
|
10
src/com/mesacarlos/webconsole/util/DateTimeUtils.java
Normal file
10
src/com/mesacarlos/webconsole/util/DateTimeUtils.java
Normal file
@ -0,0 +1,10 @@
|
||||
package com.mesacarlos.webconsole.util;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
|
||||
public class DateTimeUtils {
|
||||
public static String getDateAsString() {
|
||||
return new SimpleDateFormat("dd-MM-yyyy HH:mm:ss").format(new Date());
|
||||
}
|
||||
}
|
138
src/com/mesacarlos/webconsole/util/LogFilter.java
Normal file
138
src/com/mesacarlos/webconsole/util/LogFilter.java
Normal file
@ -0,0 +1,138 @@
|
||||
package com.mesacarlos.webconsole.util;
|
||||
|
||||
import org.apache.logging.log4j.Level;
|
||||
import org.apache.logging.log4j.Marker;
|
||||
import org.apache.logging.log4j.core.Filter;
|
||||
import org.apache.logging.log4j.core.LogEvent;
|
||||
import org.apache.logging.log4j.core.Logger;
|
||||
import org.apache.logging.log4j.message.Message;
|
||||
|
||||
import com.mesacarlos.webconsole.websockets.WSServer;
|
||||
|
||||
public class LogFilter implements Filter{
|
||||
private WSServer wsServer;
|
||||
|
||||
public LogFilter(WSServer wsServer) {
|
||||
this.wsServer = wsServer;
|
||||
}
|
||||
|
||||
@Override
|
||||
public State getState() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void initialize() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void start() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stop() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isStarted() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isStopped() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result getOnMismatch() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result getOnMatch() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result filter(Logger logger, Level level, Marker marker, String msg, Object... params) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result filter(Logger logger, Level level, Marker marker, String message, Object p0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result filter(Logger logger, Level level, Marker marker, String message, Object p0, Object p1) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result filter(Logger logger, Level level, Marker marker, String message, Object p0, Object p1, Object p2) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result filter(Logger logger, Level level, Marker marker, String message, Object p0, Object p1, Object p2,
|
||||
Object p3) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result filter(Logger logger, Level level, Marker marker, String message, Object p0, Object p1, Object p2,
|
||||
Object p3, Object p4) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result filter(Logger logger, Level level, Marker marker, String message, Object p0, Object p1, Object p2,
|
||||
Object p3, Object p4, Object p5) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result filter(Logger logger, Level level, Marker marker, String message, Object p0, Object p1, Object p2,
|
||||
Object p3, Object p4, Object p5, Object p6) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result filter(Logger logger, Level level, Marker marker, String message, Object p0, Object p1, Object p2,
|
||||
Object p3, Object p4, Object p5, Object p6, Object p7) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result filter(Logger logger, Level level, Marker marker, String message, Object p0, Object p1, Object p2,
|
||||
Object p3, Object p4, Object p5, Object p6, Object p7, Object p8) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result filter(Logger logger, Level level, Marker marker, String message, Object p0, Object p1, Object p2,
|
||||
Object p3, Object p4, Object p5, Object p6, Object p7, Object p8, Object p9) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result filter(Logger logger, Level level, Marker marker, Object msg, Throwable t) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result filter(Logger logger, Level level, Marker marker, Message msg, Throwable t) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result filter(LogEvent event) {
|
||||
String message = event.getMessage().getFormattedMessage().replaceAll("\u001b"," ");
|
||||
wsServer.onNewConsoleLinePrinted(message);
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
29
src/com/mesacarlos/webconsole/util/LoginManager.java
Normal file
29
src/com/mesacarlos/webconsole/util/LoginManager.java
Normal file
@ -0,0 +1,29 @@
|
||||
package com.mesacarlos.webconsole.util;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class LoginManager {
|
||||
private List<String> loggedInUsers = new ArrayList<String>();
|
||||
private static LoginManager instance;
|
||||
|
||||
private LoginManager() {}
|
||||
|
||||
public static LoginManager getInstance() {
|
||||
if(instance == null)
|
||||
instance = new LoginManager();
|
||||
return instance;
|
||||
}
|
||||
|
||||
public void logIn(String address) {
|
||||
loggedInUsers.add(address);
|
||||
}
|
||||
|
||||
public void logOut(String address) {
|
||||
loggedInUsers.remove(address);
|
||||
}
|
||||
|
||||
public boolean isLoggedIn(String address) {
|
||||
return loggedInUsers.contains(address);
|
||||
}
|
||||
}
|
98
src/com/mesacarlos/webconsole/websockets/WSServer.java
Normal file
98
src/com/mesacarlos/webconsole/websockets/WSServer.java
Normal file
@ -0,0 +1,98 @@
|
||||
package com.mesacarlos.webconsole.websockets;
|
||||
|
||||
import java.net.InetSocketAddress;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.java_websocket.WebSocket;
|
||||
import org.java_websocket.handshake.ClientHandshake;
|
||||
import org.java_websocket.server.WebSocketServer;
|
||||
|
||||
import com.mesacarlos.webconsole.WebConsole;
|
||||
import com.mesacarlos.webconsole.command.CommandFactory;
|
||||
import com.mesacarlos.webconsole.command.WSCommand;
|
||||
import com.mesacarlos.webconsole.util.LoginManager;
|
||||
|
||||
public class WSServer extends WebSocketServer {
|
||||
private WebConsole plugin;
|
||||
private HashMap<String, WSCommand> commands = CommandFactory.getCommandsHashMap();
|
||||
|
||||
public WSServer(WebConsole plugin, InetSocketAddress address) {
|
||||
super(address);
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onOpen(WebSocket conn, ClientHandshake handshake) {
|
||||
conn.send("Connection started, waiting login");
|
||||
Bukkit.getLogger().info("[WebConsole] Connected and waiting login from " + conn.getRemoteSocketAddress());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onMessage(WebSocket conn, String message) {
|
||||
// Log this action to console
|
||||
Bukkit.getLogger().info("[WebConsole] Received signal from " + conn.getRemoteSocketAddress() + ": " + message);
|
||||
|
||||
// Get command and params
|
||||
String wsCommand = message.split(" ")[0];
|
||||
String wsCommandParams = "";
|
||||
if (message.contains(" "))
|
||||
wsCommandParams = message.split(" ", 2)[1];
|
||||
|
||||
// Run command
|
||||
WSCommand cmd = commands.get(wsCommand);
|
||||
|
||||
if (cmd == null) {
|
||||
Bukkit.getLogger().info(
|
||||
"[WebConsole] Signal was not processed since is not valid. Is your plugin/web interface up to date?");
|
||||
} else if (!LoginManager.getInstance().isLoggedIn(conn.getRemoteSocketAddress().getAddress().toString())
|
||||
&& !wsCommand.equals("LOGIN")) {
|
||||
// DO NOTHING. User is not authorised
|
||||
conn.send("403 Forbidden");
|
||||
Bukkit.getLogger().warning("[WebConsole] " + conn.getRemoteSocketAddress()
|
||||
+ " tried to run a command while not authenticated!");
|
||||
} else {
|
||||
cmd.execute(this, conn, wsCommandParams);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onClose(WebSocket conn, int code, String reason, boolean remote) {
|
||||
LoginManager.getInstance().logOut(conn.getRemoteSocketAddress().getAddress().toString());
|
||||
Bukkit.getLogger()
|
||||
.info("[WebConsole] Closed WS connection " + conn.getRemoteSocketAddress() + ". Reason: " + reason);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(WebSocket conn, Exception ex) {
|
||||
Bukkit.getLogger()
|
||||
.warning("[WebConsole] Error occured on connection " + conn.getRemoteSocketAddress() + ":" + ex);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStart() {
|
||||
Bukkit.getLogger().info("[WebConsole] WebSockets Server started successfully");
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends the message to all connected AND logged-in users
|
||||
*/
|
||||
public void onNewConsoleLinePrinted(String line) {
|
||||
Collection<WebSocket> connections = getConnections();
|
||||
for (WebSocket connection : connections) {
|
||||
if (LoginManager.getInstance().isLoggedIn(connection.getRemoteSocketAddress().getAddress().toString()))
|
||||
connection.send("LOG " + line);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns main class
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public WebConsole getMainClass() {
|
||||
return plugin;
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user