work in progress

This commit is contained in:
Telesphoreo 2022-04-15 13:55:40 -05:00
parent b4ab3b609a
commit 7550db5fb0
5 changed files with 34 additions and 21 deletions

View File

@ -79,6 +79,16 @@ public class AbstractServlet extends HttpServlet
}); });
} }
public String createBasicHTML(String title, String body)
{
return "<html><head><title>" + title + "</title></head><body>" + body + "</body></html>";
}
public String createJSONHTML(String title, String json)
{
return "<html><head><title>" + title + "</title></head><body><pre><code>" + json + "</code></pre></body></html>";
}
@Data @Data
public static class Mapping public static class Mapping
{ {

View File

@ -12,28 +12,28 @@ import dev.plex.request.GetMapping;
import dev.plex.util.PlexLog; import dev.plex.util.PlexLog;
import dev.plex.util.adapter.LocalDateTimeSerializer; import dev.plex.util.adapter.LocalDateTimeSerializer;
import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletRequest;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.UUID;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer; import org.bukkit.OfflinePlayer;
public class AdminsEndpoint extends AbstractServlet public class AdminsEndpoint extends AbstractServlet
{ {
private static final String TITLE = "Admins - Plex HTTPD";
@GetMapping(endpoint = "/api/admins/") @GetMapping(endpoint = "/api/admins/")
public String getAdmins(HttpServletRequest request) public String getAdmins(HttpServletRequest request)
{ {
String ipAddress = request.getRemoteAddr(); String ipAddress = request.getRemoteAddr();
if (ipAddress == null) if (ipAddress == null)
{ {
return "An IP address could not be detected. Please ensure you are connecting using IPv4."; return createBasicHTML(TITLE, "An IP address could not be detected. Please ensure you are connecting using IPv4.");
} }
final PlexPlayer player = DataUtils.getPlayerByIP(ipAddress); final PlexPlayer player = DataUtils.getPlayerByIP(ipAddress);
if (player == null) if (player == null)
{ {
// This likely means they've never joined the server before. That's okay. We can just not return IPs. // This likely means they've never joined the server before. That's okay. We can just not return IPs.
return new GsonBuilder().registerTypeAdapter(LocalDateTime.class, new LocalDateTimeSerializer()).setPrettyPrinting().create().toJson(Plex.get().getAdminList().getAllAdminPlayers().stream().peek(plexPlayer -> plexPlayer.setIps(Lists.newArrayList())).peek(plexPlayer -> plexPlayer.setPunishments(Lists.newArrayList())).collect(Collectors.toList())); return createJSONHTML(TITLE, new GsonBuilder().registerTypeAdapter(LocalDateTime.class, new LocalDateTimeSerializer()).setPrettyPrinting().create().toJson(Plex.get().getAdminList().getAllAdminPlayers().stream().peek(plexPlayer -> plexPlayer.setIps(Lists.newArrayList())).peek(plexPlayer -> plexPlayer.setPunishments(Lists.newArrayList())).collect(Collectors.toList())));
} }
if (Plex.get().getSystem().equalsIgnoreCase("ranks")) if (Plex.get().getSystem().equalsIgnoreCase("ranks"))
{ {
@ -41,7 +41,7 @@ public class AdminsEndpoint extends AbstractServlet
if (!player.getRankFromString().isAtLeast(Rank.ADMIN)) if (!player.getRankFromString().isAtLeast(Rank.ADMIN))
{ {
// Don't return IPs either if the person is not an Admin or above. // 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(Plex.get().getAdminList().getAllAdminPlayers().stream().peek(plexPlayer -> plexPlayer.setIps(Lists.newArrayList())).peek(plexPlayer -> plexPlayer.setPunishments(Lists.newArrayList())).collect(Collectors.toList())); return createJSONHTML(TITLE, new GsonBuilder().registerTypeAdapter(LocalDateTime.class, new LocalDateTimeSerializer()).setPrettyPrinting().create().toJson(Plex.get().getAdminList().getAllAdminPlayers().stream().peek(plexPlayer -> plexPlayer.setIps(Lists.newArrayList())).peek(plexPlayer -> plexPlayer.setPunishments(Lists.newArrayList())).collect(Collectors.toList())));
} }
} }
else if (Plex.get().getSystem().equalsIgnoreCase("permissions")) else if (Plex.get().getSystem().equalsIgnoreCase("permissions"))
@ -51,9 +51,9 @@ public class AdminsEndpoint extends AbstractServlet
if (!HTTPDModule.getPermissions().playerHas(null, offlinePlayer, "plex.httpd.admins.access")) if (!HTTPDModule.getPermissions().playerHas(null, offlinePlayer, "plex.httpd.admins.access"))
{ {
// If the person doesn't have permission, don't return IPs // If the person doesn't have permission, don't return IPs
return new GsonBuilder().registerTypeAdapter(LocalDateTime.class, new LocalDateTimeSerializer()).setPrettyPrinting().create().toJson(Plex.get().getAdminList().getAllAdminPlayers().stream().peek(plexPlayer -> plexPlayer.setIps(Lists.newArrayList())).peek(plexPlayer -> plexPlayer.setPunishments(Lists.newArrayList())).collect(Collectors.toList())); return createJSONHTML(TITLE, new GsonBuilder().registerTypeAdapter(LocalDateTime.class, new LocalDateTimeSerializer()).setPrettyPrinting().create().toJson(Plex.get().getAdminList().getAllAdminPlayers().stream().peek(plexPlayer -> plexPlayer.setIps(Lists.newArrayList())).peek(plexPlayer -> plexPlayer.setPunishments(Lists.newArrayList())).collect(Collectors.toList())));
} }
} }
return new GsonBuilder().registerTypeAdapter(LocalDateTime.class, new LocalDateTimeSerializer()).setPrettyPrinting().create().toJson(Plex.get().getAdminList().getAllAdminPlayers()); return createJSONHTML(TITLE, new GsonBuilder().registerTypeAdapter(LocalDateTime.class, new LocalDateTimeSerializer()).setPrettyPrinting().create().toJson(Plex.get().getAdminList().getAllAdminPlayers()));
} }
} }

