|
| 1 | +package dev.plex.request; |
| 2 | + |
| 3 | +import dev.plex.Plex; |
| 4 | +import dev.plex.authentication.AuthenticatedUser; |
| 5 | +import dev.plex.cache.DataUtils; |
| 6 | +import dev.plex.logging.Log; |
| 7 | +import dev.plex.player.PlexPlayer; |
| 8 | +import dev.plex.punishment.Punishment; |
| 9 | +import dev.plex.punishment.PunishmentType; |
| 10 | +import dev.plex.util.BungeeUtil; |
| 11 | +import dev.plex.util.TimeUtils; |
| 12 | +import jakarta.servlet.ServletException; |
| 13 | +import jakarta.servlet.http.HttpServlet; |
| 14 | +import jakarta.servlet.http.HttpServletRequest; |
| 15 | +import jakarta.servlet.http.HttpServletResponse; |
| 16 | +import org.bukkit.Bukkit; |
| 17 | +import org.bukkit.entity.Player; |
| 18 | + |
| 19 | +import java.io.IOException; |
| 20 | +import java.time.ZoneId; |
| 21 | +import java.time.ZonedDateTime; |
| 22 | +import java.util.List; |
| 23 | +import java.util.UUID; |
| 24 | + |
| 25 | +public class PlayerActionServlet extends HttpServlet |
| 26 | +{ |
| 27 | + private static final long FAR_FUTURE_DAYS = 365L * 50L; |
| 28 | + private static final List<String> PERMANENT_ACTIONS = List.of("ban", "mute"); |
| 29 | + private static final List<String> TEMP_ACTIONS = List.of("tempban", "tempmute", "freeze"); |
| 30 | + |
| 31 | + @Override |
| 32 | + protected void doPost(HttpServletRequest request, HttpServletResponse response) |
| 33 | + throws ServletException, IOException |
| 34 | + { |
| 35 | + AuthenticatedUser staff = AbstractServlet.currentStaff(request); |
| 36 | + if (staff == null) |
| 37 | + { |
| 38 | + response.setStatus(HttpServletResponse.SC_FORBIDDEN); |
| 39 | + response.getWriter().write("Not authorized."); |
| 40 | + return; |
| 41 | + } |
| 42 | + |
| 43 | + String uuidStr = request.getParameter("uuid"); |
| 44 | + String action = request.getParameter("action"); |
| 45 | + String reason = request.getParameter("reason"); |
| 46 | + String durationStr = request.getParameter("duration"); |
| 47 | + |
| 48 | + if (uuidStr == null || action == null) |
| 49 | + { |
| 50 | + response.setStatus(HttpServletResponse.SC_BAD_REQUEST); |
| 51 | + response.getWriter().write("Missing parameters."); |
| 52 | + return; |
| 53 | + } |
| 54 | + if (!PERMANENT_ACTIONS.contains(action) && !TEMP_ACTIONS.contains(action)) |
| 55 | + { |
| 56 | + response.setStatus(HttpServletResponse.SC_BAD_REQUEST); |
| 57 | + response.getWriter().write("Unknown action."); |
| 58 | + return; |
| 59 | + } |
| 60 | + |
| 61 | + UUID uuid; |
| 62 | + try |
| 63 | + { |
| 64 | + uuid = UUID.fromString(uuidStr); |
| 65 | + } |
| 66 | + catch (IllegalArgumentException e) |
| 67 | + { |
| 68 | + response.setStatus(HttpServletResponse.SC_BAD_REQUEST); |
| 69 | + response.getWriter().write("Bad UUID."); |
| 70 | + return; |
| 71 | + } |
| 72 | + |
| 73 | + PlexPlayer target = DataUtils.getPlayer(uuid); |
| 74 | + if (target == null) |
| 75 | + { |
| 76 | + response.setStatus(HttpServletResponse.SC_NOT_FOUND); |
| 77 | + response.getWriter().write("Player not found."); |
| 78 | + return; |
| 79 | + } |
| 80 | + |
| 81 | + String safeReason = (reason == null || reason.isBlank()) ? "No reason provided" : reason.trim(); |
| 82 | + if (safeReason.length() > 500) safeReason = safeReason.substring(0, 500); |
| 83 | + |
| 84 | + PunishmentType type = mapType(action); |
| 85 | + ZonedDateTime now = ZonedDateTime.now(ZoneId.of(TimeUtils.TIMEZONE)); |
| 86 | + ZonedDateTime endDate = TEMP_ACTIONS.contains(action) |
| 87 | + ? now.plusSeconds(parseDurationSeconds(durationStr)) |
| 88 | + : now.plusDays(FAR_FUTURE_DAYS); |
| 89 | + |
| 90 | + Punishment punishment = new Punishment(uuid, null); |
| 91 | + punishment.setType(type); |
| 92 | + punishment.setReason(safeReason); |
| 93 | + punishment.setPunishedUsername(target.getName()); |
| 94 | + punishment.setPunisherName("xf:" + staff.username()); |
| 95 | + punishment.setEndDate(endDate); |
| 96 | + punishment.setCustomTime(TEMP_ACTIONS.contains(action)); |
| 97 | + punishment.setActive(true); |
| 98 | + List<String> ips = target.getIps(); |
| 99 | + if (ips != null && !ips.isEmpty()) punishment.setIp(ips.getLast()); |
| 100 | + |
| 101 | + String ipAddress = request.getRemoteAddr(); |
| 102 | + if ("127.0.0.1".equals(ipAddress)) |
| 103 | + { |
| 104 | + String forwarded = request.getHeader("X-FORWARDED-FOR"); |
| 105 | + if (forwarded != null) ipAddress = forwarded; |
| 106 | + } |
| 107 | + Log.log(ipAddress + " (xf:" + staff.username() + ") issued " + action + " on " + target.getName() + " (" + uuid + ")"); |
| 108 | + |
| 109 | + final boolean kick = action.equals("ban") || action.equals("tempban"); |
| 110 | + final Punishment toApply = punishment; |
| 111 | + Bukkit.getScheduler().runTask(Plex.get(), () -> |
| 112 | + { |
| 113 | + try |
| 114 | + { |
| 115 | + Plex.get().getPunishmentManager().punish(target, toApply); |
| 116 | + } |
| 117 | + catch (Throwable t) |
| 118 | + { |
| 119 | + t.printStackTrace(); |
| 120 | + return; |
| 121 | + } |
| 122 | + if (kick) |
| 123 | + { |
| 124 | + Player online = Bukkit.getPlayer(uuid); |
| 125 | + if (online != null) |
| 126 | + { |
| 127 | + try { BungeeUtil.kickPlayer(online, Punishment.generateBanMessage(toApply)); } |
| 128 | + catch (Throwable t) { t.printStackTrace(); } |
| 129 | + } |
| 130 | + } |
| 131 | + }); |
| 132 | + |
| 133 | + response.sendRedirect("/player/" + uuid); |
| 134 | + } |
| 135 | + |
| 136 | + private static PunishmentType mapType(String action) |
| 137 | + { |
| 138 | + return switch (action) |
| 139 | + { |
| 140 | + case "ban" -> PunishmentType.BAN; |
| 141 | + case "tempban" -> PunishmentType.TEMPBAN; |
| 142 | + case "mute", "tempmute" -> PunishmentType.MUTE; |
| 143 | + case "freeze" -> PunishmentType.FREEZE; |
| 144 | + default -> throw new IllegalArgumentException("unknown action: " + action); |
| 145 | + }; |
| 146 | + } |
| 147 | + |
| 148 | + private static long parseDurationSeconds(String s) |
| 149 | + { |
| 150 | + if (s == null || s.length() < 2) return 24L * 3600L; |
| 151 | + char unit = s.charAt(s.length() - 1); |
| 152 | + long n; |
| 153 | + try { n = Long.parseLong(s.substring(0, s.length() - 1)); } |
| 154 | + catch (NumberFormatException e) { return 24L * 3600L; } |
| 155 | + if (n <= 0) return 24L * 3600L; |
| 156 | + return switch (unit) |
| 157 | + { |
| 158 | + case 'm' -> Math.min(n, 60L * 24L * 365L) * 60L; |
| 159 | + case 'h' -> Math.min(n, 24L * 365L) * 3600L; |
| 160 | + case 'd' -> Math.min(n, 365L * 50L) * 86400L; |
| 161 | + default -> 24L * 3600L; |
| 162 | + }; |
| 163 | + } |
| 164 | +} |
0 commit comments