Punishments somewhat working

This commit is contained in:
Telesphoreo 2022-04-02 18:34:19 -05:00
parent 1096ed1e09
commit 3dbc5e78bd
4 changed files with 105 additions and 4 deletions

View file

@ -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);

View file

@ -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
{

View file

@ -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()));
}
}

View file

@ -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;
}
}