Skip to content
This repository has been archived by the owner on Apr 5, 2024. It is now read-only.

Commit

Permalink
Refactor commands + add more commands
Browse files Browse the repository at this point in the history
  • Loading branch information
vlOd2 committed Sep 5, 2023
1 parent 122a5ea commit 0e8c8a0
Show file tree
Hide file tree
Showing 17 changed files with 227 additions and 84 deletions.
2 changes: 1 addition & 1 deletion src/main/java/me/vlod/pinto/PintoServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ public void sendHeartbeat() {

int onlineClients = 0;
for (NetworkHandler client : this.clients) {
if (client.loggedIn && NetHandlerUtils
if (client.userName != null && NetHandlerUtils
.getToOthersStatus(client.databaseEntry.status) != UserStatus.OFFLINE) {
onlineClients++;
}
Expand Down
20 changes: 19 additions & 1 deletion src/main/java/me/vlod/pinto/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ public static String[] splitBySpace(String str) {
/**
* Gets a salted MD5 hash from the specified string
*
* @param salt the salt to use
* @param salt the salt to use (can be empty)
* @param content the string to hash
* @return the MD5 hash or empty on error
*/
Expand All @@ -174,6 +174,24 @@ public static String getMD5HashFromStr(String salt, String content) {
}
}


/**
* Gets a salted SHA256 hash from the specified string
*
* @param salt the salt to use (can be empty)
* @param content the string to hash
* @return the SHA256 hash or empty on error
*/
public static String getSHA256HashFromStr(String salt, String content) {
try {
MessageDigest messageDigest = MessageDigest.getInstance("SHA-256");
messageDigest.update((salt + content).getBytes());
return (new BigInteger(1, messageDigest.digest())).toString(16);
} catch (Exception ex) {
return "";
}
}

/**
* Splits the specified string into chunks<br>
* From <a href="https://stackoverflow.com/a/3760193">https://stackoverflow.com/a/3760193</a>
Expand Down
44 changes: 24 additions & 20 deletions src/main/java/me/vlod/pinto/consolehandler/ConsoleHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,18 @@
import me.vlod.pinto.PintoServer;
import me.vlod.pinto.Tuple;
import me.vlod.pinto.Utils;
import me.vlod.pinto.consolehandler.commands.BanCMD;
import me.vlod.pinto.consolehandler.commands.BanIPCMD;
import me.vlod.pinto.consolehandler.commands.GetIPCMD;
import me.vlod.pinto.consolehandler.commands.KickCMD;
import me.vlod.pinto.consolehandler.commands.KickIPCMD;
import me.vlod.pinto.consolehandler.commands.NotificationCMD;
import me.vlod.pinto.consolehandler.commands.ReloadCMD;
import me.vlod.pinto.consolehandler.commands.StopCMD;
import me.vlod.pinto.consolehandler.commands.UnbanCMD;
import me.vlod.pinto.consolehandler.commands.UnbanIPCMD;
import me.vlod.pinto.consolehandler.commands.Ban;
import me.vlod.pinto.consolehandler.commands.BanIP;
import me.vlod.pinto.consolehandler.commands.ChangePassword;
import me.vlod.pinto.consolehandler.commands.GetIP;
import me.vlod.pinto.consolehandler.commands.Kick;
import me.vlod.pinto.consolehandler.commands.KickIP;
import me.vlod.pinto.consolehandler.commands.ListUsers;
import me.vlod.pinto.consolehandler.commands.Notification;
import me.vlod.pinto.consolehandler.commands.Reload;
import me.vlod.pinto.consolehandler.commands.Stop;
import me.vlod.pinto.consolehandler.commands.Unban;
import me.vlod.pinto.consolehandler.commands.UnbanIP;

public class ConsoleHandler {
protected PintoServer server;
Expand All @@ -28,16 +30,18 @@ public class ConsoleHandler {
public ConsoleHandler(PintoServer server, ConsoleCaller caller) {
this.server = server;
this.caller = caller;
this.commands.add(new BanCMD());
this.commands.add(new BanIPCMD());
this.commands.add(new GetIPCMD());
this.commands.add(new KickCMD());
this.commands.add(new KickIPCMD());
this.commands.add(new ReloadCMD());
this.commands.add(new UnbanCMD());
this.commands.add(new UnbanIPCMD());
this.commands.add(new StopCMD());
this.commands.add(new NotificationCMD());
this.commands.add(new Ban());
this.commands.add(new BanIP());
this.commands.add(new GetIP());
this.commands.add(new Kick());
this.commands.add(new KickIP());
this.commands.add(new Reload());
this.commands.add(new Unban());
this.commands.add(new UnbanIP());
this.commands.add(new Stop());
this.commands.add(new ListUsers());
this.commands.add(new Notification());
this.commands.add(new ChangePassword());
}

public void handleInput(String input) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import me.vlod.pinto.consolehandler.ConsoleCaller;
import me.vlod.pinto.consolehandler.ConsoleCommand;

public class BanCMD implements ConsoleCommand {
public class Ban implements ConsoleCommand {
@Override
public String getName() {
return "ban";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
import me.vlod.pinto.consolehandler.ConsoleCaller;
import me.vlod.pinto.consolehandler.ConsoleCommand;

public class BanIPCMD implements ConsoleCommand {
public class BanIP implements ConsoleCommand {
@Override
public String getName() {
return "banip";
return "ban-ip";
}

@Override
Expand All @@ -17,7 +17,7 @@ public String getDescription() {

@Override
public String getUsage() {
return "banip <ip> <reason>";
return "ban-ip <ip> <reason>";
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package me.vlod.pinto.consolehandler.commands;

import me.vlod.pinto.PintoServer;
import me.vlod.pinto.UserDatabaseEntry;
import me.vlod.pinto.Utils;
import me.vlod.pinto.consolehandler.ConsoleCaller;
import me.vlod.pinto.consolehandler.ConsoleCommand;
import me.vlod.pinto.networking.NetworkHandler;

public class ChangePassword implements ConsoleCommand {
@Override
public String getName() {
return "change-password";
}

@Override
public String getDescription() {
return "Changes a user's password, they will be kicked";
}

@Override
public String getUsage() {
return "change-password <user> <new password>";
}

@Override
public int getMinArgsCount() {
return 2;
}

@Override
public int getMaxArgsCount() {
return 2;
}

@Override
public void execute(PintoServer server, ConsoleCaller caller, String[] args) throws Exception {
String user = args[0];
String password = args[1];

if (!UserDatabaseEntry.isRegistered(server, user)) {
PintoServer.logger.error("The account %s is not registered", user);
return;
}

UserDatabaseEntry userDatabaseEntry = new UserDatabaseEntry(server, user);
userDatabaseEntry.load();
userDatabaseEntry.passwordHash = Utils.getSHA256HashFromStr("", password);
userDatabaseEntry.save();
PintoServer.logger.info("Successfully changed %s's password", user);

NetworkHandler netHandler = server.getHandlerByName(user);
if (netHandler != null) {
netHandler.kick("Your password has been modified!");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
import me.vlod.pinto.consolehandler.ConsoleCommand;
import me.vlod.pinto.networking.NetworkHandler;

public class GetIPCMD implements ConsoleCommand {
public class GetIP implements ConsoleCommand {
@Override
public String getName() {
return "getip";
return "get-ip";
}

@Override
Expand All @@ -18,7 +18,7 @@ public String getDescription() {

@Override
public String getUsage() {
return "getip <user>";
return "get-ip <user>";
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import me.vlod.pinto.consolehandler.ConsoleCommand;
import me.vlod.pinto.networking.NetworkHandler;

public class KickCMD implements ConsoleCommand {
public class Kick implements ConsoleCommand {
@Override
public String getName() {
return "kick";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
import me.vlod.pinto.consolehandler.ConsoleCommand;
import me.vlod.pinto.networking.NetworkHandler;

public class KickIPCMD implements ConsoleCommand {
public class KickIP implements ConsoleCommand {
@Override
public String getName() {
return "kickip";
return "kick-ip";
}

@Override
Expand All @@ -18,7 +18,7 @@ public String getDescription() {

@Override
public String getUsage() {
return "kickip <user> <reason>";
return "kick-ip <user> <reason>";
}

@Override
Expand Down
45 changes: 45 additions & 0 deletions src/main/java/me/vlod/pinto/consolehandler/commands/ListUsers.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package me.vlod.pinto.consolehandler.commands;

import me.vlod.pinto.PintoServer;
import me.vlod.pinto.configuration.MainConfig;
import me.vlod.pinto.consolehandler.ConsoleCaller;
import me.vlod.pinto.consolehandler.ConsoleCommand;
import me.vlod.pinto.networking.NetworkHandler;

public class ListUsers implements ConsoleCommand {
@Override
public String getName() {
return "list-users";
}

@Override
public String getDescription() {
return "Lists all connected users";
}

@Override
public String getUsage() {
return "list-users";
}

@Override
public int getMinArgsCount() {
return 0;
}

@Override
public int getMaxArgsCount() {
return 0;
}

@Override
public void execute(PintoServer server, ConsoleCaller caller, String[] args) throws Exception {
PintoServer.logger.info("There are %d clients out of the max of %d connected%s",
server.clients.size(), MainConfig.instance.maxUsers, server.clients.size() > 0 ? ":" : "");

for (NetworkHandler client : server.clients.toArray(new NetworkHandler[0])) {
PintoServer.logger.info("- %s (%s)", client.networkAddress,
client.userName != null ? client.userName : "** UNAUTHENTICATED **");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import me.vlod.pinto.consolehandler.ConsoleCommand;
import me.vlod.pinto.networking.packet.PacketPopup;

public class NotificationCMD implements ConsoleCommand {
public class Notification implements ConsoleCommand {
@Override
public String getName() {
return "notification";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import me.vlod.pinto.consolehandler.ConsoleCaller;
import me.vlod.pinto.consolehandler.ConsoleCommand;

public class ReloadCMD implements ConsoleCommand {
public class Reload implements ConsoleCommand {
@Override
public String getName() {
return "reload";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import me.vlod.pinto.consolehandler.ConsoleCaller;
import me.vlod.pinto.consolehandler.ConsoleCommand;

public class StopCMD implements ConsoleCommand {
public class Stop implements ConsoleCommand {
@Override
public String getName() {
return "stop";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import me.vlod.pinto.consolehandler.ConsoleCaller;
import me.vlod.pinto.consolehandler.ConsoleCommand;

public class UnbanCMD implements ConsoleCommand {
public class Unban implements ConsoleCommand {
@Override
public String getName() {
return "unban";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
import me.vlod.pinto.consolehandler.ConsoleCaller;
import me.vlod.pinto.consolehandler.ConsoleCommand;

public class UnbanIPCMD implements ConsoleCommand {
public class UnbanIP implements ConsoleCommand {
@Override
public String getName() {
return "unbanip";
return "unban-ip";
}

@Override
Expand All @@ -17,7 +17,7 @@ public String getDescription() {

@Override
public String getUsage() {
return "unbanip <ip>";
return "unban-ip <ip>";
}

@Override
Expand Down
Loading

0 comments on commit 0e8c8a0

Please sign in to comment.