mirror of
https://github.com/plexusorg/Module-HTTPD.git
synced 2025-07-02 00:16:42 +00:00
Update Gradle etc
This commit is contained in:
@ -1,21 +1,25 @@
|
||||
package dev.plex;
|
||||
|
||||
import dev.plex.config.Config;
|
||||
import dev.plex.config.ModuleConfig;
|
||||
import dev.plex.module.PlexModule;
|
||||
import dev.plex.request.impl.GetEndpoints;
|
||||
import dev.plex.util.PlexLog;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
import lombok.Getter;
|
||||
import net.milkbowl.vault.permission.Permission;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.plugin.RegisteredServiceProvider;
|
||||
import org.eclipse.jetty.server.*;
|
||||
import org.eclipse.jetty.server.Connector;
|
||||
import org.eclipse.jetty.server.ForwardedRequestCustomizer;
|
||||
import org.eclipse.jetty.server.HttpConfiguration;
|
||||
import org.eclipse.jetty.server.HttpConnectionFactory;
|
||||
import org.eclipse.jetty.server.Server;
|
||||
import org.eclipse.jetty.server.ServerConnector;
|
||||
import org.eclipse.jetty.servlet.ServletContextHandler;
|
||||
import org.eclipse.jetty.servlet.ServletHandler;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
public class HTTPDModule extends PlexModule {
|
||||
public class HTTPDModule extends PlexModule
|
||||
{
|
||||
|
||||
public static ServletContextHandler context;
|
||||
private Thread serverThread;
|
||||
@ -27,14 +31,17 @@ public class HTTPDModule extends PlexModule {
|
||||
private ModuleConfig config;
|
||||
|
||||
@Override
|
||||
public void enable() {
|
||||
config = new ModuleConfig(this, "settings.yml");
|
||||
public void enable()
|
||||
{
|
||||
config = new ModuleConfig(this, "settings.yml");
|
||||
config.load();
|
||||
PlexLog.debug("HTTPD Module Port: {0}", config.getInt("server.port"));
|
||||
if (!setupPermissions() && getPlex().getSystem().equalsIgnoreCase("permissions") && !Bukkit.getPluginManager().isPluginEnabled("Vault")) {
|
||||
if (!setupPermissions() && getPlex().getSystem().equalsIgnoreCase("permissions") && !Bukkit.getPluginManager().isPluginEnabled("Vault"))
|
||||
{
|
||||
throw new RuntimeException("Plex-HTTPD requires the 'Vault' plugin as well as a Permissions plugin that hooks into 'Vault.' We recommend LuckPerms!");
|
||||
}
|
||||
serverThread = new Thread(() -> {
|
||||
serverThread = new Thread(() ->
|
||||
{
|
||||
Server server = new Server();
|
||||
ServletHandler servletHandler = new ServletHandler();
|
||||
|
||||
@ -53,10 +60,13 @@ public class HTTPDModule extends PlexModule {
|
||||
|
||||
atomicServer.set(server);
|
||||
PlexLog.debug("Set atomicServer value? {0}", atomicServer.get() != null);
|
||||
try {
|
||||
try
|
||||
{
|
||||
server.start();
|
||||
server.join();
|
||||
} catch (Exception e) {
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
}, "jetty-server");
|
||||
@ -64,17 +74,22 @@ public class HTTPDModule extends PlexModule {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void disable() {
|
||||
public void disable()
|
||||
{
|
||||
PlexLog.debug("Stopping jetty server");
|
||||
try {
|
||||
try
|
||||
{
|
||||
atomicServer.get().stop();
|
||||
atomicServer.get().destroy();
|
||||
} catch (Exception e) {
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private boolean setupPermissions() {
|
||||
private boolean setupPermissions()
|
||||
{
|
||||
RegisteredServiceProvider<Permission> rsp = Bukkit.getServicesManager().getRegistration(Permission.class);
|
||||
permissions = rsp.getProvider();
|
||||
return permissions != null;
|
||||
|
@ -7,26 +7,29 @@ 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;
|
||||
import lombok.Data;
|
||||
import org.eclipse.jetty.servlet.ServletHolder;
|
||||
|
||||
public class AbstractServlet extends HttpServlet {
|
||||
public class AbstractServlet extends HttpServlet
|
||||
{
|
||||
|
||||
private final List<Mapping> GET_MAPPINGS = Lists.newArrayList();
|
||||
|
||||
public AbstractServlet() {
|
||||
for (Method declaredMethod : this.getClass().getDeclaredMethods()) {
|
||||
public AbstractServlet()
|
||||
{
|
||||
for (Method declaredMethod : this.getClass().getDeclaredMethods())
|
||||
{
|
||||
declaredMethod.setAccessible(true);
|
||||
if (declaredMethod.isAnnotationPresent(GetMapping.class)) {
|
||||
if (declaredMethod.isAnnotationPresent(GetMapping.class))
|
||||
{
|
||||
GetMapping getMapping = declaredMethod.getAnnotation(GetMapping.class);
|
||||
Mapping mapping = new Mapping(declaredMethod, getMapping);
|
||||
if (declaredMethod.isAnnotationPresent(MappingHeaders.class)) {
|
||||
if (declaredMethod.isAnnotationPresent(MappingHeaders.class))
|
||||
{
|
||||
mapping.setHeaders(declaredMethod.getAnnotation(MappingHeaders.class));
|
||||
}
|
||||
GET_MAPPINGS.add(mapping);
|
||||
@ -37,12 +40,14 @@ public class AbstractServlet extends HttpServlet {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
||||
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) {
|
||||
if (ipAddress == null)
|
||||
{
|
||||
ipAddress = req.getRemoteAddr();
|
||||
}
|
||||
PlexLog.debug("HTTP Remote IP: " + ipAddress);
|
||||
@ -55,19 +60,25 @@ public class AbstractServlet extends HttpServlet {
|
||||
}*/
|
||||
|
||||
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()) {
|
||||
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 {
|
||||
try
|
||||
{
|
||||
Object object = mapping.method.invoke(this, req);
|
||||
resp.getWriter().println(object.toString());
|
||||
} catch (IOException | IllegalAccessException | InvocationTargetException e) {
|
||||
}
|
||||
catch (IOException | IllegalAccessException | InvocationTargetException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
});
|
||||
@ -75,7 +86,8 @@ public class AbstractServlet extends HttpServlet {
|
||||
|
||||
|
||||
@Data
|
||||
public class Mapping {
|
||||
public class Mapping
|
||||
{
|
||||
private final Method method;
|
||||
private final GetMapping mapping;
|
||||
private MappingHeaders headers;
|
||||
|
@ -6,8 +6,7 @@ import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface GetMapping {
|
||||
|
||||
public @interface GetMapping
|
||||
{
|
||||
String endpoint();
|
||||
|
||||
}
|
||||
|
@ -4,8 +4,7 @@ import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface MappingHeaders {
|
||||
|
||||
public @interface MappingHeaders
|
||||
{
|
||||
String[] headers();
|
||||
|
||||
}
|
||||
|
@ -6,40 +6,48 @@ 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;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
|
||||
//@RestController
|
||||
//@RequestMapping("/api/admins")
|
||||
public class GetEndpoints extends AbstractServlet {
|
||||
|
||||
public class GetEndpoints extends AbstractServlet
|
||||
{
|
||||
@GetMapping(endpoint = "/api/admins/")
|
||||
public String getAdmins(HttpServletRequest request) {
|
||||
public String getAdmins(HttpServletRequest request)
|
||||
{
|
||||
String ipAddress = request.getHeader("X-FORWARDED-FOR");
|
||||
if (ipAddress == null) {
|
||||
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")) {
|
||||
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)) {
|
||||
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")) {
|
||||
}
|
||||
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")) {
|
||||
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()));
|
||||
}
|
||||
}
|
||||
@ -47,22 +55,32 @@ public class GetEndpoints extends AbstractServlet {
|
||||
}
|
||||
|
||||
@GetMapping(endpoint = "/api/indefbans/")
|
||||
public String getBans(HttpServletRequest request) {
|
||||
public String getBans(HttpServletRequest request)
|
||||
{
|
||||
String ipAddress = request.getHeader("X-FORWARDED-FOR");
|
||||
if (ipAddress == null) {
|
||||
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")) {
|
||||
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)) {
|
||||
if (!player.getRankFromString().isAtLeast(Rank.ADMIN))
|
||||
{
|
||||
return "Not a high enough rank to view this page.";
|
||||
}
|
||||
} else if (Plex.get().getSystem().equalsIgnoreCase("permissions")) {
|
||||
}
|
||||
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")) {
|
||||
if (!HTTPDModule.getPermissions().playerHas(null, offlinePlayer, "plex.httpd.indefbans.access"))
|
||||
{
|
||||
return "Not enough permissions to view this page.";
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
name: Plex-HTTPD
|
||||
version: 0.1
|
||||
version: 1.0
|
||||
description: HTTPD server for Plex
|
||||
main: dev.plex.HTTPDModule
|
Reference in New Issue
Block a user