From 3dbc5e78bde6edf767388ec28327e19c7441b68f Mon Sep 17 00:00:00 2001 From: Telesphoreo Date: Sat, 2 Apr 2022 18:34:19 -0500 Subject: [PATCH] Punishments somewhat working --- src/main/java/dev/plex/HTTPDModule.java | 2 + .../dev/plex/request/AbstractServlet.java | 5 +- .../dev/plex/request/impl/AdminsEndpoint.java | 5 +- .../request/impl/PunishmentsEndpoint.java | 97 +++++++++++++++++++ 4 files changed, 105 insertions(+), 4 deletions(-) create mode 100644 src/main/java/dev/plex/request/impl/PunishmentsEndpoint.java diff --git a/src/main/java/dev/plex/HTTPDModule.java b/src/main/java/dev/plex/HTTPDModule.java index 8805058..1fd3e70 100644 --- a/src/main/java/dev/plex/HTTPDModule.java +++ b/src/main/java/dev/plex/HTTPDModule.java @@ -5,6 +5,7 @@ import dev.plex.module.PlexModule; import dev.plex.request.impl.AdminsEndpoint; import dev.plex.request.impl.IndefBansEndpoint; import dev.plex.request.impl.ListEndpoint; +import dev.plex.request.impl.PunishmentsEndpoint; import dev.plex.util.PlexLog; import java.util.concurrent.atomic.AtomicReference; import lombok.Getter; @@ -62,6 +63,7 @@ public class HTTPDModule extends PlexModule new AdminsEndpoint(); new IndefBansEndpoint(); new ListEndpoint(); + new PunishmentsEndpoint(); server.setConnectors(new Connector[]{connector}); server.setHandler(context); diff --git a/src/main/java/dev/plex/request/AbstractServlet.java b/src/main/java/dev/plex/request/AbstractServlet.java index 3beebb4..3362b5b 100644 --- a/src/main/java/dev/plex/request/AbstractServlet.java +++ b/src/main/java/dev/plex/request/AbstractServlet.java @@ -41,10 +41,10 @@ public class AbstractServlet extends HttpServlet @Override public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { - String ipAddress = req.getHeader("X-FORWARDED-FOR"); + String ipAddress = req.getRemoteAddr(); if (ipAddress == null) { - ipAddress = req.getRemoteAddr(); + ipAddress = req.getHeader("X-FORWARDED-FOR"); } Log.log(ipAddress + " visited endpoint " + req.getHttpServletMapping().getMatchValue()); @@ -79,7 +79,6 @@ public class AbstractServlet extends HttpServlet }); } - @Data public static class Mapping { diff --git a/src/main/java/dev/plex/request/impl/AdminsEndpoint.java b/src/main/java/dev/plex/request/impl/AdminsEndpoint.java index ad732d9..f39fe00 100644 --- a/src/main/java/dev/plex/request/impl/AdminsEndpoint.java +++ b/src/main/java/dev/plex/request/impl/AdminsEndpoint.java @@ -29,13 +29,15 @@ public class AdminsEndpoint extends AbstractServlet final PlexPlayer player = DataUtils.getPlayerByIP(ipAddress); if (player == null) { - return "Couldn't load your IP Address: " + ipAddress + ". Have you joined the server before?"; + // This likely means they've never joined the server before. That's okay. We can just not return IPs. + return new GsonBuilder().setPrettyPrinting().create().toJson(Plex.get().getAdminList().getAllAdminPlayers().stream().peek(plexPlayer -> plexPlayer.setIps(Lists.newArrayList())).collect(Collectors.toList())); } if (Plex.get().getSystem().equalsIgnoreCase("ranks")) { PlexLog.debug("Plex-HTTPD using ranks check"); if (!player.getRankFromString().isAtLeast(Rank.ADMIN)) { + // Don't return IPs either if the person is not an Admin or above. return new GsonBuilder().setPrettyPrinting().create().toJson(Plex.get().getAdminList().getAllAdminPlayers().stream().peek(plexPlayer -> plexPlayer.setIps(Lists.newArrayList())).collect(Collectors.toList())); } } @@ -45,6 +47,7 @@ public class AdminsEndpoint extends AbstractServlet final OfflinePlayer offlinePlayer = Bukkit.getOfflinePlayer(UUID.fromString(player.getUuid())); if (!HTTPDModule.getPermissions().playerHas(null, offlinePlayer, "plex.httpd.admins.access")) { + // If the person doesn't have permission, don't return IPs return new GsonBuilder().setPrettyPrinting().create().toJson(Plex.get().getAdminList().getAllAdminPlayers().stream().peek(plexPlayer -> plexPlayer.setIps(Lists.newArrayList())).collect(Collectors.toList())); } } diff --git a/src/main/java/dev/plex/request/impl/PunishmentsEndpoint.java b/src/main/java/dev/plex/request/impl/PunishmentsEndpoint.java new file mode 100644 index 0000000..81b1932 --- /dev/null +++ b/src/main/java/dev/plex/request/impl/PunishmentsEndpoint.java @@ -0,0 +1,97 @@ +package dev.plex.request.impl; + +import com.google.common.collect.Lists; +import com.google.gson.GsonBuilder; +import dev.plex.HTTPDModule; +import dev.plex.Plex; +import dev.plex.cache.DataUtils; +import dev.plex.player.PlexPlayer; +import dev.plex.player.PunishedPlayer; +import dev.plex.rank.enums.Rank; +import dev.plex.request.AbstractServlet; +import dev.plex.request.GetMapping; +import dev.plex.util.PlexLog; +import dev.plex.util.adapter.LocalDateTimeSerializer; +import jakarta.servlet.http.HttpServletRequest; +import java.io.File; +import java.io.IOException; +import java.time.LocalDateTime; +import java.util.UUID; +import java.util.stream.Collectors; +import org.bukkit.Bukkit; +import org.bukkit.OfflinePlayer; + +public class PunishmentsEndpoint extends AbstractServlet +{ + @GetMapping(endpoint = "/api/punishments/") + public String getPunishments(HttpServletRequest request) + { + String ipAddress = request.getRemoteAddr(); + if (ipAddress == null) + { + return "An IP address could not be detected. Please ensure you are connecting using IPv4."; + } + if (request.getPathInfo() == null) + { + return "Please specify the UUID of the player you would like to check."; + } + try + { + UUID uuid = UUID.fromString(request.getPathInfo().replace("/", "")); + final PunishedPlayer punishedPlayer = new PunishedPlayer(uuid); + final PlexPlayer player = DataUtils.getPlayerByIP(ipAddress); + if (player == null) + { + // If the player is null, give it to them without the IPs + return new GsonBuilder().registerTypeAdapter(LocalDateTime.class, new LocalDateTimeSerializer()).setPrettyPrinting().create().toJson(punishedPlayer.getPunishments().stream().peek(punishment -> punishment.setIp(""))); + } + if (Plex.get().getSystem().equalsIgnoreCase("ranks")) + { + PlexLog.debug("Plex-HTTPD using ranks check"); + if (!player.getRankFromString().isAtLeast(Rank.ADMIN)) + { + // Don't return IPs either if the person is not an Admin or above. + return new GsonBuilder().registerTypeAdapter(LocalDateTime.class, new LocalDateTimeSerializer()).setPrettyPrinting().create().toJson(punishedPlayer.getPunishments().stream().peek(punishment -> punishment.setIp(""))); + } + } + else if (Plex.get().getSystem().equalsIgnoreCase("permissions")) + { + PlexLog.debug("Plex-HTTPD using permissions check"); + final OfflinePlayer offlinePlayer = Bukkit.getOfflinePlayer(UUID.fromString(player.getUuid())); + if (!HTTPDModule.getPermissions().playerHas(null, offlinePlayer, "plex.httpd.punishments.access")) + { + // If the person doesn't have permission, don't return IPs + return new GsonBuilder().registerTypeAdapter(LocalDateTime.class, new LocalDateTimeSerializer()).setPrettyPrinting().create().toJson(punishedPlayer.getPunishments().stream().peek(punishment -> punishment.setIp(""))); + } + } + return new GsonBuilder().registerTypeAdapter(LocalDateTime.class, new LocalDateTimeSerializer()).setPrettyPrinting().create().toJson(punishedPlayer.getPunishments().stream().toList()); + } + catch (java.lang.IllegalArgumentException ignored) + { + return "Invalid UUID string"; + } + } + + public File getPunishmentsFile(UUID uuid) + { + File folder = new File(Plex.get().getDataFolder() + File.separator + "punishments"); + if (!folder.exists()) + { + folder.mkdir(); + } + + File file = new File(folder, "" + uuid.toString() + ".json"); + if (!file.exists()) + { + try + { + file.createNewFile(); + } + catch (IOException e) + { + e.printStackTrace(); + } + } + return file; + } +}