View File

@ -10,31 +10,32 @@ import dev.plex.request.AbstractServlet;
import dev.plex.request.GetMapping; import dev.plex.request.GetMapping;
import dev.plex.util.PlexLog; import dev.plex.util.PlexLog;
import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletRequest;
import java.util.UUID;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer; import org.bukkit.OfflinePlayer;
public class IndefBansEndpoint extends AbstractServlet public class IndefBansEndpoint extends AbstractServlet
{ {
private static final String TITLE = "Indefinite Bans - Plex HTTPD";
@GetMapping(endpoint = "/api/indefbans/") @GetMapping(endpoint = "/api/indefbans/")
public String getBans(HttpServletRequest request) public String getBans(HttpServletRequest request)
{ {
String ipAddress = request.getRemoteAddr(); String ipAddress = request.getRemoteAddr();
if (ipAddress == null) if (ipAddress == null)
{ {
return "An IP address could not be detected. Please ensure you are connecting using IPv4."; return createBasicHTML(TITLE, "An IP address could not be detected. Please ensure you are connecting using IPv4.");
} }
final PlexPlayer player = DataUtils.getPlayerByIP(ipAddress); final PlexPlayer player = DataUtils.getPlayerByIP(ipAddress);
if (player == null) if (player == null)
{ {
return "Couldn't load your IP Address: " + ipAddress + ". Have you joined the server before?"; return createBasicHTML(TITLE, "Couldn't load your IP Address: " + ipAddress + ". Have you joined the server before?");
} }
if (Plex.get().getSystem().equalsIgnoreCase("ranks")) if (Plex.get().getSystem().equalsIgnoreCase("ranks"))
{ {
PlexLog.debug("Plex-HTTPD using ranks check"); PlexLog.debug("Plex-HTTPD using ranks check");
if (!player.getRankFromString().isAtLeast(Rank.ADMIN)) if (!player.getRankFromString().isAtLeast(Rank.ADMIN))
{ {
return "Not a high enough rank to view this page."; return createBasicHTML(TITLE, "Not a high enough rank to view this page.");
} }
} }
else if (Plex.get().getSystem().equalsIgnoreCase("permissions")) else if (Plex.get().getSystem().equalsIgnoreCase("permissions"))
@ -43,9 +44,9 @@ public class IndefBansEndpoint extends AbstractServlet
final OfflinePlayer offlinePlayer = Bukkit.getOfflinePlayer(player.getUuid()); final OfflinePlayer offlinePlayer = Bukkit.getOfflinePlayer(player.getUuid());
if (!HTTPDModule.getPermissions().playerHas(null, offlinePlayer, "plex.httpd.indefbans.access")) if (!HTTPDModule.getPermissions().playerHas(null, offlinePlayer, "plex.httpd.indefbans.access"))
{ {
return "Not enough permissions to view this page."; return createBasicHTML(TITLE, "Not enough permissions to view this page.");
} }
} }
return new GsonBuilder().setPrettyPrinting().create().toJson(Plex.get().getPunishmentManager().getIndefiniteBans().stream().toList()); return createJSONHTML(TITLE, new GsonBuilder().setPrettyPrinting().create().toJson(Plex.get().getPunishmentManager().getIndefiniteBans().stream().toList()));
} }
} }

