diff --git a/src/main/java/dev/plex/HTTPDModule.java b/src/main/java/dev/plex/HTTPDModule.java index e50f3cb..2cdb9bb 100644 --- a/src/main/java/dev/plex/HTTPDModule.java +++ b/src/main/java/dev/plex/HTTPDModule.java @@ -4,6 +4,7 @@ import dev.plex.config.ModuleConfig; import dev.plex.module.PlexModule; import dev.plex.request.impl.AdminsEndpoint; import dev.plex.request.impl.IndefBansEndpoint; +import dev.plex.request.impl.IndexEndpoint; import dev.plex.request.impl.ListEndpoint; import dev.plex.request.impl.PunishmentsEndpoint; import dev.plex.util.PlexLog; @@ -63,6 +64,7 @@ public class HTTPDModule extends PlexModule new AdminsEndpoint(); new IndefBansEndpoint(); + new IndexEndpoint(); new ListEndpoint(); new PunishmentsEndpoint(); diff --git a/src/main/java/dev/plex/logging/Log.java b/src/main/java/dev/plex/logging/Log.java index 99d5360..7579fa0 100644 --- a/src/main/java/dev/plex/logging/Log.java +++ b/src/main/java/dev/plex/logging/Log.java @@ -4,7 +4,6 @@ import dev.plex.HTTPDModule; import org.bukkit.Bukkit; import org.bukkit.ChatColor; - public class Log { public static void log(String message, Object... strings) diff --git a/src/main/java/dev/plex/request/AbstractServlet.java b/src/main/java/dev/plex/request/AbstractServlet.java index 3f97bc7..82b092b 100644 --- a/src/main/java/dev/plex/request/AbstractServlet.java +++ b/src/main/java/dev/plex/request/AbstractServlet.java @@ -3,15 +3,18 @@ package dev.plex.request; import com.google.common.collect.Lists; import dev.plex.HTTPDModule; import dev.plex.logging.Log; -import jakarta.servlet.AsyncContext; import jakarta.servlet.ServletException; import jakarta.servlet.http.HttpServlet; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; +import java.io.BufferedReader; import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.List; +import java.util.Objects; import lombok.Data; import org.eclipse.jetty.servlet.ServletHolder; @@ -79,14 +82,23 @@ public class AbstractServlet extends HttpServlet }); } - public String createBasicHTML(String title, String body) + public String readFile(InputStream filename) { - return "
" + json + "
";
+ StringBuilder contentBuilder = new StringBuilder();
+ try
+ {
+ BufferedReader in = new BufferedReader(new InputStreamReader(Objects.requireNonNull(filename)));
+ String str;
+ while ((str = in.readLine()) != null)
+ {
+ contentBuilder.append(str);
+ }
+ in.close();
+ }
+ catch (IOException ignored)
+ {
+ }
+ return contentBuilder.toString();
}
@Data
diff --git a/src/main/java/dev/plex/request/impl/AdminsEndpoint.java b/src/main/java/dev/plex/request/impl/AdminsEndpoint.java
index fc39e87..0331013 100644
--- a/src/main/java/dev/plex/request/impl/AdminsEndpoint.java
+++ b/src/main/java/dev/plex/request/impl/AdminsEndpoint.java
@@ -27,13 +27,13 @@ public class AdminsEndpoint extends AbstractServlet
String ipAddress = request.getRemoteAddr();
if (ipAddress == null)
{
- return createBasicHTML(TITLE, "An IP address could not be detected. Please ensure you are connecting using IPv4.");
+ return adminsHTML("An IP address could not be detected. Please ensure you are connecting using IPv4.");
}
final PlexPlayer player = DataUtils.getPlayerByIP(ipAddress);
if (player == null)
{
// This likely means they've never joined the server before. That's okay. We can just not return IPs.
- 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().stream().peek(plexPlayer -> plexPlayer.setIps(Lists.newArrayList())).peek(plexPlayer -> plexPlayer.setPunishments(Lists.newArrayList())).collect(Collectors.toList()));
}
if (Plex.get().getSystem().equalsIgnoreCase("ranks"))
{
@@ -41,7 +41,7 @@ public class AdminsEndpoint extends AbstractServlet
if (!player.getRankFromString().isAtLeast(Rank.ADMIN))
{
// Don't return IPs either if the person is not an Admin or above.
- 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().stream().peek(plexPlayer -> plexPlayer.setIps(Lists.newArrayList())).peek(plexPlayer -> plexPlayer.setPunishments(Lists.newArrayList())).collect(Collectors.toList()));
}
}
else if (Plex.get().getSystem().equalsIgnoreCase("permissions"))
@@ -51,9 +51,16 @@ public class AdminsEndpoint extends AbstractServlet
if (!HTTPDModule.getPermissions().playerHas(null, offlinePlayer, "plex.httpd.admins.access"))
{
// If the person doesn't have permission, don't return IPs
- 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().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()));
+ return new GsonBuilder().registerTypeAdapter(LocalDateTime.class, new LocalDateTimeSerializer()).setPrettyPrinting().create().toJson(Plex.get().getAdminList().getAllAdminPlayers());
+ }
+
+ private String adminsHTML(String message)
+ {
+ String file = readFile(this.getClass().getResourceAsStream("/httpd/admins.html"));
+ file = file.replace("${MESSAGE}", message);
+ return file;
}
}
diff --git a/src/main/java/dev/plex/request/impl/IndefBansEndpoint.java b/src/main/java/dev/plex/request/impl/IndefBansEndpoint.java
index 0db3a24..e35637f 100644
--- a/src/main/java/dev/plex/request/impl/IndefBansEndpoint.java
+++ b/src/main/java/dev/plex/request/impl/IndefBansEndpoint.java
@@ -23,19 +23,19 @@ public class IndefBansEndpoint extends AbstractServlet
String ipAddress = request.getRemoteAddr();
if (ipAddress == null)
{
- return createBasicHTML(TITLE, "An IP address could not be detected. Please ensure you are connecting using IPv4.");
+ return indefbansHTML("An IP address could not be detected. Please ensure you are connecting using IPv4.");
}
final PlexPlayer player = DataUtils.getPlayerByIP(ipAddress);
if (player == null)
{
- return createBasicHTML(TITLE, "Couldn't load your IP Address: " + ipAddress + ". Have you joined the server before?");
+ return indefbansHTML("Couldn't load your IP Address: " + ipAddress + ". Have you joined the server before?");
}
if (Plex.get().getSystem().equalsIgnoreCase("ranks"))
{
PlexLog.debug("Plex-HTTPD using ranks check");
if (!player.getRankFromString().isAtLeast(Rank.ADMIN))
{
- return createBasicHTML(TITLE, "Not a high enough rank to view this page.");
+ return indefbansHTML("Not a high enough rank to view this page.");
}
}
else if (Plex.get().getSystem().equalsIgnoreCase("permissions"))
@@ -44,9 +44,16 @@ public class IndefBansEndpoint extends AbstractServlet
final OfflinePlayer offlinePlayer = Bukkit.getOfflinePlayer(player.getUuid());
if (!HTTPDModule.getPermissions().playerHas(null, offlinePlayer, "plex.httpd.indefbans.access"))
{
- return createBasicHTML(TITLE, "Not enough permissions to view this page.");
+ return indefbansHTML("Not enough permissions to view this page.");
}
}
- return createJSONHTML(TITLE, new GsonBuilder().setPrettyPrinting().create().toJson(Plex.get().getPunishmentManager().getIndefiniteBans().stream().toList()));
+ return new GsonBuilder().setPrettyPrinting().create().toJson(Plex.get().getPunishmentManager().getIndefiniteBans().stream().toList());
+ }
+
+ private String indefbansHTML(String message)
+ {
+ String file = readFile(this.getClass().getResourceAsStream("/httpd/indefbans.html"));
+ file = file.replace("${MESSAGE}", message);
+ return file;
}
}
diff --git a/src/main/java/dev/plex/request/impl/IndexEndpoint.java b/src/main/java/dev/plex/request/impl/IndexEndpoint.java
new file mode 100644
index 0000000..edb66f9
--- /dev/null
+++ b/src/main/java/dev/plex/request/impl/IndexEndpoint.java
@@ -0,0 +1,33 @@
+package dev.plex.request.impl;
+
+import dev.plex.request.AbstractServlet;
+import dev.plex.request.GetMapping;
+import jakarta.servlet.http.HttpServletRequest;
+import org.bukkit.Bukkit;
+
+public class IndexEndpoint extends AbstractServlet
+{
+ @GetMapping(endpoint = "//")
+ public String getIndex(HttpServletRequest request)
+ {
+ return indexHTML();
+ }
+
+ @GetMapping(endpoint = "/api/")
+ public String getAPI(HttpServletRequest request)
+ {
+ return indexHTML();
+ }
+
+ private String indexHTML()
+ {
+ String file = readFile(this.getClass().getResourceAsStream("/httpd/index.html"));
+ String isAre = Bukkit.getOnlinePlayers().size() == 1 ? " is " : " are ";
+ String pluralOnline = Bukkit.getOnlinePlayers().size() == 1 ? " player " : " players ";
+ String pluralMax = Bukkit.getMaxPlayers() == 1 ? " player " : " players ";
+ file = file.replace("${is_are}", isAre);
+ file = file.replace("${server_online_players}", Bukkit.getOnlinePlayers().size() + pluralOnline);
+ file = file.replace("${server_total_players}", Bukkit.getMaxPlayers() + pluralMax);
+ return file;
+ }
+}
diff --git a/src/main/java/dev/plex/request/impl/ListEndpoint.java b/src/main/java/dev/plex/request/impl/ListEndpoint.java
index 3c33a82..8b6fcf5 100644
--- a/src/main/java/dev/plex/request/impl/ListEndpoint.java
+++ b/src/main/java/dev/plex/request/impl/ListEndpoint.java
@@ -20,6 +20,6 @@ public class ListEndpoint extends AbstractServlet
{
players.add(player.getName());
}
- return createJSONHTML("List - Plex HTTPD", new GsonBuilder().setPrettyPrinting().create().toJson(players.stream().toList()));
+ return new GsonBuilder().setPrettyPrinting().create().toJson(players.stream().toList());
}
}
diff --git a/src/main/java/dev/plex/request/impl/PunishmentsEndpoint.java b/src/main/java/dev/plex/request/impl/PunishmentsEndpoint.java
index 535c63b..0db0fd8 100644
--- a/src/main/java/dev/plex/request/impl/PunishmentsEndpoint.java
+++ b/src/main/java/dev/plex/request/impl/PunishmentsEndpoint.java
@@ -11,44 +11,24 @@ import dev.plex.request.GetMapping;
import dev.plex.util.PlexLog;
import dev.plex.util.adapter.LocalDateTimeSerializer;
import jakarta.servlet.http.HttpServletRequest;
-import java.io.BufferedReader;
-import java.io.IOException;
-import java.io.InputStreamReader;
import java.time.LocalDateTime;
-import java.util.Objects;
import java.util.UUID;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
public class PunishmentsEndpoint extends AbstractServlet
{
- private static final String TITLE = "Punishments - Plex HTTPD";
-
@GetMapping(endpoint = "/api/punishments/")
public String getPunishments(HttpServletRequest request)
{
String ipAddress = request.getRemoteAddr();
if (ipAddress == null)
{
- return createBasicHTML(TITLE, "An IP address could not be detected. Please ensure you are connecting using IPv4.");
+ return punishmentsHTML("An IP address could not be detected. Please ensure you are connecting using IPv4.");
}
- if (request.getPathInfo() == null)
+ if (request.getPathInfo() == null || request.getPathInfo().equals("/"))
{
- StringBuilder contentBuilder = new StringBuilder();
- try
- {
- BufferedReader in = new BufferedReader(new InputStreamReader(Objects.requireNonNull(this.getClass().getResourceAsStream("/httpd/punishments.html"))));
- String str;
- while ((str = in.readLine()) != null)
- {
- contentBuilder.append(str);
- }
- in.close();
- }
- catch (IOException ignored)
- {
- }
- return contentBuilder.toString();
+ return readFile(this.getClass().getResourceAsStream("/httpd/punishments.html"));
}
UUID pathUUID;
String pathPlexPlayer;
@@ -67,16 +47,16 @@ public class PunishmentsEndpoint extends AbstractServlet
final PlexPlayer player = DataUtils.getPlayerByIP(ipAddress);
if (punishedPlayer == null)
{
- return createBasicHTML(TITLE, "This player has never joined the server before.");
+ return punishmentsHTML("This player has never joined the server before.");
}
if (punishedPlayer.getPunishments().isEmpty())
{
- return createBasicHTML(TITLE, "This player has been a good boy. They have no punishments!");
+ return punishmentsGoodHTML("This player has been a good boy. They have no punishments!");
}
if (player == null)
{
// If the player is null, give it to them without the IPs
- 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().peek(punishment -> punishment.setIp("")).toList());
}
if (Plex.get().getSystem().equalsIgnoreCase("ranks"))
{
@@ -84,7 +64,7 @@ public class PunishmentsEndpoint extends AbstractServlet
if (!player.getRankFromString().isAtLeast(Rank.ADMIN))
{
// Don't return IPs either if the person is not an Admin or above.
- 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().peek(punishment -> punishment.setIp("")).toList());
}
}
else if (Plex.get().getSystem().equalsIgnoreCase("permissions"))
@@ -94,9 +74,23 @@ public class PunishmentsEndpoint extends AbstractServlet
if (!HTTPDModule.getPermissions().playerHas(null, offlinePlayer, "plex.httpd.punishments.access"))
{
// If the person doesn't have permission, don't return IPs
- 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().peek(punishment -> punishment.setIp("")).toList());
}
}
- return createJSONHTML(TITLE, new GsonBuilder().registerTypeAdapter(LocalDateTime.class, new LocalDateTimeSerializer()).setPrettyPrinting().create().toJson(punishedPlayer.getPunishments().stream().toList()));
+ return new GsonBuilder().registerTypeAdapter(LocalDateTime.class, new LocalDateTimeSerializer()).setPrettyPrinting().create().toJson(punishedPlayer.getPunishments().stream().toList());
+ }
+
+ private String punishmentsHTML(String message)
+ {
+ String file = readFile(this.getClass().getResourceAsStream("/httpd/punishments_error.html"));
+ file = file.replace("${MESSAGE}", message);
+ return file;
+ }
+
+ private String punishmentsGoodHTML(String message)
+ {
+ String file = readFile(this.getClass().getResourceAsStream("/httpd/punishments_good.html"));
+ file = file.replace("${MESSAGE}", message);
+ return file;
}
}
diff --git a/src/main/resources/httpd/admins.html b/src/main/resources/httpd/admins.html
new file mode 100644
index 0000000..3024658
--- /dev/null
+++ b/src/main/resources/httpd/admins.html
@@ -0,0 +1,46 @@
+
+
+
+
+
+
+