2022-04-02 03:59:47 +00:00
|
|
|
package dev.plex.request;
|
|
|
|
|
|
|
|
import com.google.common.collect.Lists;
|
|
|
|
import dev.plex.HTTPDModule;
|
2022-04-02 20:51:14 +00:00
|
|
|
import dev.plex.logging.Log;
|
2022-04-02 03:59:47 +00:00
|
|
|
import jakarta.servlet.ServletException;
|
|
|
|
import jakarta.servlet.http.HttpServlet;
|
|
|
|
import jakarta.servlet.http.HttpServletRequest;
|
|
|
|
import jakarta.servlet.http.HttpServletResponse;
|
2022-04-17 21:39:08 +00:00
|
|
|
import java.io.BufferedReader;
|
2022-04-02 03:59:47 +00:00
|
|
|
import java.io.IOException;
|
2022-04-17 21:39:08 +00:00
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.InputStreamReader;
|
2022-04-02 03:59:47 +00:00
|
|
|
import java.lang.reflect.InvocationTargetException;
|
|
|
|
import java.lang.reflect.Method;
|
2022-04-17 23:54:38 +00:00
|
|
|
import java.text.CharacterIterator;
|
|
|
|
import java.text.StringCharacterIterator;
|
2022-04-02 03:59:47 +00:00
|
|
|
import java.util.List;
|
2022-04-17 21:39:08 +00:00
|
|
|
import java.util.Objects;
|
2022-04-02 04:14:12 +00:00
|
|
|
import lombok.Data;
|
|
|
|
import org.eclipse.jetty.servlet.ServletHolder;
|
2022-04-02 03:59:47 +00:00
|
|
|
|
2022-04-02 04:14:12 +00:00
|
|
|
public class AbstractServlet extends HttpServlet
|
|
|
|
{
|
2022-04-02 03:59:47 +00:00
|
|
|
private final List<Mapping> GET_MAPPINGS = Lists.newArrayList();
|
|
|
|
|
2022-04-02 04:14:12 +00:00
|
|
|
public AbstractServlet()
|
|
|
|
{
|
|
|
|
for (Method declaredMethod : this.getClass().getDeclaredMethods())
|
|
|
|
{
|
2022-04-02 03:59:47 +00:00
|
|
|
declaredMethod.setAccessible(true);
|
2022-04-02 04:14:12 +00:00
|
|
|
if (declaredMethod.isAnnotationPresent(GetMapping.class))
|
|
|
|
{
|
2022-04-02 03:59:47 +00:00
|
|
|
GetMapping getMapping = declaredMethod.getAnnotation(GetMapping.class);
|
|
|
|
Mapping mapping = new Mapping(declaredMethod, getMapping);
|
2022-04-02 04:14:12 +00:00
|
|
|
if (declaredMethod.isAnnotationPresent(MappingHeaders.class))
|
|
|
|
{
|
2022-04-02 03:59:47 +00:00
|
|
|
mapping.setHeaders(declaredMethod.getAnnotation(MappingHeaders.class));
|
|
|
|
}
|
|
|
|
GET_MAPPINGS.add(mapping);
|
|
|
|
ServletHolder holder = new ServletHolder(this);
|
|
|
|
HTTPDModule.context.addServlet(holder, getMapping.endpoint() + "*");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2022-04-02 04:14:12 +00:00
|
|
|
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
|
|
|
|
{
|
2022-04-02 23:34:19 +00:00
|
|
|
String ipAddress = req.getRemoteAddr();
|
2022-04-17 23:21:44 +00:00
|
|
|
if (ipAddress.equals("127.0.0.1"))
|
2022-04-02 04:14:12 +00:00
|
|
|
{
|
2022-04-02 23:34:19 +00:00
|
|
|
ipAddress = req.getHeader("X-FORWARDED-FOR");
|
2022-04-02 03:59:47 +00:00
|
|
|
}
|
2022-04-02 20:51:14 +00:00
|
|
|
|
|
|
|
Log.log(ipAddress + " visited endpoint " + req.getHttpServletMapping().getMatchValue());
|
2022-04-02 03:59:47 +00:00
|
|
|
|
|
|
|
/*Enumeration<String> headerz = req.getHeaderNames();
|
|
|
|
while (headerz.hasMoreElements()) {
|
|
|
|
String header = headerz.nextElement();
|
|
|
|
PlexLog.debug("Header: {0} Value {1}", header, req.getHeader(header));
|
|
|
|
}*/
|
2022-04-02 04:14:12 +00:00
|
|
|
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())
|
|
|
|
{
|
2022-04-02 03:59:47 +00:00
|
|
|
String header = headers.split(";")[0];
|
|
|
|
String value = headers.split(";")[1];
|
|
|
|
resp.addHeader(header, value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
resp.setStatus(HttpServletResponse.SC_OK);
|
2022-04-02 04:14:12 +00:00
|
|
|
try
|
|
|
|
{
|
2022-04-17 23:21:44 +00:00
|
|
|
Object object = mapping.method.invoke(this, req, resp);
|
2022-04-17 23:54:38 +00:00
|
|
|
if (object != null)
|
|
|
|
{
|
|
|
|
resp.getWriter().println(object.toString());
|
|
|
|
}
|
2022-04-02 04:14:12 +00:00
|
|
|
}
|
|
|
|
catch (IOException | IllegalAccessException | InvocationTargetException e)
|
|
|
|
{
|
2022-04-02 03:59:47 +00:00
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-04-18 03:05:20 +00:00
|
|
|
public static String readFile(InputStream filename)
|
|
|
|
{
|
|
|
|
String base = HTTPDModule.template;
|
|
|
|
String page = readFileReal(filename);
|
|
|
|
String[] info = page.split("\n", 3);
|
|
|
|
base = base.replace("${TITLE}", info[0]);
|
|
|
|
base = base.replace("${ACTIVE_" + info[1] + "}", "active\" aria-current=\"page");
|
|
|
|
base = base.replace("${ACTIVE_HOME}", "");
|
|
|
|
base = base.replace("${ACTIVE_ADMINS}", "");
|
|
|
|
base = base.replace("${ACTIVE_INDEFBANS}", "");
|
|
|
|
base = base.replace("${ACTIVE_LIST}", "");
|
2023-12-10 04:35:30 +00:00
|
|
|
base = base.replace("${ACTIVE_COMMANDS}", "");
|
2022-04-18 03:05:20 +00:00
|
|
|
base = base.replace("${ACTIVE_PUNISHMENTS}", "");
|
|
|
|
base = base.replace("${ACTIVE_SCHEMATICS}", "");
|
|
|
|
base = base.replace("${CONTENT}", info[2]);
|
|
|
|
return base;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static String readFileReal(InputStream filename)
|
2022-04-15 18:55:40 +00:00
|
|
|
{
|
2022-04-17 21:39:08 +00:00
|
|
|
StringBuilder contentBuilder = new StringBuilder();
|
|
|
|
try
|
|
|
|
{
|
|
|
|
BufferedReader in = new BufferedReader(new InputStreamReader(Objects.requireNonNull(filename)));
|
|
|
|
String str;
|
|
|
|
while ((str = in.readLine()) != null)
|
|
|
|
{
|
2022-04-18 03:05:20 +00:00
|
|
|
contentBuilder.append(str).append("\n");
|
2022-04-17 21:39:08 +00:00
|
|
|
}
|
|
|
|
in.close();
|
|
|
|
}
|
|
|
|
catch (IOException ignored)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
return contentBuilder.toString();
|
2022-04-15 18:55:40 +00:00
|
|
|
}
|
|
|
|
|
2022-04-17 23:54:38 +00:00
|
|
|
// Code from https://programming.guide/java/formatting-byte-size-to-human-readable-format.html
|
|
|
|
public static String formattedSize(long bytes)
|
|
|
|
{
|
|
|
|
long absB = bytes == Long.MIN_VALUE ? Long.MAX_VALUE : Math.abs(bytes);
|
|
|
|
if (absB < 1024)
|
|
|
|
{
|
|
|
|
return bytes + " B";
|
|
|
|
}
|
|
|
|
long value = absB;
|
|
|
|
CharacterIterator ci = new StringCharacterIterator("KMGTPE");
|
|
|
|
for (int i = 40; i >= 0 && absB > 0xfffccccccccccccL >> i; i -= 10)
|
|
|
|
{
|
|
|
|
value >>= 10;
|
|
|
|
ci.next();
|
|
|
|
}
|
|
|
|
value *= Long.signum(bytes);
|
|
|
|
return String.format("%.1f %ciB", value / 1024.0, ci.current());
|
|
|
|
}
|
|
|
|
|
2022-04-02 03:59:47 +00:00
|
|
|
@Data
|
2022-04-02 20:51:14 +00:00
|
|
|
public static class Mapping
|
2022-04-02 04:14:12 +00:00
|
|
|
{
|
2022-04-02 03:59:47 +00:00
|
|
|
private final Method method;
|
|
|
|
private final GetMapping mapping;
|
|
|
|
private MappingHeaders headers;
|
|
|
|
}
|
|
|
|
}
|