View File

@ -20,6 +20,6 @@ public class ListEndpoint extends AbstractServlet
{ {
players.add(player.getName()); players.add(player.getName());
} }
return new GsonBuilder().setPrettyPrinting().create().toJson(players.stream().toList()); return createJSONHTML("List - Plex HTTPD", new GsonBuilder().setPrettyPrinting().create().toJson(players.stream().toList()));
} }
} }

View File

@ -22,13 +22,15 @@ import org.bukkit.OfflinePlayer;
public class PunishmentsEndpoint extends AbstractServlet public class PunishmentsEndpoint extends AbstractServlet
{ {
private static final String TITLE = "Punishments - Plex HTTPD";
@GetMapping(endpoint = "/api/punishments/") @GetMapping(endpoint = "/api/punishments/")
public String getPunishments(HttpServletRequest request) public String getPunishments(HttpServletRequest request)
{ {
String ipAddress = request.getRemoteAddr(); String ipAddress = request.getRemoteAddr();
if (ipAddress == null) if (ipAddress == null)
{ {
return "An IP address could not be detected. Please ensure you are connecting using IPv4."; return createBasicHTML(TITLE, "An IP address could not be detected. Please ensure you are connecting using IPv4.");
} }
if (request.getPathInfo() == null) if (request.getPathInfo() == null)
{ {
@ -65,16 +67,16 @@ public class PunishmentsEndpoint extends AbstractServlet
final PlexPlayer player = DataUtils.getPlayerByIP(ipAddress); final PlexPlayer player = DataUtils.getPlayerByIP(ipAddress);
if (punishedPlayer == null) if (punishedPlayer == null)
{ {
return "This player has never joined the server before."; return createBasicHTML(TITLE, "This player has never joined the server before.");
} }
if (punishedPlayer.getPunishments().isEmpty()) if (punishedPlayer.getPunishments().isEmpty())
{ {
return "This player has been a good boy. They have no punishments!"; return createBasicHTML(TITLE, "This player has been a good boy. They have no punishments!");
} }
if (player == null) if (player == null)
{ {
// If the player is null, give it to them without the IPs // 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("")).toList()); return createJSONHTML(TITLE, new GsonBuilder().registerTypeAdapter(LocalDateTime.class, new LocalDateTimeSerializer()).setPrettyPrinting().create().toJson(punishedPlayer.getPunishments().stream().peek(punishment -> punishment.setIp("")).toList()));
} }
if (Plex.get().getSystem().equalsIgnoreCase("ranks")) if (Plex.get().getSystem().equalsIgnoreCase("ranks"))
{ {
@ -82,7 +84,7 @@ public class PunishmentsEndpoint extends AbstractServlet
if (!player.getRankFromString().isAtLeast(Rank.ADMIN)) if (!player.getRankFromString().isAtLeast(Rank.ADMIN))
{ {
// Don't return IPs either if the person is not an Admin or above. // 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("")).toList()); return createJSONHTML(TITLE, new GsonBuilder().registerTypeAdapter(LocalDateTime.class, new LocalDateTimeSerializer()).setPrettyPrinting().create().toJson(punishedPlayer.getPunishments().stream().peek(punishment -> punishment.setIp("")).toList()));
} }
} }
else if (Plex.get().getSystem().equalsIgnoreCase("permissions")) else if (Plex.get().getSystem().equalsIgnoreCase("permissions"))
@ -92,9 +94,9 @@ public class PunishmentsEndpoint extends AbstractServlet
if (!HTTPDModule.getPermissions().playerHas(null, offlinePlayer, "plex.httpd.punishments.access")) if (!HTTPDModule.getPermissions().playerHas(null, offlinePlayer, "plex.httpd.punishments.access"))
{ {
// If the person doesn't have permission, don't return IPs // 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("")).toList()); return createJSONHTML(TITLE, new GsonBuilder().registerTypeAdapter(LocalDateTime.class, new LocalDateTimeSerializer()).setPrettyPrinting().create().toJson(punishedPlayer.getPunishments().stream().peek(punishment -> punishment.setIp("")).toList()));
} }
} }
return new GsonBuilder().registerTypeAdapter(LocalDateTime.class, new LocalDateTimeSerializer()).setPrettyPrinting().create().toJson(punishedPlayer.getPunishments().stream().toList()); return createJSONHTML(TITLE, new GsonBuilder().registerTypeAdapter(LocalDateTime.class, new LocalDateTimeSerializer()).setPrettyPrinting().create().toJson(punishedPlayer.getPunishments().stream().toList()));
} }
} }