Organize a few things

This commit is contained in:
Telesphoreo 2022-04-02 15:51:14 -05:00
parent e0fbe479c5
commit f41d09d7bf
6 changed files with 92 additions and 55 deletions

View file

@ -2,7 +2,8 @@ package dev.plex;
import dev.plex.config.ModuleConfig; import dev.plex.config.ModuleConfig;
import dev.plex.module.PlexModule; import dev.plex.module.PlexModule;
import dev.plex.request.impl.GetEndpoints; import dev.plex.request.impl.AdminsEndpoint;
import dev.plex.request.impl.IndefBansEndpoint;
import dev.plex.util.PlexLog; import dev.plex.util.PlexLog;
import java.util.concurrent.atomic.AtomicReference; import java.util.concurrent.atomic.AtomicReference;
import lombok.Getter; import lombok.Getter;
@ -27,12 +28,11 @@ public class HTTPDModule extends PlexModule
@Getter @Getter
private static Permission permissions = null; private static Permission permissions = null;
private ModuleConfig moduleConfig; public static ModuleConfig moduleConfig;
@Override @Override
public void load() public void load()
{ {
moduleConfig = new ModuleConfig(this, "settings.yml"); moduleConfig = new ModuleConfig(this, "settings.yml");
} }
@ -58,13 +58,13 @@ public class HTTPDModule extends PlexModule
connector.setHost(moduleConfig.getString("server.bind-address")); connector.setHost(moduleConfig.getString("server.bind-address"));
connector.setPort(moduleConfig.getInt("server.port")); connector.setPort(moduleConfig.getInt("server.port"));
new GetEndpoints(); new AdminsEndpoint();
new IndefBansEndpoint();
server.setConnectors(new Connector[]{connector}); server.setConnectors(new Connector[]{connector});
server.setHandler(context); server.setHandler(context);
atomicServer.set(server); atomicServer.set(server);
PlexLog.debug("Set atomicServer value? {0}", atomicServer.get() != null);
try try
{ {
server.start(); server.start();
@ -76,6 +76,7 @@ public class HTTPDModule extends PlexModule
} }
}, "Jetty-Server"); }, "Jetty-Server");
serverThread.start(); serverThread.start();
PlexLog.log("Starting Jetty server on port " + moduleConfig.getInt("server.port"));
} }
@Override @Override

View file

@ -0,0 +1,25 @@
package dev.plex.logging;
import dev.plex.HTTPDModule;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
public class Log
{
public static void log(String message, Object... strings)
{
for (int i = 0; i < strings.length; i++)
{
if (message.contains("{" + i + "}"))
{
message = message.replace("{" + i + "}", strings[i].toString());
}
}
if (HTTPDModule.moduleConfig.getBoolean("server.logging"))
{
Bukkit.getConsoleSender().sendMessage(String.format(ChatColor.DARK_AQUA + "[Plex HTTPD] " + ChatColor.GRAY + "%s", message));
}
}
}

View file

@ -2,7 +2,7 @@ package dev.plex.request;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import dev.plex.HTTPDModule; import dev.plex.HTTPDModule;
import dev.plex.util.PlexLog; import dev.plex.logging.Log;
import jakarta.servlet.ServletException; import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet; import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletRequest;
@ -16,7 +16,6 @@ import org.eclipse.jetty.servlet.ServletHolder;
public class AbstractServlet extends HttpServlet public class AbstractServlet extends HttpServlet
{ {
private final List<Mapping> GET_MAPPINGS = Lists.newArrayList(); private final List<Mapping> GET_MAPPINGS = Lists.newArrayList();
public AbstractServlet() public AbstractServlet()
@ -42,16 +41,13 @@ public class AbstractServlet extends HttpServlet
@Override @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"); String ipAddress = req.getHeader("X-FORWARDED-FOR");
if (ipAddress == null) if (ipAddress == null)
{ {
ipAddress = req.getRemoteAddr(); ipAddress = req.getRemoteAddr();
} }
PlexLog.debug("HTTP Remote IP: " + ipAddress);
PlexLog.debug("HTTP Local IP: " + req.getLocalAddr()); Log.log(ipAddress + " visited endpoint " + req.getHttpServletMapping().getMatchValue());
/*Enumeration<String> headerz = req.getHeaderNames(); /*Enumeration<String> headerz = req.getHeaderNames();
while (headerz.hasMoreElements()) { while (headerz.hasMoreElements()) {
@ -59,7 +55,6 @@ public class AbstractServlet extends HttpServlet
PlexLog.debug("Header: {0} Value {1}", header, req.getHeader(header)); 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 -> 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) if (mapping.headers != null)
@ -86,7 +81,7 @@ public class AbstractServlet extends HttpServlet
@Data @Data
public class Mapping public static class Mapping
{ {
private final Method method; private final Method method;
private final GetMapping mapping; private final GetMapping mapping;

View file

@ -11,15 +11,12 @@ import dev.plex.request.AbstractServlet;
import dev.plex.request.GetMapping; import dev.plex.request.GetMapping;
import dev.plex.util.PlexLog; import dev.plex.util.PlexLog;
import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletRequest;
import java.util.UUID;
import java.util.stream.Collectors;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer; import org.bukkit.OfflinePlayer;
import java.util.UUID; public class AdminsEndpoint extends AbstractServlet
import java.util.stream.Collectors;
//@RestController
//@RequestMapping("/api/admins")
public class GetEndpoints extends AbstractServlet
{ {
@GetMapping(endpoint = "/api/admins/") @GetMapping(endpoint = "/api/admins/")
public String getAdmins(HttpServletRequest request) public String getAdmins(HttpServletRequest request)
@ -32,7 +29,7 @@ public class GetEndpoints extends AbstractServlet
final PlexPlayer player = DataUtils.getPlayerByIP(ipAddress); final PlexPlayer player = DataUtils.getPlayerByIP(ipAddress);
if (player == null) if (player == null)
{ {
return "Couldn't load your IP Address: " + ipAddress + ". Check if your SSL settings are setup correctly."; return "Couldn't load your IP Address: " + ipAddress + ". Have you joined the server before?";
} }
if (Plex.get().getSystem().equalsIgnoreCase("ranks")) if (Plex.get().getSystem().equalsIgnoreCase("ranks"))
{ {
@ -53,37 +50,4 @@ public class GetEndpoints extends AbstractServlet
} }
return new GsonBuilder().setPrettyPrinting().create().toJson(Plex.get().getAdminList().getAllAdminPlayers()); return new GsonBuilder().setPrettyPrinting().create().toJson(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(Plex.get().getPunishmentManager().getIndefiniteBans().stream().toList());
}
} }

View file

@ -0,0 +1,51 @@
package dev.plex.request.impl;
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.rank.enums.Rank;
import dev.plex.request.AbstractServlet;
import dev.plex.request.GetMapping;
import dev.plex.util.PlexLog;
import jakarta.servlet.http.HttpServletRequest;
import java.util.UUID;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
public class IndefBansEndpoint extends AbstractServlet
{
@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 + ". 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 "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(Plex.get().getPunishmentManager().getIndefiniteBans().stream().toList());
}
}

View file

@ -1,3 +1,4 @@
server: server:
bind-address: 0.0.0.0 bind-address: 0.0.0.0
port: 27192 port: 27192
logging: false