mirror of
https://github.com/plexusorg/Module-HTTPD.git
synced 2025-07-01 07:56:43 +00:00
httpd module
This commit is contained in:
83
src/main/java/dev/plex/request/AbstractServlet.java
Normal file
83
src/main/java/dev/plex/request/AbstractServlet.java
Normal file
@ -0,0 +1,83 @@
|
||||
package dev.plex.request;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import dev.plex.HTTPDModule;
|
||||
import dev.plex.util.PlexLog;
|
||||
import jakarta.servlet.ServletException;
|
||||
import jakarta.servlet.http.HttpServlet;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import lombok.Data;
|
||||
import org.eclipse.jetty.servlet.ServletHolder;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Enumeration;
|
||||
import java.util.List;
|
||||
|
||||
public class AbstractServlet extends HttpServlet {
|
||||
|
||||
private final List<Mapping> GET_MAPPINGS = Lists.newArrayList();
|
||||
|
||||
public AbstractServlet() {
|
||||
for (Method declaredMethod : this.getClass().getDeclaredMethods()) {
|
||||
declaredMethod.setAccessible(true);
|
||||
if (declaredMethod.isAnnotationPresent(GetMapping.class)) {
|
||||
GetMapping getMapping = declaredMethod.getAnnotation(GetMapping.class);
|
||||
Mapping mapping = new Mapping(declaredMethod, getMapping);
|
||||
if (declaredMethod.isAnnotationPresent(MappingHeaders.class)) {
|
||||
mapping.setHeaders(declaredMethod.getAnnotation(MappingHeaders.class));
|
||||
}
|
||||
GET_MAPPINGS.add(mapping);
|
||||
ServletHolder holder = new ServletHolder(this);
|
||||
HTTPDModule.context.addServlet(holder, getMapping.endpoint() + "*");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
||||
|
||||
PlexLog.debug("Context Path: " + req.getHttpServletMapping().getMatchValue());
|
||||
|
||||
String ipAddress = req.getHeader("X-FORWARDED-FOR");
|
||||
if (ipAddress == null) {
|
||||
ipAddress = req.getRemoteAddr();
|
||||
}
|
||||
PlexLog.debug("HTTP Remote IP: " + ipAddress);
|
||||
PlexLog.debug("HTTP Local IP: " + req.getLocalAddr());
|
||||
|
||||
/*Enumeration<String> headerz = req.getHeaderNames();
|
||||
while (headerz.hasMoreElements()) {
|
||||
String header = headerz.nextElement();
|
||||
PlexLog.debug("Header: {0} Value {1}", header, req.getHeader(header));
|
||||
}*/
|
||||
|
||||
PlexLog.debug("-------------------------");
|
||||
GET_MAPPINGS.stream().filter(mapping -> mapping.getMapping().endpoint().substring(1, mapping.getMapping().endpoint().length() - 1).equalsIgnoreCase(req.getHttpServletMapping().getMatchValue())).forEach(mapping -> {
|
||||
if (mapping.headers != null) {
|
||||
for (String headers : mapping.headers.headers()) {
|
||||
String header = headers.split(";")[0];
|
||||
String value = headers.split(";")[1];
|
||||
resp.addHeader(header, value);
|
||||
}
|
||||
}
|
||||
resp.setStatus(HttpServletResponse.SC_OK);
|
||||
try {
|
||||
Object object = mapping.method.invoke(this, req);
|
||||
resp.getWriter().println(object.toString());
|
||||
} catch (IOException | IllegalAccessException | InvocationTargetException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@Data
|
||||
public class Mapping {
|
||||
private final Method method;
|
||||
private final GetMapping mapping;
|
||||
private MappingHeaders headers;
|
||||
}
|
||||
}
|
13
src/main/java/dev/plex/request/GetMapping.java
Normal file
13
src/main/java/dev/plex/request/GetMapping.java
Normal file
@ -0,0 +1,13 @@
|
||||
package dev.plex.request;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface GetMapping {
|
||||
|
||||
String endpoint();
|
||||
|
||||
}
|
11
src/main/java/dev/plex/request/MappingHeaders.java
Normal file
11
src/main/java/dev/plex/request/MappingHeaders.java
Normal file
@ -0,0 +1,11 @@
|
||||
package dev.plex.request;
|
||||
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface MappingHeaders {
|
||||
|
||||
String[] headers();
|
||||
|
||||
}
|
71
src/main/java/dev/plex/request/impl/GetEndpoints.java
Normal file
71
src/main/java/dev/plex/request/impl/GetEndpoints.java
Normal file
@ -0,0 +1,71 @@
|
||||
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.punishment.PunishmentManager;
|
||||
import dev.plex.rank.enums.Rank;
|
||||
import dev.plex.request.AbstractServlet;
|
||||
import dev.plex.request.GetMapping;
|
||||
import dev.plex.util.PlexLog;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.UUID;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
//@RestController
|
||||
//@RequestMapping("/api/admins")
|
||||
public class GetEndpoints extends AbstractServlet {
|
||||
|
||||
@GetMapping(endpoint = "/api/admins/")
|
||||
public String getAdmins(HttpServletRequest request) {
|
||||
String ipAddress = request.getHeader("X-FORWARDED-FOR");
|
||||
if (ipAddress == null) {
|
||||
ipAddress = request.getRemoteAddr();
|
||||
}
|
||||
final PlexPlayer player = DataUtils.getPlayerByIP(ipAddress);
|
||||
if (player == null) return "Couldn't load your IP Address: " + ipAddress + ". Check if your SSL settings are setup correctly.";
|
||||
if (Plex.get().getSystem().equalsIgnoreCase("ranks")) {
|
||||
PlexLog.debug("Plex-HTTPD using ranks check");
|
||||
if (!player.getRankFromString().isAtLeast(Rank.ADMIN)) {
|
||||
return new GsonBuilder().setPrettyPrinting().create().toJson(Plex.get().getAdminList().getAllAdminPlayers().stream().peek(plexPlayer -> plexPlayer.setIps(Lists.newArrayList())).collect(Collectors.toList()));
|
||||
}
|
||||
} 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.indefbans.access")) {
|
||||
return new GsonBuilder().setPrettyPrinting().create().toJson(Plex.get().getAdminList().getAllAdminPlayers().stream().peek(plexPlayer -> plexPlayer.setIps(Lists.newArrayList())).collect(Collectors.toList()));
|
||||
}
|
||||
}
|
||||
return new GsonBuilder().setPrettyPrinting().create().toJson(new ArrayList<>(Plex.get().getAdminList().getAllAdminPlayers()));
|
||||
}
|
||||
|
||||
@GetMapping(endpoint = "/api/indefbans/")
|
||||
public String getBans(HttpServletRequest request) {
|
||||
String ipAddress = request.getHeader("X-FORWARDED-FOR");
|
||||
if (ipAddress == null) {
|
||||
ipAddress = request.getRemoteAddr();
|
||||
}
|
||||
final PlexPlayer player = DataUtils.getPlayerByIP(ipAddress);
|
||||
if (player == null) return "Couldn't load your IP Address: " + ipAddress + ". Check if your SSL settings are setup correctly.";
|
||||
if (Plex.get().getSystem().equalsIgnoreCase("ranks")) {
|
||||
PlexLog.debug("Plex-HTTPD using ranks check");
|
||||
if (!player.getRankFromString().isAtLeast(Rank.ADMIN)) {
|
||||
return "Not a high enough rank to view this page.";
|
||||
}
|
||||
} 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.indefbans.access")) {
|
||||
return "Not enough permissions to view this page.";
|
||||
}
|
||||
}
|
||||
return new GsonBuilder().setPrettyPrinting().create().toJson(new ArrayList<>(Plex.get().getPunishmentManager().getIndefiniteBans().stream().toList()));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user