Staff IP checking

This commit is contained in:
Gxorge committed 2023-05-16 22:03:12 +01:00
1 parent 41f746b5ef
commit e764ff0df9
5 files changed
+289

No files matched your search

@@ -7,6 +7,7 @@ import uk.hotten.staffog.commands.TempPunishCommand;
import uk.hotten.staffog.commands.UnpunishCommand;
import uk.hotten.staffog.data.DatabaseManager;
import uk.hotten.staffog.punish.PunishManager;
import uk.hotten.staffog.security.SecurityManager;
import uk.hotten.staffog.utils.Console;
public class StaffOGPlugin extends JavaPlugin {
@@ -19,6 +20,7 @@ public class StaffOGPlugin extends JavaPlugin {
DatabaseManager databaseManager = new DatabaseManager(this);
PunishManager punishManager = new PunishManager(this);
SecurityManager securityManager = new SecurityManager(this);
getCommand("permban").setExecutor(new PermPunishCommand());
getCommand("tempban").setExecutor(new TempPunishCommand());
@@ -36,6 +36,9 @@ public class DatabaseManager {
checkNameHistoryTable();
checkTaskTable();
checkStatTable();
checkWebTable();
checkLinkCodeTable();
checkStaffIpTable();
}
public Connection createConnection() throws SQLException, ClassNotFoundException {
@@ -230,4 +233,95 @@ public class DatabaseManager {
e.printStackTrace();
}
}
private void checkWebTable() {
try (Connection connection = createConnection()) {
DatabaseMetaData meta = connection.getMetaData();
ResultSet resultSet = meta.getTables(null, null, "staffog_web", new String[] {"TABLE"});
if (resultSet.next()) {
return;
}
// Table does not exist
Statement statement = connection.createStatement();
String sql = "CREATE TABLE `staffog_web` (" +
" `id` int(11) NOT NULL AUTO_INCREMENT," +
" `username` varchar(128) NOT NULL," +
" `uuid` varchar(36) NOT NULL," +
" `password` varchar(2048) NOT NULL," +
" `admin` bit(1) NOT NULL," +
" PRIMARY KEY (`id`)," +
" UNIQUE KEY `username` (`username`,`uuid`)" +
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4";
statement.executeUpdate(sql);
Console.info("Created 'staffog_web' table, was missing.");
} catch (Exception e) {
Console.error("Failed to check 'staffog_web' table.");
e.printStackTrace();
}
}
private void checkLinkCodeTable() {
try (Connection connection = createConnection()) {
DatabaseMetaData meta = connection.getMetaData();
ResultSet resultSet = meta.getTables(null, null, "staffog_linkcode", new String[] {"TABLE"});
if (resultSet.next()) {
return;
}
// Table does not exist
Statement statement = connection.createStatement();
String sql = "CREATE TABLE `staffog_linkcode` (" +
" `uuid` varchar(36) NOT NULL," +
" `code` varchar(5) NOT NULL," +
" `admin` bit(1) NOT NULL," +
" UNIQUE KEY `uuid` (`uuid`)" +
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4";
statement.executeUpdate(sql);
Console.info("Created 'staffog_linkcode' table, was missing.");
} catch (Exception e) {
Console.error("Failed to check 'staffog_linkcode' table.");
e.printStackTrace();
}
}
private void checkStaffIpTable() {
try (Connection connection = createConnection()) {
DatabaseMetaData meta = connection.getMetaData();
ResultSet resultSet = meta.getTables(null, null, "staffog_staffip", new String[] {"TABLE"});
if (resultSet.next()) {
return;
}
// Table does not exist
Statement statement = connection.createStatement();
String sql = "CREATE TABLE `staffog_staffip` (" +
" `id` int(11) NOT NULL AUTO_INCREMENT," +
" `ip` varchar(15) NOT NULL," +
" `uuid` varchar(36) NOT NULL," +
" `initial` bit(1) NOT NULL," +
" `panel_acknowledged` bit(1) NOT NULL," +
" `panel_verified` bit(1) NOT NULL," +
" `game_verified` bit(1) NOT NULL," +
" PRIMARY KEY (`id`)" +
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4";
statement.executeUpdate(sql);
Console.info("Created 'staffog_staffip' table, was missing.");
} catch (Exception e) {
Console.error("Failed to check 'staffog_staffip' table.");
e.printStackTrace();
}
}
}
@@ -0,0 +1,45 @@
package uk.hotten.staffog.security;
import org.bukkit.ChatColor;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.AsyncPlayerPreLoginEvent;
import uk.hotten.staffog.security.data.StaffIPInfo;
import java.util.UUID;
public class SecurityEventListener implements Listener {
@EventHandler
public void onPreJoin(AsyncPlayerPreLoginEvent event) {
SecurityManager sm = SecurityManager.getInstance();
UUID uuid = event.getUniqueId();
String ip = event.getAddress().getHostAddress();
if (!sm.hasUUIDGotIp(event.getUniqueId())) {
StaffIPInfo ipInfo = new StaffIPInfo(-1, uuid, ip, true, false, false, true);
sm.createIpEntry(ipInfo);
return;
}
StaffIPInfo ipInfo = sm.isIpRecognised(event.getUniqueId(), event.getAddress().getHostAddress());
if (ipInfo == null) {
ipInfo = new StaffIPInfo(-1, uuid, ip, false, false, false, true);
sm.createIpEntry(ipInfo);
}
if (!ipInfo.isPanelAcknowledged() && !ipInfo.isInitial()) {
event.setLoginResult(AsyncPlayerPreLoginEvent.Result.KICK_OTHER);
event.setKickMessage(ChatColor.RED + "New IP address detected!\n" +
ChatColor.WHITE + "Please login to the staff panel to authenticate your new IP address.\n\n" +
ChatColor.RED + "If you are using a VPN, please disconnect it and try again.");
return;
}
if (!ipInfo.isGameVerified()) {
ipInfo.setGameVerified(true);
sm.updateIpEntry(ipInfo);
}
}
}
@@ -0,0 +1,111 @@
package uk.hotten.staffog.security;
import lombok.Getter;
import org.bukkit.plugin.java.JavaPlugin;
import uk.hotten.staffog.data.DatabaseManager;
import uk.hotten.staffog.security.data.StaffIPInfo;
import uk.hotten.staffog.utils.Console;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.UUID;
public class SecurityManager {
@Getter private static SecurityManager instance;
private JavaPlugin plugin;
public SecurityManager(JavaPlugin plugin) {
this.plugin = plugin;
instance = this;
plugin.getServer().getPluginManager().registerEvents(new SecurityEventListener(), plugin);
}
public StaffIPInfo isIpRecognised(UUID uuid, String ip) {
try (Connection connection = DatabaseManager.getInstance().createConnection()) {
PreparedStatement statement = connection.prepareStatement("SELECT * FROM `staffog_staffip` WHERE `uuid`=? AND `ip`=?");
statement.setString(1, uuid.toString());
statement.setString(2, ip);
ResultSet rs = statement.executeQuery();
if (!rs.next())
return null;
return new StaffIPInfo(
rs.getInt("id"),
UUID.fromString(rs.getString("uuid")),
rs.getString("ip"),
rs.getInt("initial") == 1,
rs.getInt("panel_acknowledged") == 1,
rs.getInt("panel_verified") == 1,
rs.getInt("game_verified") == 1
);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public boolean hasUUIDGotIp(UUID uuid) {
try (Connection connection = DatabaseManager.getInstance().createConnection()) {
PreparedStatement statement = connection.prepareStatement("SELECT * FROM `staffog_staffip` WHERE `uuid`=?");
statement.setString(1, uuid.toString());
ResultSet rs = statement.executeQuery();
return rs.next();
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
public void createIpEntry(StaffIPInfo info) {
try (Connection connection = DatabaseManager.getInstance().createConnection()) {
PreparedStatement statement = connection.prepareStatement(
"INSERT INTO `staffog_staffip` (`ip`, `uuid`, `initial`, `panel_acknowledged`, `panel_verified`, `game_verified`) VALUES (?, ?, ?, ?, ?, ?)"
);
statement.setString(1, info.getIp());
statement.setString(2, info.getUuid().toString());
statement.setInt(3, (info.isInitial() ? 1 : 0));
statement.setInt(4, (info.isPanelAcknowledged() ? 1 : 0));
statement.setInt(5, (info.isPanelVerified() ? 1 : 0));
statement.setInt(6, (info.isGameVerified() ? 1 : 0));
statement.executeUpdate();
} catch (Exception e) {
Console.error("Failed to create ip entry.");
e.printStackTrace();
}
}
public void updateIpEntry(StaffIPInfo info) {
try (Connection connection = DatabaseManager.getInstance().createConnection()) {
PreparedStatement statement = connection.prepareStatement(
"UPDATE `staffog_staffip` SET `ip`=?, `uuid`=?, `initial`=?, `panel_acknowledged`=?, `panel_verified`=?, `game_verified`=? WHERE `id`=?"
);
statement.setString(1, info.getIp());
statement.setString(2, info.getUuid().toString());
statement.setInt(3, (info.isInitial() ? 1 : 0));
statement.setInt(4, (info.isPanelAcknowledged() ? 1 : 0));
statement.setInt(5, (info.isPanelVerified() ? 1 : 0));
statement.setInt(6, (info.isGameVerified() ? 1 : 0));
statement.setInt(7, info.getId());
statement.executeUpdate();
} catch (Exception e) {
Console.error("Failed to update ip entry.");
e.printStackTrace();
}
}
}
@@ -0,0 +1,37 @@
package uk.hotten.staffog.security.data;
import lombok.Getter;
import lombok.Setter;
import java.util.UUID;
public class StaffIPInfo {
@Getter private int id;
@Getter private UUID uuid;
@Getter private String ip;
@Getter @Setter private boolean initial;
@Getter @Setter private boolean panelAcknowledged;
@Getter @Setter private boolean panelVerified;
@Getter @Setter private boolean gameVerified;
public StaffIPInfo(int id, UUID uuid, String ip, boolean initial, boolean panelAcknowledged, boolean panelVerified, boolean gameVerified) {
this.id = id;
this.uuid = uuid;
this.ip = ip;
this.initial = initial;
this.panelAcknowledged = panelAcknowledged;
this.panelVerified = panelVerified;
this.gameVerified = gameVerified;
}
public StaffIPInfo(int id, UUID uuid, String ip) {
this.id = id;
this.uuid = uuid;
this.ip = ip;
this.initial = false;
this.panelVerified = false;
this.gameVerified = false;
}
}